I! I have problem with output logical. When the scketch is uploaded it work well but with many project with many online server blynk code only is do this thing: randomly the output change state, example after time “x” the output change state imself, no programme for change it absolulty nothing are programmed in eventor or other they change imself, i have made many project and all do the same thing, randomly they change state of pin , sometime it glitch and sometime it change for ever( or when the blynk server reconnect (seem to be pike here the problem) it rechange of state output. )Anybody have idea please tank you
#include <WiFiManager.h>
#include <SPI.h>
#include <Wire.h>
#include <FS.h>
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
#include <BlynkSimpleEsp8266.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>
#include <ArduinoJson.h>
#define BLYNK_PRINT Serial
#include <Ticker.h>
#include <DHT.h>
#define PIN_LED D8 // Pin D4 mapped to pin GPIO2/TXD1 of ESP8266, NodeMCU and WeMoS, control on-board LED
#define DHTPIN 5
BlynkTimer timer;
DHT dht(DHTPIN, DHT11);
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22, AM2302, AM2321
//#define DHTTYPE DHT21 // DHT 21, AM2301
Ticker led_ticker;
#define AREF_VOLTAGE 3.27
//#define BLYNK_DEBUG
char server[15] = "192.168.4.1" ;
char port[6] = "8080";
char auth[34] = "";
bool shouldSaveConfig = false;
void set_led(byte status)
{
digitalWrite(LED_BUILTIN, status);
}
void heartBeatPrint(void)
{
static int num = 1;
if (Blynk.connected())
{
set_led(LOW);
led_ticker.once_ms(111, set_led, (byte) HIGH);
Serial.print("B");
}
else
{
Serial.print("F");
ESP.restart();
}
if (num == 80)
{
Serial.println();
num = 1;
}
else if (num++ % 10 == 0)
{
Serial.print(" ");
}
}
void check_status()
{
static unsigned long checkstatus_timeout = 0;
#define STATUS_CHECK_INTERVAL 6000L
// Send status report every STATUS_REPORT_INTERVAL (60) seconds: we don't need to send updates frequently if there is no status change.
if ((millis() > checkstatus_timeout) || (checkstatus_timeout == 0))
{
// report status to Blynk
heartBeatPrint();
checkstatus_timeout = millis() + STATUS_CHECK_INTERVAL;
}
}
void resetting()
{
if (digitalRead(13) == LOW)
{
WiFiManager wifiManager;
wifiManager.resetSettings();
SPIFFS.format();
Serial.println("BLYNK resset");
ESP.restart();
ESP.reset();
ESP.restart();
}
}
void checkPhysicalButtond0()
{
if (digitalRead(16) == LOW) //D1 is GPIO5
{
Blynk.virtualWrite(V99, LOW);
}
else
{
Blynk.virtualWrite(V99, HIGH);
}
}
// void checkPhysicalButtond1()
//{
// if (digitalRead(5) == LOW) //D1 is GPIO5
// {
// Blynk.virtualWrite(V1, LOW);
// }
// else
// {
// Blynk.virtualWrite(V1, HIGH);
// }
//}
void checkPhysicalButtond2()
{
if (digitalRead(4) == LOW) //D1 is GPIO5
{
Blynk.virtualWrite(V2, LOW);
Blynk.virtualWrite(V2, LOW);
}
else
{
Blynk.virtualWrite(V2, HIGH);
}
}
void checkPhysicalButtond3()
{
if (digitalRead(0) == LOW) //D1 is GPIO5
{
Blynk.virtualWrite(V3, LOW);
Blynk.virtualWrite(V0, LOW);
}
else
{
Blynk.virtualWrite(V3, HIGH);
Blynk.virtualWrite(V0, HIGH);
}
}
void checkPhysicalButtond4()
{
if (digitalRead(2) == LOW) //D1 is GPIO5
{
Blynk.virtualWrite(V4, LOW);
}
else
{
Blynk.virtualWrite(V4, HIGH);
}
}
void checkPhysicalButtond5()
{
if (digitalRead(14) == LOW) //D1 is GPIO5
{
Blynk.virtualWrite(V5, LOW);
}
else
{
Blynk.virtualWrite(V5, HIGH);
}
}
void checkPhysicalButtond6()
{
if (digitalRead(12) == LOW) //D1 is GPIO5
{
Blynk.virtualWrite(V6, LOW);
}
else
{
Blynk.virtualWrite(V6, HIGH);
}
}
void checkPhysicalButtond7()
{
if (digitalRead(13) == LOW) //D1 is GPIO5
{
WiFiManager wifiManager;
wifiManager.resetSettings();
SPIFFS.format();
Blynk.virtualWrite(V7, LOW);
Serial.println("ERASING");
SPIFFS.format();
ESP.reset(); // reset button
ESP.restart();
}
else
{
Blynk.virtualWrite(V7, HIGH);
}
}
void checkPhysicalButtond8()
{
if (digitalRead(15) == LOW) //D1 is GPIO5
{
Blynk.virtualWrite(V8, LOW);
}
else
{
Blynk.virtualWrite(V8, HIGH);
}
}
void saveConfigCallback () {
Serial.println("Should save config");
shouldSaveConfig = true;
}
void sendSensor()
{
float h = dht.readHumidity();
float t = dht.readTemperature(true); // or dht.readTemperature(true) for Fahrenheit
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// You can send any value at any time.
// Please don't send more that 10 values per second.
Blynk.virtualWrite(V20, h);
Blynk.virtualWrite(V21, t);
}
void setup() {{
dht.begin();
// Setup a function to be called every second
timer.setInterval(10000L, sendSensor);
pinMode(16, INPUT_PULLUP);
// Setup a function to be called every 100 ms
timer.setInterval(200L, checkPhysicalButtond0);
}
// {
// pinMode(5, INPUT_PULLUP);
// Setup a function to be called every 100 ms
// timer.setInterval(200L, checkPhysicalButtond1);
//}
{
pinMode(4, INPUT_PULLUP);
// Setup a function to be called every 100 ms
timer.setInterval(200L, checkPhysicalButtond2);
}
{
pinMode(0, INPUT_PULLUP);
// Setup a function to be called every 100 ms
timer.setInterval(200L, checkPhysicalButtond3);
}
{
pinMode(2, INPUT_PULLUP);
// Setup a function to be called every 100 ms
timer.setInterval(200L, checkPhysicalButtond4);
}
{
pinMode(14, INPUT_PULLUP);
// Setup a function to be called every 100 ms
timer.setInterval(200L, checkPhysicalButtond5);
}
{
pinMode(12, INPUT_PULLUP);
// Setup a function to be called every 100 ms
timer.setInterval(200L, checkPhysicalButtond6);
}
{
pinMode(13, INPUT_PULLUP);
// Setup a function to be called every 100 ms
timer.setInterval(100L, checkPhysicalButtond7);
timer.setInterval(10L, resetting);
}
{
pinMode(15, INPUT_PULLUP);
// Setup a function to be called every 100 ms
timer.setInterval(100L, checkPhysicalButtond8);
}
Serial.begin(115200);
Serial.println();
//***************DON'T TOUTCH WIFI-MANAGER AND BLYNK PART *************************
//clean FS, for testing
//SPIFFS.format();
//read configuration from FS json
Serial.println("mounting FS...");
if (SPIFFS.begin()) {
Serial.println("mounted file system");
if (SPIFFS.exists("/config.json")) {
//file exists, reading and loading
Serial.println("reading config file");
File configFile = SPIFFS.open("/config.json", "r");
if (configFile) {
Serial.println("opened config file");
size_t size = configFile.size();
// Allocate a buffer to store contents of the file.
std::unique_ptr<char[]> buf(new char[size]);
configFile.readBytes(buf.get(), size);
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.parseObject(buf.get());
json.printTo(Serial);
if (json.success()) {
Serial.println("\nparsed json");
strcpy(server, json["server"]);
strcpy(port, json["port"]);
strcpy(auth, json["auth"]);
} else {
Serial.println("failed to load json config");
}
}
}
} else {
Serial.println("failed to mount FS");
SPIFFS.format();
}
//end read
// The extra parameters to be configured (can be either global or just in the setup)
// After connecting, parameter.getValue() will get you the configured value
// id/name placeholder/prompt default length
WiFiManagerParameter custom_server("server", "server", server, 15);
WiFiManagerParameter custom_port("port", "port", port, 6);
WiFiManagerParameter custom_auth("blynk", "blynk token", auth, 34);
//WiFiManager
//Local intialization. Once its business is done, there is no need to keep it around
WiFiManager wifiManager;
//set config save notify callback
wifiManager.setSaveConfigCallback(saveConfigCallback);
//set static ip
//wifiManager.setSTAStaticIPConfig(IPAddress(10,0,1,99), IPAddress(10,0,1,1), IPAddress(255,255,255,0));
//add all your parameters here
wifiManager.addParameter(&custom_server);
wifiManager.addParameter(&custom_port);
wifiManager.addParameter(&custom_auth);
//reset settings - for testing
//wifiManager.resetSettings();
//set minimu quality of signal so it ignores AP's under that quality
//defaults to 8%
//wifiManager.setMinimumSignalQuality();
//sets timeout until configuration portal gets turned off
//useful to make it all retry or go to sleep
//in seconds
wifiManager.setTimeout(300);
//fetches ssid and pass and tries to connect
//if it does not connect it starts an access point with the specified name
//here "AutoConnectAP"
//and goes into a blocking loop awaiting configuration
if (!wifiManager.autoConnect("NotifySPA", "")) {
Serial.println("failed to connect and hit timeout");
//reset and try again, or maybe put it to deep sleep
ESP.restart();// ici ESP.reset();
}
//if you get here you have connected to the WiFi
Serial.println("connected...yeey :)");
//read updated parameters
strcpy(server, custom_server.getValue());
strcpy(port, custom_port.getValue());
strcpy(auth, custom_auth.getValue());
//save the custom parameters to FS
if (shouldSaveConfig) {
Serial.println("saving config");
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.createObject();
json["server"] = server;
json["port"] = port;
json["auth"] = auth;
File configFile = SPIFFS.open("/config.json", "w");
if (!configFile) {
Serial.println("failed to open config file for writing");
}
json.printTo(Serial);
json.printTo(configFile);
configFile.close();
//end save
}
Serial.println("local ip");
Serial.println(WiFi.localIP());
Blynk.config(auth);
bool result = Blynk.connect();
Blynk.run();
if (result != true)
{
Serial.println("BLYNK Connection Fail");
//SPIFFS.format();
Serial.println(auth);
//wifiManager.resetSettings();
Serial.println("local ip");
Serial.println(WiFi.localIP());
Blynk.config(auth);
bool result = Blynk.connect();
Blynk.run();
if (result != true)
ESP.restart();
}
else
{
Serial.println("BLYNK Connected");
//WiFi.mode(WIFI_STA);
// ***************END DON'T TOUTCH WIFI-MANAGER AND BLYNK PART *************************
}}
void notify(){
}
void loop() {
notify();
check_status();
notify();
timer.run(); // Initiates BlynkTimer
Blynk.run();
}