I have set out to build a smart incubator which read and records temperature and humidity values by the Arduino and then Serially send the information as well as others to the NodeMCU ESP8266 which is to then send to the Blynk IoT platform by Wifi. I have encoded the information in a string in the arduino code and then designed a decoding function in the esp to get the vaules from the string by the substring extract function.
My problems are these:
- The Serial monitor shows that my NodeMCU is receiving data but then the data is not being sent to the Blynk IoT platform.
- The line that is supposed to display the value I obtained does not do so.
- when the NodeMCU is programed to just receive the data and then interperet it, the interpretation works, but when I use it by calling the Blynk Library and then using the same decrypting function, the decrypting function somehow stops doing its job.
- My device on the blynk platform shows that its connected even though I don’t get to see on serial monitor that my hardware is connected to the blynk platform.
Below are my codes:
ARDUINO CODE
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Stepper.h>
#include <SoftwareSerial.h>
#include <DHT.h>
#define DHTPIN 2
#define DHTTYPE DHT22
int servoPin=10;
int servoPos;
int lamps=9;
int humidifier=10;
int fan=11;
String str;
int dt=1000;
int stepPerRev=32;
int motSpeed=3;
int Day=0;
int Tmax=37;
int Hmax=50;
int mfactor=2;
int angle=60;
int h=0;
int t=0;
int turn=(angle*stepPerRev)/360;
unsigned long currentTime;
unsigned long startTime;
unsigned long lastMturn;
unsigned long currentMturn;
long day_millis=3600;
long hour_millis=60;
long motorTHr=mfactor*60;
SoftwareSerial espSerial (5,6);
LiquidCrystal_I2C lcd(0x27,16,2);
DHT dht(DHTPIN,DHTTYPE);
Stepper myStepper(stepPerRev,3,4,7,8);
void setup() {
Serial.begin(115200);
espSerial.begin(115200);
lcd.init();
dht.begin();
delay(dt);
lcd.begin(16,2);
pinMode(lamps,OUTPUT);
pinMode(humidifier,OUTPUT);
pinMode(fan,OUTPUT);
lcd.backlight();
lcd.clear();
lcd.setCursor(0,0);
lcd.print("TEMP:");
lcd.setCursor(0,1);
lcd.print("HUMI:");
lcd.setCursor(9,0);
lcd.print("C");
lcd.setCursor(9,1);
lcd.print("%");
lcd.setCursor(13,0);
lcd.print("DAY");
myStepper.setSpeed(motSpeed);
startTime=millis();
lastMturn=millis();
}
void loop() {
t=dht.readTemperature();
h=dht.readHumidity();
lcd.setCursor(6,0);
lcd.print(t);
lcd.setCursor(6,1);
lcd.print(h);
delay(dt/2);
lampControl(t);
humControl(h);
currentTime=millis();
daycounter(currentTime);
servoControl(currentTime);
readNodeMCU();
str=String(t)+String(",")+String(h)+String(".")+String(Day)+String(";")+String(Tmax)+String(":")+String(Hmax)+String("*")+String(angle)+String("/")+String(mfactor)+String("|")+String(motSpeed)+String("!");
espSerial.println(str);
Serial.println(str);
}
void lampControl(int T)
{
if(T<=Tmax)
{
digitalWrite(lamps,HIGH);
digitalWrite(fan,LOW);
}
else {
digitalWrite(lamps,LOW);
digitalWrite(fan,HIGH);
}
}
void humControl(int H)
{
if(H<=Hmax)
{digitalWrite(humidifier,HIGH);}
else {digitalWrite(humidifier,LOW);}
}
int daycounter (unsigned long milisecs)
{
if(milisecs-startTime>=day_millis)
{
++Day;
startTime=milisecs;
}
lcd.setCursor(14,1);
lcd.print(Day);
}
int servoControl(unsigned long motorhr)
{
if(motorhr-lastMturn>=motorTHr)
{
myStepper.step(turn);
delay(dt);
}
if(motorhr-lastMturn>=2*motorTHr)
{
myStepper.step(-turn);
delay(dt);
}
if(motorhr-lastMturn>=3*motorTHr)
{
myStepper.step(-turn);
delay(dt);
}
if(motorhr-lastMturn>=4*motorTHr)
{
myStepper.step(turn);
delay(dt);
}
}
void readNodeMCU()
{
if(Serial.available())
{ Serial.write(Serial.read());}
}
NODEMCU CODE
#include <SoftwareSerial.h>
#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "TMPLfGT7lj1i"
#define BLYNK_DEVICE_NAME "INCUBATOR"
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
char auth[] = "MY AUTH";
char ssid[] = "HOTSPOT NAME";
char pass[] = "MY PASS";
String str;
String temp;
String humi;
String Day;
String Tmax;
String Hmax;
String Angle;
String Mfactor;
String Speed;
String readStr;
int dt=1000;
int t,h,D,T,H,A,M,S;
BlynkTimer timer;
void setup()
{
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
timer.setInterval(1000L,sensorvalue1);
timer.setInterval(1000L,sensorvalue2);
timer.setInterval(1000L,sensorvalue3);
timer.setInterval(1000L,sensorvalue4);
timer.setInterval(1000L,sensorvalue5);
timer.setInterval(1000L,sensorvalue6);
timer.setInterval(1000L,sensorvalue7);
timer.setInterval(1000L,sensorvalue8);
}
void loop()
{
Blynk.run();
timer.run();
}
void sensorvalue1()
{
String readStr=Serial.readString();
temp=readStr.substring(0,readStr.indexOf(','));
t=temp.toInt();
Blynk.virtualWrite(V1, t);
Serial.println(String("Temperature")+String(t));
}
void sensorvalue2()
{
String readStr=Serial.readString();
humi=readStr.substring(readStr.indexOf(',')+1,readStr.indexOf('.'));
h=humi.toInt();
Serial.println(String("Humidity ")+String(h));
Blynk.virtualWrite(V2, h);
}
void sensorvalue3()
{
String readStr=Serial.readString();
Day=readStr.substring(readStr.indexOf('.')+1,readStr.indexOf(';'));
D=Day.toInt();
Serial.println(String("Day")+String(D));
Blynk.virtualWrite(V3, D);
}
void sensorvalue4()
{
String readStr=Serial.readString();
Tmax=readStr.substring(readStr.indexOf(';')+1,readStr.indexOf(':'));
T=Tmax.toInt();
Serial.println(String("Tmax ")+String(T));
Blynk.virtualWrite(V4, T);
}
void sensorvalue5()
{
String readStr=Serial.readString();
Hmax=readStr.substring(readStr.indexOf(':')+1,readStr.indexOf('*'));
H=Hmax.toInt();
Serial.println(String("Hmax")+String(H));
Blynk.virtualWrite(V5, H);
}
void sensorvalue6()
{
String readStr=Serial.readString();
Angle=readStr.substring(readStr.indexOf('*')+1,readStr.indexOf('/'));
A=Angle.toInt();
Serial.println(String("Angle")+String(A));
Blynk.virtualWrite(V6, A);
}
void sensorvalue7()
{
String readStr=Serial.readString();
Mfactor=readStr.substring(readStr.indexOf('/')+1,readStr.indexOf('|'));
M=Mfactor.toInt();
Serial.println(String("Mfactor")+String(M));
Blynk.virtualWrite(V7, M);
}
void sensorvalue8()
{
String readStr=Serial.readString();
Speed=readStr.substring(readStr.indexOf('|')+1,readStr.indexOf('!'));
S=Speed.toInt();
Serial.println(String("Speed")+String(S));
Blynk.virtualWrite(V8, S);
}
THE SERIAL MONITOR
COM10 is the Arduino and COM5 is the NodeMCU
