Updated Aten Magic Box
This commit is contained in:
parent
f2c95ed630
commit
b217b29ad7
1 changed files with 19 additions and 84 deletions
|
@ -40,8 +40,6 @@
|
||||||
#include <PubSubClient.h>
|
#include <PubSubClient.h>
|
||||||
#include "ESP8266-MQTT-Aten-MagicBox.h"
|
#include "ESP8266-MQTT-Aten-MagicBox.h"
|
||||||
|
|
||||||
#define DEBUGTOSERIAL 1
|
|
||||||
|
|
||||||
// Timestamp for last publish
|
// Timestamp for last publish
|
||||||
long lastMsg = 0;
|
long lastMsg = 0;
|
||||||
long publishInterval = 5000;
|
long publishInterval = 5000;
|
||||||
|
@ -55,7 +53,7 @@ PubSubClient client(espClient);
|
||||||
*/
|
*/
|
||||||
void setup() {
|
void setup() {
|
||||||
// Configure serial port
|
// Configure serial port
|
||||||
Serial.begin(115200);
|
Serial.begin(19200);
|
||||||
delay(10);
|
delay(10);
|
||||||
|
|
||||||
// Prepare WiFi connection
|
// Prepare WiFi connection
|
||||||
|
@ -74,25 +72,9 @@ void setup_wifi() {
|
||||||
WiFi.mode(WIFI_STA);
|
WiFi.mode(WIFI_STA);
|
||||||
|
|
||||||
// Do the connection
|
// Do the connection
|
||||||
if (DEBUGTOSERIAL) {
|
|
||||||
Serial.println();
|
|
||||||
Serial.print("Connecting to ");
|
|
||||||
Serial.println(ssid);
|
|
||||||
}
|
|
||||||
WiFi.begin(ssid, password);
|
WiFi.begin(ssid, password);
|
||||||
while (WiFi.status() != WL_CONNECTED) {
|
while (WiFi.status() != WL_CONNECTED) {
|
||||||
delay(500);
|
delay(500);
|
||||||
if (DEBUGTOSERIAL) {
|
|
||||||
Serial.print(".");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (DEBUGTOSERIAL) {
|
|
||||||
Serial.println("");
|
|
||||||
Serial.println("WiFi connected");
|
|
||||||
|
|
||||||
// Print IP address to serial
|
|
||||||
Serial.print("My IP address: ");
|
|
||||||
Serial.println(WiFi.localIP());
|
|
||||||
}
|
}
|
||||||
ArduinoOTA.setHostname(ota_hostname);
|
ArduinoOTA.setHostname(ota_hostname);
|
||||||
ArduinoOTA.setPassword(ota_hostname);
|
ArduinoOTA.setPassword(ota_hostname);
|
||||||
|
@ -103,28 +85,12 @@ void setup_wifi() {
|
||||||
* Callback method for incoming mqtt messages
|
* Callback method for incoming mqtt messages
|
||||||
*/
|
*/
|
||||||
void incoming_mqtt(char* topic, byte* payload, unsigned int length) {
|
void incoming_mqtt(char* topic, byte* payload, unsigned int length) {
|
||||||
if (DEBUGTOSERIAL) {
|
if (strcmp(topic, mqtt_topic_port) == 0) {
|
||||||
Serial.print("Message arrived [");
|
|
||||||
Serial.print(topic);
|
|
||||||
Serial.print("] ");
|
|
||||||
for (int i = 0; i < length; i++) {
|
|
||||||
Serial.print((char)payload[i]);
|
|
||||||
}
|
|
||||||
Serial.println();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (strcmp(topic, mqtt_topic_power) == 0) {
|
|
||||||
if (length == 1) {
|
if (length == 1) {
|
||||||
if (DEBUGTOSERIAL) {
|
// Select the port
|
||||||
Serial.println("Setting new power state");
|
Serial.print("\rsw i0");
|
||||||
}
|
Serial.print((char)payload[0]);
|
||||||
if ((char)payload[0] == '0') {
|
Serial.print("\r");
|
||||||
// Switch power off
|
|
||||||
Serial.print("\r*pow=off#\r");
|
|
||||||
} else {
|
|
||||||
// Switch power on
|
|
||||||
Serial.print("\r*pow=on#\r");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -135,22 +101,11 @@ void incoming_mqtt(char* topic, byte* payload, unsigned int length) {
|
||||||
void reconnect() {
|
void reconnect() {
|
||||||
// Loop until we're reconnected
|
// Loop until we're reconnected
|
||||||
while (!client.connected()) {
|
while (!client.connected()) {
|
||||||
if (DEBUGTOSERIAL) {
|
|
||||||
Serial.print("Attempting MQTT connection...");
|
|
||||||
}
|
|
||||||
// Attempt to connect
|
// Attempt to connect
|
||||||
if (client.connect(mqtt_device)) {
|
if (client.connect(mqtt_device)) {
|
||||||
if (DEBUGTOSERIAL) {
|
|
||||||
Serial.println("connected");
|
|
||||||
}
|
|
||||||
// subscribe to incoming topics
|
// subscribe to incoming topics
|
||||||
client.subscribe(mqtt_topic_power);
|
client.subscribe(mqtt_topic_port);
|
||||||
} else {
|
} else {
|
||||||
if (DEBUGTOSERIAL) {
|
|
||||||
Serial.print("failed, rc=");
|
|
||||||
Serial.print(client.state());
|
|
||||||
Serial.println(" try again in 5 seconds");
|
|
||||||
}
|
|
||||||
// Wait 5 seconds before retrying
|
// Wait 5 seconds before retrying
|
||||||
delay(5000);
|
delay(5000);
|
||||||
}
|
}
|
||||||
|
@ -163,44 +118,25 @@ void clearSerialInput() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void updateLamptime() {
|
void updateState() {
|
||||||
|
Serial.print("\r");
|
||||||
|
delay(250);
|
||||||
// Clean input buffer
|
// Clean input buffer
|
||||||
clearSerialInput();
|
clearSerialInput();
|
||||||
|
|
||||||
// Write request to serial
|
// Write request to serial
|
||||||
Serial.print("\r*ltim=?#\r");
|
Serial.print("read\r");
|
||||||
delay(250);
|
delay(250);
|
||||||
|
|
||||||
// Extract lamp time from response
|
// Extract lamp time from response
|
||||||
String message = Serial.readString();
|
String message = Serial.readString();
|
||||||
int startIndex = message.lastIndexOf('=');
|
|
||||||
int endIndex = message.lastIndexOf('#');
|
int startIndex = message.lastIndexOf("Input:port");
|
||||||
if (startIndex != -1 && endIndex != -1) {
|
if (startIndex != -1) {
|
||||||
char publi[endIndex - startIndex];
|
String state = message.substring(startIndex + 11, startIndex + 12);
|
||||||
message.substring(startIndex + 1, endIndex).toCharArray(publi, (endIndex - startIndex));
|
char buffer[2];
|
||||||
// client.publish(mqtt_topic_lamptime, publi);
|
state.toCharArray(buffer, 2);
|
||||||
}
|
client.publish(mqtt_topic_portState, buffer);
|
||||||
}
|
|
||||||
|
|
||||||
void updatePowerState() {
|
|
||||||
// Clean input buffer
|
|
||||||
clearSerialInput();
|
|
||||||
|
|
||||||
// Write request to serial
|
|
||||||
Serial.print("\r*pow=?#\r");
|
|
||||||
delay(250);
|
|
||||||
|
|
||||||
// Extract lamp time from response
|
|
||||||
String message = Serial.readString();
|
|
||||||
int startIndex = message.lastIndexOf('=');
|
|
||||||
int endIndex = message.lastIndexOf('#');
|
|
||||||
if (startIndex != -1 && endIndex != -1) {
|
|
||||||
String state = message.substring(startIndex + 1, endIndex);
|
|
||||||
if (state.equalsIgnoreCase("off")) {
|
|
||||||
// client.publish(mqtt_topic_powerState, "0");
|
|
||||||
} else if (state.equalsIgnoreCase("on")) {
|
|
||||||
// client.publish(mqtt_topic_powerState, "1");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -220,7 +156,6 @@ void loop() {
|
||||||
long now = millis();
|
long now = millis();
|
||||||
if (now - lastMsg > publishInterval) {
|
if (now - lastMsg > publishInterval) {
|
||||||
lastMsg = now;
|
lastMsg = now;
|
||||||
updateLamptime();
|
updateState();
|
||||||
updatePowerState();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue