Wifi controlled car, arduino freezes, how to fix

:grinning:Ok, but how timer will start if code is stopped? The problem is that if connection is lost the whole program stops.
And all timers or other things stop working. I wanted to write something like (if nothing new in 5 seconds my robot stop driving). But no results, because no Wi-Fi.

I have also 2 sonar sensors, but they also stop working, because the Wi-Fi connection is lost.

My idea was to take second Arduino (Nano for example) it will receive all data that goes to motors, and if Nano does not receive more new information, it will block the motors. (but I don’t like this variant, because I don’t want to use 2 boards).

Not if you use the proper code and programming methods (for Blynk).

Look here for one method of continuous code operation…

I wrote this long ago and there might be even better ways, later documented in this forum… So this is also important… :innocent:

Blynk - RE-SEAR-CH

1 Like

Ok thank you for this advice :handshake:, i have read your code, and i think this should work for me.
but one thing I didn’t understand:

 WiFi.begin(ssid, pass); 

what library are you using? because i have " ‘WiFi’ was not declared in this scope" error.

It’s part of the ESP8266WiFi.h library.

The code you were looking at was written for an ESP8266 based board, not an Arduino plus ESP-01 combination, so would require modifications to work with your hardware.

I’m also sceptical about whether this will solve the problem that you’ve described, but you seem unwilling to hear the guidance that’s being offered about the root cause of the issue.

Pete.

Sorry, I might not understand you, can you repeat your advice. I will try to read more carefully.
Thanks.

True… however I only intended it to be used as a reference for the Blynk.config() and reconnection option… the OP apparently just loaded the whole thing :slight_smile: My mistake for not being clearer on which part of the code I was referring too :innocent:

@vektobus

Instead of Blynk.begin()

Blynk.config(auth, server, port);
Blynk.connect();

See here: https://docs.blynk.cc/#blynk-firmware-configuration-blynkconfig

And then the bare minimum “check test” in the void loop()

  if (Blynk.connected()) {  // If connected run as normal
    Blynk.run();
  }

@vektobus Here is another sketch (which could have been found with some searching :wink: ) that I use for an old RC car conversion… Running on an UNO and ESP-01.

Note: there is NO “keep running without connection” code in this one (no need for my uses), so you would need to add that. But it does show a (very rudimentary) “fail safe” timer solution to shut off the motors.

1 Like

Thank you so much. That is very useful information and I will try to do this also with my robot.
But one thing I didn’t understand in your code. Why I need this:

  wifi.setDHCP(1, 1, 1); //Enable dhcp in station mode and save in flash of esp8266

???

After 10 probes: I don’t know why, but if I disable my server, my code stop running for 10 seconds.
I have added these

if (Blynk.connected()) {  // If connected run as normal
   Blynk.run();
 }

Is it possible to solve it?

These 10 seconds are killing me

post your latest code so that we can see what techniques you have tried, and what you have not.

#define BLYNK_PRINT Serial

#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <Servo.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
//char auth[] = "CEl-cpfVJcZ0OgdjNOvKFktWlUkvGMit";

char auth[] = "zk9LYDsplNnxXvahKZdICKtiCVUVtZgg";



char ssid[] = "";
char pass[] = "";
char server[] = "";  // IP for your Local Server
int port = 8080;



#include <NeoSWSerial.h>  // https://github.com/SlashDevin/NeoSWSerial
NeoSWSerial EspSerial(2, 3);  // RX, TX - using Analog pins A0 & A1
#define ESP8266_BAUD 9600
ESP8266 wifi(&EspSerial);


Servo servo;

# include "GyverButton.h"
GButton but (5);

const int sensorPin = 2;

int lightVal;
BLYNK_WRITE(V3)
{
  servo.write(param.asInt());
}
int ReCnctFlag;  // Reconnection Flag
int ReCnctCount = 0;  // Reconnection counter

unsigned long lon;
void setup()
{


  // Debug console
  Serial.begin(9600);


  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(10);
  //Blynk.begin(auth, wifi, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
  //wifi.setDHCP(1, 1, 1); //Enable dhcp in station mode and save in flash of esp8266
  Blynk.config(wifi, auth, server, port);
  if (Blynk.connectWiFi(ssid, pass)) {
    Blynk.connect();
  }
  servo.write(40);
  int lightCal = analogRead(sensorPin);
  servo.attach(4);
}

