Hello,
I am a beginner in the field of Arduino and I need a help in the following.
I am designing a device to monitor air pollutants and the data sensed by the sensors are to be uploaded on the MySQL database.
Below, I have written a basic code for sending multiple static values from Arduino UNO to Nodemcu ESP8266 using JSON.
The sole problem here is that, in the code part for Nodemcu, only this "String incomingString = linkSerial.readString();" line gets executed and the code below this is not executed.
Can somebody help me with this. I have also attached the code snippets and the output.
I am open to any other solution which will help me out in sending sensor data from Arduino UNO to Nodemcu ESP8266 .
PS. In the following code I only want the solution for transferring values from Arduino to NOdemcu and not for uploading the values to MySQL database .
Please HELP!.
CODE FOR ARDUINO
#include <ArduinoJson.h>
#include <SoftwareSerial.h>
// Declare the "link" serial port
// Please see SoftwareSerial library for detail
SoftwareSerial linkSerial(4, 5); // RX, TX
void setup() {
// Initialize "debug" serial port
// The data rate must be much higher than the "link" serial port
Serial.begin(115200);
while (!Serial) continue;
// Initialize the "link" serial port
// Use the lowest possible data rate to reduce error ratio
linkSerial.begin(4800);
}
void loop() {
// Values we want to transmit
long timestamp = millis();
int value = random(100);
int value1 = random(500);
// Print the values on the "debug" serial port
Serial.print("timestamp = ");
Serial.println(timestamp);
Serial.print("value = ");
Serial.println(value);
Serial.print("value1 = ");
Serial.println(value1);
Serial.println("---");
// Create the JSON document
StaticJsonDocument<200> doc;
doc["timestamp"] = timestamp;
doc["value"] = value;
doc["value1"] = value1;
// Send the JSON document over the "link" serial port
serializeJson(doc, linkSerial);
// Wait
delay(5000);
}
CODE FOR NODEMCU
#include <ArduinoJson.h>
#include <SoftwareSerial.h>
// Declare the "link" serial port
// Please see SoftwareSerial library for detail
SoftwareSerial linkSerial(4, 5); // RX, TX
void setup() {
// Initialize "debug" serial port
// The data rate must be much higher than the "link" serial port
Serial.begin(115200);
while (!Serial) continue;
// Initialize the "link" serial port
// Use the lowest possible data rate to reduce error ratio
linkSerial.begin(4800);
}
void loop() {
// Check if the other Arduino is transmitting
if (linkSerial.available())
{
String incomingString = linkSerial.readString();
Serial.println(incomingString);
// Allocate the JSON document
// This one must be bigger than for the sender because it must store the strings
StaticJsonDocument<300> doc;
// Read the JSON document from the "link" serial port
DeserializationError err = deserializeJson(doc, incomingString );
if (err == DeserializationError::Ok)
{
// Print the values
// (we must use as() to resolve the ambiguity)
Serial.print("timestamp = ");
Serial.println(doc["timestamp"].as());
Serial.print("value = ");
Serial.println(doc["value"].as());
Serial.print("value1 = ");
Serial.println(doc["value1"].as());
}
else
{
// Print error to the "debug" serial port
Serial.print("deserializeJson() returned ");
Serial.println(err.c_str());
// Flush all bytes in the "link" serial port buffer
while (linkSerial.available() > 0)
linkSerial.read();
}
}
}