Arduino/ESP8266-MQTT-Teufel-DS5/ESP8266-MQTT-Teufel-DS5.ino
2018-05-20 14:29:01 +02:00

375 lines
9.9 KiB
C++

/**********************************************************************
* 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-Teufel-DS5-Control
*
* Links the ESP8266 based control board to an mqtt broker.
* The program uses multiple topics for incoming and outgoing messages.
* Incoming topics:
* - <mqtt_device>/command
* Outgoing topics:
* - <mqtt_device>/powerState
**********************************************************************/
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <PubSubClient.h>
#include "ESP8266-MQTT-Teufel-DS5.h"
#define DEBUGTOSERIAL 1
// Timing for publishing powerState
unsigned long lastMsg = 0;
unsigned long publishInterval = 5000;
// Message buffer
char msg[50];
// Last known state
int powerState = 0;
// Volume control. Valid range is [-55,10]
int curVol = 0;
int targetVol = 0;
unsigned long lastVolChange = 0;
unsigned long volumeInterval = 500;
unsigned long volDisplayDelay = 7000;
uint8_t otherCommandSend = 0;
// Process tracker for calibration. 0 = nothing, 1 = decreasing, 2 = increasing
uint8_t recalibrateState = 0;
int calibrateLowerLimit = -55;
int calibrateUpperLimit = -22;
// Pin config
const int irSendPin = 13;
const int powerProbePin = 12;
// Global mqtt client object
WiFiClient espClient;
PubSubClient client(espClient);
// Command/code mapping
char* commands[] = {
"power", "mute", "51", "opt1",
"opt2", "coax1", "coax2", "tv",
"cd", "aux", "display", "return",
"mode", "speaker", "test", //"volUp",
"left", "ok", "right", //"volDown",
"fLeft", "center", "fRight", "sLeft",
"sub", "sRight"};
uint32_t codes[] = {
0x807F50AF, 0x807FD02F, 0x807F708F, 0x807F609F,
0x807FF00F, 0x807F48B7, 0x807FE01F, 0x807FC837,
0x807F6897, 0x807F40BF, 0x807FE817, 0x807FC03F,
0x807FE21D, 0x807F629D, 0x807FA25D, //0x807F7A85,
0x807FDA25, 0x807F5AA5, 0x807F1AE5, //0x807F6A95,
0x807FCA35, 0x807F4AB5, 0x807F8A75, 0x807FF20D,
0x807F728D, 0x807FB24D};
/*
* Initial setup for arduino
*/
void setup() {
// Configure pins
pinMode(irSendPin, OUTPUT);
digitalWrite(irSendPin, HIGH);
// 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 (uint8_t i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
}
char* msgCopy = (char*) malloc(length + 1);
memcpy(msgCopy, payload, length);
msgCopy[length] = 0;
// Variable to store the IR code
uint32_t code = 0;
if (strcmp(topic, mqtt_topic_command) == 0) {
// Regular command channel triggered
for (uint8_t i = 0; i < 24; ++i) {
// Compare payload against known commands
if (strcmp(msgCopy, commands[i]) == 0) {
// Store code and be done
code = codes[i];
break;
}
}
// Check other special commands
if (strcmp(msgCopy, "volReset") == 0) {
// Recalibrate volume
recalibrateState = 1;
curVol = 10;
targetVol = calibrateLowerLimit;
}
if (strcmp(msgCopy, "volUp") == 0) {
if (recalibrateState == 0 && targetVol < 10) {
if (DEBUGTOSERIAL) {
Serial.print("Setting target volume to ");
Serial.println(targetVol + 1);
}
targetVol += 1;
}
}
if (strcmp(msgCopy, "volDown") == 0) {
if (recalibrateState == 0 && targetVol > -55) {
if (DEBUGTOSERIAL) {
Serial.print("Setting target volume to ");
Serial.println(targetVol - 1);
}
targetVol -= 1;
}
}
}
if (strcmp(topic, mqtt_topic_volume) == 0) {
int newTargetVol = atoi(msgCopy);
if (recalibrateState == 0) {
if (DEBUGTOSERIAL) {
Serial.print("Setting target volume to ");
Serial.println(newTargetVol);
}
targetVol = newTargetVol;
}
}
if (strcmp(topic, mqtt_topic_power) == 0) {
code = codes[0];
}
free(msgCopy);
// Check if we found a code to send
if (code != 0) {
replicateNEC(irSendPin, code, 32);
}
}
/*
* 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_command);
client.subscribe(mqtt_topic_volume);
client.subscribe(mqtt_topic_power);
if (DEBUGTOSERIAL) {
Serial.println("Subscriptions done");
}
} 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 updateState() {
// Read value from digital pin (Levels are inverted)
powerState = digitalRead(powerProbePin);
if (DEBUGTOSERIAL) {
Serial.print("New power state determined: ");
Serial.println(powerState);
}
snprintf(msg, 49, "%d", powerState);
client.publish(mqtt_topic_powerState, msg);
// Update volume state
snprintf(msg, 49, "%d", curVol);
client.publish(mqtt_topic_volumeState, msg);
}
void updateVolume(unsigned long timeSinceChange) {
if (otherCommandSend || timeSinceChange > volDisplayDelay) {
// Switch display to volume mode
replicateNEC(irSendPin, 0x807F7A85, 32);
} else {
// Increase volume a notch if requested
if (targetVol > curVol) {
replicateNEC(irSendPin, 0x807F7A85, 32);
curVol += 1;
snprintf(msg, 49, "%d", curVol);
client.publish(mqtt_topic_volumeState, msg);
}
// Decrease volume a notch if requested
if (targetVol < curVol) {
replicateNEC(irSendPin, 0x807F6A95, 32);
curVol -= 1;
snprintf(msg, 49, "%d", curVol);
client.publish(mqtt_topic_volumeState, msg);
}
// Check recalibration state
if (recalibrateState == 2 && curVol == calibrateUpperLimit) {
recalibrateState = 0;
}
if (recalibrateState == 1 && curVol == calibrateLowerLimit) {
recalibrateState = 2;
targetVol = calibrateUpperLimit;
}
}
}
void replicateNEC(int pin, uint32_t data, int dataLength) {
// Track if some other command than volume control has been sent in the meantime
if (data == 0x807F7A85 || data == 0x807F6A95) {
otherCommandSend = 0;
} else {
otherCommandSend = 1;
}
if (DEBUGTOSERIAL) {
Serial.print("Sending code ");
Serial.println(data, HEX);
}
// Prepare mask
uint32_t mask = 1L << (dataLength -1);
// Write initial 9ms+4.5ms pulse
digitalWrite(pin, LOW);
delayMicroseconds(9000);
digitalWrite(pin, HIGH);
delayMicroseconds(4500);
// Write pulses from data
while (mask > 0) {
digitalWrite(pin, LOW);
delayMicroseconds(562);
digitalWrite(pin, HIGH);
delayMicroseconds(562);
if (data & mask) {
delayMicroseconds(1124);
}
mask = mask >> 1;
}
// Write trailing pulse
digitalWrite(pin, LOW);
delayMicroseconds(562);
digitalWrite(pin, HIGH);
}
void loop() {
// Check OTA update
ArduinoOTA.handle();
// Ensure MQTT connection
if (!client.connected()) {
reconnect();
}
// Check the inbox
client.loop();
// Maybe update the volume
unsigned long now = millis();
if (targetVol != curVol && now - lastVolChange > volumeInterval) {
updateVolume(now - lastVolChange);
lastVolChange = now;
}
// Maybe push the current status
if (now - lastMsg > publishInterval) {
lastMsg = now;
updateState();
}
}