Hardware disconnects with specific code

I have the following sketch that is working well on a nano with esp01 softserial :

// vult een textveld in de app op de foon

#define BLYNK_PRINT Serial
#define SERIALDEBUG
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>

// CONSTANTS
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "<TOKEN>";
char ssid[] = "<<SSID>>";
char pass[] = "<PASS>";
char blserver[] = "<<IP ADDRES>>";
int blport = <<PORT>>;

unsigned long currentMillis ;
unsigned long previousMillis1 = 0;         
unsigned long previousMillis2 = 0;
unsigned long previousMillis3 = 0; 
unsigned long previousMillis4 = 0; 

const long interval1 = 250;         
const long interval2 = 5000; 
const long interval3 = 10000;
const long interval4 = 20000;


#include <SoftwareSerial.h>
SoftwareSerial EspSerial(2, 3); // RX, TX

// Your ESP8266 baud rate:
#define ESP8266_BAUD 9600

ESP8266 wifi(&EspSerial);

BlynkTimer timer;

// VARIABLES
int tempPM =20;
#define SEGA 8 // actif LOW : Ă©tat segment a Ă©galement jetLed
#define SEGB 16  // actif LOW : Ă©tat segment b Ă©galement heater2Led
#define SEGC 17  // actif LOW : Ă©tat segment c Ă©galement sunheatPos
#define SEGD 9  // actif LOW : Ă©tat segment d Ă©galement set1Led
#define SEGE 10  // actif LOW : Ă©tat segment e Ă©galement filterLed
#define SEGF 11  // actif LOW : Ă©tat segment f Ă©galement heater1Led
#define SEGG 7  // actif LOW : Ă©tat segment g 
#define SEGDP 12  // actif LOW : Ă©tat segment dp Ă©galement set2Led  
#define AFF1 A4 // actif LOW : Données afficheur digit 1 disponibles
#define AFF2 A1 // actif LOW : Données afficheur digit 2 disponibles
#define AFF3 A0 // actif LOW : Données afficheur digit 3 disponibles
#define AFF4 A5 // actif LOW : Données afficheur digit 4 disponibles
#define LEDS 13 // actif LOW : Données des autres LED's disponibles 
#define LEDPOWER A7 // lecture d'un Ă©tat on/off via Analog input car plus de digital input disponible
#define C4051 4 // D4 D5 et D6 utilisées en OUTPUT pour codage de 7 Commandes (+ 1 état neutre)... 
#define B4051 5 // ....via 3 sorties et multiplexeur-interrupteur analogique CD4051
#define A4051 6 // CD4051 : C = pin 9; B= pin 10; A = pin 11 

boolean a,b,c,d,e,f,g,dp,h; // lecture de l'Ă©tat des segments digits
boolean cdeAff1,cdeAff2,cdeAff3,cdeAff4,cdeLeds; // lecture du digit pour lequel les infos sont transmises
int powerLed; //lecture valeur analogique
boolean heater1Led,heater2Led,filterLed,jetLed; //bubbleLed non utilisée par manque d'entrées sur Nano


// déclaration et initialisation de variables

char digit[2];
int adigit=0,adigit1=0,adigit2=0,adigit3=0;
String digitseg="";
String digitsegled="11111111";
float tempWater=20.0;
int delayReadSeg=15;

// déclaration divers compteurs
int i=0;
int j=0;
int k1=0;
int k2=0;
int l=0;
int m=0;
int n=0;
int o=0;
int p=0;
int q=0;
int r=0;
int s=0;
int imax=1000; // nombre max d'itérations de lecture des bits de données afficheurs et états leds
int jmax=5; // autre compteur de base utilisé à divers endroits


BLYNK_CONNECTED() {
    Blynk.syncAll();
}


BLYNK_WRITE(V1)
{
  int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
  if(pinValue == 1) {
    Serial.println("power aan");
    digitalWrite(C4051,1);
    digitalWrite(B4051,0);
    digitalWrite(A4051,0); 
  }
  else
  {
    // execute this code if the switch widget is now OFF
    Serial.println("test 0");
    digitalWrite(C4051,1);
    digitalWrite(B4051,0);
    digitalWrite(A4051,0); 
  }
}

BLYNK_WRITE(V2)
{
  int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
  if(pinValue == 1) {
    Serial.println("heater aan");
    digitalWrite(C4051,1);
    digitalWrite(B4051,0);
    digitalWrite(A4051,1);
  }
  else
  {
    // execute this code if the switch widget is now OFF
    Serial.println("heater uit");
    digitalWrite(C4051,1);
    digitalWrite(B4051,0);
    digitalWrite(A4051,1);
  }
}

