Change mqtt settings
This commit is contained in:
@@ -22,92 +22,25 @@ WiFiEventHandler wifiConnectHandler;
|
||||
WiFiEventHandler wifiDisconnectHandler;
|
||||
Ticker wifiReconnectTimer;
|
||||
|
||||
bool lStat1, oldLStat1;
|
||||
bool lStat2, oldLStat2;
|
||||
//bool lStat1, oldLStat1;
|
||||
//bool lStat2, oldLStat2;
|
||||
unsigned long cRun;
|
||||
//bool rcv;
|
||||
#define B_LED (1)
|
||||
#define R_LED (12)
|
||||
#define BUTT (5)
|
||||
bool led = false;
|
||||
//bool led = false;
|
||||
|
||||
OneButton button(BUTT);
|
||||
bool switchLight(uint8_t nLamp, int state, bool pub);
|
||||
void connectToWifi();
|
||||
void connectToMqtt();
|
||||
void onWifiConnect(const WiFiEventStationModeGotIP& event);
|
||||
void onWifiDisconnect(const WiFiEventStationModeDisconnected& event);
|
||||
void onMqttConnect(bool sessionPresent);
|
||||
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason);
|
||||
void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total);
|
||||
|
||||
void connectToWifi() {
|
||||
//Serial.println("Connecting to Wi-Fi...");
|
||||
WiFi.begin(ssid, password);
|
||||
}
|
||||
|
||||
void connectToMqtt() {
|
||||
//Serial.println("Connecting to MQTT...");
|
||||
mqttClient.connect();
|
||||
}
|
||||
|
||||
void onWifiConnect(const WiFiEventStationModeGotIP& event) {
|
||||
//Serial.println("Connected to Wi-Fi.");
|
||||
//Serial.print("IP: ");
|
||||
//Serial.println(WiFi.localIP());
|
||||
connectToMqtt();
|
||||
}
|
||||
|
||||
void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) {
|
||||
//Serial.println("Disconnected from Wi-Fi.");
|
||||
mqttReconnectTimer.detach(); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi
|
||||
wifiReconnectTimer.once(2, connectToWifi);
|
||||
}
|
||||
|
||||
void onMqttConnect(bool sessionPresent) {
|
||||
// Serial.println("Connected to MQTT.");
|
||||
// Serial.print("Session present: ");
|
||||
// Serial.println(sessionPresent);
|
||||
//uint16_t packetIdSub =
|
||||
mqttClient.subscribe("/home/kor/lamp1_set", 0);
|
||||
// Serial.print("Subscribing Lamp1, packetId: ");
|
||||
// Serial.println(packetIdSub);
|
||||
//packetIdSub = mqttClient.subscribe("/home/kor/lamp2_set", 1);
|
||||
//Serial.print("Subscribing Lamp2, packetId: ");
|
||||
//Serial.println(packetIdSub);
|
||||
mqttClient.publish("/home/kor/lamp1", 0, false, lStat1 ? "1" : "0");
|
||||
//Serial.println("Publishing at Lamp 1");
|
||||
//mqttClient.publish("/home/kor/lamp2", 1, false, lStat2 ? "1" : "0");
|
||||
//Serial.println("Publishing at Lamp 2");
|
||||
digitalWrite(B_LED, LOW);
|
||||
}
|
||||
|
||||
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
|
||||
//Serial.println("Disconnected from MQTT.");
|
||||
|
||||
if (WiFi.isConnected()) {
|
||||
mqttReconnectTimer.once(2, connectToMqtt);
|
||||
}
|
||||
digitalWrite(B_LED, HIGH);
|
||||
}
|
||||
|
||||
void onMqttSubscribe(uint16_t packetId, uint8_t qos) {
|
||||
// Serial.println("Subscribe acknowledged.");
|
||||
// Serial.print(" packetId: ");
|
||||
// Serial.println(packetId);
|
||||
// Serial.print(" qos: ");
|
||||
// Serial.println(qos);
|
||||
}
|
||||
|
||||
void onMqttUnsubscribe(uint16_t packetId) {
|
||||
// Serial.println("Unsubscribe acknowledged.");
|
||||
// Serial.print(" packetId: ");
|
||||
// Serial.println(packetId);
|
||||
}
|
||||
|
||||
void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
|
||||
if(strcmp(topic, "/home/kor/lamp1_set") == 0){
|
||||
if (atoi(payload) == 1) lStat1 = true;
|
||||
else lStat1 = false;
|
||||
}
|
||||
}
|
||||
|
||||
void onMqttPublish(uint16_t packetId) {
|
||||
// Serial.println("Publish acknowledged.");
|
||||
// Serial.print(" packetId: ");
|
||||
// Serial.println(packetId);
|
||||
}
|
||||
|
||||
// void ICACHE_RAM_ATTR sw_func()
|
||||
// {
|
||||
@@ -116,18 +49,21 @@ void onMqttPublish(uint16_t packetId) {
|
||||
|
||||
void oneClick()
|
||||
{
|
||||
lStat1 = !lStat1;
|
||||
switchLight(R_LED, !digitalRead(R_LED), true);
|
||||
//lStat1 = !lStat1;
|
||||
}
|
||||
|
||||
void longPress()
|
||||
{
|
||||
lStat1 = false;
|
||||
mqttClient.publish("/home/bigroom/lamp1_set", 0, false, "0");
|
||||
mqttClient.publish("/home/bigroom/lamp2_set", 0, false, "0");
|
||||
mqttClient.publish("/home/midroom/lamp1_set", 0, false, "0");
|
||||
mqttClient.publish("/home/midroom/lamp2_set", 0, false, "0");
|
||||
mqttClient.publish("/home/smallroom/lamp1_set", 0, false, "0");
|
||||
mqttClient.publish("/home/smallroom/lamp2_set", 0, false, "0");
|
||||
//lStat1 = false;
|
||||
switchLight(R_LED, 0, true);
|
||||
mqttClient.publish("/home/bigroom/lamp1", 0, false, "0");
|
||||
mqttClient.publish("/home/bigroom/lamp2", 0, false, "0");
|
||||
mqttClient.publish("/home/midroom/lamp1", 0, false, "0");
|
||||
mqttClient.publish("/home/midroom/lamp2", 0, false, "0");
|
||||
mqttClient.publish("/home/smallroom/lamp1", 0, false, "0");
|
||||
mqttClient.publish("/home/smallroom/lamp2", 0, false, "0");
|
||||
mqttClient.publish("/home/kuh/lighttbl", 0, false, "0");
|
||||
}
|
||||
|
||||
void setup() {
|
||||
@@ -165,11 +101,12 @@ void setup() {
|
||||
pinMode(R_LED, OUTPUT);
|
||||
pinMode(BUTT, INPUT_PULLUP);
|
||||
EEPROM.begin(16);
|
||||
EEPROM.get(0, lStat1);
|
||||
oldLStat1 = lStat1;
|
||||
uint8_t lstat = 0;
|
||||
EEPROM.get(0, lstat);
|
||||
//oldLStat1 = lStat1;
|
||||
// Serial.print("Lamp1: ");
|
||||
// Serial.println(lStat1);
|
||||
digitalWrite(R_LED, lStat1);
|
||||
digitalWrite(R_LED, lstat);
|
||||
//oldLStat2 = lStat2;
|
||||
|
||||
wifiConnectHandler = WiFi.onStationModeGotIP(onWifiConnect);
|
||||
@@ -177,10 +114,10 @@ void setup() {
|
||||
|
||||
mqttClient.onConnect(onMqttConnect);
|
||||
mqttClient.onDisconnect(onMqttDisconnect);
|
||||
mqttClient.onSubscribe(onMqttSubscribe);
|
||||
mqttClient.onUnsubscribe(onMqttUnsubscribe);
|
||||
// mqttClient.onSubscribe(onMqttSubscribe);
|
||||
// mqttClient.onUnsubscribe(onMqttUnsubscribe);
|
||||
mqttClient.onMessage(onMqttMessage);
|
||||
mqttClient.onPublish(onMqttPublish);
|
||||
// mqttClient.onPublish(onMqttPublish);
|
||||
mqttClient.setServer(mqtt_server, 1883);
|
||||
mqttClient.setClientId("SW_Koridor");
|
||||
|
||||
@@ -198,13 +135,16 @@ void setup() {
|
||||
void loop() {
|
||||
ArduinoOTA.handle();
|
||||
button.tick();
|
||||
if(lStat1 != oldLStat1){
|
||||
digitalWrite(R_LED, lStat1);
|
||||
oldLStat1 = lStat1;
|
||||
mqttClient.publish("/home/kor/lamp1", 0, false, lStat1 ? "1" : "0");
|
||||
EEPROM.put(0, lStat1);
|
||||
EEPROM.commit();
|
||||
}
|
||||
// if(lStat1 != oldLStat1){
|
||||
// digitalWrite(R_LED, lStat1);
|
||||
// oldLStat1 = lStat1;
|
||||
// if(!rcv)
|
||||
// mqttClient.publish("/home/kor/lamp1", 0, false, lStat1 ? "1" : "0");
|
||||
// else
|
||||
// rcv = false;
|
||||
// EEPROM.put(0, lStat1);
|
||||
// EEPROM.commit();
|
||||
// }
|
||||
if (cRun + 9999 < millis()){
|
||||
cRun = millis();
|
||||
char v[11];
|
||||
@@ -213,3 +153,77 @@ void loop() {
|
||||
}
|
||||
}
|
||||
|
||||
bool switchLight(uint8_t nLamp, int state, bool pub)
|
||||
{
|
||||
digitalWrite(nLamp, state);
|
||||
EEPROM.put(0, state);
|
||||
EEPROM.commit();
|
||||
if (pub) mqttClient.publish("/home/kor/lamp1", 0, false, state ? "1" : "0");
|
||||
return state;
|
||||
}
|
||||
|
||||
void connectToWifi() {
|
||||
//Serial.println("Connecting to Wi-Fi...");
|
||||
WiFi.begin(ssid, password);
|
||||
}
|
||||
|
||||
void connectToMqtt() {
|
||||
//Serial.println("Connecting to MQTT...");
|
||||
mqttClient.connect();
|
||||
}
|
||||
|
||||
void onWifiConnect(const WiFiEventStationModeGotIP& event) {
|
||||
//Serial.println("Connected to Wi-Fi.");
|
||||
//Serial.print("IP: ");
|
||||
//Serial.println(WiFi.localIP());
|
||||
connectToMqtt();
|
||||
}
|
||||
|
||||
void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) {
|
||||
//Serial.println("Disconnected from Wi-Fi.");
|
||||
mqttReconnectTimer.detach(); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi
|
||||
wifiReconnectTimer.once(2, connectToWifi);
|
||||
}
|
||||
|
||||
void onMqttConnect(bool sessionPresent) {
|
||||
mqttClient.publish("/home/kor/lamp1", 0, false, digitalRead(R_LED) == 1 ? "1" : "0");
|
||||
mqttClient.subscribe("/home/kor/lamp1", 0);
|
||||
digitalWrite(B_LED, LOW);
|
||||
}
|
||||
|
||||
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
|
||||
//Serial.println("Disconnected from MQTT.");
|
||||
|
||||
if (WiFi.isConnected()) {
|
||||
mqttReconnectTimer.once(2, connectToMqtt);
|
||||
}
|
||||
digitalWrite(B_LED, HIGH);
|
||||
}
|
||||
|
||||
void onMqttSubscribe(uint16_t packetId, uint8_t qos) {
|
||||
// Serial.println("Subscribe acknowledged.");
|
||||
// Serial.print(" packetId: ");
|
||||
// Serial.println(packetId);
|
||||
// Serial.print(" qos: ");
|
||||
// Serial.println(qos);
|
||||
}
|
||||
|
||||
void onMqttUnsubscribe(uint16_t packetId) {
|
||||
// Serial.println("Unsubscribe acknowledged.");
|
||||
// Serial.print(" packetId: ");
|
||||
// Serial.println(packetId);
|
||||
}
|
||||
|
||||
void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
|
||||
if(strcmp(topic, "/home/kor/lamp1") == 0){
|
||||
if (atoi(payload) == 1) switchLight(R_LED, 1, false);//lStat1 = true;
|
||||
else switchLight(R_LED, 0, false);//lStat1 = false;
|
||||
//rcv = true;
|
||||
}
|
||||
}
|
||||
|
||||
void onMqttPublish(uint16_t packetId) {
|
||||
// Serial.println("Publish acknowledged.");
|
||||
// Serial.print(" packetId: ");
|
||||
// Serial.println(packetId);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user