Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Esp32Server/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.pio
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch
10 changes: 10 additions & 0 deletions Esp32Server/.vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
// See http://go.microsoft.com/fwlink/?LinkId=827846
// for the documentation about the extensions.json format
"recommendations": [
"platformio.platformio-ide"
],
"unwantedRecommendations": [
"ms-vscode.cpptools-extension-pack"
]
}
39 changes: 39 additions & 0 deletions Esp32Server/include/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

This directory is intended for project header files.

A header file is a file containing C declarations and macro definitions
to be shared between several project source files. You request the use of a
header file in your project source file (C, C++, etc) located in `src` folder
by including it, with the C preprocessing directive `#include'.

```src/main.c

#include "header.h"

int main (void)
{
...
}
```

Including a header file produces the same results as copying the header file
into each source file that needs it. Such copying would be time-consuming
and error-prone. With a header file, the related declarations appear
in only one place. If they need to be changed, they can be changed in one
place, and programs that include the header file will automatically use the
new version when next recompiled. The header file eliminates the labor of
finding and changing all the copies as well as the risk that a failure to
find one copy will result in inconsistencies within a program.

In C, the usual convention is to give header files names that end with `.h'.
It is most portable to use only letters, digits, dashes, and underscores in
header file names, and at most one dot.

Read more about using header files in official GCC documentation:

* Include Syntax
* Include Operation
* Once-Only Headers
* Computed Includes

https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html
46 changes: 46 additions & 0 deletions Esp32Server/lib/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@

This directory is intended for project specific (private) libraries.
PlatformIO will compile them to static libraries and link into executable file.

The source code of each library should be placed in a an own separate directory
("lib/your_library_name/[here are source files]").

For example, see a structure of the following two libraries `Foo` and `Bar`:

|--lib
| |
| |--Bar
| | |--docs
| | |--examples
| | |--src
| | |- Bar.c
| | |- Bar.h
| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
| |
| |--Foo
| | |- Foo.c
| | |- Foo.h
| |
| |- README --> THIS FILE
|
|- platformio.ini
|--src
|- main.c

and a contents of `src/main.c`:
```
#include <Foo.h>
#include <Bar.h>

int main (void)
{
...
}

```

PlatformIO Library Dependency Finder will find automatically dependent
libraries scanning project source files.

More information about PlatformIO Library Dependency Finder
- https://docs.platformio.org/page/librarymanager/ldf.html
27 changes: 27 additions & 0 deletions Esp32Server/platformio.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[env:esp32doit-devkit-v1]
platform = espressif32
board = esp32doit-devkit-v1
framework = arduino
monitor_filters = time
monitor_speed = 9600
lib_extra_dirs = ~/Documents/Arduino/libraries
lib_deps =
ottowinter/ESPAsyncWebServer-esphome@^3.0.0
bblanchon/ArduinoJson@^6.21.2
arduino-libraries/Servo@^1.2.0
roboticsbrno/ServoESP32@^1.0.3
madhephaestus/ESP32Servo@^0.13.0
adafruit/Adafruit TouchScreen@^1.1.3
bodmer/TFT_eSPI@^2.5.30
plerup/EspSoftwareSerial@^8.0.3
upload_port = /dev/ttyUSB0
153 changes: 153 additions & 0 deletions Esp32Server/src/CourseWork.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
#include <WiFi.h>
#include <Wire.h>
#include "WebServer.h"
#include "WiFiConnection.h"
#include "ESP32Server.h"
#include "UltrasonicSensor.h"
#include "RemoteServer.h"
#include "TimerInterruptions.h"
#include "StepMotor.h"
#include "Filter.h"

#include <HardwareSerial.h>
#include <SoftwareSerial.h>


Filter filter(1);
const char* serverUrlUltrasonic = "http://192.168.31.123:8080/ultrasonic/data/";
const char* serverUrlStepper = "http://192.168.31.123:8080/stepper_motor/data/";
const char* serverUrlDcMotorON= "http://192.168.31.123:8080/dc_motor/data/";
const char* serverUrlDcMotorOFF= "http://192.168.31.123:8080/dc_motor/endDate/";
// const std::string serverUrlDcMotorOFF = "http://192.168.31.123:8080/dc_motor/data/endDate/" + std::to_string(filter.getID());
// const char* serverUrlDcMotorOFFChar = serverUrlDcMotorOFF.c_str();


WebServer server;
Ultrasonic ultrasonic;
RemoteServer remoteServer(1);


volatile bool flag = false;
extern volatile bool flag2 = false;
extern volatile bool flag3;
volatile bool flag4 = false;


void IRAM_ATTR isr() {
flag = true;
}

void IRAM_ATTR stepperInterrupt();
// void IRAM_ATTR isr1() {
// flag2 = true;
// }

const uint8_t motorPin1 = 26;
const uint8_t motorPin2 = 25;
const uint8_t motorPin3 = 33;
const uint8_t motorPin4 = 32;
const uint8_t interruptPin = 2;
const int stepperID = 1;
const int stepDelay = 3000;

StepperMotor myStepper(motorPin1, motorPin2, motorPin3, motorPin4, stepDelay, stepperID);

HardwareSerial SerialUNO2(2);

