Skip to content

Instantly share code, notes, and snippets.

@chaeplin
Created January 26, 2016 11:39
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save chaeplin/be4d9ca41a991aa327bb to your computer and use it in GitHub Desktop.
Save chaeplin/be4d9ca41a991aa327bb to your computer and use it in GitHub Desktop.
esp8266_fpm_mdem_sleep.ino
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include "/usr/local/src/ap_setting.h"
extern "C" {
#include "user_interface.h"
}
#define FPM_SLEEP_MAX_TIME 0xFFFFFFF
#define IPSET_STATIC { 192, 168, 10, 7 }
#define IPSET_GATEWAY { 192, 168, 10, 1 }
#define IPSET_SUBNET { 255, 255, 255, 0 }
#define IPSET_DNS { 192, 168, 10, 10 }
IPAddress ip_static = IPSET_STATIC;
IPAddress ip_gateway = IPSET_GATEWAY;
IPAddress ip_subnet = IPSET_SUBNET;
IPAddress ip_dns = IPSET_DNS;
const char* ssid = WIFI_SSID;
const char* password = WIFI_PASSWORD;
IPAddress mqtt_server = MQTT_SERVER;
char* topic = "pubtest";
String clientName;
long lastReconnectAttempt = 0;
long lastMsg = 0;
int test_para = 1;
unsigned long startMills;
WiFiClient wifiClient;
PubSubClient client(mqtt_server, 1883, wifiClient);
void sendmqttMsg(char* topictosend, String payload)
{
if (client.connected()) {
Serial.print("Sending payload: ");
Serial.print(payload);
unsigned int msg_length = payload.length();
Serial.print(" length: ");
Serial.println(msg_length);
byte* p = (byte*)malloc(msg_length);
memcpy(p, (char*) payload.c_str(), msg_length);
if ( client.publish(topictosend, p, msg_length)) {
Serial.println("Publish ok");
free(p);
//return 1;
} else {
Serial.println("Publish failed");
free(p);
//return 0;
}
}
}
String macToStr(const uint8_t* mac)
{
String result;
for (int i = 0; i < 6; ++i) {
result += String(mac[i], 16);
if (i < 5)
result += ':';
}
return result;
}
boolean reconnect()
{
if (!client.connected()) {
if (client.connect((char*) clientName.c_str())) {
Serial.println("===> mqtt connected");
} else {
Serial.print("---> mqtt failed, rc=");
Serial.println(client.state());
}
}
return client.connected();
}
void wifi_connect()
{
if (WiFi.status() != WL_CONNECTED) {
// WIFI
Serial.println();
Serial.print("===> WIFI ---> Connecting to ");
Serial.println(ssid);
delay(10);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
WiFi.config(IPAddress(ip_static), IPAddress(ip_gateway), IPAddress(ip_subnet), IPAddress(ip_dns));
int Attempt = 0;
while (WiFi.status() != WL_CONNECTED) {
Serial.print(". ");
Serial.print(Attempt);
delay(100);
Attempt++;
if (Attempt == 250)
{
Serial.println();
Serial.println("-----> Could not connect to WIFI");
ESP.restart();
delay(200);
}
}
Serial.println();
Serial.print("===> WiFi connected");
Serial.print(" ------> IP address: ");
Serial.println(WiFi.localIP());
}
}
void setup()
{
startMills = millis();
Serial.begin(74880);
Serial.println("");
Serial.println("rtc mem test");
Serial.println(wifi_station_get_auto_connect());
WiFi.setAutoConnect(true);
wifi_connect();
clientName += "esp8266-";
uint8_t mac[6];
WiFi.macAddress(mac);
clientName += macToStr(mac);
clientName += "-";
clientName += String(micros() & 0xff, 16);
}
void loop()
{
Serial.println("starting main loop");
if (WiFi.status() == WL_CONNECTED) {
if (!client.connected()) {
long now = millis();
if (now - lastReconnectAttempt > 200) {
lastReconnectAttempt = now;
if (reconnect()) {
lastReconnectAttempt = 0;
}
}
}
} else {
//wifi_connect();
}
if (client.connected()) {
String payload = "{\"startMills\":";
payload += (millis() - startMills);
payload += ",\"FreeHeap\":";
payload += ESP.getFreeHeap();
payload += ",\"RSSI\":";
payload += WiFi.RSSI();
payload += "}";
sendmqttMsg(topic, payload);
}
Serial.println("diconnecting client and wifi");
client.disconnect();
wifi_station_disconnect();
wifi_set_opmode(NULL_MODE);
wifi_set_sleep_type(MODEM_SLEEP_T);
wifi_fpm_open();
wifi_fpm_do_sleep(FPM_SLEEP_MAX_TIME);
int counts = 0;
while (true) {
Serial.print("> ");
Serial.print(counts);
delay(200);
counts++;
if (counts == 50)
{
break;
}
}
// wake up to use WiFi again
wifi_fpm_do_wakeup();
wifi_fpm_close();
Serial.println("Reconnecting");
wifi_set_opmode(STATION_MODE);
wifi_station_connect();
wifi_connect();
}
Copy link

ghost commented Jul 6, 2017

Thank you very much for your sketch. It helped me a lot!

@pfabri
Copy link

pfabri commented Dec 2, 2017

This is amazing, thank you so much! Been looking for a solid solution for a while now.

@vimes666
Copy link

vimes666 commented Oct 9, 2019

Nice piece of code.
However if you are planning to run this a long time, be aware that millis() will roll over every 50-some days.

If that happens, 'if (now - lastReconnectAttempt > 200) {' will go in a non productive loop for a very long time ;)
You may want to include testing for 'now' to be less than 'lastReconnectAttempt', setting the latter to zero in that case.

@dirtmover
Copy link

Nice piece of code.
However if you are planning to run this a long time, be aware that millis() will roll over every 50-some days.

If that happens, 'if (now - lastReconnectAttempt > 200) {' will go in a non productive loop for a very long time ;)
You may want to include testing for 'now' to be less than 'lastReconnectAttempt', setting the latter to zero in that case.

He should use unsigned long for "now" and it will work fine as-is

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment