Arduino Nano + ESP8266 problem

I have the problem withj my project, tested on Mega was OK, but on Nano it’s switching relay ON/OFF/ON/OFF… constantly and doesn’t connect to WiFi.
When I upload simple sketch only with Blynk Connection it’s OK, but on mine sketch there is problem :frowning2: Please help

cpp
        
#include <Blynk.h>

#define BLYNK_PRINT Serial

#include <ESP8266_Lib.h>
//#include <ESP8266WiFi.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <SimpleTimer.h>
#include <TimeLib.h>
#include <WidgetRTC.h>
#include <Wire.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 4
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

char auth[] = "87d571de817c44588e37aa8b27140f6d";
int czujnik_gleby=A0;
int wilg_gleby;
int wilg_gleby_skala = 0;
int wilg_gleby_zadane = 0;
int wyjscie = 2;
int stan_systemu = 0;


char ssid[] = "UKOSNA";
char pass[] = "ukosna123";

#define EspSerial Serial

//#include <SoftwareSerial.h>
//SoftwareSerial EspSerial(8, 9); // RX, TX

#define ESP8266_BAUD 115200

ESP8266 wifi(&EspSerial);

SimpleTimer timer;

WidgetRTC rtc;

void podlewanie()
{
  if ((stan_systemu == 1) && (wilg_gleby_zadane > wilg_gleby_skala))
  {
    digitalWrite(wyjscie,HIGH);
  }
  else
    {
      digitalWrite(wyjscie,LOW);
  }
}

void clockDisplay()
{
  String currentTime = String(hour()) + ":" + minute() + ":" + second();
  String currentDate = String(day()) + " " + month() + " " + year();
  Serial.print("Current time: ");
  Serial.print(currentTime);
  Serial.print(" ");
  Serial.print(currentDate);
  Serial.println();
  sensors.requestTemperatures(); // Polls the sensors

  float temperatura = sensors.getTempCByIndex(0); // Gets first probe on wire in lieu of by address

  Blynk.virtualWrite(V8, temperatura);
  Blynk.virtualWrite(V1, currentTime);
  Blynk.virtualWrite(V2, currentDate);
  Blynk.virtualWrite(V4, digitalRead(22)*15);
}

BLYNK_WRITE(V7)
{
  int timer = param.asInt(); 
  if (timer == 1)
     {
      podlewanie();
     }
}

BLYNK_WRITE(V6)
{
  int manual = param.asInt();
  if (manual == 1)
     {
      digitalWrite(wyjscie, HIGH);
     }         
  else
  {
    digitalWrite(wyjscie,LOW);
  }
}

BLYNK_WRITE(V5)
{
  stan_systemu = param.asInt();
  if (stan_systemu == 0)
  {
   digitalWrite(wyjscie, LOW); 
  }
}

BLYNK_WRITE(V9)
{
  wilg_gleby_zadane = param.asInt();
}

void setup()
{
  Serial.begin(115200);
  pinMode(wyjscie, OUTPUT);
  digitalWrite(wyjscie, LOW);
  EspSerial.begin(ESP8266_BAUD);
  delay(10);
  Blynk.begin(auth, wifi, ssid, pass);
  rtc.begin();
  sensors.begin();
  timer.setInterval(1000L, clockDisplay);
  }

void loop()
{
  Blynk.run();
  timer.run();
  wilg_gleby=analogRead(czujnik_gleby);
  wilg_gleby_skala=(1024-wilg_gleby)/10;
  Blynk.virtualWrite(V
3, wilg_gleby_skala);
  //delay(1000);
}

We are not a code factory here, nor are we code mechanics.

The Nano and Mega are significantly different. Have you noted those differences when referencing your hardware pins and how you interface the ESP?

You should also move all of this into it’s own timer loop.

It’s not a real solution. but I would highly recommend using English veriable names. This makes troubleshooting for us a little easier. Judging by your post you are sufficiently skilled in English :slight_smile:

Please, adjust the Baudrate… 115200 is far from being the proper one.
Start with 9600 in both devices (Esp and the Nano)

use hardware serial rather than software serial. software serial seems to cause problems for me

On hardware serialthe same :frowning:

The same, but I wrote earlier that when I use only connection to Blynk and blank program everything is OK

please use a external usb ttl converter and check the serial messages. this would help to debug the problem

There are no messages on serial. Now I transfer this project to NodeMCU and there is same problem

Your new setup is completely different…
NodeMCU has nothing to do with the Nano and Esp as Shield.

Configuration changed, only problem the same

Can you show your code?


#include <Blynk.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <TimeLib.h>
#include <WidgetRTC.h>
#include <Wire.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 4
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

char auth[] = "b69c971e9f8f4ddfa0cac745b34e4fff";
int czujnik_gleby=A0;
int wilg_gleby;
int wilg_gleby_skala = 0;
int wilg_gleby_zadane = 0;
int wyjscie = 2;
int stan_systemu = 0;


char ssid[] = "UKOSNA";
char pass[] = "ukosna123";

SimpleTimer timer;

WidgetRTC rtc;

void podlewanie()
{
  if ((stan_systemu == 1) && (wilg_gleby_zadane > wilg_gleby_skala))
  {
    digitalWrite(wyjscie,HIGH);
  }
  else
    {
      digitalWrite(wyjscie,LOW);
  }
}

void clockDisplay()
{
  String currentTime = String(hour()) + ":" + minute() + ":" + second();
  String currentDate = String(day()) + " " + month() + " " + year();

  sensors.requestTemperatures(); // Polls the sensors
  float temperatura = sensors.getTempCByIndex(0); // Gets first probe on wire in lieu of by address

  Blynk.virtualWrite(V8, temperatura);
  Blynk.virtualWrite(V1, currentTime);
  Blynk.virtualWrite(V2, currentDate);
  Blynk.virtualWrite(V4, digitalRead(2)*15);
  wilg_gleby=analogRead(czujnik_gleby);
  wilg_gleby_skala=(1024-wilg_gleby)/10;
  Blynk.virtualWrite(V3, wilg_gleby_skala);
}

BLYNK_WRITE(V7)
{
  int timer = param.asInt(); 
  if (timer == 1)
     {
      podlewanie();
     }
}

BLYNK_WRITE(V6)
{
  int manual = param.asInt();
  if (manual == 1)
     {
      digitalWrite(wyjscie, HIGH);
     }         
  else
  {
    digitalWrite(wyjscie,LOW);
  }
}

BLYNK_WRITE(V5)
{
  stan_systemu = param.asInt();
  if (stan_systemu == 0)
  {
   digitalWrite(wyjscie, LOW); 
  }
}

BLYNK_WRITE(V9)
{
  wilg_gleby_zadane = param.asInt();
}

void setup()
{
  pinMode(wyjscie, OUTPUT);
  digitalWrite(wyjscie, LOW);
  Blynk.begin(auth, ssid, pass);
  rtc.begin();
  sensors.begin();
  timer.setInterval(1000L, clockDisplay);
  }

void loop()
{
  Blynk.run();
  timer.run();
}
#define ONE_WIRE_BUS 4

Where do you connect your sensor?
Gpio4 = D2 on the NodeMCU

psoro - you are right - my mistake, I’ve connected this to D4.
Thanks for help