receive data from arduino uno and send to server, receive gate open/command and send it to uno

This commit is contained in:
kicap 2024-08-22 23:56:21 +08:00
parent 6d20228bbd
commit 597b2e74a7
3 changed files with 26 additions and 16 deletions

BIN
image-1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 143 KiB

View File

@ -2,4 +2,6 @@
#### the esp-01 receive data from arduino uno and send it to node js server #### the esp-01 receive data from arduino uno and send it to node js server
![alt text](image.png) ![alt text](image.png)
![alt text](image-1.png)

View File

@ -3,8 +3,10 @@
#include <ESP8266HTTPClient.h> #include <ESP8266HTTPClient.h>
// WiFi credentials // WiFi credentials
const char *ssid = "KARAN"; // const char *ssid = "KARAN";
const char *password = "12345679"; // const char *password = "12345679";
const char *ssid = "RIZALDY";
const char *password = "capputo322";
WiFiClient client; WiFiClient client;
@ -12,6 +14,8 @@ WiFiClient client;
void setup() void setup()
{ {
Serial.begin(115200); Serial.begin(115200);
// pinMode(2, OUTPUT);
// digitalWrite(2, HIGH);
Serial.println("ESP-01 ready"); Serial.println("ESP-01 ready");
WiFi.begin(ssid, password); WiFi.begin(ssid, password);
@ -32,6 +36,8 @@ void loop()
{ {
if (WiFi.status() == WL_CONNECTED) if (WiFi.status() == WL_CONNECTED)
{ {
// Serial.println("Wifi connected");
// digitalWrite(2, LOW);
String IncomingStr = ""; String IncomingStr = "";
while (Serial.available() > 0) while (Serial.available() > 0)
@ -43,7 +49,7 @@ void loop()
if (IncomingStr.length() > 0) if (IncomingStr.length() > 0)
{ {
Serial.print("Wifi connected,");
IncomingStr = IncomingStr.substring(0, IncomingStr.indexOf('\n')); IncomingStr = IncomingStr.substring(0, IncomingStr.indexOf('\n'));
String jsonSent = "{\"value\": \"" + IncomingStr + "\"}"; String jsonSent = "{\"value\": \"" + IncomingStr + "\"}";
@ -57,15 +63,15 @@ void loop()
if (httpResponseCode > 0) if (httpResponseCode > 0)
{ {
Serial.print("HTTP Response code: "); // Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode); // Serial.println(httpResponseCode);
String payload = http.getString(); String payload = http.getString();
Serial.println(payload); Serial.print(payload);
} }
else else
{ {
Serial.print("Error code: "); Serial.print("Error code: ");
Serial.println(httpResponseCode); Serial.print(httpResponseCode);
} }
// Close connection // Close connection
@ -74,33 +80,35 @@ void loop()
} }
// Send data to Arduino // Send data to Arduino
Serial.println("Hello from ESP-01!"); // Serial.println("Hello from ESP-01!");
} }
else else
{ {
// Reconnect to WiFi if disconnected // Reconnect to WiFi if disconnected
WiFi.begin(ssid, password); WiFi.begin(ssid, password);
Serial.println("Wifi disconnected");
// digitalWrite(2, HIGH);
// Wait for connection // Wait for connection
int timeout = 0; int timeout = 0;
while (WiFi.status() != WL_CONNECTED && timeout < 20) while (WiFi.status() != WL_CONNECTED && timeout < 20)
{ // wait for 10 seconds max { // wait for 10 seconds max
delay(500); delay(500);
Serial.print("."); // Serial.print(".");
timeout++; timeout++;
} }
if (WiFi.status() == WL_CONNECTED) if (WiFi.status() == WL_CONNECTED)
{ {
Serial.println(""); // Serial.println("");
Serial.println("Reconnected to WiFi"); // Serial.println("Reconnected to WiFi");
Serial.println("IP address: "); // Serial.println("IP address: ");
Serial.println(WiFi.localIP()); // Serial.println(WiFi.localIP());
} }
else else
{ {
Serial.println(""); // Serial.println("");
Serial.println("Failed to reconnect to WiFi"); // Serial.println("Failed to reconnect to WiFi");
} }
} }
delay(2000); delay(2000);