BLYNK_WRITE(V3)
{
  int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
  if(pinValue == 1) {
    Serial.println("filter aan");
    digitalWrite(C4051,0);
    digitalWrite(B4051,0);
    digitalWrite(A4051,0); 
  }
  else
  {
    // execute this code if the switch widget is now OFF
    Serial.println("filter uit");
    digitalWrite(C4051,0);
    digitalWrite(B4051,0);
    digitalWrite(A4051,0); 
  }
}


BLYNK_WRITE(V13)
{
  previousMillis4 = millis();
  Serial.println("temp");
  switch (param.asInt()) {
    case -1:
      // Use code to move your Robot left
      tempPM = tempPM -1;
      Blynk.virtualWrite(V14, tempPM);
      Serial.println("temp -");
      break;
    case 1:
      // Use code to move your Robot right
      tempPM = tempPM +1;
      Blynk.virtualWrite(V14, tempPM);
      Serial.println("temp +");
      break;
      // Do nothing
  }
}



void setup()
{
  Serial.begin(9600);
  delay(10);
  analogReference(INTERNAL);
  // pinmodes
  pinMode(SEGA,INPUT); // voir également les autres affectations de ces entrées dans la fonction Void lecture digit ci-dessous
  pinMode(SEGB,INPUT);
  pinMode(SEGC,INPUT);
  pinMode(SEGD,INPUT);
  pinMode(SEGE,INPUT);
  pinMode(SEGF,INPUT);
  pinMode(SEGG,INPUT);
  pinMode(SEGDP,INPUT);
  pinMode(AFF1,INPUT);
  pinMode(AFF2,INPUT);
  pinMode(AFF3,INPUT);
  pinMode(AFF4,INPUT);
  pinMode(LEDS,INPUT);
  // No need to define A7 (powerLed) because INPUT only
  pinMode(C4051,OUTPUT); // 3 Sorties vers CD 4051 positionnées à 1 
  pinMode(B4051,OUTPUT);
  pinMode(A4051,OUTPUT);
  digitalWrite(C4051,1);
  digitalWrite(B4051,1);
  digitalWrite(A4051,1);
  //end pinmodes
  EspSerial.begin(ESP8266_BAUD);
  delay(10);
  pinMode(4, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(13, OUTPUT);
  Blynk.begin(auth,wifi, ssid, pass, blserver, blport);
  timer.setInterval(2000L, sendSeconds); //elke 2 sec een update
  Blynk.virtualWrite(V14, tempPM);
}

void loop()
{
  Blynk.run();
  timer.run();
  currentMillis = millis();
  Serial.println(currentMillis);
if (currentMillis - previousMillis1 >= interval1) {
  previousMillis1 = currentMillis;

  digitalWrite(C4051,1);
  digitalWrite(B4051,1);
  digitalWrite(A4051,1);
}

lectureleds();



}

void sendSeconds() {
  int seconden = millis() /1000;
  if (seconden > 1000) {seconden=seconden-1000; }
  Blynk.virtualWrite(V0, seconden);
}



void lectureleds(){

  // tests delay usec: de 10 à 200: c'est 15 usec qui donne mes meilleurs résultats (99% de réussite)
  // détection platine sous tension (=> Led Rouge allumée)  + lecture et décodage des led's d'état
  // jmax changements consécutifs pour être pris en compte 

  i=0;
  cdeLeds=digitalRead(LEDS);
  while (cdeLeds==1 && i<imax){cdeLeds=digitalRead(LEDS);i++;Blynk.run();} // détection présence de bits états LED
  if (cdeLeds==0 && i<imax){
    delayMicroseconds(delayReadSeg);
    powerLed=analogRead(LEDPOWER); // utilisation d'une entrée analogique pour lecture d'une valeur digitale suite manque d'entrées digitales
    lecturedigit();
    if(powerLed<=900){a=0;}
    else{a=1;}
    digitseg=String(String(a)+String(f)+String(b)+String(d)+String(dp)+String(e)+String(c));
    s=0;
    while(digitsegled!=digitseg && s<jmax){
      i=0;s++;Blynk.run();
      cdeLeds=digitalRead(LEDS);
      while (cdeLeds==1 && i<imax){cdeLeds=digitalRead(LEDS);i++;Blynk.run();}
      if (cdeLeds==0 && i<imax){
        delayMicroseconds(delayReadSeg);
        powerLed=analogRead(LEDPOWER);
        lecturedigit();
        if(powerLed<=900){a=0;}
        else{a=1;}
        digitseg=String(String(a)+String(f)+String(b)+String(d)+String(dp)+String(e)+String(c));
      }
    } 
    
    if(digitsegled!=digitseg){

      digitsegled=digitseg;
      heater1Led=f;heater2Led=b;filterLed=e;

//      if(powerLed<=900){RemoteXY.ledPower_r=255;RemoteXY.ledPower_g=0;}
//      else{RemoteXY.ledPower_r=0;RemoteXY.ledPower_g=0;}
      
//      if(heater2Led==0 && heater1Led==1){RemoteXY.ledHeater_r=255;RemoteXY.ledHeater_g=0;}  // led rouge
//      if(heater2Led==1 && heater1Led==0){RemoteXY.ledHeater_r=0;RemoteXY.ledHeater_g=255;}  // led verte
//      if(heater2Led==1 && heater1Led==1){RemoteXY.ledHeater_r=0;RemoteXY.ledHeater_g=0;} // led Ă©teinte
      
//      if(filterLed==0){RemoteXY.ledFilter_r=0;RemoteXY.ledFilter_g=255;} // led verte
//      else{RemoteXY.ledFilter_r=0;RemoteXY.ledFilter_g=0;}  // led Ă©teinte
      
      }        
  }
}


void decodage(){
// Actif LOW

digitseg=String(String(a)+String(b)+String(c)+String(d)+String(e)+String(f)+String(g));
digit[0]='x';

if (digitseg == "1111111") {digit[0]=' ';adigit=0;}
if (digitseg == "0000001") {digit[0]='0';adigit=0;}
if (digitseg == "1111001") {digit[0]='1';adigit=1;}
if (digitseg == "0010010") {digit[0]='2';adigit=2;}
if (digitseg == "0110000") {digit[0]='3';adigit=3;}
if (digitseg == "1101000") {digit[0]='4';adigit=4;}
if (digitseg == "0100100") {digit[0]='5';adigit=5;}
if (digitseg == "0000100") {digit[0]='6';adigit=6;}
if (digitseg == "1110001") {digit[0]='7';adigit=7;}
if (digitseg == "0000000") {digit[0]='8';adigit=8;}  
if (digitseg == "0100000") {digit[0]='9';adigit=9;}
if (digitseg == "0000111") {digit[0]='c';adigit=10;}
if (digitseg == "1001000") {digit[0]='H';adigit=11;}
if (digitseg == "0000110") {digit[0]='E';adigit=12;}
if (digitseg == "1000110") {digit[0]='F';adigit=13;}
if (digitseg == "1011100") {digit[0]='n';adigit=14;}
if (digitseg == "0011000") {digit[0]='d';adigit=13;}

// Serial.print(digit[0]);
}

void lecturedigit(){
a=digitalRead(SEGA); // Ă©galement jetLed
b=digitalRead(SEGB); // Ă©galement heater2Led
c=digitalRead(SEGC); 
d=digitalRead(SEGD); 
e=digitalRead(SEGE); // Ă©galement filterLed
f=digitalRead(SEGF); // Ă©galement heater1Led
g=digitalRead(SEGG);
dp=digitalRead(SEGDP); 
}

Now after the lectureleds(); in the loop I need the following:

i=0;
cdeAff1=digitalRead(AFF1);
while (cdeAff1==1 && i<imax) {cdeAff1=digitalRead(AFF1);i++;Blynk.run()}
if (cdeAff1==0 && i<imax) {
  delayMicroseconds(delayReadSeg);
  lecturedigit();
  decodage();
  if (digit[0] != 'x' && digit[0] != 'c' && digit[0] != ' ') { 
  adigit1=adigit;
//  strcpy (RemoteXY.digit1, digit);
}
  }

If I add this code the Serial.println(currentMillis); in the loop is not printing anymore and about every 10 seconds my nano disconnects
From the Blynk.log of my local server:

20:57:11.925 INFO - <<EMAIL>> hardware joined.
20:57:22.176 INFO - <<EMAIL>> hardware joined.
20:57:32.422 INFO - <<EMAIL>> hardware joined.
20:57:42.809 INFO - <<EMAIL>> hardware joined.
20:57:53.116 INFO - <<EMAIL>> hardware joined.
20:58:03.413 INFO - <<EMAIL>> hardware joined.
20:59:06.548 INFO - <<EMAIL>> hardware joined.
...
...

Can anybody tell me how to add the code so that it keeps working?

Thx in adance

if I comment out the line Serial.println(currentMillis); in the loop, I have the same issus

Your while loop is a blocking loop for blynk.

1 Like

Yep, I explained the same thing to @mikje the other day in one of his other posts…

Pete.

1 Like

I see that the while loop is blocking, however I extra put an Blynk.run in it.
I can;t see how I can change the while loop in an non blocking piece of code.

Try that

while (cdeLeds==1 && i<imax){
Blynk.run();

@mikje have you read this:
http://help.blynk.cc/getting-started-library-auth-token-code-examples/blynk-basics/keep-your-void-loop-clean

You already have a timer declared, but you’re only using it for one thing, which is to to send your device uptime to the app.

This is in effect a 250ms interval timer, but done in a less efficient way than BlynkTimer would do it,

You need to be de-cluttering your void loop, not adding to it

You need something with a lot more processing power and much higher clock speed in my opinion.

Pete.

I think in the above code I already use Blynk.run ?

@PeteKnight
You are right where it comes to processing power. Alas, this is the hardware I have to deal with as I bought it on a electronic print board to mount it on my jacuzzi.
The original thing was programmed with remotexy, which is less powerfull since it cannot be connected with for example nodered.
That is the reason I want to reprogram the whole thing with the fantastic Blynk.
I can have a look at the time thing you mention.
I certainly read to avoid too much stuf in the void loop, however don;t see a clue on how to prevent it yet

If you’re using Node-Red then why are you running Blynk on the Nano?
You should install the node-red-contrib-blynk-ws contrib and let your Node-Red server do all the hard work.
Then it’s simply a case of sending MQTT messages to your Nano to trigger certain routines.

This is how my home automation setup works, I don’t have any Blynk code running on any of my devices, just MQTT Pub/Sub code.

Pete.

had to be replace with

//in setup
timer.setInterval(250L, something);



void something() {
  digitalWrite(C4051,1);
  digitalWrite(B4051,1);
  digitalWrite(A4051,1);
}

Hi @PeteKnight

Well you certainly have a point, The original Remotexy does only offer a remotexy app and no connection to nodered or other stuff. This is on how I came to Blynk. It looks very simpel to me to work with it and I like the ideaof having my own server instead of a cloud dependency. Later on I discovered that you can integrate with nodered and that was even more a WOW factor for me.

For the hardware provided (nano with esp01) however I cannot find a simple example on how to start with mqtt.
Most of the examples are working with nodemcu or the sketch on the wifi hardware itself.
The problem is the electronic printboard I bought that has a nano and an esp01 connected to it , so I guess I am dependant on that hardware.

What is to your opinion the culprit: esp01 or the nano? If it is the esp, I could give it a try to replace it by a wemos d1, not by pressing it on the printboard but bayme by using dupont cables. (To give Blynk a try)

If I cannot manage to get mqtt working, you mean to do the whole thing in nodered and use blynk as a broker between nodered and the arduino would me a solution? (so use blynk only to read and write to certain arduino ports)?

The ESP-01 is simply acting as a WiFi modem for the Nano. The ESP already has far more memory and processing power than the Nano, it simply lacks GPIO pins that can be accessed via physical pins.
Replacing the ESP-01 won’t help, it’s the nano that is under powered and under-resourced compared to modern boards.

I’ve not tried it, but this looks promising…

No, that’s not what I had in mind and wouldn’t be the way that I’d approach it.

Pete.

@PeteKnight alas the example is not working for the nano, thx anyway

What exactly does “not working” mean?
Not compiling?
Not connecting to your WiFi network?
Not authenticating to your MQTT server?
Not publishing MQTT messages?
Something else?

Pete.

@PeteKnight
not compilling for the nano

It compiles for me, once I downloaded and installed the WiFiEsp library.

Pete.

@PeteKnight
Did you compile for the Nano or the Nano IOT
I have installed wifiesp but still cannot compile it for the basic Nano

@PeteKnight I have not installed a sketch on the ESP, just flashed it with basic expressif firmware. Maybe there I have to also upload a kind of sketch?

Nano

image

Sketch uses 15890 bytes (51%) of program storage space. Maximum is 30720 bytes.
Global variables use 824 bytes (40%) of dynamic memory, leaving 1224 bytes for local variables. Maximum is 2048 bytes.

If you do that then your ESP-01 will no longer function as a WiFi modem for your Nano. The ESP-01 needs to be running the factory “AT” firmware for it to function as a WiFi modem.

Do you have any details for this board?

Pete.