update to start with millis reporting
This commit is contained in:
@@ -9,6 +9,16 @@ struct Settings {
|
|||||||
|
|
||||||
time_t now;
|
time_t now;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Modules will plug in to this thing via function pointers
|
||||||
|
*/
|
||||||
|
// see also http://www.gammon.com.au/forum/?id=12607
|
||||||
|
//void module_message(const String& shorttopic, const String& message) { //@@@@FIXME@@@@ instead of brightness, adjust mode?
|
||||||
|
//void module_message(const String& shorttopic, const String& message) {
|
||||||
|
//
|
||||||
|
//}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Debugging might be nice sometimes
|
* Debugging might be nice sometimes
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ void wifi_associate() {
|
|||||||
net.setInsecure(); // Do not check fingerprint
|
net.setInsecure(); // Do not check fingerprint
|
||||||
net.setTimeout(15000); // 15 Seconds
|
net.setTimeout(15000); // 15 Seconds
|
||||||
|
|
||||||
/* PRINT_SERIAL("Setting time using SNTP");
|
PRINT_SERIAL("Setting time using SNTP");
|
||||||
configTime(-5 * 3600, 0, "pool.ntp.org", "time.nist.gov");
|
configTime(-5 * 3600, 0, "pool.ntp.org", "time.nist.gov");
|
||||||
now = time(nullptr);
|
now = time(nullptr);
|
||||||
while (now < 1510592825) {
|
while (now < 1510592825) {
|
||||||
@@ -47,16 +47,64 @@ void wifi_associate() {
|
|||||||
struct tm timeinfo;
|
struct tm timeinfo;
|
||||||
gmtime_r(&now, &timeinfo);
|
gmtime_r(&now, &timeinfo);
|
||||||
PRINT_SERIAL("Current time: ");
|
PRINT_SERIAL("Current time: ");
|
||||||
APPENDLN_SERIAL(asctime(&timeinfo)); */
|
APPENDLN_SERIAL(asctime(&timeinfo));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void wifi_checkin() {
|
||||||
|
// 'Native' in WiFiClient because we need to push the entire clientbuffer in the API
|
||||||
|
// As a result copying the buffer in to a new String for HTTPClient library use is highly inefficient
|
||||||
|
if (!net.connect(OTA_SERVER, OTA_PORT)) {
|
||||||
|
Serial.println("HTTPS connection failed");
|
||||||
|
} else {
|
||||||
|
Serial.println("HTTPS connection up");
|
||||||
|
|
||||||
|
// Check for firmware upgrades
|
||||||
|
String version = settings.name;
|
||||||
|
version.concat("-v");
|
||||||
|
version.concat(FW_VERSION);
|
||||||
|
|
||||||
|
String json = "{\"mac\":\"" + WiFi.macAddress() + "\",";
|
||||||
|
json.concat("\"version\":\"" + version + "\",");
|
||||||
|
json.concat("\"millis\":" + String(millis()) + "}");
|
||||||
|
|
||||||
|
// Send request to the server:
|
||||||
|
net.println("POST " OTA_CHECKIN " HTTP/1.1");
|
||||||
|
net.println("Host: " OTA_SERVER);
|
||||||
|
net.println("Accept: */*");
|
||||||
|
net.println("Content-Type: application/json");
|
||||||
|
net.print("Content-Length: ");
|
||||||
|
net.println(json.length());
|
||||||
|
net.println("Connection: close");
|
||||||
|
net.println();
|
||||||
|
// Construct the REST/JSON POST data
|
||||||
|
net.print(json);
|
||||||
|
Serial.println("POST sent");
|
||||||
|
|
||||||
|
while (net.connected()) {
|
||||||
|
String line = net.readStringUntil('\n');
|
||||||
|
if (line == "\r") {
|
||||||
|
Serial.println("Headers received");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
String line = net.readStringUntil('\n');
|
||||||
|
Serial.println("reply was:");
|
||||||
|
Serial.println("==========");
|
||||||
|
Serial.println(line);
|
||||||
|
Serial.println("==========");
|
||||||
|
Serial.println("closing connection");
|
||||||
|
net.stop(); // DISCONNECT FROM THE SERVER
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void wifi_setup() {
|
void wifi_setup() {
|
||||||
// WifiManager.setAPCallback(WifiManagerCallback); // Enable WifiManager callback to clear the cache
|
// WifiManager.setAPCallback(WifiManagerCallback); // Enable WifiManager callback to clear the cache
|
||||||
wifi_associate();
|
wifi_associate();
|
||||||
}
|
}
|
||||||
|
|
||||||
void wifi_loop() {
|
void wifi_loop() {
|
||||||
|
|
||||||
if(WiFi.status() != WL_CONNECTED) {
|
if(WiFi.status() != WL_CONNECTED) {
|
||||||
PRINTLN_SERIAL("Wifi disconnected, trying to reconnect");
|
PRINTLN_SERIAL("Wifi disconnected, trying to reconnect");
|
||||||
wifi_associate();
|
wifi_associate();
|
||||||
@@ -74,6 +122,8 @@ void wifi_loop() {
|
|||||||
PRINTLN_SERIAL("net.status: " + String(net.status()));
|
PRINTLN_SERIAL("net.status: " + String(net.status()));
|
||||||
|
|
||||||
t_httpUpdate_return ret = ESPhttpUpdate.update(net, OTA_SERVER, OTA_PORT, OTA_URI, version);
|
t_httpUpdate_return ret = ESPhttpUpdate.update(net, OTA_SERVER, OTA_PORT, OTA_URI, version);
|
||||||
|
PRINTLN_SERIAL("after httpupdate check net.status: " + String(net.status()));
|
||||||
|
|
||||||
switch(ret) {
|
switch(ret) {
|
||||||
case HTTP_UPDATE_FAILED: // HTTP 403, 404
|
case HTTP_UPDATE_FAILED: // HTTP 403, 404
|
||||||
PRINT_SERIAL("Connection error, update failed ");
|
PRINT_SERIAL("Connection error, update failed ");
|
||||||
@@ -90,5 +140,9 @@ void wifi_loop() {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
ota_lastcheck = ota_check;
|
ota_lastcheck = ota_check;
|
||||||
|
|
||||||
|
// Check in with IoT server
|
||||||
|
wifi_checkin();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,23 +1,13 @@
|
|||||||
#ifdef ENABLE_MQTT
|
#ifdef ENABLE_MQTT
|
||||||
|
|
||||||
|
|
||||||
// sample schema's to use: https://www.home-assistant.io/integrations/light.mqtt/
|
// sample schema's to use: https://www.home-assistant.io/integrations/light.mqtt/
|
||||||
|
|
||||||
/*
|
|
||||||
* Define a function newcmd in your module to be able to accept mqtt commands
|
|
||||||
* Syntax and parameters are like this:
|
|
||||||
*/
|
|
||||||
void newcmd(String cmd);
|
|
||||||
void module_message(const String& shorttopic, const String& message);
|
|
||||||
|
|
||||||
#include <PubSubClient.h>
|
#include <PubSubClient.h>
|
||||||
|
|
||||||
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
|
// Setup the MQTT client class by passing in the WiFi client
|
||||||
PubSubClient mqtt(net);
|
PubSubClient mqtt(net);
|
||||||
String deviceprefix;
|
String deviceprefix;
|
||||||
|
|
||||||
const char* fingerprint = "EA:2A:2B:80:C1:00:57:35:66:26:FF:FC:FA:27:EA:5F:3D:91:5A:F9";
|
|
||||||
|
|
||||||
long logtimerMillis = 0;
|
long logtimerMillis = 0;
|
||||||
|
|
||||||
void mqtt_subscribe(const String& topic) {
|
void mqtt_subscribe(const String& topic) {
|
||||||
@@ -76,24 +66,37 @@ void mqtt_callback(char* topic, byte* payload, unsigned int length) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void mqtt_connect() {
|
void mqtt_connect() {
|
||||||
|
/* bool success= false;
|
||||||
|
success = net.connect(MQTT_SERVER, MQTT_SERVERPORT);
|
||||||
|
if (success) {
|
||||||
|
PRINTLN_SERIAL("Connection complete, valid cert, valid fingerprint.");
|
||||||
|
} else {
|
||||||
|
PRINTLN_SERIAL("Connection failed!");
|
||||||
|
} */
|
||||||
|
PRINTLN_SERIAL("prepare mqtt connect check net.status: " + String(net.status()));
|
||||||
|
|
||||||
mqtt.setServer(MQTT_SERVER, MQTT_SERVERPORT);
|
mqtt.setServer(MQTT_SERVER, MQTT_SERVERPORT);
|
||||||
mqtt.setCallback(mqtt_callback);
|
mqtt.setCallback(mqtt_callback);
|
||||||
|
PRINTLN_SERIAL("before mqtt connect check net.status: " + String(net.status()));
|
||||||
if(!mqtt.connected()) {
|
|
||||||
while (!mqtt.connected()) {
|
while (!mqtt.connected()) {
|
||||||
PRINTLN_SERIAL("MQTT not connected, connecting...");
|
PRINTLN_SERIAL("MQTT not connected, connecting...");
|
||||||
|
PRINTLN_SERIAL("while mqtt connecting check net.status: " + String(net.status()));
|
||||||
// Attempt to connect
|
|
||||||
// Note - the default maximum packet size is 128 bytes. If the
|
// Attempt to connect
|
||||||
// combined length of clientId, username and password exceed this,
|
// Note - the default maximum packet size is 128 bytes. If the
|
||||||
// you will need to increase the value of MQTT_MAX_PACKET_SIZE in
|
// combined length of clientId, username and password exceed this,
|
||||||
// PubSubClient.h
|
// you will need to increase the value of MQTT_MAX_PACKET_SIZE in
|
||||||
if (mqtt.connect(settings.name.c_str(), MQTT_USERNAME, MQTT_PASSWORD)) {
|
// PubSubClient.h
|
||||||
PRINTLN_SERIAL("MQTT connected");
|
if (mqtt.connect(settings.name.c_str(), MQTT_USERNAME, MQTT_PASSWORD)) {
|
||||||
} else {
|
PRINTLN_SERIAL("MQTT connected");
|
||||||
PRINTLN_SERIAL("MQTT connection failed, rc=" + String(mqtt.state()) + "...");
|
PRINTLN_SERIAL("mqtt just connected check net.status: " + String(net.status()));
|
||||||
delay(10000); // @@@FIXME@@@ delays are not cool
|
|
||||||
}
|
} else {
|
||||||
|
PRINTLN_SERIAL("MQTT connection failed, rc=" + String(mqtt.state()) + "...");
|
||||||
|
PRINTLN_SERIAL("mqtt failed connected check net.status: " + String(net.status()));
|
||||||
|
|
||||||
|
delay(10000); // @@@FIXME@@@ delays are not cool
|
||||||
}
|
}
|
||||||
|
|
||||||
// (re)subscribe to the topics we need
|
// (re)subscribe to the topics we need
|
||||||
@@ -104,6 +107,7 @@ void mqtt_connect() {
|
|||||||
mqtt_subscribe(deviceprefix + "#");
|
mqtt_subscribe(deviceprefix + "#");
|
||||||
mqtt_publish(deviceprefix + "log", "Connected " + settings.name + " running v" + FW_VERSION + " at " + settings.ip);
|
mqtt_publish(deviceprefix + "log", "Connected " + settings.name + " running v" + FW_VERSION + " at " + settings.ip);
|
||||||
mqtt_publish(deviceprefix + "log", "Free Heap: " + String(ESP.getFreeHeap()));
|
mqtt_publish(deviceprefix + "log", "Free Heap: " + String(ESP.getFreeHeap()));
|
||||||
|
PRINTLN_SERIAL("after mqtt connect check net.status: " + String(net.status()));
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,14 +6,10 @@
|
|||||||
void newcmd(String cmd) {
|
void newcmd(String cmd) {
|
||||||
cmd.toLowerCase();
|
cmd.toLowerCase();
|
||||||
if(cmd.equals("off")) {
|
if(cmd.equals("off")) {
|
||||||
settings.mode = 0;
|
|
||||||
PRINTLN_SERIAL("Lights off");
|
PRINTLN_SERIAL("Lights off");
|
||||||
state.text = "Sonoff switched off";
|
|
||||||
}
|
}
|
||||||
if(cmd.equals("on")) {
|
if(cmd.equals("on")) {
|
||||||
settings.mode = 1;
|
|
||||||
PRINTLN_SERIAL("Lights on");
|
PRINTLN_SERIAL("Lights on");
|
||||||
state.text = "Sonoff switched off";
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user