Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

An example of sending data to the server using the Light Sleep mode. #525

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
/*************************************************************
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 runs directly on ESP8266 chip.
Note: This requires ESP8266 support package:
https://github.com/esp8266/Arduino
Please be sure to select the right ESP8266 module
in the Tools -> Board menu!
*************************************************************/

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <BlynkSimpleEsp8266.h>
#include <TimeLib.h>
#include <WidgetRTC.h>

char auth[] = "YourAuthToken";
char ssid[] = "YourNetworkName";
char pass[] = "YourPassword";
char OTApass[] = "YourOTAPassword"; // any password for OTA security access

int RSSIvalue = 0;
float Voltage=0.00f;
unsigned long Timer_5000 = 0;
byte mac[6];


#define PIN_RSSI V2
#define PIN_UBAT V3
#define LED 2

ADC_MODE (ADC_VCC);
WidgetTerminal terminal(V1);
WidgetRTC rtc;

void myTimerEvent()
{
Blynk.virtualWrite(PIN_RSSI, RSSIvalue);
Blynk.virtualWrite(PIN_UBAT, Voltage);
}

BLYNK_CONNECTED() {
// Synchronize time on connection
rtc.begin();
}


void setup()
{

Serial.begin(9600); // can be change for faster, ex. 115200, or 921600
Serial.println("Blynk.begin");
terminal.println(" -----> Blynk.begin");

Blynk_init();
if (WiFi.status() != WL_CONNECTED) ESP.restart();

rtc.begin();
terminal.print(currentTime());
terminal.println(" Connected to server");
terminal.println(WiFi.localIP());
WiFi.macAddress(mac);
terminal.print("MAC: ");
terminal.print(mac[5],HEX);
terminal.print(":");
terminal.print(mac[4],HEX);
terminal.print(":");
terminal.print(mac[3],HEX);
terminal.print(":");
terminal.print(mac[2],HEX);
terminal.print(":");
terminal.print(mac[1],HEX);
terminal.print(":");
terminal.println(mac[0],HEX);

pinMode(LED, OUTPUT);
digitalWrite(LED, HIGH);

Serial.println("Blynk Started OK...");

OTA_init();

Voltage = (ESP.getVcc()-210.0f)/1000.0f; // [-210.0f] - correction for real measurement with voltmeter

myTimerEvent(); // send data to server
terminal.flush();

Timer_5000 = millis();

}

String currentTime() {
char str[8];
sprintf( str, "%02d:%02d:%02d", hour(), minute(), second() );
return str;
}

void Blynk_init() {
int conB_count = 5;
while ((!Blynk.begin_t(auth, ssid, pass, IPAddress(172,18,1,111), 8080, 30000)) && (conB_count>0)) --conB_count; // Should change IPAddress(172,18,1,111) to address of your server
}

void OTA_init() {
ArduinoOTA.setHostname("MyESP"); //Set OTA host name (optional)
ArduinoOTA.setPassword(OTApass); //Set OTA host password (optional)
ArduinoOTA.onStart([]() {
String type;
if (ArduinoOTA.getCommand() == U_FLASH) {
type = "sketch";
} else { // U_FS
type = "filesystem";
}
Serial.println("Start updating " + type);
});
ArduinoOTA.onEnd([]() {
Serial.println("\nEnd");
// terminal.println("\nEnd");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) {
Serial.println("Auth Failed");
} else if (error == OTA_BEGIN_ERROR) {
Serial.println("Begin Failed");
} else if (error == OTA_CONNECT_ERROR) {
Serial.println("Connect Failed");
} else if (error == OTA_RECEIVE_ERROR) {
Serial.println("Receive Failed");
} else if (error == OTA_END_ERROR) {
Serial.println("End Failed");
}
});
ArduinoOTA.begin();

terminal.print(currentTime());
Serial.println("OTA Started OK...");
terminal.println(" OTA Started OK...");
}

void connectBlynk() {
Serial.print(currentTime());
Serial.println(F(" Woke up!"));
Serial.flush();
}

void loop()
{

ArduinoOTA.handle();
Blynk.run();
if ((Timer_5000+5000) < millis()) {
ESPsleep(); // ESP will run 5 sec for detect OTA request
Timer_5000 = millis();
}

}

