I! i use wemos d1 mini pro and i try to make connection with blynk and wifimanager with the option for new user start always at the same place (use always the same step by step method for first configuration).
i have 2 problem i can’t resolve . the first it is the connection, when i start the code all as good for setup the wifi to connect and the blynk ‘‘auth’’ too. when it restart and connect to blynk succesfully , it send me notification to my email (all is ok for now).
the problem is
after this the code/blynk/wifi don’t work more time, it stop working and when i try to send me notification with the normal condition , they don’t work more long time(aproxymativly 2 min) and i have try many thing but i can found and fix the problem.
i’m not very experimented with arduino expression c++ but i learn speedly (when is teach good XD) . i need help please and i don’t found the fix of my problem …
the normal fonction i want is: ( 1 ) first boot = wifi open , when connect to it then redirect to 192.168.4.1 for add all information need(ssid/pass/ blynk authentification).
( 2 ) : receive one time email for show is really connected and goodly synchronized with blynk.
( 3 ): can store and save the wifi ssid pass and blynk auth and restart with this same identification if try is fail restart with first boot option and delete all setting before
( 4 ) finally the wemos can as connected for very long time(physical reset with button only (not be reset if is possible)
P.S. when i take code with manual enter the ssid and the password and the blynk autentification, all work good but i can use this methode if the user change wifi setting i can go at there home each time is need to change… i really want use wifimanager and blynk connection at the same time and the same place ( 192.168.4.1)
the oled can deleted if is in conflic
if my english is poor i’m very sorry i speak french normaly
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// SCL GPIO5
// SDA GPIO4
#define OLED_RESET 0 // GPIO0
Adafruit_SSD1306 display(OLED_RESET);
#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2
#define LOGO16_GLCD_HEIGHT 16
#define LOGO16_GLCD_WIDTH 16
#include <FS.h>
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
#include <BlynkSimpleEsp8266.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>
//#include <SimpleTimer.h>
#include <ArduinoJson.h>
#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG
char server[15] = "45.55.96.146" ;
char port[6] = "80";
char auth[34]; //= "JDyxKH28IZu5NO8d48GuUbhwQMzNPEEC";
int niveau;
const int eau =4 ;
int debordement = 4; //D2
int sent;
const int level = 0; // D3
int buttonState = 0;
int fin ;
const int finish = 16; // D0
int done = 16;
bool shouldSaveConfig = false;
void check(){
if (buttonState == LOW) {
sent = LOW;}
if (sent == HIGH) {
done = HIGH;}
}
void saveConfigCallback () {
Serial.println("Should save config");
shouldSaveConfig = true;
}
void setup() {
{Serial.begin(9600);
// by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 64x48)
// init done
display.display();
delay(2000);
// Clear the buffer.
display.clearDisplay();
// text display tests
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(35,16);
display.println("demmarage");
display.display();
delay(100);
display.clearDisplay();
}
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println();
pinMode(finish, INPUT);
pinMode(level, INPUT);
pinMode(niveau, INPUT);
//clean FS, for testing
SPIFFS.format();
//read configuration from FS json
Serial.println("mounting FS...");
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(35, 16);
display.print("FS mounting");
display.display();
delay(1000);
display.startscrollleft(0x00, 0x0F);
delay(2000);
display.clearDisplay();
display.stopscroll();
display.clearDisplay();
display.setTextSize(0);
display.setTextColor(WHITE);
display.setCursor(35, 16);
display.print("set wifi&auth");
display.display();
delay(1000);
display.startscrollleft(0x00, 0x0F);
delay(5000);
display.clearDisplay();
display.stopscroll();
delay(1000);
display.clearDisplay();
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");
}
//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(120);
//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("Wifi_Manager", "")) {
Serial.println("failed to connect and hit timeout");
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(35, 16);
display.print("reset esp wifi");
display.display();
delay(100);
display.startscrollleft(0x00, 0x0F);
delay(3000);
display.stopscroll();
delay(10);
display.clearDisplay();
//reset and try again, or maybe put it to deep sleep
ESP.reset();
}
//if you get here you have connected to the WiFi
Serial.println("connected...yeey :)");
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(35, 16);
display.print("wifi connected");
display.display();
delay(100);
display.startscrollleft(0x00, 0x0F);
delay(2000);
display.stopscroll();
delay(100);
display.clearDisplay();
//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");
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(35, 16);
display.print("saving config");
display.display();
delay(1000);
display.startscrollleft(0x00, 0x0F);
delay(2000);
display.stopscroll();
delay(10);
display.clearDisplay();
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");
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(35, 16);
display.print("noBLYNK I reset");
display.display();
delay(1000);
display.startscrollleft(0x00, 0x0F);
delay(5000);
display.stopscroll();
delay(1000);
display.clearDisplay();
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)
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(35, 16);
display.print("reset wifi&auth");
display.display();
delay(1000);
display.startscrollleft(0x00, 0x0F);
delay(3000);
display.stopscroll();
delay(1000);
display.clearDisplay();
ESP.reset();
}
else
{
Serial.println("BLYNK Connected");
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(35, 16);
display.print("BLYNK connected");
display.display();
delay(1000);
display.startscrollleft(0x00, 0x0F);
delay(3000);
display.clearDisplay();
}
//WiFi.mode(WIFI_STA);
}
void notify(){
if ((buttonState == LOW) && (sent == LOW)) {
Blynk.email("grr.la@grr.la", " VOTRE APPAREIL SEMBLE EN PROBLÈME ÉLÉCTIQUE ET EST A VÉRIFIÉ ", "VOTRE APPAREIL A BESOIN D'ÊTRE VÉRIFIÉ Alert MODULE ESP8266 UNE PANNE ÉLÉTRIQUE A ÉTÉ DÉTÉCTÉ ");
sent = HIGH;
fin = LOW;
}
if ((buttonState == HIGH) && (fin == LOW)) {
Blynk.email("grr.la@grr.la", "VOTRE APPAREIL EST ALIMENTÉ EN ELECTRICITÉ ", "VÉRIFIÉ QUAND MÊME LE BON FONCTIONNEMENT DE VOTRE APPAREIL S.V.P. DÈS QUE POSSIBLE! ");
fin = HIGH;
}
if ((debordement == LOW) && (niveau == LOW)) {
Blynk.email("grr.la@grr.la", " VOTRE POMPE SUBMERSIBLE EST A VÉRIFIÉ ", "HAUT NIVEAU DÉTÉCTÉ SUR VOTRE POMPE SUBMERSIBLE Alert MODULE ESP8266 ");
niveau = HIGH;
}
// if ((debordement == HIGH) && (niveau == HIGH)) {
// Blynk.email("grr.la@grr.la", " LE NIVEAU D'EAU DE VOTRE POMPE SUBMERSIBLE SEMBLE RÉTABLE ", "LE HAUT NIVEAU DÉTÉCTÉ SUR VOTRE POMPE SUBMERSIBLE SEMBLE EST REVENUE A LA NORMAL VÉRIFIÉ QUAND MÊME LE BON FONCTIONNEMENT DE VOTRE APPAREIL S.V.P. DÈS QUE POSSIBLE! ");
// niveau = LOW;
// }
}
void loop() {
notify();
buttonState = digitalRead(level);
debordement = digitalRead(eau);
{
if (buttonState == HIGH) {
sent = LOW;}
if (sent == HIGH) {
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(35, 16);
display.print("ALARM");
display.display();
display.clearDisplay();
}
{
if (debordement == HIGH) {
niveau = LOW;}
if (niveau == HIGH) {
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(35, 16);
display.print("HAUT NIVEAU");
display.display();
display.clearDisplay();
}
}}}