void loop()
{

  if (Blynk.connected()) {

    Blynk.run();
  }

  else {
    Serial.print("disconnected");
    Blynk.connect();
  }


  but.tick();

  Serial.println(lightVal);
  if (but.isHold() == 1 || lightVal < 820) {
    servo.write(40);
  }

  if (lightVal > 820 && but.isHold() == 0) {
    while (1) {
      int lightVal1 = analogRead(sensorPin);
      if (lightVal1 > 820) {
        delay(1000);
        servo.write(8);
      }

      int lightVal2 = analogRead(sensorPin);
      if (lightVal2 > 820) {
        delay(1000);
        servo.write(70);
      }
      int lightVal3 = analogRead(sensorPin);
      if (lightVal3 < 850) {
        break;
      }
    }
  }

  lightVal = analogRead(sensorPin);

}

This is my last code(not Wi-Fi car) it’s shorter, but here we can see this 10-second delay.

If you run this code with local server and after everything is connected, you disconnect the server, you will see, that Serial.print stop printing for 10 seconds.

In my rower code this is the big problem, because 10 seconds it’s enough to bump in to the wall.

You have to much code in the loop() this is will cause your disconnection issue, which in turn causes the 10-second delay (BLYNK is trying to re-establish connection to the server). You cannot get rid of this delay when BLYNK is trying to establish a connection, there is no way around it. You can shorten it by adding in a timeout, for example Blynk.connect(500);, but the best way to prevent it is by keeping the device connected to the server. This is done with:

Where should I add this?
How to add timeout?
If will be 5 or 3 seconds, it will be great.

Replace this:

The 500 after the Blynk.connect is the timeout time in milliseconds, but Blynk uses 3 times this value.

However, I don’t think that will help, as it’s not the reconnect timeout that’s the issue.

The 10 seconds is coming from the heartbeat time with the server. If the Blynk library hasn’t connected to the server for 10 seconds then Blynk.connected() will be false. If you shorten this heartbeat time then you’ll get more disconnections. If you lengthen it you’ll get fewer disconnections, but that isn’t the way to solve your problem.

As I said in my first post in this topic:

But, as I said later:

You still have far too much happening during each execution of your void loop. The loop should be executing tens, if not hundreds, of times per second and it’s not. Solve that, and you’ll solve your problem.
Ignore that, and you’ll continue to have problems.

Pete.

1 Like

Ok I have cut everything from my loop:

void loop()
{
  
 if(Blynk.connected()){

Blynk.run();
 }
Serial.println("1");
 
}

This is my loop part now, but nothing has changed.

And I do this because my Wi-Fi connection is not stable, and some times, if Wi-Fi is lost, my car steel running and bumps. With my server everything is ok, and when Wi-Fi is good, I have full controlled car.

My question is how to reduce these 10 seconds?

If your problem really is your WiFi connection, why aren’t you using WiFi.status() instead of Blynk.connected() ?

Pete.

If you can’t fix your WiFi issue, then instead of trying to change how Blynk needs to work, simply change your motor control for short intermittent movements,.so that the worst that happens is it sits there waiting until you can control it again. Not really a Blynk issue.

I think I will do so, I will check the status of the Wi-Fi, I will make the movements short and I will optimize my code.

But I’m worried about the question, there is no way to change the delay of 10 seconds? Maybe I can change something in the system files or add something?

On e again, you’re not listening to the answers you’re being given.
But to answer your question, the 10 seconds comes from the BLYNK_HEARTBEAT setting, which defaults to 10 seconds. You can change it by adding this line to your sketch…

#define BLYNK_HEARTBEAT xx // replace xx with the time in seconds.

But, as I said earlier this I st the solution…

Pete.

Maybe I’m doing something wrong, but I added this:

#define BLYNK_HEARTBEAT 5

But 10 seconds remained.
I have also changed in Blynkconfig.h file but no results.

I beg you, I need it very much, pleeees :pray:

Maybe 10 seconds is the minimum, I’ve never seen a situation where people are trying to go for a longer timeout.

You don’t need it at all, if you follow the advice that’s been given.

Pete.

Ok, but if i write

#define BLYNK_HEARTBEAT 20

Also, no results.

10 seconds don’t change :thinking: