I try to use serial to communicate between two boards (NodeMCU and Arduino). Arduino for receiving and processing sensor data, and NodeMCU for sending reading to Blynk. Every second, Serial.println() on Arduino and Serial.parseInt() on NodeMCU. However, the reading received by NodeMCU is normal for only about 5-10 seconds and it jumps to 0. My current solution is that once the reading becomes 0, restart the board. But this is a lame solution.
BTW if I remove all the Blynk element on my NodeMCU board, the reading works perfectly well.
I connected serial port for two boards.
NodeMCU | Arduino
TX | RX
RX | TX
Here is my code:
NodeMCU
#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
char auth[] = "replaced";
char ssid[] = "aa";
char pass[] = "aa";
SimpleTimer uploadtimer;
SimpleTimer sensortimer;
SimpleTimer reconnecttimmer;
int a = -1;
void(* resetFunc) (void) = 0; //declare reset function @ address 0
void uploadTimerEvent()
{
if(a>200)
Blynk.virtualWrite(1, a);
}
void sensorTimerEvent()
{
a = Serial.parseInt();
Serial.println(a);
if(a==0) resetFunc(); //call reset
}
void reconnectBlynk() {
if (!Blynk.connected()) {
if(Blynk.connect()) {
BLYNK_LOG("Reconnected");
} else {
BLYNK_LOG("Not reconnected");
}
}
}
BLYNK_READ(V2)
{
Blynk.virtualWrite(V2, millis() / 1000);
}
void setup()
{
Serial.begin(115200);
Blynk.begin(auth, ssid, pass);
uploadtimer.setInterval(1000L, uploadTimerEvent);
sensortimer.setInterval(1000L, sensorTimerEvent);
reconnecttimmer.setInterval(10000, reconnectBlynk);
}
void loop()
{
if (Blynk.connected()) {
Blynk.run();
}
uploadtimer.run();
sensortimer.run();
reconnecttimmer.run();
}
Arduino
int pin1 = A0;
int pin2 = A1;
int pin3 = A2;
int pinout1 = 7;
void setup() {
Serial.begin(115200);
pinMode(pin1, INPUT);
pinMode(pin2, INPUT);
pinMode(pin3, INPUT);
pinMode(pinout1, OUTPUT);
}
void loop() {
int a = analogRead(A0);
int b = analogRead(A1);
int c = analogRead(A2);
int avg = (a + b + c) / 3;
Serial.println(avg);
delay(1000);
}
Serial output for NodeMCU
[57] Connecting to TANJX
[1559] Connected to WiFi
[1559] IP: 192.168.1.9
[1559]
___ __ __
/ _ )/ /_ _____ / /__
/ _ / / // / _ \/ '_/
/____/_/\_, /_//_/_/\_\
/___/ v0.4.6 on NodeMCU
[5001] Connecting to blynk-cloud.com:8442
[5312] Ready (ping: 3ms).
916
915
914
917
915
914
0