bonjour,
je travaille sur un projet à base de Node MCU ESP8266 et je veux commander une LED avec deux conditions :
- la LED sera allumer si j'appuie sur un bouton ou si la donnée reçue de la Base de Données est 1
- la LED sera éteindre si j'appuie sur un deuxième bouton ou si la donnée reçue de la Base de Données est 0
le programme que j'ai essayé est le suivant:
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
WiFiClient Wificlient;
const char* wifiName = "SouMia Il";
const char* wifiPass = "xxxxxxxx";
unsigned long lastTime = 0;
unsigned long timerDelay = 5000;
int btn = 5;
int btb = 4;
int val=0;
//Web Server address to read/write from
const char *host = "http://greenhouse-gega.herokuapp.com/latest";
void write(String url,int Valeur){
if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status
HTTPClient http; //Declare object of class HTTPClient
// String url = "http://greenhouse-gega.herokuapp.com/send";
http.begin(Wificlient,url);
http.addHeader("Content-Type", "application/x-www-form-urlencoded"); //Specify content-type header
int httpCode = http.POST("valeur="+String(Valeur)); //Send the request
String payload = http.getString(); //Get the response payload
Serial.println(httpCode); //Print HTTP return code
Serial.println(payload); //Print request response payload
http.end(); //Close connection
} else {
Serial.println("Error in WiFi connection");
}
}
int read(){
HTTPClient http; //Declare object of class HTTPClient
Serial.print("Request Link:");
Serial.println(host);
http.begin(Wificlient,"http://greenhouse-gega.herokuapp.com/latest"); //Specify request destination
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
int httpCode = http.GET(); //Send the request
String payload = http.getString(); //Get the response payload from server
Serial.print("Response Code:"); //200 is OK
Serial.println(httpCode); //Print HTTP return code
Serial.println("Returned data from Server:");
//Serial.println(payload); //Print request response payload
if(httpCode == 200)
{
// Allocate JsonBuffer
// Use arduinojson.org/assistant to compute the capacity.
const size_t capacity = JSON_OBJECT_SIZE(1) + JSON_ARRAY_SIZE(4) + 50;
DynamicJsonBuffer jsonBuffer(capacity);
JsonArray& root = jsonBuffer.parseArray(payload);
JsonObject& root_0 = root[0];
int root_0_valeur = root_0["valeur"]; // 1
Serial.println("valeur="+root_0_valeur);
return (root_0_valeur);
}
}
void setup() {
pinMode(16,OUTPUT);
pinMode(btn,INPUT);
pinMode(btb,INPUT);
Serial.begin(115200);
delay(10);
Serial.println();
Serial.print("Connecting to ");
Serial.println(wifiName);
WiFi.begin(wifiName, wifiPass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP()); //You can get IP address assigned to ESP
}
void loop() {
if ((millis() - lastTime) > timerDelay) {
Serial.println(read());
if (read()==1 || digitalRead(btn)==1)
{
digitalWrite(16,HIGH);
Serial.println("LED ON");
if (digitalRead(btn)==1){
write("http://greenhouse-gega.herokuapp.com/send",1);
}
}
else if(read()==0 || digitalRead(btb)==1)
{
digitalWrite(16,LOW);
Serial.println("LED OFF");
if (digitalRead(btb)==1){
write("http://greenhouse-gega.herokuapp.com/send",0);
}
}
lastTime = millis();
}
à chaque exécution de programme il donne un résultat différent qui ne respecte pas les conditions.
Avez-vous une solution ?
MERCI.