Change mqtt settings
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
#include <Arduino.h>
|
||||
//#include <ESP_EEPROM.h>
|
||||
#include <ArduinoOTA.h>
|
||||
#include <Ticker.h>
|
||||
#include <AsyncMqttClient.h>
|
||||
@@ -24,79 +23,18 @@ Ticker wifiReconnectTimer;
|
||||
unsigned long cRun = 0;
|
||||
unsigned long lastSense;
|
||||
uint16_t nSec = 0;
|
||||
uint8_t lamp, lampP;
|
||||
//uint8_t lamp, lampP;
|
||||
uint8_t light;
|
||||
|
||||
void connectToWifi() {
|
||||
Serial.println("Connecting to Wi-Fi...");
|
||||
WiFi.begin(ssid, password);
|
||||
}
|
||||
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 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();
|
||||
digitalWrite(LED_WF, HIGH);
|
||||
}
|
||||
|
||||
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);
|
||||
digitalWrite(LED_WF, LOW);
|
||||
}
|
||||
|
||||
void onMqttConnect(bool sessionPresent) {
|
||||
Serial.println("Connected to MQTT.");
|
||||
Serial.print("Session present: ");
|
||||
Serial.println(sessionPresent);
|
||||
mqttClient.subscribe("/home/kuh/lighttbl_set", 0);
|
||||
mqttClient.publish("/home/kuh/lighttbl", 0, false, light ? "1" : "0");
|
||||
digitalWrite(LED_MQ, HIGH);
|
||||
}
|
||||
|
||||
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
|
||||
//Serial.println("Disconnected from MQTT.");
|
||||
|
||||
if (WiFi.isConnected()) {
|
||||
mqttReconnectTimer.once(2, connectToMqtt);
|
||||
}
|
||||
digitalWrite(LED_MQ, LOW);
|
||||
}
|
||||
|
||||
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/kuh/lighttbl_set") == 0){
|
||||
light = atoi(payload);
|
||||
digitalWrite(LAMP, light);
|
||||
mqttClient.publish("/home/kuh/lighttbl", 0, false, light ? "1" : "0");
|
||||
}
|
||||
}
|
||||
|
||||
void onMqttPublish(uint16_t packetId) {
|
||||
Serial.println("Publish acknowledged.");
|
||||
Serial.print(" packetId: ");
|
||||
Serial.println(packetId);
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
@@ -132,14 +70,9 @@ void setup() {
|
||||
|
||||
mqttClient.onConnect(onMqttConnect);
|
||||
mqttClient.onDisconnect(onMqttDisconnect);
|
||||
mqttClient.onSubscribe(onMqttSubscribe);
|
||||
mqttClient.onUnsubscribe(onMqttUnsubscribe);
|
||||
mqttClient.onMessage(onMqttMessage);
|
||||
mqttClient.onPublish(onMqttPublish);
|
||||
mqttClient.setServer(mqtt_server, 1883);
|
||||
|
||||
// EEPROM.begin(16);
|
||||
// EEPROM.get(0, lamp);
|
||||
|
||||
pinMode(LAMP, OUTPUT);
|
||||
pinMode(LED_WF, OUTPUT);
|
||||
@@ -150,12 +83,11 @@ void setup() {
|
||||
digitalWrite(LED_WF, LOW);
|
||||
digitalWrite(LED_MQ, LOW);
|
||||
digitalWrite(LED_WRK, LOW);
|
||||
digitalWrite(LAMP, LOW);
|
||||
|
||||
Serial.println("Connect to WiFi");
|
||||
connectToWifi();
|
||||
lastSense = millis();
|
||||
light = false;
|
||||
Serial.println("Loop");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
@@ -163,13 +95,9 @@ void loop() {
|
||||
ArduinoOTA.handle();
|
||||
|
||||
if(digitalRead(SENS) == 0){
|
||||
if(lastSense + 3000 < millis()){
|
||||
if(lastSense + 2999 < millis()){
|
||||
lastSense = millis();
|
||||
light = !light;
|
||||
digitalWrite(LAMP, light);
|
||||
mqttClient.publish("/home/kuh/lighttbl", 0, false, light ? "1" : "0");
|
||||
Serial.println("Change state");
|
||||
Serial.println(light);
|
||||
switchLight(LAMP, !digitalRead(LAMP), true);
|
||||
}
|
||||
}
|
||||
if(cRun + 999 < millis()){
|
||||
@@ -182,10 +110,54 @@ void loop() {
|
||||
char v[11];
|
||||
itoa(millis(), v, 10);
|
||||
mqttClient.publish("/home/kuh/ltblmillis", 0, false, v);
|
||||
Serial.print("Millis: ");
|
||||
Serial.println(millis());
|
||||
}
|
||||
nSec = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool switchLight(uint8_t nLamp, int state, bool pub)
|
||||
{
|
||||
digitalWrite(nLamp, state);
|
||||
if (pub) mqttClient.publish("/home/kuh/lighttbl", 0, false, state ? "1" : "0");
|
||||
return state;
|
||||
}
|
||||
|
||||
void connectToWifi() {
|
||||
WiFi.begin(ssid, password);
|
||||
}
|
||||
|
||||
void connectToMqtt() {
|
||||
mqttClient.connect();
|
||||
}
|
||||
|
||||
void onWifiConnect(const WiFiEventStationModeGotIP& event) {
|
||||
connectToMqtt();
|
||||
digitalWrite(LED_WF, HIGH);
|
||||
}
|
||||
|
||||
void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) {
|
||||
mqttReconnectTimer.detach(); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi
|
||||
wifiReconnectTimer.once(2, connectToWifi);
|
||||
digitalWrite(LED_WF, LOW);
|
||||
}
|
||||
|
||||
void onMqttConnect(bool sessionPresent) {
|
||||
mqttClient.publish("/home/kuh/lighttbl", 0, false, digitalRead(LAMP) == 1 ? "1" : "0");
|
||||
mqttClient.subscribe("/home/kuh/lighttbl", 0);
|
||||
digitalWrite(LED_MQ, HIGH);
|
||||
}
|
||||
|
||||
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
|
||||
if (WiFi.isConnected()) {
|
||||
mqttReconnectTimer.once(2, connectToMqtt);
|
||||
}
|
||||
digitalWrite(LED_MQ, LOW);
|
||||
}
|
||||
|
||||
void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
|
||||
if(strcmp(topic, "/home/kuh/lighttbl") == 0){
|
||||
if (atoi(payload) == 1) switchLight(LAMP, 1, false);//lStat2 = true;
|
||||
else switchLight(LAMP, 0, false);//lStat2 = false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user