First commit
This commit is contained in:
191
KuhLight_ESP/src/main.cpp
Normal file
191
KuhLight_ESP/src/main.cpp
Normal file
@@ -0,0 +1,191 @@
|
||||
#include <Arduino.h>
|
||||
//#include <ESP_EEPROM.h>
|
||||
#include <ArduinoOTA.h>
|
||||
#include <Ticker.h>
|
||||
#include <AsyncMqttClient.h>
|
||||
|
||||
const char* ssid = "wf-home";
|
||||
const char* password = "0ndthnrf";
|
||||
const char* mqtt_server = "192.168.1.250";
|
||||
|
||||
#define LED_WF (12)
|
||||
#define LED_MQ (13)
|
||||
#define LED_WRK (14)
|
||||
#define SENS (4)
|
||||
#define LAMP (5)
|
||||
|
||||
AsyncMqttClient mqttClient;
|
||||
Ticker mqttReconnectTimer;
|
||||
|
||||
WiFiEventHandler wifiConnectHandler;
|
||||
WiFiEventHandler wifiDisconnectHandler;
|
||||
Ticker wifiReconnectTimer;
|
||||
|
||||
unsigned long cRun = 0;
|
||||
unsigned long lastSense;
|
||||
uint16_t nSec = 0;
|
||||
uint8_t lamp, lampP;
|
||||
uint8_t light;
|
||||
|
||||
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();
|
||||
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", 1);
|
||||
mqttClient.publish("/home/kuh/lighttbl", 1, 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", 1, false, light ? "1" : "0");
|
||||
}
|
||||
}
|
||||
|
||||
void onMqttPublish(uint16_t packetId) {
|
||||
Serial.println("Publish acknowledged.");
|
||||
Serial.print(" packetId: ");
|
||||
Serial.println(packetId);
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
Serial.println("Begin");
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.hostname("KuhLight");
|
||||
ArduinoOTA.onStart([]() {
|
||||
Serial.println("Start"); // "Начало OTA-апдейта"
|
||||
});
|
||||
ArduinoOTA.onEnd([]() {
|
||||
Serial.println("\nEnd"); // "Завершение OTA-апдейта"
|
||||
});
|
||||
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
|
||||
// Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
|
||||
});
|
||||
ArduinoOTA.onError([](ota_error_t error) {
|
||||
Serial.printf("Error[%u]: ", error);
|
||||
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
|
||||
// // "Ошибка при аутентификации"
|
||||
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
|
||||
// // "Ошибка при начале OTA-апдейта"
|
||||
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
|
||||
// // "Ошибка при подключении"
|
||||
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
|
||||
// // "Ошибка при получении данных"
|
||||
else if (error == OTA_END_ERROR) Serial.println("End Failed");
|
||||
// // "Ошибка при завершении OTA-апдейта"
|
||||
});
|
||||
ArduinoOTA.begin();
|
||||
|
||||
wifiConnectHandler = WiFi.onStationModeGotIP(onWifiConnect);
|
||||
wifiDisconnectHandler = WiFi.onStationModeDisconnected(onWifiDisconnect);
|
||||
|
||||
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);
|
||||
pinMode(LED_MQ, OUTPUT);
|
||||
pinMode(LED_WRK, OUTPUT);
|
||||
pinMode(SENS, INPUT);
|
||||
|
||||
digitalWrite(LED_WF, LOW);
|
||||
digitalWrite(LED_MQ, LOW);
|
||||
digitalWrite(LED_WRK, LOW);
|
||||
|
||||
Serial.println("Connect to WiFi");
|
||||
connectToWifi();
|
||||
lastSense = millis();
|
||||
light = false;
|
||||
Serial.println("Loop");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
static bool led_wrk = false;
|
||||
ArduinoOTA.handle();
|
||||
|
||||
if(digitalRead(SENS) == 0){
|
||||
if(lastSense + 3000 < millis()){
|
||||
lastSense = millis();
|
||||
light = !light;
|
||||
digitalWrite(LAMP, light);
|
||||
mqttClient.publish("/home/kuh/lighttbl", 1, false, light ? "1" : "0");
|
||||
Serial.println("Change state");
|
||||
Serial.println(light);
|
||||
}
|
||||
}
|
||||
if(cRun + 999 < millis()){
|
||||
cRun = millis();
|
||||
nSec++;
|
||||
led_wrk = !led_wrk;
|
||||
digitalWrite(LED_WRK, led_wrk);
|
||||
if(nSec > 59){
|
||||
if(mqttClient.connected()){
|
||||
char v[11];
|
||||
itoa(millis(), v, 10);
|
||||
mqttClient.publish("/home/kuh/ltblmillis", 1, false, v);
|
||||
Serial.print("Millis: ");
|
||||
Serial.println(millis());
|
||||
}
|
||||
nSec = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user