I want to control the same LED with a button on the web and physical push-button. I use NodeMCU. I tried running each of them and running well. My web is made in PHP & Mysql and HTML. When I press a button on the web led will turn on or off. Then NodeMCU will give feedback on GPIO status to the database and show it on the web. Then when I press a push-button led will turn on or off then NodeMCU will give feedback GPIO status to database and show it on the web too. It's running well when I'm running each of them. but when I'm combining them it doesn't work. Here is my code. Can anybody help me out?
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266HTTPClient.h>
#define ON_Board_LED D0
int button_on = D5;
int button_off = D6;
int led = D4;
const char* ssid = "joday";
const char* password = "";
const char* host = "http://192.168.43.64/";
void setup() {
pinMode(button_on, INPUT);
pinMode(button_off, INPUT);
pinMode(led, OUTPUT);
pinMode(ON_Board_LED,OUTPUT);
Serial.begin(115200);
delay(500);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password); //--> Connect to your WiFi router
Serial.println("");
pinMode(ON_Board_LED,OUTPUT); //--> On Board LED port Direction output
digitalWrite(ON_Board_LED, HIGH); //--> Turn off Led On Board
pinMode(led,OUTPUT); //--> LED port Direction output
digitalWrite(led, LOW); //--> Turn off Led
//----------------------------------------Wait for connection
Serial.print("Connecting");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
//----------------------------------------Make the On Board Flashing LED on the process of connecting to the wifi router.
digitalWrite(ON_Board_LED, LOW);
delay(250);
digitalWrite(ON_Board_LED, HIGH);
delay(250);
//----------------------------------------
}
//----------------------------------------
digitalWrite(ON_Board_LED, HIGH); //--> Turn off the On Board LED when it is connected to the wifi router.
//----------------------------------------If successfully connected to the wifi router, the IP Address that will be visited is displayed in the serial monitor
Serial.println("");
Serial.print("Successfully connected to : ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Serial.println();
//----------------------------------------
}
void loop() {
HTTPClient http;
//----------------------------------------Getting Data from MySQL Database
String GetAddress, LinkGet, getData, LEDStatResultSend;
int id = 0; //--> ID in Database
GetAddress = "Nodemcu_web/GetData.php";
LinkGet = host + GetAddress; //--> Make a Specify request destination
getData = "ID=" + String(id);
Serial.println("----------------Connect to Server-----------------");
Serial.println("Get LED Status from Server or Database");
Serial.print("Request Link : ");
Serial.println(LinkGet);
http.begin(LinkGet); //--> Specify request destination
http.addHeader("Content-Type", "application/x-www-form-urlencoded"); //Specify content-type header
int httpCodeGet = http.POST(getData); //--> Send the request
String payloadGet = http.getString(); //--> Get the response payload from server
Serial.print("Response Code : "); //--> If Response Code = 200 means Successful connection, if -1 means connection failed. For more information see here : https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
Serial.println(httpCodeGet); //--> Print HTTP return code
Serial.print("Returned data from Server : ");
Serial.println(payloadGet); //--> Print request response payload
if (payloadGet == "1") {
digitalWrite(led, HIGH);
LEDStatResultSend = "1";
}
else if (payloadGet == "0") {
digitalWrite(led, LOW);
LEDStatResultSend = "0";
}
else if (digitalRead(button_on) == HIGH){
digitalWrite(led, HIGH);
LEDStatResultSend = "1";
}
else if (digitalRead(button_off) == HIGH){
digitalWrite(led, LOW);
LEDStatResultSend = "0";
}
//----------------------Sends LED status feedback data to server-------------------------
Serial.println();
Serial.println("Sending LED Status to Server");
String postData, LinkSend, SendAddress;
SendAddress = "Nodemcu_web/getLEDStatFromNodeMCU.php";
LinkSend = host + SendAddress;
postData = "getLEDStatusFromNodeMCU=" + LEDStatResultSend;
Serial.print("Request Link : ");
Serial.println(LinkSend);
http.begin(LinkSend); //--> Specify request destination
http.addHeader("Content-Type", "application/x-www-form-urlencoded"); //--> Specify content-type header
int httpCodeSend = http.POST(postData); //--> Send the request
String payloadSend = http.getString(); //--> Get the response payload
Serial.print("Response Code : "); //--> If Response Code = 200 means Successful connection, if -1 means connection failed
Serial.println(httpCodeSend); //--> Print HTTP return code
Serial.print("Returned data from Server : ");
Serial.println(payloadSend); //--> Print request response payload
//-----------------------------------------------------------------------------------------
Serial.println("----------------Closing Connection----------------");
http.end(); //--> Close connection
Serial.println();
Serial.println("Please wait a seconds for the next connection.");
Serial.println();
//delay(1000);
}
Aucun commentaire:
Enregistrer un commentaire