Good day,
I am trying to have the status of PIR sensors and reed sensors displayed on a webpage using a NodeMCU. In the serial monitor it displays the ip address or the status for 2 seconds and starts throwing an INVALID JSON OBJECT error. I have gone through the code many times but i have not been able to identify the source of the error. Kindly assist.
//Include Lib for Arduino to Nodemcu
#include <SoftwareSerial.h>
#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
//D6 = Rx & D5 = Tx
SoftwareSerial nodemcu(5, 6);
//Timer to run Arduino code every 5 seconds
unsigned long previousMillis = 0;
unsigned long currentMillis;
const unsigned long period = 10000;
WiFiUDP ntpUDP;
const long utcOffsetInSeconds = 28800;
NTPClient timeClient(ntpUDP, "pool.ntp.org", utcOffsetInSeconds);
unsigned long epochTime = timeClient.getEpochTime();
struct tm *ptm = gmtime ((time_t *)&epochTime);
const char* ssid = "Eric";
const char* password = "Jani";
ESP8266WebServer server(80);
String SendHTML(boolean pir1_State,boolean pir2_State,boolean reed1_State,boolean reed2_State,String TimeWeb, String DateWeb);
void handle_OnConnect();
void handle_NotFound();
String formattedTime;
String Date;
int Day;
int Month;
int Year;
void setup() {
// Initialize Serial port
Serial.begin(9600);
nodemcu.begin(9600);
while (!Serial) continue;
Serial.println("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("");
Serial.println("Connected to WiFi");
Serial.print("IP: ");
Serial.println(WiFi.localIP());
server.on("/", handle_OnConnect);
server.onNotFound(handle_NotFound);
server.begin();
//dht.begin();
timeClient.begin();
}
void loop() {
//Get current time
currentMillis = millis();
if ((currentMillis - previousMillis >= period)) {
StaticJsonDocument<1000> doc;
DeserializationError error = deserializeJson(doc, nodemcu);
// Test parsing
if (error) {
Serial.println("Invalid JSON Object");
//delay(500);
//DeserializationError error = deserializeJson(doc, nodemcu);
return;
}
Serial.println("JSON Object Recieved");
Serial.print("Recieved PIR 1 State: ");
boolean pir1_State = doc["PIR 1 STATUS"];
Serial.println(pir1_State);
Serial.print("Recieved PIR 2 State: ");
boolean pir2_State = doc["PIR 2 STATUS"];
Serial.println(pir2_State);
Serial.print("Recieved REED 1 State: ");
boolean reed1_State = doc["REED 1 STATUS"];
Serial.println(reed1_State);
Serial.print("Recieved REED 2 State: ");
boolean reed2_State = doc["REED 2 STATUS"];
Serial.println(reed2_State);
Serial.println("-----------------------------------------");
//previousMillis = previousMillis + period;
}
server.handleClient();
}
void handle_OnConnect() {
timeClient.update();
unsigned long epochTime = timeClient.getEpochTime();
String formattedTime = timeClient.getFormattedTime();
struct tm *ptm = gmtime ((time_t *)&epochTime);
int monthDay = ptm->tm_mday;
int currentMonth = ptm->tm_mon+1;
int currentYear = ptm->tm_year+1900;
StaticJsonDocument<1000> doc;
DeserializationError error = deserializeJson(doc, nodemcu);
formattedTime = timeClient.getFormattedTime();
Date = String(currentYear) + "-" + String(currentMonth) + "-" + String(monthDay);
boolean pir1_State = doc["PIR 1 STATUS"];
boolean pir2_State = doc["PIR 2 STATUS"];
boolean reed1_State = doc["REED 1 STATUS"];
boolean reed2_State = doc["REED 2 STATUS"];
server.send(200, "text/html", SendHTML(pir1_State,pir2_State,reed1_State,reed2_State,formattedTime,Date));
}
void handle_NotFound(){
server.send(404, "text/plain", "Not found");
}
String SendHTML(boolean pir1_State,boolean pir2_State,boolean reed1_State,boolean reed2_State, String TimeWeb,String DateWeb){
String ptr = " \n";
ptr +="<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">\n";
ptr +="Alarm Global Server Monitor\n";
ptr +="<meta http-equiv="refresh" content="10">\n";
ptr +="\n";
ptr +="\n";
ptr +="<div id="webpage">\n";
ptr +="
Sensor Readings
\n";ptr +="
Date: ";
ptr +=(String)DateWeb;
ptr +="
ptr +="
Time: ";
ptr +=(String)TimeWeb;
ptr +="
ptr +="
PIR 1 Status: ";
ptr +=(boolean)pir1_State;
ptr +="
ptr +="
PIR 2 Status: ";
ptr +=(boolean)pir2_State;
ptr +="
ptr +="
REED 1 Status: ";
ptr +=(boolean)reed1_State;
ptr +="
ptr +="
REED 2 Status: ";
ptr +=(boolean)reed2_State;
ptr +="
ptr +="\n";
ptr +="\n";
ptr +="\n";
return ptr;
}
Regards,