Bigroom esp32
This commit is contained in:
@@ -1,9 +1,324 @@
|
||||
//#define MY_DEBUG
|
||||
//#define MY_RADIO_RF24
|
||||
//#define MY_RF24_CHANNEL (105)
|
||||
//#define MY_RF24_PA_LEVEL RF24_PA_MAX
|
||||
//#define MY_TRANSPORT_WAIT_READY_MS 10000
|
||||
|
||||
#define WIFI_SSID "wf-home"
|
||||
#define WIFI_PASSWORD "0ndthnrf"
|
||||
|
||||
#define MQTT_HOST IPAddress(192, 168, 1, 111)
|
||||
#define MQTT_PORT 1883
|
||||
#define MQTT_TOPIC "home/kor/"
|
||||
|
||||
#define LAMP_OUT 5
|
||||
#define PIN_MOVE 7
|
||||
|
||||
#include <Arduino.h>
|
||||
#include "SdsDustSensor.h"
|
||||
#include <RunningMedian.h>
|
||||
#include <EEPROM.h>
|
||||
#include <WiFi.h>
|
||||
#include <esp_now.h>
|
||||
|
||||
extern "C" {
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/timers.h"
|
||||
}
|
||||
#include <AsyncMqttClient.h>
|
||||
|
||||
typedef struct message {
|
||||
float temperature;
|
||||
float humidity;
|
||||
};
|
||||
struct message myMessage;
|
||||
|
||||
#define EEPROM_SIZE 24
|
||||
|
||||
unsigned long cRun;
|
||||
int cSec, adc, move, oldmov, minLight, minLightDB, LightInt, timeDelay, curDelay, fadeTime;
|
||||
uint8_t sdsPeriod;
|
||||
bool lamp;
|
||||
RunningMedian samples = RunningMedian(5 * sizeof(int));
|
||||
SdsDustSensor sds(Serial2);
|
||||
|
||||
AsyncMqttClient mqttClient;
|
||||
TimerHandle_t mqttReconnectTimer;
|
||||
TimerHandle_t wifiReconnectTimer;
|
||||
|
||||
void onMqttPublish(uint16_t packetId);
|
||||
void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total);
|
||||
void onMqttUnsubscribe(uint16_t packetId);
|
||||
void onMqttSubscribe(uint16_t packetId, uint8_t qos);
|
||||
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason);
|
||||
void onMqttConnect(bool sessionPresent);
|
||||
void WiFiEvent(WiFiEvent_t event);
|
||||
void connectToMqtt();
|
||||
void connectToWifi();
|
||||
void onDataReceiver(const uint8_t * mac, const uint8_t *incomingData, int len);
|
||||
|
||||
void setup() {
|
||||
// put your setup code here, to run once:
|
||||
Serial.begin(115200);
|
||||
sds.begin();
|
||||
pinMode(PIN_MOVE, INPUT_PULLUP);
|
||||
ledcSetup(0, 5000, 8);
|
||||
ledcAttachPin(LAMP_OUT, 0);
|
||||
//pinMode(LAMP_OUT, OUTPUT);
|
||||
EEPROM.begin(EEPROM_SIZE);
|
||||
EEPROM.get(0, minLight);
|
||||
EEPROM.get(4, minLightDB);
|
||||
EEPROM.get(8, LightInt);
|
||||
EEPROM.get(12, sdsPeriod);
|
||||
EEPROM.get(16, timeDelay);
|
||||
EEPROM.get(20, fadeTime);
|
||||
curDelay = -1;
|
||||
Serial.print(F("Min Light "));Serial.println(minLight);
|
||||
Serial.print(F("Min LightDB "));Serial.println(minLightDB);
|
||||
Serial.print(F("LightInt "));Serial.println(LightInt);
|
||||
Serial.print(F("SDS Period "));Serial.println(sdsPeriod);
|
||||
Serial.print(F("Time Delay "));Serial.println(timeDelay);
|
||||
Serial.println(sds.queryFirmwareVersion().toString()); // prints firmware version
|
||||
Serial.println(sds.setActiveReportingMode().toString()); // ensures sensor is in 'query' reporting mode
|
||||
sds.setCustomWorkingPeriod(sdsPeriod);
|
||||
|
||||
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);
|
||||
mqttClient.onSubscribe(onMqttSubscribe);
|
||||
mqttClient.onUnsubscribe(onMqttUnsubscribe);
|
||||
mqttClient.onMessage(onMqttMessage);
|
||||
mqttClient.onPublish(onMqttPublish);
|
||||
mqttClient.setServer(MQTT_HOST, MQTT_PORT);
|
||||
|
||||
WiFi.mode(WIFI_MODE_APSTA);
|
||||
connectToWifi();
|
||||
|
||||
// Initializing the ESP-NOW
|
||||
if (esp_now_init() != 0) {
|
||||
Serial.println("Problem during ESP-NOW init");
|
||||
return;
|
||||
}
|
||||
esp_now_register_recv_cb(onDataReceiver);
|
||||
oldmov = 0;
|
||||
lamp = false;
|
||||
cRun = millis();
|
||||
cSec = 0;
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// put your main code here, to run repeatedly:
|
||||
if(digitalRead(PIN_MOVE) > 0){
|
||||
if(curDelay == -1) ;//sendData(msgMove, true);
|
||||
move = true;
|
||||
curDelay = timeDelay;
|
||||
}
|
||||
adc = analogRead(GPIO_NUM_0);
|
||||
samples.add(adc);
|
||||
adc = samples.getMedian();
|
||||
if ((adc <= minLight) && (move == 1)){
|
||||
ledcWrite(0, LightInt);
|
||||
//analogWrite(LAMP_OUT, LightInt);
|
||||
if(lamp == false){
|
||||
Serial.println("Lamp ON");
|
||||
Serial.print("ADC: ");Serial.print(adc);
|
||||
Serial.print(", Move: ");Serial.println(move);
|
||||
//sendData(msgLightLev, adc);
|
||||
//send(msgLightLev.set(adc, 2));
|
||||
//wait(50);
|
||||
//sendData(msgLamp, true);
|
||||
//send(msgLamp.set(true));
|
||||
lamp = true;
|
||||
}
|
||||
}
|
||||
else if((adc > (minLight + minLightDB)) || (move == 0)){
|
||||
ledcWrite(0, 0);
|
||||
//curDelay = 2;
|
||||
if(lamp == true){
|
||||
Serial.println("Lamp OFF");
|
||||
Serial.print("ADC: ");Serial.print(adc);
|
||||
Serial.print(", Move: ");Serial.println(move);
|
||||
//sendData(msgLightLev, adc);
|
||||
//send(msgLightLev.set(adc, 2));
|
||||
//wait(50);
|
||||
//sendData(msgLamp, false);
|
||||
//send(msgLamp.set(false));
|
||||
lamp = false;
|
||||
//curTime = millis() + fadeTime * 1000 ;
|
||||
}
|
||||
}
|
||||
if(cRun + 999 < millis()){
|
||||
cRun = millis();
|
||||
if(curDelay == 0) {
|
||||
//sendData(msgMove, false);
|
||||
move = false;
|
||||
curDelay = -1;
|
||||
}
|
||||
if(curDelay > 0) curDelay--;
|
||||
//if(curDelay == 1) curTime = cRun + fadeTime * 1000;
|
||||
PmResult pm = sds.queryPm();
|
||||
|
||||
if (pm.isOk()) {
|
||||
// Serial.println(millis()/1000);
|
||||
// Serial.println("P2.5: " + String(p25));
|
||||
// Serial.println("P10: " + String(p10));
|
||||
//sendData(msgHum25, p25);
|
||||
//wait(100);
|
||||
//sendData(msgHum10, p10);
|
||||
}
|
||||
else{
|
||||
//pm.statusToString();
|
||||
}
|
||||
if (++cSec > 19){
|
||||
cSec = 0;
|
||||
//wait(100);
|
||||
//sendData(msgLightLev, adc);
|
||||
//wait(100);
|
||||
uint32_t ms = millis() /1000;
|
||||
//send(msgMillis.set(ms));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void connectToWifi() {
|
||||
Serial.println("Connecting to Wi-Fi...");
|
||||
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
|
||||
}
|
||||
|
||||
void connectToMqtt() {
|
||||
Serial.println("Connecting to MQTT...");
|
||||
mqttClient.connect();
|
||||
}
|
||||
|
||||
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());
|
||||
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);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void onMqttConnect(bool sessionPresent) {
|
||||
char v[10];
|
||||
Serial.println("Connected to MQTT.");
|
||||
Serial.print("Session present: ");
|
||||
Serial.println(sessionPresent);
|
||||
EEPROM.get(0, minLight);
|
||||
EEPROM.get(4, minLightDB);
|
||||
EEPROM.get(8, LightInt);
|
||||
EEPROM.get(12, sdsPeriod);
|
||||
EEPROM.get(16, timeDelay);
|
||||
EEPROM.get(20, fadeTime);
|
||||
uint16_t packetIdSub = mqttClient.subscribe(MQTT_TOPIC"minlight", 1);
|
||||
packetIdSub = mqttClient.subscribe(MQTT_TOPIC"minlightdb", 1);
|
||||
packetIdSub = mqttClient.subscribe(MQTT_TOPIC"lightint", 1);
|
||||
packetIdSub = mqttClient.subscribe(MQTT_TOPIC"sdsperiod", 1);
|
||||
packetIdSub = mqttClient.subscribe(MQTT_TOPIC"timedelay", 1);
|
||||
packetIdSub = mqttClient.subscribe(MQTT_TOPIC"fadetime", 1);
|
||||
itoa(minLight, v, 10);
|
||||
mqttClient.publish(MQTT_TOPIC"minlight", 0, true, v);
|
||||
itoa(minLightDB, v, 10);
|
||||
mqttClient.publish(MQTT_TOPIC"minlightdb", 0, true, v);
|
||||
itoa(LightInt, v, 10);
|
||||
mqttClient.publish(MQTT_TOPIC"lightint", 0, true, v);
|
||||
itoa(sdsPeriod, v, 10);
|
||||
mqttClient.publish(MQTT_TOPIC"sdsperiod", 0, true, v);
|
||||
itoa(timeDelay, v, 10);
|
||||
mqttClient.publish(MQTT_TOPIC"timedelay", 0, true, v);
|
||||
itoa(fadeTime, v, 10);
|
||||
mqttClient.publish(MQTT_TOPIC"fadetime", 0, true, v);
|
||||
}
|
||||
|
||||
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
|
||||
Serial.println("Disconnected from MQTT.");
|
||||
|
||||
if (WiFi.isConnected()) {
|
||||
xTimerStart(mqttReconnectTimer, 0);
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
bool w = false;
|
||||
if(strcmp(topic, MQTT_TOPIC"minlight") == 0){
|
||||
w = true;
|
||||
minLight = atoi(payload);
|
||||
EEPROM.put(0, minLight);
|
||||
}
|
||||
if(strcmp(topic, MQTT_TOPIC"minlightdb") == 0){
|
||||
w = true;
|
||||
minLightDB = atoi(payload);
|
||||
EEPROM.put(4, minLightDB);
|
||||
}
|
||||
if(strcmp(topic, MQTT_TOPIC"lightint") == 0){
|
||||
w = true;
|
||||
LightInt = atoi(payload);
|
||||
EEPROM.put(8, LightInt);
|
||||
}
|
||||
if(strcmp(topic, MQTT_TOPIC"sdsperiod") == 0){
|
||||
w = true;
|
||||
sdsPeriod = atoi(payload);
|
||||
EEPROM.put(12, sdsPeriod);
|
||||
}
|
||||
if(strcmp(topic, MQTT_TOPIC"timedelay") == 0){
|
||||
w = true;
|
||||
timeDelay = atoi(payload);
|
||||
EEPROM.put(16, timeDelay);
|
||||
}
|
||||
if(strcmp(topic, MQTT_TOPIC"fadetime") == 0){
|
||||
w = true;
|
||||
fadeTime = atoi(payload);
|
||||
EEPROM.put(20, fadeTime);
|
||||
}
|
||||
|
||||
if(w) EEPROM.end();// commit();
|
||||
}
|
||||
|
||||
void onMqttPublish(uint16_t packetId) {
|
||||
Serial.println("Publish acknowledged.");
|
||||
Serial.print(" packetId: ");
|
||||
Serial.println(packetId);
|
||||
}
|
||||
|
||||
void onDataReceiver(const uint8_t * mac, const uint8_t *incomingData, int len) {
|
||||
Serial.println("Message received.");
|
||||
// We don't use mac to verify the sender
|
||||
// Let us transform the incomingData into our message structure
|
||||
memcpy(&myMessage, incomingData, sizeof(myMessage));
|
||||
Serial.println("=== Data ===");
|
||||
Serial.print("Mac address: ");
|
||||
for (int i = 0; i < 6; i++) {
|
||||
Serial.print("0x");
|
||||
Serial.print(mac[i], HEX);
|
||||
Serial.print(":");
|
||||
}
|
||||
Serial.print("\n\nTemperature: ");
|
||||
Serial.println(myMessage.temperature);
|
||||
Serial.print("\nHumidity: ");
|
||||
Serial.println(myMessage.humidity);
|
||||
Serial.println();
|
||||
}
|
||||
Reference in New Issue
Block a user