void setup() {
Serial.begin(9600);
SerialUNO2.begin(9600, SERIAL_8N1, 16, 17); // UART2 for Arduino Uno

delay(1000);

connectWiFi();

TimerClass myTimer(0, 80, true, false, &isr);
myTimer.setAlarm(4000000, true); // Set the alarm to trigger every 1 second
myTimer.enableTimer();

Serial.print("SETUP runs");
pinMode(15, OUTPUT);
digitalWrite(15, HIGH);

pinMode(interruptPin, OUTPUT);
digitalWrite(interruptPin, HIGH);

attachInterrupt(interruptPin,stepperInterrupt, FALLING);
myStepper.initialize();

setupESP32Server(server);
ultrasonic.setupUltrasonic();
// myStepper.step(20);
}


void loop() {
server.handleClient();
ultrasonic.measureDistance();

static unsigned long startTime = 0;


if (flag) {
flag = false;
remoteServer.postRequestUltrasonic(serverUrlUltrasonic, ultrasonic.getDistance(), 1);
}

if (SerialUNO2.available()) {
int data = SerialUNO2.read();
if (data != -1) {
int receivedInt = data - '0';
if(receivedInt >= 0 & receivedInt <= 2) {
Serial.print("Received data from Arduino Uno: ");
Serial.println(receivedInt);

switch(receivedInt) {
case 0:
digitalWrite(15, 1);
Serial.println(0);
remoteServer.updateRequestDCOFF(serverUrlDcMotorON,filter.getID());
break;
case 1:
digitalWrite(15, 0);
Serial.println(1);
remoteServer.postRequestDCON(serverUrlDcMotorOFF,filter.getID());
break;
case 2:
remoteServer.postRequestStepper(serverUrlStepper,myStepper.getID());
flag3 = true;
Serial.println(2);
break;
}
}

}

}

// if (flag2) {
// flag2 = false;
// // servoController.updateServo();
// // Serial.println("l");
// }

if (flag3) {
myStepper.step(260);
startTime = millis();

flag3 = false;
flag4 = true;
}

if (flag4) {
unsigned long currentTime = millis();
if (currentTime - startTime >= 2400) {
myStepper.step(-260);
flag4 = false;
startTime = currentTime;
}
}
}



84 changes: 84 additions & 0 deletions Esp32Server/src/ESP32Server.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#include "ESP32Server.h"
#include "StepMotor.h"


// const uint8_t motorPin1 = 26;
// const uint8_t motorPin2 = 25;
// const uint8_t motorPin3 = 33;
// const uint8_t motorPin4 = 32;
// const uint8_t interruptPin = 2;

// const int stepDelay = 3000;

// StepperMotor myStepper(motorPin1, motorPin2, motorPin3, motorPin4, stepDelay);
extern StepperMotor myStepper;
volatile bool flag3 = false;


void IRAM_ATTR stepperInterrupt() {
flag3 = true;
digitalWrite(interruptPin, HIGH);
}

void setupESP32Server(WebServer& server) {
server.on("/", std::bind(handleRoot, std::ref(server)));
server.on("/filter", std::bind(handleDataFilter, std::ref(server)));
server.on("/feeder", std::bind(handleDataFeeder, std::ref(server)));

server.begin();
}

void handleRoot(WebServer& server) {
String html = "<html><body>";
html += "<h1>ESP32 Web Server</h1>";
html += "<form method='post' action='/data'>";
html += "<input type='number' name='value' />";
html += "<input type='submit' value='Submit' />";
html += "</form>";
html += "</body></html>";
server.send(200, "text/html", html);
}

void handleDataFilter(WebServer& server) {
String value = server.arg("value");

// Do something with the received value
// For example, you can print it to the Serial Monitor
// Serial.print("Received value: ");
// Serial.println(value);
// Serial.println("atoi(value.c_str())");
// Serial.print(atoi(value.c_str()));
int pinVal = atoi(value.c_str());
if (pinVal == 1) {
digitalWrite(15, 0);
} else if (pinVal == 0) {
digitalWrite(15, 1);
}


int pinState = digitalRead(15);

// Serial.print("State of pin ");
// Serial.print(pinState);
// Allow CORS
server.sendHeader("Access-Control-Allow-Origin", "*");
server.sendHeader("Access-Control-Max-Age", "10000");
server.sendHeader("Access-Control-Allow-Methods", "POST");
// server.sendHeader("Access-Control-Allow-Headers", "Content-Type");
server.sendHeader("Access-Control-Allow-Headers", "*");

server.send(200, "text/plain", "Data received successfully!");
}


void handleDataFeeder(WebServer& server) {
digitalWrite(interruptPin, LOW); // Set the 2nd pin to low to trigger the interrupt

server.sendHeader("Access-Control-Allow-Origin", "*");
server.sendHeader("Access-Control-Max-Age", "10000");
server.sendHeader("Access-Control-Allow-Methods", "POST");
// server.sendHeader("Access-Control-Allow-Headers", "Content-Type");
server.sendHeader("Access-Control-Allow-Headers", "*");

server.send(200, "text/plain", "Data received successfully!");
}
10 changes: 10 additions & 0 deletions Esp32Server/src/ESP32Server.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

#include "WebServer.h"

void setupESP32Server(WebServer& server);
void handleRoot(WebServer& server);
void handleDataFilter(WebServer& server);
void handleDataFeeder(WebServer& server);


16 changes: 16 additions & 0 deletions Esp32Server/src/Filter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

class Filter {
private:

int filterID;

public:
Filter(int filterID){
filterID = filterID;
}

int getID() {
return filterID;
}
};
Loading