Hi!
I’m looking for help. I made project to control temperature in some remote house, run heater when necessary, send data to control it and adjust it remotely. Because not reliable WiFi I used library ESP8266WiFiMulti.h to connect to all available network&Blynk by timer (to prevent code stack).
Result is following: WiFi is connecting, Blynk is connecting BUT data is not posting and not getting back.
Please advice whats is wrong?
PS - this is one of my first code and I far not good in coding so ready to constructive critics
@Vitalii_Mamchur please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```
Secondly, you shouldn’t be using ERPROM, as it has a very limited read/write life. You should use SPIFFS, or preferably LittleFS.
I’d also suggest adding #define BLYNK_PRINT Serial to the beginning of your sketch, then you’ll get more info about the Blynk connection process in your serial monitor.
Thanks, as i mentioned above, it’s just one of my firs code, mainly assembled from examples, but it’s work any way. About usage SPIFFS and LittleFS instead ERPROM interesting, need to be learned.
But still not understand why I see in Blynk app and Serial monitor that connection is established but no data sent / received? Any idea?
You don’t have any code in your sketch to write data to Blynk, but you need to sort-out your void loop before you add that code, otherwise you will flood the Blynk server.
I’d suggest that you take a look at the Sketch Builder examples for the DHT sensor to see how it should be done.
The main idea of my code was work with no internet connection, I like blynk because it simple BUT it totally useless without internet connection, code simply stack. On this reason was separated main function in void loop from wifi connection and Blynk connection.
There are ways around that, by using Blynk.config and Blynk.connect instead of Blynk.begin, as you’ve done in your original sketch, but there aren’t ways around putting Blynk.virtualWrite in a cluttered loop and flooding the server.
/*************************************************************
Download latest Blynk library here:
https://github.com/blynkkk/blynk-library/releases/latest
Blynk is a platform with iOS and Android apps to control
Arduino, Raspberry Pi and the likes over the Internet.
You can easily build graphic interfaces for all your
projects by simply dragging and dropping widgets.
Downloads, docs, tutorials: http://www.blynk.cc
Sketch generator: http://examples.blynk.cc
Blynk community: http://community.blynk.cc
Follow us: http://www.fb.com/blynkapp
http://twitter.com/blynk_app
Blynk library is licensed under MIT license
This example code is in public domain.
*************************************************************
This example shows how to keep WiFi connection on ESP8266.
*************************************************************/
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
/* Fill-in your Template ID (only if using Blynk.Cloud) */
//#define BLYNK_TEMPLATE_ID "YourTemplateID"
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "YourAuthToken";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "YourNetworkName";
char pass[] = "YourPassword";
int lastConnectionAttempt = millis();
int connectionDelay = 5000; // try to reconnect every 5 seconds
void setup()
{
// Debug console
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
}
void loop()
{
// check WiFi connection:
if (WiFi.status() != WL_CONNECTED)
{
// (optional) "offline" part of code
// check delay:
if (millis() - lastConnectionAttempt >= connectionDelay)
{
lastConnectionAttempt = millis();
// attempt to connect to Wifi network:
if (pass && strlen(pass))
{
WiFi.begin((char*)ssid, (char*)pass);
}
else
{
WiFi.begin((char*)ssid);
}
}
}
else
{
Blynk.run();
}
}
I don’t think this sketch is going to help @Vitalii_Mamchur because it uses the blocking Blynk.begin command, and will always call Blynk.run if there’s a WiFi connection.
A better approach would be to use a Blynk.connected() test in the void loop, and only call Blynk.run if this evaluates to true, in combination with a timer that attempts to re-connect to WiFi and/or Blynk if they are currently disconnected.
Blynk expects a constant connection to the Blynk server, with Blynk.run being executed at least once in every cycle of the void loop (which needs to not be delayed by blocking code or a cluttered void loop, so that it can execute hundreds if not thousands of times per second).
The strategy for handling a situation where you still want additional processing to continue uninterrupted when the connection to the Blynk server is down requires the approach we discussed above.
If for some reason you are trying to reduce internet traffic, and only connect to Blynk occasionally to send updates, then go offline, then a different approach is required.
However, f you do this then BLYNK_WRITE(vPin) callbacks won’t function as expected, and dome additional coding, such as the use of the BLYNK_CONNECTED() callback and the Blynk.syncVirtual(vPin) command are required.
The problem with that approach is that Blynk.connect() will be executed on every void loop cycle, and this command has a default timeout period which blocks all other code execution. You can specify a different timeout period using Blynk.connect(timeout), but this doesn’t work that well - there are still relatively long blocking delays each time this command is called.
That’s why I said…
This allows you to decide how often to call the timer which attempts to do the re-connection.
Also, there are two parts to the re-connection. Blynk.connect() will attempt to use the current network (WiFi) connection to establish a connection to the Blynk server using the settings defined in Blynk.config, it wont attempt to establish or re0-establush a WiFi connection.
So the two part test is:
check if WiFi is connected, and if not attempt to re-connect
check is Blynk is connected and if not then attempt to re-connect.
Dear Pete! do you have some example of usage below commands?
However, f you do this then BLYNK_WRITE(vPin) callbacks won’t function as expected, and dome additional coding, such as the use of the BLYNK_CONNECTED() callback and the Blynk.syncVirtual(vPin) command are required.
I’ll strongly appreciated if you post some. It’s help to better understand