Arduino/ESP8266-MQTT-Aten-MagicBox/ESP8266-MQTT-Aten-MagicBox.ino

162 lines
4.1 KiB
Arduino
Raw Permalink Normal View History

2021-08-01 11:23:07 +02:00
/**********************************************************************
* 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"
// 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
2022-03-07 09:23:25 +01:00
Serial.begin(19200);
2021-08-01 11:23:07 +02:00
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
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
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) {
2022-03-07 09:23:25 +01:00
if (strcmp(topic, mqtt_topic_port) == 0) {
2021-08-01 11:23:07 +02:00
if (length == 1) {
2022-03-07 09:23:25 +01:00
// Select the port
Serial.print("\rsw i0");
Serial.print((char)payload[0]);
Serial.print("\r");
2021-08-01 11:23:07 +02:00
}
}
}
/*
* Blocking reconnect to the mqtt broker
*/
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
// Attempt to connect
if (client.connect(mqtt_device)) {
// subscribe to incoming topics
2022-03-07 09:23:25 +01:00
client.subscribe(mqtt_topic_port);
2021-08-01 11:23:07 +02:00
} else {
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void clearSerialInput() {
while(Serial.available() > 0) {
char t = Serial.read();
}
}
2022-03-07 09:23:25 +01:00
void updateState() {
Serial.print("\r");
2021-08-01 11:23:07 +02:00
delay(250);
// Clean input buffer
clearSerialInput();
// Write request to serial
2022-03-07 09:23:25 +01:00
Serial.print("read\r");
2021-08-01 11:23:07 +02:00
delay(250);
// Extract lamp time from response
String message = Serial.readString();
2022-03-07 09:23:25 +01:00
int startIndex = message.lastIndexOf("Input:port");
if (startIndex != -1) {
String state = message.substring(startIndex + 11, startIndex + 12);
char buffer[2];
state.toCharArray(buffer, 2);
client.publish(mqtt_topic_portState, buffer);
2021-08-01 11:23:07 +02:00
}
}
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;
2022-03-07 09:23:25 +01:00
updateState();
2021-08-01 11:23:07 +02:00
}
}