Bigroom esp32
This commit is contained in:
@@ -1,45 +1,21 @@
|
||||
#include <Arduino.h>
|
||||
#include <ArduinoOTA.h>
|
||||
#include <Ticker.h>
|
||||
#include <AsyncMqttClient.h>
|
||||
#include <leds.h>
|
||||
#include "main.h"
|
||||
|
||||
#include "Adafruit_HTU21DF.h"
|
||||
|
||||
#define R_LED (0)
|
||||
#define G_LED (2)
|
||||
#define B_LED (14)
|
||||
#define P_SENS (12)
|
||||
#define MOV_SENS (13)
|
||||
|
||||
bool meas = false;
|
||||
short minCnt;
|
||||
bool send_move = false;
|
||||
int old_mov = 0;
|
||||
int adc = 0;
|
||||
int lastADC = 0;
|
||||
MHZ19 myMHZ19;
|
||||
Adafruit_HTU21DF htu = Adafruit_HTU21DF();
|
||||
RunningMedian samples = RunningMedian(5 * sizeof(int));
|
||||
|
||||
unsigned long cRun;
|
||||
float temp, rel_hum;
|
||||
// bool dir = true;
|
||||
// int dc = 0;
|
||||
AsyncMqttClient mqttClient;
|
||||
TimerHandle_t mqttReconnectTimer;
|
||||
TimerHandle_t wifiReconnectTimer;
|
||||
|
||||
const char* ssid = "wf-home";
|
||||
const char* password = "0ndthnrf";
|
||||
const char* mqtt_server = "192.168.1.250";
|
||||
const char* mqtt_server = "192.168.1.111";
|
||||
|
||||
AsyncMqttClient mqttClient;
|
||||
Ticker mqttReconnectTimer;
|
||||
|
||||
WiFiEventHandler wifiConnectHandler;
|
||||
WiFiEventHandler wifiDisconnectHandler;
|
||||
Ticker wifiReconnectTimer;
|
||||
|
||||
void connectToWifi();
|
||||
void connectToMqtt();
|
||||
void onWifiConnect(const WiFiEventStationModeGotIP& event);
|
||||
void onWifiDisconnect(const WiFiEventStationModeDisconnected& event);
|
||||
void WiFiEvent(WiFiEvent_t event);
|
||||
void onMqttConnect(bool sessionPresent);
|
||||
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason);
|
||||
void onMqttSubscribe(uint16_t packetId, uint8_t qos);
|
||||
@@ -47,12 +23,10 @@ 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);
|
||||
|
||||
leds b_led(B_LED);
|
||||
leds g_led(G_LED);
|
||||
leds r_led(R_LED);
|
||||
leds bled(LED_B, 300, true);
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
Serial.begin(115200);
|
||||
Serial.println("Booting"); // "Загрузка"
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.hostname("BigRoom");
|
||||
@@ -78,10 +52,13 @@ void setup() {
|
||||
// else if (error == OTA_END_ERROR) Serial.println("End Failed");
|
||||
// // "Ошибка при завершении OTA-апдейта"
|
||||
});
|
||||
|
||||
ArduinoOTA.begin();
|
||||
|
||||
wifiConnectHandler = WiFi.onStationModeGotIP(onWifiConnect);
|
||||
wifiDisconnectHandler = WiFi.onStationModeDisconnected(onWifiDisconnect);
|
||||
mqttReconnectTimer = xTimerCreate("mqttTimer", pdMS_TO_TICKS(2000), pdFALSE, (void*)0, reinterpret_cast<TimerCallbackFunction_t>(connectToMqtt));
|
||||
wifiReconnectTimer = xTimerCreate("wifiTimer", pdMS_TO_TICKS(2000), pdFALSE, (void*)0, reinterpret_cast<TimerCallbackFunction_t>(connectToWifi));
|
||||
|
||||
WiFi.onEvent(WiFiEvent);
|
||||
|
||||
mqttClient.onConnect(onMqttConnect);
|
||||
mqttClient.onDisconnect(onMqttDisconnect);
|
||||
@@ -92,92 +69,210 @@ void setup() {
|
||||
mqttClient.setServer(mqtt_server, 1883);
|
||||
mqttClient.setClientId("BigRoom");
|
||||
|
||||
pinMode(MOV_SENS, INPUT_PULLUP);
|
||||
pinMode(P_SENS, OUTPUT);
|
||||
pinMode(R_LED, OUTPUT);
|
||||
pinMode(G_LED, OUTPUT);
|
||||
pinMode(B_LED, OUTPUT);
|
||||
pinMode(MOVE_SENS, INPUT_PULLUP);
|
||||
pinMode(LED_R, OUTPUT);
|
||||
pinMode(LED_G, OUTPUT);
|
||||
pinMode(LED_B, OUTPUT);
|
||||
ledcSetup(PWM_WHITE, 5000, 8);
|
||||
ledcSetup(PWM_BLUE, 5000, 8);
|
||||
ledcAttachPin(LED_WHITE, PWM_WHITE);
|
||||
ledcAttachPin(LED_BLUE, PWM_BLUE);
|
||||
|
||||
digitalWrite(P_SENS, HIGH);
|
||||
delay(200);
|
||||
Serial.println("Begin HTU");
|
||||
if (!htu.begin()) {
|
||||
digitalWrite(LED_R, LOW);
|
||||
digitalWrite(LED_G, HIGH);
|
||||
digitalWrite(LED_B, HIGH);
|
||||
Serial2.begin(9600);
|
||||
myMHZ19.begin(Serial2); // *Important, Pass your Stream reference here
|
||||
myMHZ19.autoCalibration(false);
|
||||
|
||||
char myVersion[4];
|
||||
myMHZ19.getVersion(myVersion);
|
||||
Serial.print("\nFirmware Version: ");
|
||||
for(byte i = 0; i < 4; i++)
|
||||
{
|
||||
Serial.print(myVersion[i]);
|
||||
if(i == 1)
|
||||
Serial.print(".");
|
||||
}
|
||||
|
||||
Serial.println("");
|
||||
Serial.print("Range: ");
|
||||
Serial.println(myMHZ19.getRange());
|
||||
Serial.print("Background CO2: ");
|
||||
Serial.println(myMHZ19.getBackgroundCO2());
|
||||
Serial.print("Temperature Cal: ");
|
||||
Serial.println(myMHZ19.getTempAdjustment());
|
||||
Serial.print("ABC Status: "); myMHZ19.getABC() ? Serial.println("ON") : Serial.println("OFF");
|
||||
|
||||
//digitalWrite(P_SENS, LOW);
|
||||
#undef SDA //delete dafault SDA pin number
|
||||
#undef SCL //delete dafault SCL pin number
|
||||
#define SDA 33 //assign new SDA pin to GPIO1/D2/0TX for all slaves on i2c bus
|
||||
#define SCL 32 //assign new SCL pin to GPIO3/D7/0RX for all slaves on i2c bus
|
||||
if (!htu.begin(SDA, SCL)) {
|
||||
Serial.println("Couldn't find sensor!");
|
||||
while (1);
|
||||
}
|
||||
Serial.println("HTU Ok");
|
||||
temp = htu.readTemperature();
|
||||
rel_hum = htu.readHumidity();
|
||||
Serial.print("Temp: "); Serial.print(temp); Serial.print(" C");
|
||||
Serial.print("\t\t");
|
||||
Serial.print("Humidity: "); Serial.print(rel_hum); Serial.println(" \%");
|
||||
digitalWrite(R_LED, HIGH);
|
||||
digitalWrite(P_SENS, LOW);
|
||||
Serial.println("Sensor found!");
|
||||
|
||||
char s[7];
|
||||
EEPROM.begin(10);
|
||||
EEPROM.get(0, periodMotion);
|
||||
itoa(periodMotion, s, 10);
|
||||
mqttClient.publish(TOPIC"", 1, false, s);
|
||||
//publish period
|
||||
EEPROM.get(1, spLight);
|
||||
itoa(spLight, s, 10);
|
||||
mqttClient.publish(TOPIC"", 1, false, s);
|
||||
//publish splight
|
||||
EEPROM.get(3, dbLight);
|
||||
itoa(dbLight, s, 10);
|
||||
mqttClient.publish(TOPIC"", 1, false, s);
|
||||
EEPROM.get(5, levelBlue);
|
||||
itoa(levelBlue, s, 10);
|
||||
mqttClient.publish(TOPIC"", 1, false, s);
|
||||
EEPROM.get(6, levelWhite);
|
||||
itoa(levelWhite, s, 10);
|
||||
mqttClient.publish(TOPIC"", 1, false, s);
|
||||
if(spLight == 65535) {
|
||||
spLight = 1000;
|
||||
EEPROM.writeShort(1, spLight);
|
||||
}
|
||||
if(dbLight == 65535) {
|
||||
dbLight = 1000;
|
||||
EEPROM.writeShort(3, dbLight);
|
||||
}
|
||||
if(levelBlue == 255) {
|
||||
levelBlue = 100;
|
||||
EEPROM.writeShort(5, levelBlue);
|
||||
}
|
||||
if(levelWhite == 255) {
|
||||
levelWhite = 100;
|
||||
EEPROM.writeByte(6, levelWhite);
|
||||
}
|
||||
if(periodMotion == 255) {
|
||||
periodMotion = 60;
|
||||
EEPROM.writeByte(0, periodMotion);
|
||||
}
|
||||
EEPROM.commit();
|
||||
Serial.print(F("PeriodMove:"));Serial.println(periodMotion);
|
||||
Serial.print(F("SPLight:"));Serial.println(spLight);
|
||||
Serial.print(F("DBLight:"));Serial.println(dbLight);
|
||||
Serial.print(F("BLevel:"));Serial.println(levelBlue);
|
||||
Serial.print(F("WLevel:"));Serial.println(levelWhite);
|
||||
|
||||
curDelay = -1;
|
||||
wLamp = bLamp = false;
|
||||
statLamp = 0;
|
||||
|
||||
connectToWifi();
|
||||
|
||||
cRun = millis();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
char v[11];
|
||||
static uint32_t cRun = millis();
|
||||
static uint8_t sec = 0;
|
||||
char s[7];
|
||||
|
||||
ArduinoOTA.handle();
|
||||
g_led.tick();
|
||||
if (digitalRead(MOV_SENS) != old_mov){
|
||||
old_mov = digitalRead(MOV_SENS);
|
||||
Serial.println("Change mov detected");
|
||||
mqttClient.publish("/home/bigroom/move", 1, false, old_mov ? "1" : "0");
|
||||
bled.tick();
|
||||
if(digitalRead(MOVE_SENS) > 0){
|
||||
if(curDelay == -1){
|
||||
bled.start();
|
||||
mqttClient.publish(TOPIC"move", 1, false, "1");
|
||||
//Serial.println("move");
|
||||
}
|
||||
move = true;
|
||||
curDelay = periodMotion;
|
||||
}
|
||||
|
||||
if(cRun + 999 < millis()){
|
||||
uint16_t LightLev = analogRead(LIGHT_SENS);
|
||||
samples.add(LightLev);
|
||||
LightLev = samples.getMedian();
|
||||
if ((LightLev < spLight) && move && !bLamp){
|
||||
ledcWrite(PWM_BLUE,(levelBlue) * 2.55);
|
||||
statLamp |= 0x01;
|
||||
bLamp = true;
|
||||
Serial.println(F("BLamp On"));
|
||||
}
|
||||
else if((LightLev > (spLight + dbLight)) || (move == 0)){
|
||||
ledcWrite(PWM_BLUE, 0);
|
||||
statLamp &= 0xFE;
|
||||
bLamp = false;
|
||||
}
|
||||
if(lightWhite && !wLamp){
|
||||
ledcWrite(PWM_WHITE, (levelBlue) * 2.55);
|
||||
statLamp |= 0x02;
|
||||
wLamp = true;
|
||||
Serial.println(F("WLamp On"));
|
||||
}
|
||||
else if (!lightWhite){
|
||||
ledcWrite(PWM_WHITE, 0);
|
||||
statLamp &= 0xFD;
|
||||
wLamp = false;
|
||||
}
|
||||
if(statLamp > 0) digitalWrite(POWER, HIGH);
|
||||
else digitalWrite(POWER, LOW);
|
||||
|
||||
if((cRun + 1000) <= millis()){ //Once per second
|
||||
cRun = millis();
|
||||
//analogWrite(B_LED, dc);
|
||||
/*if(dir){
|
||||
dc += 64;
|
||||
if(dc >= 959) dir = !dir;
|
||||
if(curDelay > -1){
|
||||
Serial.println("ADC: " + String(LightLev) + " MV:" + String(move) + " TMv:" + curDelay + " SL:" + String(statLamp) + " PS:" + digitalRead(POWER));
|
||||
Serial.println("SPL: " + String(spLight) + " DBL:" + String(dbLight)+ " BL:" + String(bLamp));
|
||||
}
|
||||
else{
|
||||
dc -= 64;
|
||||
if(dc <= 64) dir = !dir;
|
||||
}*/
|
||||
//Serial.println(dc);
|
||||
adc = analogRead(A0);
|
||||
if(abs(adc - lastADC) > 20){
|
||||
lastADC = adc;
|
||||
itoa(adc, v, 10);
|
||||
mqttClient.publish("/home/bigroom/light", 1, false, v);
|
||||
sec++;
|
||||
if(curDelay > 0) curDelay--;
|
||||
if(curDelay == 0){
|
||||
mqttClient.publish(TOPIC"move", 1, false, "0");
|
||||
bled.start();
|
||||
Serial.println("no move");
|
||||
move = false;
|
||||
curDelay = -1;
|
||||
}
|
||||
if(++minCnt == 60){
|
||||
minCnt = 0;
|
||||
digitalWrite(P_SENS, HIGH);
|
||||
delay(10);
|
||||
if((sec + 2) % 30 == 0){
|
||||
temp = htu.readTemperature();
|
||||
rel_hum = htu.readHumidity();
|
||||
Serial.print(cRun);
|
||||
Serial.print("\t");
|
||||
Serial.print(minCnt);
|
||||
Serial.print("\t");
|
||||
Serial.print("Temp: "); Serial.print(temp); Serial.print(" C");
|
||||
Serial.print("\t\t");
|
||||
Serial.print("Humidity: "); Serial.print(rel_hum); Serial.println(" \%");
|
||||
digitalWrite(P_SENS, LOW);
|
||||
if(mqttClient.connected()){
|
||||
//char s[50];
|
||||
g_led.start();
|
||||
Serial.println("Begin Publish");
|
||||
dtostrf(temp, 5, 1,v);
|
||||
mqttClient.publish("/home/bigroom/temp", 0, false, v);
|
||||
Serial.println("Publish1");
|
||||
dtostrf(rel_hum, 5, 1,v);
|
||||
mqttClient.publish("/home/bigroom/humid", 0, false, v);
|
||||
Serial.println("Publish2");
|
||||
//sprintf(s, "{temperatura: %.1f, humidity: %.1f}", temp, rel_hum);
|
||||
//Serial.println(s);
|
||||
//mqttClient.publish("/home/bigroom/climat", 0, false, s);
|
||||
}
|
||||
hum = htu.readHumidity();
|
||||
Serial.println("Temp: " + String(temp));
|
||||
Serial.println("Hum: " + String(hum));
|
||||
Serial.println(sec);
|
||||
}
|
||||
else if(minCnt % 10 == 0){
|
||||
if(mqttClient.connected()) mqttClient.publish("/home/bigroom/millis", 0, false, ultoa(millis(), v, 10));
|
||||
//b_led.start();
|
||||
if(sec == 59){
|
||||
//minuts++;
|
||||
sec = 0;
|
||||
// }
|
||||
// if(minuts == periodMinCO2){
|
||||
int CO2; // Buffer for CO2
|
||||
CO2 = myMHZ19.getCO2(); // Request CO2 (as ppm)
|
||||
|
||||
if(myMHZ19.errorCode == RESULT_OK) // RESULT_OK is an alis for 1. Either can be used to confirm the response was OK.
|
||||
{
|
||||
Serial.println(CO2);
|
||||
itoa(CO2, s, 10);
|
||||
bled.start();
|
||||
mqttClient.publish(TOPIC"co2", 1, false, s);
|
||||
}
|
||||
else{
|
||||
Serial.println("CO2err:" + String(myMHZ19.errorCode));
|
||||
}
|
||||
if(temp != 0.0f){
|
||||
// Serial.println("Send");
|
||||
|
||||
// Serial.println("Temp: " + String(temp));
|
||||
// Serial.println("Hum: " + String(hum));
|
||||
dtostrf(temp, 6, 1, s);
|
||||
bled.start();
|
||||
mqttClient.publish(TOPIC"temp", 1, false, s);
|
||||
}
|
||||
if(hum != 0.0f){
|
||||
dtostrf(hum, 6, 1, s);
|
||||
mqttClient.publish(TOPIC"hum", 1, false, s);
|
||||
//sendDataI(msgLight, LightLev);
|
||||
//minuts = 0;
|
||||
}
|
||||
bled.start();
|
||||
dtostrf(cRun / 60000.0, 6, 1, s);
|
||||
mqttClient.publish(TOPIC"mins", 1, false, s);
|
||||
itoa(LightLev, s, 10);
|
||||
mqttClient.publish(TOPIC"lightlev", 1, false, s);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -192,43 +287,55 @@ void connectToMqtt() {
|
||||
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.");
|
||||
digitalWrite(B_LED, LOW);
|
||||
mqttReconnectTimer.detach(); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi
|
||||
wifiReconnectTimer.once(2, connectToWifi);
|
||||
void WiFiEvent(WiFiEvent_t event) {
|
||||
Serial.printf("[WiFi-event] event: %d\n", event);
|
||||
switch(event) {
|
||||
case SYSTEM_EVENT_STA_GOT_IP:
|
||||
Serial.println("WiFi connected");
|
||||
Serial.println("IP address: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
digitalWrite(LED_R, HIGH);
|
||||
connectToMqtt();
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_DISCONNECTED:
|
||||
Serial.println("WiFi lost connection");
|
||||
xTimerStop(mqttReconnectTimer, 0); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi
|
||||
xTimerStart(wifiReconnectTimer, 0);
|
||||
digitalWrite(LED_R, LOW);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void onMqttConnect(bool sessionPresent) {
|
||||
Serial.println("Connected to MQTT.");
|
||||
Serial.print("Session present: ");
|
||||
Serial.println(sessionPresent);
|
||||
digitalWrite(B_LED, HIGH);
|
||||
digitalWrite(R_LED, LOW);
|
||||
//uint16_t packetIdSub =
|
||||
// Serial.print("Subscribing Lamp1, packetId: ");
|
||||
// Serial.println(packetIdSub);
|
||||
//packetIdSub = mqttClient.subscribe("/home/kor/lamp2_set", 1);
|
||||
//Serial.print("Subscribing Lamp2, packetId: ");
|
||||
//packetIdSub =
|
||||
mqttClient.subscribe(TOPIC"lampblevel", 1);
|
||||
mqttClient.subscribe(TOPIC"lampwlevel", 1);
|
||||
mqttClient.subscribe(TOPIC"lampw", 1);
|
||||
mqttClient.subscribe(TOPIC"moveperiod", 1);
|
||||
mqttClient.subscribe(TOPIC"spLevel", 1);
|
||||
mqttClient.subscribe(TOPIC"dbLevel", 1);
|
||||
//Serial.print("Subscribing Lamp2, packetId: ");
|
||||
//Serial.println(packetIdSub);
|
||||
//Serial.println("Publishing at Lamp 1");
|
||||
//mqttClient.publish("/home/kor/lamp2", 1, false, lStat2 ? "1" : "0");
|
||||
//Serial.println("Publishing at Lamp 2");
|
||||
digitalWrite(LED_G, LOW);
|
||||
}
|
||||
|
||||
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
|
||||
Serial.println("Disconnected from MQTT.");
|
||||
digitalWrite(B_LED, LOW);
|
||||
digitalWrite(R_LED, HIGH);
|
||||
digitalWrite(LED_R, HIGH);
|
||||
if (WiFi.isConnected()) {
|
||||
mqttReconnectTimer.once(2, connectToMqtt);
|
||||
xTimerStart(mqttReconnectTimer, 0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -247,13 +354,43 @@ 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, "/home/kor/lamp1_set") == 0){
|
||||
bool w = false;
|
||||
if(strcmp(topic, TOPIC"lampblevel") == 0){
|
||||
levelBlue = atoi(payload);
|
||||
EEPROM.put(5, levelBlue);
|
||||
w = true;
|
||||
}
|
||||
if(strcmp(topic, TOPIC"lampwlevel") == 0){
|
||||
levelWhite = atoi(payload);
|
||||
EEPROM.put(6, levelWhite);
|
||||
w = true;
|
||||
}
|
||||
if(strcmp(topic, TOPIC"lampw") == 0){
|
||||
lightWhite = atoi(payload);
|
||||
//EEPROM.put(0, levelBlue);
|
||||
//w = true;
|
||||
}
|
||||
if(strcmp(topic, TOPIC"moveperiod") == 0){
|
||||
periodMotion = atoi(payload);
|
||||
EEPROM.put(0, periodMotion);
|
||||
w = true;
|
||||
}
|
||||
if(strcmp(topic, TOPIC"splevel") == 0){
|
||||
spLight = atoi(payload);
|
||||
EEPROM.put(1, spLight);
|
||||
w = true;
|
||||
}
|
||||
if(strcmp(topic, TOPIC"dblevel") == 0){
|
||||
dbLight = atoi(payload);
|
||||
EEPROM.put(3, dbLight);
|
||||
w = true;
|
||||
}
|
||||
if(w) EEPROM.commit();
|
||||
}
|
||||
|
||||
void onMqttPublish(uint16_t packetId) {
|
||||
Serial.println("Publish acknowledged.");
|
||||
Serial.print(" packetId: ");
|
||||
Serial.println(packetId);
|
||||
g_led.start();
|
||||
//g_led.start();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user