Skip to content

Instantly share code, notes, and snippets.

@Bolukan
Last active November 18, 2018 12:15
Show Gist options
  • Save Bolukan/663fca14facad96ec94229f75828b342 to your computer and use it in GitHub Desktop.
Save Bolukan/663fca14facad96ec94229f75828b342 to your computer and use it in GitHub Desktop.
Low power WiFi Connection
#include <Arduino.h>
#if defined(ESP8266)
#include <ESP8266WiFi.h>
#elif defined(ESP32)
#include <WiFi.h>
#endif
#include <WiFiClient.h>
#include <time.h>
// #include "secrets.h"
#ifndef SECRETS_H
#define SECRETS_H
const char WIFI_SSID[] = "xxx";
const char WIFI_PASSWORD[] = "xxxx";
#endif
const uint64_t SLEEPTIME = 10e6; // 10 seconds for test
const char TIME_NTPSERVER_1[] = "nl.pool.ntp.org";
const char TIME_NTPSERVER_2[] = "pool.ntp.org";
/*
* Station connected
* @param WiFiEventStationModeConnected stationInfo struct of String ssid, uint8 bssid[6], uint8 channel
*/
void onSTAConnected(WiFiEventStationModeConnected stationInfo) {
// Serial.printf("%u: onSTAConnected\n", millis());
/* Serial.printf("SSID: %s BSSID: %02x:%02x:%02x:%02x:%02x:%02x, Channel: %d\n",
stationInfo.ssid.c_str(),
stationInfo.bssid[0], stationInfo.bssid[1], stationInfo.bssid[2], stationInfo.bssid[3], stationInfo.bssid[4], stationInfo.bssid[5],
stationInfo.channel );
*/
}
/*
* Got IP
* @param WiFiEventStationModeGotIP ipInfo struct of IPAddress ip, IPAddress mask, IPAddress gw
*/
void onSTAGotIP(WiFiEventStationModeGotIP ipInfo) {
/* Serial.printf("IP: %s Mask: %s Gateway: %s\n",
ipInfo.ip.toString().c_str(),
ipInfo.mask.toString().c_str(),
ipInfo.gw.toString().c_str() );
*/
}
/*
* Station disconnected
* @param WiFiEventStationModeDisconnected disconnectedInfo struct of String ssid, uint8 bssid[6], WiFiDisconnectReason reason
* WiFiDisconnectReason, see https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266WiFi/src/ESP8266WiFiType.h
*/
void onSTADisconnected(WiFiEventStationModeDisconnected disconnectInfo) {
// Serial.printf("Disconnected from SSID: %s\n", disconnectInfo.ssid.c_str());
// Serial.printf("Reason: %d\n", disconnectInfo.reason);
// WiFi.reconnect();
}
void connectToWiFi() {
//Serial.printf("%u: ConnectToWiFi start\n", millis());
WiFi.forceSleepWake();
delay(1);
WiFi.persistent(false);
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD); // No fixed IP: took 1.6 sec longer than my DHCP.
configTime(0, 0, TIME_NTPSERVER_1, TIME_NTPSERVER_2);
setenv("TZ", "CET-1CEST,M3.5.0/2,M10.5.0/3", 0);
Serial.printf("%u: ConnectToWiFi\n", millis());
}
void GoToSleep() {
WiFi.disconnect(true);
delay(1);
WiFi.mode(WIFI_OFF);
WiFi.forceSleepBegin();
delay(5);
Serial.printf("%u: Going to sleep\n", millis());
Serial.flush();
// WAKE_RF_DISABLED to keep the WiFi radio disabled when we wake up
ESP.deepSleep( SLEEPTIME, RF_DISABLED );
}
void setup() {
// Set WiFi Off
WiFi.mode(WIFI_OFF);
WiFi.forceSleepBegin();
delay(1);
// Serial
Serial.begin(115200);
Serial.println();
//Serial.printf("%u: \nHello world!\n", millis() );
// WiFi
static WiFiEventHandler e1, e2, e3;
// Other code. For example: Serial.begin(115200);
// WiFi
e1 = WiFi.onStationModeGotIP(onSTAGotIP);
e2 = WiFi.onStationModeConnected(onSTAConnected);
e3 = WiFi.onStationModeDisconnected(onSTADisconnected);
connectToWiFi();
// Other code
}
void loop() {
// Use WiFi as client, server, etc
if (WiFi.status() == WL_CONNECTED) {
//Serial.println("Doing stuff");
while (time(nullptr) < 24 * 3600) delay(1);
time_t tnow = time(nullptr);
Serial.print(String(ctime(&tnow)));
GoToSleep();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment