Add switch debouncing, LWT support, change topic
parent
e22cc17e3f
commit
007181dbad
|
@ -2,30 +2,54 @@
|
|||
#include <common_config.h>
|
||||
#include <SmingCore/SmingCore.h>
|
||||
|
||||
bool state = false;
|
||||
|
||||
MqttClient mqtt(MQTT_BROKER, MQTT_PORT, *[](String topic, String message) {
|
||||
Serial.printf("*** message received @ %s:\n\t%s\n***\n", topic.c_str(), message.c_str());
|
||||
digitalWrite(LED_PIN, message == "1");
|
||||
if(message == "on" || message == "1") {
|
||||
state = true;
|
||||
} else if(message == "off" || message == "0") {
|
||||
state = false;
|
||||
} else if(message == "toggle") {
|
||||
state = !state;
|
||||
}
|
||||
|
||||
digitalWrite(LED_PIN, !state);
|
||||
});
|
||||
|
||||
Timer keepaliveTimer;
|
||||
String deviceName;
|
||||
|
||||
void startMqttClient()
|
||||
{
|
||||
String deviceName = "light-" + WifiStation.getMAC().substring(6, 12);
|
||||
Serial.println("*** Connecting to MQTT as " + deviceName);
|
||||
|
||||
mqtt.setWill("main/status/" + deviceName, "offline", 1, true);
|
||||
mqtt.connect(deviceName);
|
||||
mqtt.subscribe("btn/status");
|
||||
mqtt.publish("main/status/" + deviceName, "online");
|
||||
|
||||
keepaliveTimer.initializeMs(5000, *[] {
|
||||
mqtt.publish("main/status/" + deviceName, "alive " + String(millis()));
|
||||
}).start();
|
||||
|
||||
mqtt.subscribe("light/status");
|
||||
}
|
||||
|
||||
void init()
|
||||
{
|
||||
deviceName = "light-" + WifiStation.getMAC().substring(6, 12);
|
||||
|
||||
Serial.begin(SERIAL_BAUD_RATE); // 115200 by default
|
||||
Serial.systemDebugOutput(true); // Debug output to serial
|
||||
|
||||
Serial.println("*** Starting ...");
|
||||
Serial.println("*** Starting " + deviceName + " ...");
|
||||
|
||||
WifiStation.config(WIFI_SSID, WIFI_PWD);
|
||||
WifiStation.enable(true);
|
||||
|
||||
WifiAccessPoint.enable(false);
|
||||
|
||||
|
||||
WifiStation.waitConnection(*[] {
|
||||
Serial.println("*** Connection succeeded");
|
||||
startMqttClient();
|
||||
|
@ -34,4 +58,5 @@ void init()
|
|||
});
|
||||
|
||||
pinMode(LED_PIN, OUTPUT);
|
||||
digitalWrite(LED_PIN, HIGH);
|
||||
}
|
||||
|
|
|
@ -1,19 +1,21 @@
|
|||
import paho.mqtt.client as mqtt
|
||||
import time
|
||||
import random
|
||||
|
||||
# The callback for when the client receives a CONNACK response from the server.
|
||||
def on_connect(client, userdata, rc):
|
||||
print("Connected with result code "+str(rc))
|
||||
# Subscribing in on_connect() means that if we lose the connection and
|
||||
# reconnect then subscriptions will be renewed.
|
||||
client.subscribe("btn/status")
|
||||
client.subscribe("light/status")
|
||||
client.subscribe("#")
|
||||
client.publish("main/status/penisy", "dupa")
|
||||
|
||||
# The callback for when a PUBLISH message is received from the server.
|
||||
def on_message(client, userdata, msg):
|
||||
print(str(time.time())+" "+msg.topic+" "+str(msg.payload))
|
||||
|
||||
client = mqtt.Client("test-client")
|
||||
client = mqtt.Client("test-client-%d" % random.randint(100, 999))
|
||||
client.on_connect = on_connect
|
||||
client.on_message = on_message
|
||||
|
||||
|
|
|
@ -6,25 +6,37 @@ MqttClient mqtt(MQTT_BROKER, MQTT_PORT, *[](String topic, String message) {
|
|||
Serial.printf("*** message received @ %s:\n\t%s\n***\n", topic.c_str(), message.c_str());
|
||||
});
|
||||
|
||||
Timer keepaliveTimer;
|
||||
String deviceName;
|
||||
|
||||
void startMqttClient()
|
||||
{
|
||||
String deviceName = "switch-" + WifiStation.getMAC().substring(6, 12);
|
||||
Serial.println("*** Connecting to MQTT as " + deviceName);
|
||||
|
||||
mqtt.setWill("main/status/" + deviceName, "offline", 1, true);
|
||||
mqtt.connect(deviceName);
|
||||
mqtt.subscribe("main/status/#");
|
||||
mqtt.publish("main/status/" + deviceName, "online");
|
||||
|
||||
keepaliveTimer.initializeMs(5000, *[] {
|
||||
mqtt.publish("main/status/" + deviceName, "alive " + String(millis()));
|
||||
}).start();
|
||||
}
|
||||
|
||||
void init()
|
||||
{
|
||||
deviceName = "switch-" + WifiStation.getMAC().substring(6, 12);
|
||||
|
||||
Serial.begin(SERIAL_BAUD_RATE); // 115200 by default
|
||||
Serial.systemDebugOutput(false); // Debug output to serial
|
||||
|
||||
Serial.println("*** Starting ...");
|
||||
Serial.println("*** Starting " + deviceName + " ...");
|
||||
|
||||
WifiStation.config(WIFI_SSID, WIFI_PWD);
|
||||
WifiStation.enable(true);
|
||||
|
||||
WifiAccessPoint.enable(false);
|
||||
|
||||
|
||||
WifiStation.waitConnection(*[] {
|
||||
Serial.println("*** Connection succeeded");
|
||||
startMqttClient();
|
||||
|
@ -33,8 +45,16 @@ void init()
|
|||
});
|
||||
|
||||
attachInterrupt(BTN_PIN, *[] {
|
||||
bool btnState = digitalRead(BTN_PIN);
|
||||
Serial.printf("*** Button state: %d\n", btnState);
|
||||
mqtt.publish("btn/status", String(btnState));
|
||||
}, CHANGE);
|
||||
static int lastSwitch = 0;
|
||||
|
||||
// Debouncing
|
||||
if(lastSwitch + 150 > millis()) {
|
||||
Serial.println("--- debouncing");
|
||||
return;
|
||||
}
|
||||
lastSwitch = millis();
|
||||
|
||||
Serial.println("*** Button pressed");
|
||||
mqtt.publish("light/status", "toggle");
|
||||
}, FALLING);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue