Hi everyone !
My project is to turn screens and lights on according to the RFID detection of a specific card.
Even though the POC works I have issues sending multiple POST requests. For the record, I'm only using an ESP8266. On the code you can see I'm trying to send 3 POST requests but every time I run the code, it focuses only on the first one. Thus, only one light turns on even if I put delays between each. Moreover, if I print the response of the 2nd and 3rd requests, the serial monitor shows that both requests have been sent but for some reason none of those last two requests are being treated. If anyone knows why and how to workaround the problem so that every lights turn on at the same time ?
Thank You !!! =)
Code Below
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN D8
#define RST_PIN D0
int ledPinV = 5;
int ledPinR = 4;
byte CodeVerif=0;
byte Code_Acces[4]={0x**, 0x**, 0x**, 0x**};
MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class
// Init array that will store new NUID
byte nuidPICC[4];
const char* ssid = "********";
const char* password = "**********";
const char* serverName = "http://192.168.0.15:7012/light";
unsigned long lastTime = 0;
void setup() {
// Init RS232
Serial.begin(115200);
// Init SPI bus
SPI.begin();
// Init MFRC522
rfid.PCD_Init();
// Init Led
pinMode(ledPinV, OUTPUT);
pinMode(ledPinR, OUTPUT);
WiFi.begin(ssid, password);
Serial.println("Connecting");
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
Serial.println("Timer set to 5 seconds (timerDelay variable), it will take 5 seconds before publishing the first reading.");
}
void loop() {
// Initializing the loop if no key is to be found
if ( !rfid.PICC_IsNewCardPresent()){
digitalWrite(ledPinR, LOW);
digitalWrite(ledPinV, LOW);
//Serial.print("0");
delay(100);
if(WiFi.status()== WL_CONNECTED){
WiFiClient client;
HTTPClient http;
// Your Domain name with URL path or IP address with path
http.begin(client, serverName);
// Specify content-type header
http.addHeader("Content-Type", "application/json");
// Data to send with HTTP POST - Lights off
String httpRequestDataLum1 = ("{\"nomLum\":\"imb\",\"intensite\":\"0\"}");
String httpRequestDataLum2 = ("{\"nomLum\":\"scabine\",\"intensite\":\"0\"}");
String httpRequestDataLum3 = ("{\"nomLum\":\"simb\",\"intensite\":\"0\"}");
// Send HTTP POST request
int httpResponseCodeLum1 = http.POST(httpRequestDataLum1);
int httpResponseCodeLum2 = http.POST(httpRequestDataLum2);
int httpResponseCodeLum3 = http.POST(httpRequestDataLum3);
// Free resources
http.end();
}
else {
Serial.println("WiFi Disconnected");
}
lastTime = millis();
}
else{
// Verifying the presence of a key
if ( !rfid.PICC_ReadCardSerial())
return;
// Registering the ID of the key (4 octets)
for (byte i = 0; i < 4; i++){
nuidPICC[i] = rfid.uid.uidByte[i];
}
delay(100);
// Key verification
CodeVerif= GetAccesState(Code_Acces,nuidPICC);
if (CodeVerif!=1){
digitalWrite(ledPinR, HIGH);
digitalWrite(ledPinV, LOW);
//Serial.print("0");
//Check WiFi connection status
if(WiFi.status()== WL_CONNECTED){
WiFiClient client;
HTTPClient http;
// Your Domain name with URL path or IP address with path
http.begin(client, serverName);
// Specify content-type header
http.addHeader("Content-Type", "application/json");
// Data to send with HTTP POST - Lights off still
String httpRequestDataLum1 = ("{\"nomLum\":\"imb\",\"intensite\":\"0\"}");
String httpRequestDataLum2 = ("{\"nomLum\":\"scabine\",\"intensite\":\"0\"}");
String httpRequestDataLum3 = ("{\"nomLum\":\"simb\",\"intensite\":\"0\"}");
// Send HTTP POST request
int httpResponseCodeLum1 = http.POST(httpRequestDataLum1);
int httpResponseCodeLum2 = http.POST(httpRequestDataLum2);
int httpResponseCodeLum3 = http.POST(httpRequestDataLum3);
// Free resources
http.end();
}
else {
Serial.println("WiFi Disconnected");
}
lastTime = millis();
}
else{
digitalWrite(ledPinR, LOW);
digitalWrite(ledPinV, HIGH);
//Serial.print("1");
//Check WiFi connection status
if(WiFi.status()== WL_CONNECTED){
WiFiClient client;
HTTPClient http;
// Your Domain name with URL path or IP address with path
http.begin(client, serverName);
// Specify content-type header
http.addHeader("Content-Type", "application/json");
// Data to send with HTTP POST - Lights on
String httpRequestDataLum1 = ("{\"nomLum\":\"imb\",\"intensite\":\"30\"}");
String httpRequestDataLum2 = ("{\"nomLum\":\"entree1\",\"intensite\":\"10\"}");
String httpRequestDataLum3 = ("{\"nomLum\":\"simb\",\"intensite\":\"10\"}");
// Send HTTP POST request
int httpResponseCodeLum1 = http.POST(httpRequestDataLum1);
delay(1000);
int httpResponseCodeLum2 = http.POST(httpRequestDataLum2);
delay(1000);
int httpResponseCodeLum3 = http.POST(httpRequestDataLum3);
delay(1000);
// Free resources
http.end();
}
else {
Serial.println("WiFi Disconnected");
}
lastTime = millis();
}
}
// Verifying the presence of a new key
if ( !rfid.PICC_ReadCardSerial())
return;
}
byte GetAccesState(byte *CodeAcces,byte *NewCode)
{
byte StateAcces=0;
if ((CodeAcces[0]==NewCode[0])&&(CodeAcces[1]==NewCode[1])&&
(CodeAcces[2]==NewCode[2])&& (CodeAcces[3]==NewCode[3]))
return StateAcces=1;
else
return StateAcces=0;
Serial.print(StateAcces);
}