/********************************************************************** * 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: * - /power * Outgoing topics: * **********************************************************************/ #include #include #include #include #include #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 Serial.begin(19200); 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) { if (strcmp(topic, mqtt_topic_port) == 0) { if (length == 1) { // Select the port Serial.print("\rsw i0"); Serial.print((char)payload[0]); Serial.print("\r"); } } } /* * 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 client.subscribe(mqtt_topic_port); } else { // Wait 5 seconds before retrying delay(5000); } } } void clearSerialInput() { while(Serial.available() > 0) { char t = Serial.read(); } } void updateState() { Serial.print("\r"); delay(250); // Clean input buffer clearSerialInput(); // Write request to serial Serial.print("read\r"); delay(250); // Extract lamp time from response String message = Serial.readString(); 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); } } 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; updateState(); } }