New project ESP32_Kor
This commit is contained in:
@@ -17,6 +17,7 @@ board_build.f_cpu = 26000000L
|
||||
upload_protocol = espota
|
||||
upload_port = 192.168.1.148
|
||||
lib_deps =
|
||||
Adafruit HTU21DF Library @ ^1.1.0
|
||||
;Adafruit HTU21DF Library @ ^1.1.0
|
||||
PubSubClient @ ^2.8
|
||||
jwrw/ESP_EEPROM @ ^2.0.0
|
||||
marvinroger/AsyncMqttClient @ ^0.9.0
|
||||
|
||||
@@ -5,9 +5,11 @@
|
||||
#include <ArduinoOTA.h>
|
||||
#include <Wire.h>
|
||||
#include <Adafruit_HTU21DF.h>
|
||||
#include <PubSubClient.h>
|
||||
//#include <PubSubClient.h>
|
||||
#include <leds.h>
|
||||
#include <ESP_EEPROM.h>
|
||||
#include <Ticker.h>
|
||||
#include <AsyncMqttClient.h>
|
||||
|
||||
#define LED_BLUE D5 //GPIO14
|
||||
#define LED_GREEN D6 //GPIO12
|
||||
@@ -17,7 +19,9 @@
|
||||
|
||||
Adafruit_HTU21DF htu = Adafruit_HTU21DF();
|
||||
WiFiClient espClient;
|
||||
PubSubClient client(espClient);
|
||||
//PubSubClient client(espClient);
|
||||
AsyncMqttClient mqttClient;
|
||||
Ticker mqttReconnectTimer;
|
||||
leds lGreen(LED_GREEN, 200);
|
||||
leds lRed(LED_RED, 200);
|
||||
leds lBlue(LED_BLUE, 200);
|
||||
@@ -39,20 +43,34 @@ void reconnect();
|
||||
void publishMin();
|
||||
void publishSec();
|
||||
void callback(char* topic, byte* payload, unsigned int length);
|
||||
WiFiEventHandler wifiConnectHandler;
|
||||
WiFiEventHandler wifiDisconnectHandler;
|
||||
Ticker wifiReconnectTimer;
|
||||
|
||||
void connectToWifi();
|
||||
void connectToMqtt();
|
||||
void onWifiConnect(const WiFiEventStationModeGotIP& event);
|
||||
void onWifiDisconnect(const WiFiEventStationModeDisconnected& event);
|
||||
void onMqttConnect(bool sessionPresent);
|
||||
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason);
|
||||
void onMqttSubscribe(uint16_t packetId, uint8_t qos);
|
||||
void onMqttUnsubscribe(uint16_t packetId);
|
||||
void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total);
|
||||
void onMqttPublish(uint16_t packetId);
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
pinMode(LED_BUILTIN, OUTPUT);
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.setOutputPower(20.5f);
|
||||
//WiFi.setOutputPower(20.5f);
|
||||
WiFi.hostname("ESP-SmallRoom");
|
||||
WiFi.begin(ssid, password);
|
||||
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
|
||||
//WiFi.begin(ssid, password);
|
||||
//while (WiFi.waitForConnectResult() != WL_CONNECTED) {
|
||||
//Serial.println("Connection Failed! Rebooting...");
|
||||
// "Соединиться не удалось! Перезагрузка..."
|
||||
delay(5000);
|
||||
ESP.restart();
|
||||
}
|
||||
// delay(5000);
|
||||
// ESP.restart();
|
||||
// }
|
||||
digitalWrite(LED_BUILTIN, !WiFi.isConnected());
|
||||
ArduinoOTA.onStart([]() { /*Serial.println("Start");*/}); // "Начало OTA-апдейта"
|
||||
ArduinoOTA.onEnd([]() { /*Serial.println("\nEnd");*/}); // "Завершение OTA-апдейта"
|
||||
@@ -65,15 +83,29 @@ void setup() {
|
||||
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed"); // "Ошибка при получении данных"
|
||||
else if (error == OTA_END_ERROR) Serial.println("End Failed"); // "Ошибка при завершении OTA-апдейта"*/
|
||||
});
|
||||
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);
|
||||
mqttClient.setClientId("ESPSmallRoom");
|
||||
|
||||
connectToWifi();
|
||||
|
||||
ArduinoOTA.begin();
|
||||
pinMode(LED_GREEN, OUTPUT);
|
||||
pinMode(LED_BLUE, OUTPUT);
|
||||
pinMode(LED_RED, OUTPUT);
|
||||
pinMode(PWR_SENS, OUTPUT);
|
||||
pinMode(MOVE_SENS, INPUT);
|
||||
digitalWrite(LED_GREEN, LOW);
|
||||
digitalWrite(LED_GREEN, HIGH);
|
||||
digitalWrite(LED_BLUE, LOW);
|
||||
digitalWrite(LED_RED, LOW);
|
||||
digitalWrite(LED_RED, HIGH);
|
||||
// Serial.begin(115200);
|
||||
Serial.println("HTU21D-F");
|
||||
digitalWrite(PWR_SENS, HIGH);
|
||||
@@ -85,8 +117,8 @@ void setup() {
|
||||
temp = htu.readTemperature();
|
||||
rel_hum = htu.readHumidity();
|
||||
//digitalWrite(PWR_SENS, LOW);
|
||||
client.setServer(mqtt_server, 1883);
|
||||
client.setCallback(callback);
|
||||
//client.setServer(mqtt_server, 1883);
|
||||
//client.setCallback(callback);
|
||||
EEPROM.begin(8);
|
||||
EEPROM.get(0, delta);
|
||||
adc = analogRead(A0);
|
||||
@@ -100,10 +132,10 @@ void loop() {
|
||||
lRed.tick();
|
||||
lBlue.tick();
|
||||
|
||||
if (!client.connected()) {
|
||||
reconnect();
|
||||
}
|
||||
client.loop();
|
||||
// if (!client.connected()) {
|
||||
// reconnect();
|
||||
// }
|
||||
// client.loop();
|
||||
|
||||
if(cRun + 99 < millis()){
|
||||
cRun = millis();
|
||||
@@ -111,13 +143,17 @@ void loop() {
|
||||
mv = digitalRead(MOVE_SENS);
|
||||
if(mv != oldmv){
|
||||
oldmv = mv;
|
||||
client.publish(TOPIC"smallroom/move", mv == 0 ? "0" : "1");
|
||||
lBlue.start();
|
||||
mqttClient.publish(TOPIC"smallroom/move", 0, false, mv == 0 ? "0" : "1");
|
||||
//client.publish(TOPIC"smallroom/move", mv == 0 ? "0" : "1");
|
||||
};
|
||||
//if(abs(adc - oldadc) > delta){
|
||||
if(((adc < delta) && !lsSent) || ((adc >= (delta + 5)) && !lbSent)){
|
||||
char strFVal[6];
|
||||
itoa(adc, strFVal, 10);
|
||||
client.publish(TOPIC"smallroom/light", strFVal);
|
||||
lBlue.start();
|
||||
mqttClient.publish(TOPIC"smallroom/light", 0, false, strFVal);
|
||||
//client.publish(TOPIC"smallroom/light", strFVal);
|
||||
oldadc = adc;
|
||||
if(adc < delta) {lsSent = true; lbSent = false;}
|
||||
else {lbSent = true; lsSent = false;}
|
||||
@@ -136,89 +172,164 @@ void loop() {
|
||||
publishMin();
|
||||
minCnt = 0;
|
||||
}
|
||||
if(minCnt % 100 == 0) publishSec();
|
||||
//if(minCnt % 100 == 0) publishSec();
|
||||
}
|
||||
}
|
||||
|
||||
void reconnect() {
|
||||
lRed.start();
|
||||
//digitalWrite(LED_RED, HIGH);
|
||||
//Serial.print("Attempting MQTT connection...");
|
||||
// "Попытка подключиться к MQTT-брокеру... "
|
||||
// Пытаемся подключиться:
|
||||
if (client.connect("ESPSmallRoom")) {
|
||||
//Serial.println("connected"); // "подключен"
|
||||
// подписываемся или переподписываемся на топик;
|
||||
// можно подписаться не только на один, а на несколько топиков
|
||||
char v[6];
|
||||
itoa(delta, v, 10);
|
||||
client.publish(TOPIC"smallroom/ldelta", v);
|
||||
client.subscribe(TOPIC"smallroom/ldelta");
|
||||
} else {
|
||||
//Serial.print("failed, rc="); // "подключение не удалось"
|
||||
//Serial.print(client.state());
|
||||
//Serial.println(" try again in 5 seconds");
|
||||
}
|
||||
//delay(100);
|
||||
//digitalWrite(LED_RED, LOW);
|
||||
}
|
||||
// void reconnect() {
|
||||
// lRed.start();
|
||||
// //digitalWrite(LED_RED, HIGH);
|
||||
// //Serial.print("Attempting MQTT connection...");
|
||||
// // "Попытка подключиться к MQTT-брокеру... "
|
||||
// // Пытаемся подключиться:
|
||||
// if (client.connect("ESPSmallRoom")) {
|
||||
// //Serial.println("connected"); // "подключен"
|
||||
// // подписываемся или переподписываемся на топик;
|
||||
// // можно подписаться не только на один, а на несколько топиков
|
||||
// char v[6];
|
||||
// itoa(delta, v, 10);
|
||||
// client.publish(TOPIC"smallroom/ldelta", v);
|
||||
// client.subscribe(TOPIC"smallroom/ldelta");
|
||||
// } else {
|
||||
// //Serial.print("failed, rc="); // "подключение не удалось"
|
||||
// //Serial.print(client.state());
|
||||
// //Serial.println(" try again in 5 seconds");
|
||||
// }
|
||||
// //delay(100);
|
||||
// //digitalWrite(LED_RED, LOW);
|
||||
// }
|
||||
|
||||
void publishMin()
|
||||
{
|
||||
char strFVal[11];
|
||||
if (client.connected()) {
|
||||
lGreen.start();
|
||||
//if (client.connected()) {
|
||||
//digitalWrite(LED_BLUE, HIGH);
|
||||
if (mqttClient.connected()) {
|
||||
lBlue.start();
|
||||
//digitalWrite(LED_BLUE, HIGH);
|
||||
if(!isnan(temp)){
|
||||
dtostrf(temp, 6, 1, strFVal);
|
||||
client.publish(TOPIC"smallroom/temp", strFVal);
|
||||
mqttClient.publish(TOPIC"smallroom/temp", 0, false, strFVal);
|
||||
//client.publish(TOPIC"smallroom/temp", strFVal);
|
||||
}
|
||||
if(!isnan(rel_hum)){
|
||||
dtostrf(rel_hum, 6, 1, strFVal);
|
||||
client.publish(TOPIC"smallroom/rel_hum", strFVal);
|
||||
mqttClient.publish(TOPIC"smallroom/rel_hum", 0, false, strFVal);
|
||||
//client.publish(TOPIC"smallroom/rel_hum", strFVal);
|
||||
}
|
||||
ultoa(adc, strFVal, 10);
|
||||
client.publish(TOPIC"smallroom/light", strFVal);
|
||||
mqttClient.publish(TOPIC"smallroom/light", 0, false, strFVal);
|
||||
//client.publish(TOPIC"smallroom/light", strFVal);
|
||||
ultoa(cRun, strFVal, 10);
|
||||
client.publish(TOPIC"smallroom/millis", strFVal);
|
||||
mqttClient.publish(TOPIC"smallroom/millis", 0, false, strFVal);
|
||||
//client.publish(TOPIC"smallroom/millis", strFVal);
|
||||
itoa(delta, strFVal, 10);
|
||||
client.publish(TOPIC"smallroom/ldelta", strFVal);
|
||||
mqttClient.publish(TOPIC"smallroom/ldelta", 0, false, strFVal);
|
||||
//client.publish(TOPIC"smallroom/ldelta", strFVal);
|
||||
//digitalWrite(LED_BLUE, LOW);
|
||||
}
|
||||
//digitalWrite(LED_BLUE, LOW);
|
||||
}
|
||||
|
||||
void publishSec()
|
||||
{
|
||||
char strFVal[11];
|
||||
if (client.connected()) {
|
||||
if (mqttClient.connected()) {
|
||||
lBlue.start();
|
||||
lGreen.start();
|
||||
//digitalWrite(LED_GREEN, HIGH);
|
||||
if(!isnan(temp)){
|
||||
dtostrf(temp, 7, 3, strFVal);
|
||||
client.publish("/hometest/smallroom/temp", strFVal);
|
||||
mqttClient.publish("/hometest/smallroom/temp", 0, false, strFVal);
|
||||
//client.publish("/hometest/smallroom/temp", strFVal);
|
||||
}
|
||||
if(!isnan(rel_hum)){
|
||||
dtostrf(rel_hum, 7, 3, strFVal);
|
||||
client.publish("/hometest/smallroom/rel_hum", strFVal);
|
||||
mqttClient.publish("/hometest/smallroom/rel_hum", 0, false, strFVal);
|
||||
//client.publish("/hometest/smallroom/rel_hum", strFVal);
|
||||
}
|
||||
itoa(WiFi.RSSI(), strFVal, 10);
|
||||
client.publish("/hometest/smallroom/RSSI", strFVal);
|
||||
mqttClient.publish("/hometest/smallroom/RSSI", 0, false, strFVal);
|
||||
//client.publish("/hometest/smallroom/RSSI", strFVal);
|
||||
ultoa(cRun, strFVal, 10);
|
||||
client.publish("/hometest/smallroom/millis", strFVal);
|
||||
mqttClient.publish("/hometest/smallroom/millis", 0, false, strFVal);
|
||||
//client.publish("/hometest/smallroom/millis", strFVal);
|
||||
ltoa(adc, strFVal, 10);
|
||||
client.publish("/hometest/smallroom/light", strFVal);
|
||||
mqttClient.publish("/hometest/smallroom/light", 0, false, strFVal);
|
||||
//client.publish("/hometest/smallroom/light", strFVal);
|
||||
itoa(delta, strFVal, 10);
|
||||
client.publish("/hometest/smallroom/ldelta", strFVal);
|
||||
mqttClient.publish("/hometest/smallroom/ldelta", 0, false, strFVal);
|
||||
//client.publish("/hometest/smallroom/ldelta", strFVal);
|
||||
//digitalWrite(LED_GREEN, LOW);
|
||||
}
|
||||
}
|
||||
|
||||
void callback(char* topic, byte* payload, unsigned int length) {
|
||||
// void callback(char* topic, byte* payload, unsigned int length) {
|
||||
// if(strcmp(topic,TOPIC"smallroom/ldelta") == 0){
|
||||
// payload[length] = '\0';
|
||||
// String pl = String((char*)payload);
|
||||
// delta = pl.toInt();// atoi((char*)payload);
|
||||
// EEPROM.put(0, delta);
|
||||
// EEPROM.commit();
|
||||
// }
|
||||
// }
|
||||
|
||||
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.");
|
||||
connectToMqtt();
|
||||
digitalWrite(LED_GREEN, HIGH);
|
||||
digitalWrite(LED_RED, 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_RED, HIGH);
|
||||
digitalWrite(LED_GREEN, LOW);
|
||||
}
|
||||
|
||||
void onMqttConnect(bool sessionPresent) {
|
||||
digitalWrite(LED_GREEN, LOW);
|
||||
digitalWrite(LED_RED, LOW);
|
||||
mqttClient.subscribe(TOPIC"smallroom/ldelta", 1);
|
||||
}
|
||||
|
||||
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
|
||||
Serial.println("Disconnected from MQTT.");
|
||||
|
||||
if (WiFi.isConnected()) {
|
||||
mqttReconnectTimer.once(2, connectToMqtt);
|
||||
}
|
||||
digitalWrite(LED_GREEN, HIGH);
|
||||
digitalWrite(LED_RED, HIGH);
|
||||
}
|
||||
|
||||
void onMqttSubscribe(uint16_t packetId, uint8_t qos) {
|
||||
}
|
||||
|
||||
void onMqttUnsubscribe(uint16_t packetId) {
|
||||
}
|
||||
|
||||
void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
|
||||
if(strcmp(topic,TOPIC"smallroom/ldelta") == 0){
|
||||
payload[length] = '\0';
|
||||
payload[len] = '\0';
|
||||
String pl = String((char*)payload);
|
||||
delta = pl.toInt();// atoi((char*)payload);
|
||||
EEPROM.put(0, delta);
|
||||
EEPROM.commit();
|
||||
}
|
||||
}
|
||||
|
||||
void onMqttPublish(uint16_t packetId) {
|
||||
}
|
||||
Reference in New Issue
Block a user