Disconnects on Arduino

Hi guys,

So this is my setup,
I have an Arduino Uno that uses an ESP8266. I have a temperature sensor, relay and reed switch attached to my Arduino.

  1. I am encountering a scenario where the Arduino goes into a disconnecting \ connecting loop when I add a new BLYNK_WRITE method. I want to just add a method that sends an email upon tapping a button in the app. I have done this before but it is no longer working :disappointed:

  2. I want to set a timeout of 60 seconds when I read a HIGH on the reed switch and if the reed switch is still HIGH when the timeout fires send another push notification. This too causes the arduino to hit the disconnect/connect loop.

Having mentioned all the above, I am aware that the arduino will be disconnected from the server when spamming it, but I am not sure if this is happening.

Pulling my hair out. Please help.

#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <OneWire.h>
#include <DallasTemperature.h>

String repo = "repo";
String commit = "commit";
bool isProduction = false;

// production
char prod_auth[] = "prodCode";
// dev
char dev_auth[] = "devCode";

char prod_ssid[] = "prodSSID";
char dev_ssid[] = "devSSID";
char pass[] = "pass";

#define ONE_WIRE_BUS 4
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

#define EspSerial Serial
#define ESP8266_BAUD 115200
ESP8266 wifi(&EspSerial);

const int reedSwitch = 2;
int buttonState = 0;

BlynkTimer timer;

void checkIfReedSwitchIsStillHigh() {
  if (buttonState == HIGH) {
    Blynk.notify("Gate is still open");
    sendEmail("Gate", "STILL Open");
  }
}

void checkGateActivityOnReedSwitch() {
  boolean newState = digitalRead(reedSwitch);
  if (newState != buttonState) {
    buttonState = newState;
    if (buttonState == HIGH) {
      timer.setTimeout(60000L, checkIfReedSwitchIsStillHigh);
       Blynk.virtualWrite(V8, "OPEN");
       sendEmail("Gate", "Open");
    } else {
      // turn LED off:
      Blynk.virtualWrite(V8, "CLOSED");
      sendEmail("Gate", "Closed");
    }
  }
}

void myTimerEvent()
{
  sensors.requestTemperatures();
  int temp = sensors.getTempCByIndex(0);
  Blynk.virtualWrite(V4, temp);
}

void setup()
{
  // Debug console
  Serial.begin(9600);
  EspSerial.begin(ESP8266_BAUD);
  printSketchIdentification();
  if (isProduction) {
    Blynk.begin(prod_auth, wifi, prod_ssid, pass);
  } else {
     Blynk.begin(dev_auth, wifi, dev_ssid, pass);
  }
 sensors.begin();
  // Setup a function to be called every second
  timer.setInterval(2901L, checkGateActivityOnReedSwitch);
  timer.setInterval(30003L, myTimerEvent);
  timer.setInterval(3600000L, watchdog);
}

void loop()
{
  Blynk.run();
  timer.run(); // Initiates BlynkTimer
}

void watchdog() {
  sendEmail("Arduino watchdog", "Woof!");
}

void sendEmail (String eventName, String message) {
  Blynk.email("*****@***.com", "DEBUG - " + eventName, message);
}

void printSketchIdentification() {
  Serial.println("*************");
  Serial.print("Running : ");
  if (isProduction) {
    Serial.println("PRODUCTION");   
  } else {
    Serial.println("DEV");  
  }
  Serial.println("*************");
  Serial.println("Code running:");
  Serial.println(repo);
  Serial.print("Commit:");
  Serial.println(commit); 
  Serial.println("*************");
}

BLYNK_WRITE(V3) {
   Blynk.notify("Push requested - Be careful what you ask for ;)");
}

BLYNK_WRITE(V7) {
   sendEmail("Testing", "Push via email requested");
}

So whilst trying to understand problem number 2, I commented out the sensors.begin line in my setup method in an effort to stop the connect disconnect loop by virtue of elimination. Here is where is gets really interesting.

The disconnects stopped. The 3 timers I am setting up are working and the timeout that is set in the checkGateActivityOnReedSwitch method also fires (event though it just prints to the terminal for now). I expected the temperature module (used via the DallasTemperature.h import) to not yield any values but to my surprise, it is returning values, correct ones as I tested them. This solves this problem for now.

However problem number 1 (adding a new BLYNK write method) still persists. another thing I found is that I have to remove the sendEmail(“Gate”, “STILL Open”); line in the checkIfReedSwitchIsStillHigh method to avoid further disconnect / reconnect loops.

Why would this be?

why don’t you just use the ESP8266 by itself?

The Arduino Uno has only one serial port, and you appear to be using this for both the connection to the ESP8266 and your serial debug messages. You can’t do this.

The way to do this with an Uno is to use the SoftwareSerial library to create a second serial port which is used to talk to the ESP (keeping the onboard serial port for debugging, as it has a built-in serial to USB adapter which plugs directly into your PC).
However, SoftwareSerial ports can’t operate reliably at the sort of speeds that you are currently trying to talk to the ESP in your sketch. 9600 baud is the recommended speed to use, and you’ll need to configure the ESP (via an AT command) to use the same baud rate.

A better solution, as has already been suggested, is to throw your current hardware in the bin and use a NodeMCU or Wemos D1 Mini instead…

Pete.

1 Like

Hi Dave,

I guess it is just lack of exposure. I’ve always just used it for an interface to my wifi network. I’ll do some research and try it out.

Thank you for your reply.

Hi Pete,

Thank you for the reply and the link to your very detailed writeup. I have 4 arduinos so I’d like to get something running on them.

I will definitely try your suggestions of using the software serial port so communicate with the ESP.

I am looking for consistency and will source a NodeMCU or Wemos D1 mini very soon.

Taking all you have said into consideration, do you think I am pushing the processing and memory boundaries of this Uno too much?

It depends what you are trying to do. If it’s a simple thermostat where the temp sensor is polled every few seconds then this is well within the processing capabilities of the UNO. When you compile. The code you’ll see how much memory is used. If it’s more than around 70% you may start to run into issues.

Pete.

Thnx Pete, I am on 63% so on the edge. Will see how it goes and move to a NodeMCU in future.

Thank you for your replies. It really is awesome knowing there is someone else on the other side with so much knowledge.

I wouldn’t say that 63% was on the edge.
If you’re above 70% then you’re approaching the area where you could start to get unpredictable results in my experience.

Pete.

Great, tx

Hi Pete,

Once again I want to thank you for the feedback that you provided. I bought a Wemos D1 Mini and a NodeMCU. I have only played with the NodeMCU and I have migrated my sketch to the NodeMCU and I am so impressed with this unit. Really loving it. It is light years more stable and I am finding it to be delightful to use. I won’t go back to the Uno.

Once again, thank you.

1 Like

If you want to make the Uno usefull, get a SIM800 module and connect it via RX TX hardware Serial. Works great to control / monitor something basic where wifi is not in reach.

1 Like