Hi all, I am doing an IoT project that wish to transfer real-time data of sensors to the IoT platform. I chose to perform serial communication between Arduino Uno & Node MCU ESP8266 due to the WiFi firmware from Node MCU ESP9266 is ready to use, no configuration is needed. The challenge I faced when doing serial communication is the transmission of data from Arduino to Node MCU is transferred in Byte and there's a need to transform it back to original value. I tried multiple coding but it seems not work, please kindly help.
The Sender Code is as below:
#include<SoftwareSerial.h>
SoftwareSerial s(0,1);
String sensor_data;
void setup() {
//Serial S Begin at 9600 Baud
s.begin(115200);
}
void writeString(String stringData) { // Used to serially push out a String with Serial.write()
for (int i = 0; i < stringData.length(); i++)
{
s.write(stringData[i]); // Push each char 1 by 1 on each loop pass
}
}
void loop() {
float sensorValue = analogRead(A1);
float millivolt = (sensorValue/1023)*5;
s.print("Sensor Value: ");
s.println(sensorValue);
s.print("Voltage: ");
s.print(millivolt*1000);
s.println(" mV");
s.println("");
delay(1);
sensor_data = String(millivolt*1000);
writeString(sensor_data);
delay(1000);
}
Receiver Code:
#include <ESP8266WiFi.h>
#include<SoftwareSerial.h>
SoftwareSerial s(3,1);
String sensor_data_from_arduino;
WiFiClient client;
String MakerIFTTT_Key ;
String MakerIFTTT_Event;
char *append_str(char *here, String s) { int i=0; while (*here++ = s[i]){i++;};return here-1;}
char *append_ul(char *here, unsigned long u) { char buf[20]; return append_str(here, ultoa(u, buf, 10));}
char post_rqst[256];char *p;char *content_length_here;char *json_start;int compi;
void setup()
{
sensor_data_from_arduino = "";
Serial.begin(115200);
WiFi.disconnect();
delay(3000);
Serial.println("START");
WiFi.begin("Aaron Lee","19980121");
while ((!(WiFi.status() == WL_CONNECTED))){
delay(300);
Serial.print(".");
}
Serial.println("Connected");
Serial.println("Your IP is");
Serial.println((WiFi.localIP().toString()));
}
void loop()
{
if (Serial.available()){
sensor_data_from_arduino = Serial.read(); //string
if (client.connect("maker.ifttt.com",80)) {
MakerIFTTT_Key ="lhgdr4w3Vy25J6WKRLaIVZrLMCPb1B8uACWjpRdehYo";
MakerIFTTT_Event ="EMG";
p = post_rqst;
p = append_str(p, "POST /trigger/");
p = append_str(p, MakerIFTTT_Event);
p = append_str(p, "/with/key/");
p = append_str(p, MakerIFTTT_Key);
p = append_str(p, " HTTP/1.1\r\n");
p = append_str(p, "Host: maker.ifttt.com\r\n");
p = append_str(p, "Content-Type: application/json\r\n");
p = append_str(p, "Content-Length: ");
content_length_here = p;
p = append_str(p, "NN\r\n");
p = append_str(p, "\r\n");
json_start = p;
p = append_str(p, "{\"value1\":\"");
p = append_str(p, sensor_data_from_arduino);
p = append_str(p, "\",\"value2\":\"");
p = append_str(p, "");
p = append_str(p, "\",\"value3\":\"");
p = append_str(p, "");
p = append_str(p, "\"}");
compi= strlen(json_start);
content_length_here[0] = '0' + (compi/10);
content_length_here[1] = '0' + (compi%10);
client.print(post_rqst);
Serial.print("Value sent to Google Sheets is : ");
Serial.println(sensor_data_from_arduino);
}
delay(10000);
}
}
There is a project that connects a Mega to an ESP8266. Arduino to Arduino via Serial
A few point about your current code
i) SoftwareSerial s(0,1); is using the same pins as the Uno Hardware Uart.
Change that to say s(10,11)
ii) In the ESP you delay 10sec. while you send every 1sec. so time miss match
The link above has an example of a synchronized SerialComs where the ESP8266 will control how often data is sent
Or ditch the Uno entirely and have the esp8266 perform all tasks. It's plenty powerful and if the need for more pins arises external adc's and port multipliers can be added. In the end that would probably be the simplest approach IMO.
I agree with him. I've been working on a project that includes NodeMCU with esp8266 and a sensor, it's totally possible to just ditch your arduino. I do use my arduino to run my debug programs though because I find it's easier to upload to arduino than to NodeMCU.
If you wish to continue with your way, I suggest a mapping function, map your bytes to a value and just have a ratio equation.