void ESPsleep() {

terminal.print(currentTime());
terminal.println(" Sleeping 90 sec...");
Serial.println("Sleeping 90 sec...");
terminal.flush();
delay(100);
Blynk.disconnect();
WiFi.mode(WIFI_OFF); // you must turn the modem off; using disconnect won't work
digitalWrite(LED, HIGH); // turn the LED off so they know the CPU isn't running
extern os_timer_t *timer_list;
timer_list = nullptr; // stop (but don't disable) the 4 OS timers
wifi_fpm_set_sleep_type(LIGHT_SLEEP_T);
gpio_pin_wakeup_enable(GPIO_ID_PIN(0), GPIO_PIN_INTR_LOLEVEL); // GPIO wakeup (optional)
wifi_fpm_set_wakeup_cb(connectBlynk); // set wakeup callback
wifi_fpm_open();
wifi_fpm_do_sleep(90E6); // Sleep range = 10000 ~ 268,435,454 uS (0xFFFFFFE, 2^28-1)
delay(90e3 + 1); // delay needs to be 1 mS longer than sleep or it only goes into Modem Sleep

digitalWrite(LED, LOW); // LED twice flash for sleep off
delay(50);
digitalWrite(LED, HIGH);
delay(50);
digitalWrite(LED, LOW);
delay(50);
digitalWrite(LED, HIGH);

Blynk_init();
if (WiFi.status() != WL_CONNECTED) {
Serial.println("Connection Failed! Rebooting...");
digitalWrite(LED, LOW);
delay(250);
digitalWrite(LED, HIGH);
delay(100);
ESP.restart();
} else {
rtc.begin();
terminal.print(currentTime());
terminal.print(F(" Wokeup with "));
terminal.print("WiFi ");
terminal.print(6-conB_count);
terminal.println(" Blynk atmpts");
terminal.flush();
ArduinoOTA.begin();
Serial.println("Connection restored OK...");
terminal.print(currentTime());
terminal.println(" Connection restored. OTA available.");
terminal.print(currentTime());
terminal.print(" IP: ");
terminal.println(WiFi.localIP());
terminal.flush();
}
Serial.println("Data sending started...");
terminal.print(currentTime());
terminal.println(" Data sending started...");
terminal.flush();
digitalWrite(LED, LOW);
delay(25);
digitalWrite(LED, HIGH);
Voltage = (ESP.getVcc()-210.0f)/1000.0f;
RSSIvalue = WiFi.RSSI();
// And other your measurements
myTimerEvent(); // Send datas
terminal.print(currentTime());
terminal.println(" Data Sended OK...");
terminal.flush();
Serial.println("Data Sended OK...");
}
41 changes: 41 additions & 0 deletions src/BlynkSimpleEsp8266.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,31 @@ class BlynkWifi
(void)myip; // Eliminate warnings about unused myip
BLYNK_LOG_IP("IP: ", myip);
}

bool connectWiFi_t(const char* ssid,
const char* pass,
uint16_t timeout = 10000)
{
BLYNK_LOG2(BLYNK_F("Connecting to "), ssid);
WiFi.mode(WIFI_STA);
if (WiFi.status() != WL_CONNECTED) {
if (pass && strlen(pass)) {
WiFi.begin(ssid, pass);
} else {
WiFi.begin(ssid);
}
}
int WFB = timeout / 500; // set default 20 cycles
while ((WiFi.status() != WL_CONNECTED) && (WFB>0)) {
BlynkDelay(500);
--WFB;
}
if (WiFi.status() != WL_CONNECTED) return false;
BLYNK_LOG1(BLYNK_F("Connected to WiFi"));
IPAddress myip = WiFi.localIP();
BLYNK_LOG_IP("IP: ", myip);
return true;
}

void config(const char* auth,
const char* domain = BLYNK_DEFAULT_DOMAIN,
Expand Down Expand Up @@ -93,6 +118,22 @@ class BlynkWifi
config(auth, ip, port);
while(this->connect() != true) {}
}

bool begin_t(const char* auth,
const char* ssid,
const char* pass,
IPAddress ip,
uint16_t port = BLYNK_DEFAULT_PORT,
uint16_t wifi_timeout = 30000)
{
if (WiFi.status() != WL_CONNECTED) {
uint16_t connect_count = wifi_timeout / 10000;
while ((connectWiFi_t(ssid, pass, wifi_timeout / 3)) && (connect_count>0)) --connect_count;
if (WiFi.status() != WL_CONNECTED) return false;
}
config(auth, ip, port);
return this->connect();
}

};

Expand Down