Added draft of Aten switcher
This commit is contained in:
parent
e8f2f26b7b
commit
ec7dacbbc4
3 changed files with 242 additions and 0 deletions
226
ESP8266-MQTT-Aten-MagicBox/ESP8266-MQTT-Aten-MagicBox.ino
Normal file
226
ESP8266-MQTT-Aten-MagicBox/ESP8266-MQTT-Aten-MagicBox.ino
Normal file
|
@ -0,0 +1,226 @@
|
||||||
|
/**********************************************************************
|
||||||
|
* The MIT License (MIT)
|
||||||
|
*
|
||||||
|
* Copyright (c) 2015 Nis Wechselberg
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
**********************************************************************/
|
||||||
|
|
||||||
|
/**********************************************************************
|
||||||
|
* ESP8266-MQTT-BenQ-BeamerControl
|
||||||
|
*
|
||||||
|
* Links the ESP8266 based serial beamer control board to an mqtt broker.
|
||||||
|
* The program uses multiple topics for incoming and outgoing messages.
|
||||||
|
* Incoming topics:
|
||||||
|
* - <mqtt_device>/power
|
||||||
|
* Outgoing topics:
|
||||||
|
*
|
||||||
|
**********************************************************************/
|
||||||
|
|
||||||
|
#include <ESP8266WiFi.h>
|
||||||
|
#include <ESP8266mDNS.h>
|
||||||
|
#include <WiFiUdp.h>
|
||||||
|
#include <ArduinoOTA.h>
|
||||||
|
#include <PubSubClient.h>
|
||||||
|
#include "ESP8266-MQTT-Aten-MagicBox.h"
|
||||||
|
|
||||||
|
#define DEBUGTOSERIAL 1
|
||||||
|
|
||||||
|
// Timestamp for last publish
|
||||||
|
long lastMsg = 0;
|
||||||
|
long publishInterval = 5000;
|
||||||
|
|
||||||
|
// Global mqtt client object
|
||||||
|
WiFiClient espClient;
|
||||||
|
PubSubClient client(espClient);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Initial setup for arduino
|
||||||
|
*/
|
||||||
|
void setup() {
|
||||||
|
// Configure serial port
|
||||||
|
Serial.begin(115200);
|
||||||
|
delay(10);
|
||||||
|
|
||||||
|
// Prepare WiFi connection
|
||||||
|
setup_wifi();
|
||||||
|
|
||||||
|
// Connect to mqtt broker
|
||||||
|
client.setServer(mqtt_server, 1883);
|
||||||
|
client.setCallback(incoming_mqtt);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Prepares the wireless connection
|
||||||
|
*/
|
||||||
|
void setup_wifi() {
|
||||||
|
// Connect to the WiFi as a client
|
||||||
|
WiFi.mode(WIFI_STA);
|
||||||
|
|
||||||
|
// Do the connection
|
||||||
|
if (DEBUGTOSERIAL) {
|
||||||
|
Serial.println();
|
||||||
|
Serial.print("Connecting to ");
|
||||||
|
Serial.println(ssid);
|
||||||
|
}
|
||||||
|
WiFi.begin(ssid, password);
|
||||||
|
while (WiFi.status() != WL_CONNECTED) {
|
||||||
|
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.setPassword(ota_hostname);
|
||||||
|
ArduinoOTA.begin();
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Callback method for incoming mqtt messages
|
||||||
|
*/
|
||||||
|
void incoming_mqtt(char* topic, byte* payload, unsigned int length) {
|
||||||
|
if (DEBUGTOSERIAL) {
|
||||||
|
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 (DEBUGTOSERIAL) {
|
||||||
|
Serial.println("Setting new power state");
|
||||||
|
}
|
||||||
|
if ((char)payload[0] == '0') {
|
||||||
|
// Switch power off
|
||||||
|
Serial.print("\r*pow=off#\r");
|
||||||
|
} else {
|
||||||
|
// Switch power on
|
||||||
|
Serial.print("\r*pow=on#\r");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Blocking reconnect to the mqtt broker
|
||||||
|
*/
|
||||||
|
void reconnect() {
|
||||||
|
// Loop until we're reconnected
|
||||||
|
while (!client.connected()) {
|
||||||
|
if (DEBUGTOSERIAL) {
|
||||||
|
Serial.print("Attempting MQTT connection...");
|
||||||
|
}
|
||||||
|
// Attempt to connect
|
||||||
|
if (client.connect(mqtt_device)) {
|
||||||
|
if (DEBUGTOSERIAL) {
|
||||||
|
Serial.println("connected");
|
||||||
|
}
|
||||||
|
// subscribe to incoming topics
|
||||||
|
client.subscribe(mqtt_topic_power);
|
||||||
|
} else {
|
||||||
|
if (DEBUGTOSERIAL) {
|
||||||
|
Serial.print("failed, rc=");
|
||||||
|
Serial.print(client.state());
|
||||||
|
Serial.println(" try again in 5 seconds");
|
||||||
|
}
|
||||||
|
// Wait 5 seconds before retrying
|
||||||
|
delay(5000);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void clearSerialInput() {
|
||||||
|
while(Serial.available() > 0) {
|
||||||
|
char t = Serial.read();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void updateLamptime() {
|
||||||
|
// Clean input buffer
|
||||||
|
clearSerialInput();
|
||||||
|
|
||||||
|
// Write request to serial
|
||||||
|
Serial.print("\r*ltim=?#\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) {
|
||||||
|
char publi[endIndex - startIndex];
|
||||||
|
message.substring(startIndex + 1, endIndex).toCharArray(publi, (endIndex - startIndex));
|
||||||
|
// client.publish(mqtt_topic_lamptime, publi);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
// Check OTA update
|
||||||
|
ArduinoOTA.handle();
|
||||||
|
|
||||||
|
// Ensure MQTT connection
|
||||||
|
if (!client.connected()) {
|
||||||
|
reconnect();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check the inbox
|
||||||
|
client.loop();
|
||||||
|
|
||||||
|
// Maybe push the current status
|
||||||
|
long now = millis();
|
||||||
|
if (now - lastMsg > publishInterval) {
|
||||||
|
lastMsg = now;
|
||||||
|
updateLamptime();
|
||||||
|
updatePowerState();
|
||||||
|
}
|
||||||
|
}
|
16
ESP8266-MQTT-Aten-MagicBox/command_Responses
Normal file
16
ESP8266-MQTT-Aten-MagicBox/command_Responses
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
Command: read\r
|
||||||
|
Output:
|
||||||
|
[11:14:55:119] read Command OK␊
|
||||||
|
[11:14:55:128] ␍ Input:port 3␊
|
||||||
|
[11:14:55:136] ␍ Output:ON␊
|
||||||
|
[11:14:55:141] ␍ Mode:Default␊
|
||||||
|
[11:14:55:150] ␍ Goto:OFF␊
|
||||||
|
[11:14:55:155] ␍ F/W:V1.1.101␊
|
||||||
|
|
||||||
|
Command: sw i04\r
|
||||||
|
Output:
|
||||||
|
[11:16:30:131] ␍sw i04 Command OK␊
|
||||||
|
|
||||||
|
Command: swmode default\r
|
||||||
|
Output:
|
||||||
|
[11:16:57:881] ␍swmode default Command OK␊
|
BIN
ESP8266-MQTT-Aten-MagicBox/vs481b_um_w_2021-01-13.pdf
Normal file
BIN
ESP8266-MQTT-Aten-MagicBox/vs481b_um_w_2021-01-13.pdf
Normal file
Binary file not shown.
Loading…
Reference in a new issue