Bigroom esp32
This commit is contained in:
3
BigRoomMyS/.vscode/extensions.json
vendored
3
BigRoomMyS/.vscode/extensions.json
vendored
@@ -3,5 +3,8 @@
|
||||
// for the documentation about the extensions.json format
|
||||
"recommendations": [
|
||||
"platformio.platformio-ide"
|
||||
],
|
||||
"unwantedRecommendations": [
|
||||
"ms-vscode.cpptools-extension-pack"
|
||||
]
|
||||
}
|
||||
|
||||
@@ -12,3 +12,8 @@
|
||||
platform = espressif32
|
||||
board = esp32dev
|
||||
framework = arduino
|
||||
lib_deps =
|
||||
robtillaart/RunningMedian @ ^0.3.3
|
||||
lewapek/Nova Fitness Sds dust sensors library @ ^1.5.1
|
||||
ottowinter/AsyncMqttClient-esphome @ ^0.8.6
|
||||
monitor_speed = 115200
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
3
ESP_BigRoom/.vscode/extensions.json
vendored
3
ESP_BigRoom/.vscode/extensions.json
vendored
@@ -3,5 +3,8 @@
|
||||
// for the documentation about the extensions.json format
|
||||
"recommendations": [
|
||||
"platformio.platformio-ide"
|
||||
],
|
||||
"unwantedRecommendations": [
|
||||
"ms-vscode.cpptools-extension-pack"
|
||||
]
|
||||
}
|
||||
|
||||
14
ESP_BigRoom/.vscode/settings.json
vendored
Normal file
14
ESP_BigRoom/.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"files.associations": {
|
||||
"array": "cpp",
|
||||
"*.tcc": "cpp",
|
||||
"deque": "cpp",
|
||||
"string": "cpp",
|
||||
"unordered_map": "cpp",
|
||||
"unordered_set": "cpp",
|
||||
"vector": "cpp",
|
||||
"memory": "cpp",
|
||||
"random": "cpp",
|
||||
"initializer_list": "cpp"
|
||||
}
|
||||
}
|
||||
@@ -1,18 +1,50 @@
|
||||
// #ifndef __MAIN__
|
||||
// #define __MAIN__
|
||||
#ifndef __MAIN__
|
||||
#define __MAIN__
|
||||
|
||||
// #include <Arduino.h>
|
||||
// #include <ArduinoOTA.h>
|
||||
// #include <Ticker.h>
|
||||
// #include <AsyncMqttClient.h>
|
||||
// #include "Adafruit_HTU21DF.h"
|
||||
#include <Arduino.h>
|
||||
#include <ArduinoOTA.h>
|
||||
#include <Ticker.h>
|
||||
#include <WiFi.h>
|
||||
extern "C" {
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/timers.h"
|
||||
}
|
||||
#include <AsyncMqttClient.h>
|
||||
#include <leds.h>
|
||||
#include "MHZ19.h"
|
||||
#include <Wire.h>
|
||||
//#include "HTU21D.h"
|
||||
#include <RunningMedian.h>
|
||||
#include <Adafruit_HTU21DF.h>
|
||||
#include <EEPROM.h>
|
||||
|
||||
// #define R_LED GPIO0
|
||||
// #define G_LED GPIO2
|
||||
// #define B_LED GPIO14
|
||||
#define BAUDRATE 9600
|
||||
|
||||
// bool meas = false;
|
||||
// short minCnt;
|
||||
#define LIGHT_SENS 36 //VP
|
||||
#define MOVE_SENS 39 //VN
|
||||
|
||||
|
||||
// #endif // __MAIN__
|
||||
#define LED_WHITE 25 //25
|
||||
#define PWM_WHITE 0
|
||||
#define LED_BLUE 26 //26
|
||||
#define PWM_BLUE 1
|
||||
#define POWER 27 //27
|
||||
#define TXD2 17
|
||||
#define RXD2 16
|
||||
#define LED_R 13
|
||||
#define LED_G 15
|
||||
#define LED_B 2
|
||||
|
||||
#define TOPIC "home/bigroom/"
|
||||
|
||||
byte periodMotion;
|
||||
int8_t curDelay;
|
||||
uint16_t spLight, dbLight;
|
||||
uint8_t timeLight;
|
||||
byte levelBlue, levelWhite;
|
||||
bool move, lightWhite;
|
||||
float temp, hum;
|
||||
bool bLamp, wLamp;
|
||||
uint8_t statLamp;
|
||||
|
||||
#endif // __MAIN__
|
||||
|
||||
@@ -8,7 +8,25 @@
|
||||
; Please visit documentation for the other options and examples
|
||||
; https://docs.platformio.org/page/projectconf.html
|
||||
|
||||
[env:nodemcuv2]
|
||||
[platformio]
|
||||
default_envs = lolin
|
||||
|
||||
[env:lolin]
|
||||
platform = espressif32
|
||||
board = lolin32
|
||||
framework = arduino
|
||||
monitor_speed = 115200
|
||||
lib_deps =
|
||||
adafruit/Adafruit HTU21DF Library @ ^1.0.5
|
||||
; enjoyneering/HTU21D @ ^1.2.1
|
||||
robtillaart/RunningMedian @ ^0.3.4
|
||||
wifwaf/MH-Z19 @ ^1.5.3
|
||||
plerup/EspSoftwareSerial @ ^6.15.2
|
||||
ottowinter/AsyncMqttClient-esphome @ ^0.8.6
|
||||
;upload_protocol = espota
|
||||
;upload_port = 192.168.1.31
|
||||
|
||||
[env:esp12]
|
||||
platform = espressif8266
|
||||
board = esp12e
|
||||
framework = arduino
|
||||
@@ -17,3 +35,16 @@ board_build.ldscript = eagle.flash.1m.ld
|
||||
;board_build.flash_mode = dout
|
||||
upload_protocol = espota
|
||||
upload_port = 192.168.1.131
|
||||
[env:nodemcuv2]
|
||||
platform = espressif8266
|
||||
board = nodemcuv2
|
||||
framework = arduino
|
||||
lib_deps =
|
||||
enjoyneering/HTU21D @ ^1.2.1
|
||||
robtillaart/RunningMedian @ ^0.3.4
|
||||
wifwaf/MH-Z19 @ ^1.5.3
|
||||
plerup/EspSoftwareSerial @ ^6.15.2
|
||||
ottowinter/AsyncMqttClient-esphome @ ^0.8.6
|
||||
;upload_protocol = espota
|
||||
;upload_port = 192.168.1.131
|
||||
|
||||
|
||||
@@ -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];
|
||||
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");
|
||||
}
|
||||
static uint32_t cRun = millis();
|
||||
static uint8_t sec = 0;
|
||||
char s[7];
|
||||
|
||||
if(cRun + 999 < millis()){
|
||||
ArduinoOTA.handle();
|
||||
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;
|
||||
}
|
||||
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));
|
||||
}
|
||||
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((sec + 2) % 30 == 0){
|
||||
temp = htu.readTemperature();
|
||||
hum = htu.readHumidity();
|
||||
Serial.println("Temp: " + String(temp));
|
||||
Serial.println("Hum: " + String(hum));
|
||||
Serial.println(sec);
|
||||
}
|
||||
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{
|
||||
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);
|
||||
Serial.println("CO2err:" + String(myMHZ19.errorCode));
|
||||
}
|
||||
if(++minCnt == 60){
|
||||
minCnt = 0;
|
||||
digitalWrite(P_SENS, HIGH);
|
||||
delay(10);
|
||||
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);
|
||||
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;
|
||||
}
|
||||
else if(minCnt % 10 == 0){
|
||||
if(mqttClient.connected()) mqttClient.publish("/home/bigroom/millis", 0, false, ultoa(millis(), v, 10));
|
||||
//b_led.start();
|
||||
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();
|
||||
}
|
||||
|
||||
3
I2C Scan/.vscode/extensions.json
vendored
3
I2C Scan/.vscode/extensions.json
vendored
@@ -3,5 +3,8 @@
|
||||
// for the documentation about the extensions.json format
|
||||
"recommendations": [
|
||||
"platformio.platformio-ide"
|
||||
],
|
||||
"unwantedRecommendations": [
|
||||
"ms-vscode.cpptools-extension-pack"
|
||||
]
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
; https://docs.platformio.org/page/projectconf.html
|
||||
|
||||
[platformio]
|
||||
default_envs = uno_ob
|
||||
default_envs = lolin
|
||||
[env:uno]
|
||||
platform = atmelavr
|
||||
board = uno
|
||||
@@ -26,3 +26,7 @@ framework = arduino
|
||||
platform = atmelavr
|
||||
board = nanoatmega328
|
||||
framework = arduino
|
||||
[env:lolin]
|
||||
platform = espressif32
|
||||
board = lolin32
|
||||
framework = arduino
|
||||
|
||||
@@ -18,19 +18,19 @@ void setup() {
|
||||
}
|
||||
|
||||
Serial.println ();
|
||||
//#undef SCL
|
||||
//#undef SDA
|
||||
#undef SCL
|
||||
#undef SDA
|
||||
#define SCL 32
|
||||
#define SDA 33
|
||||
Serial.print("SCL=");Serial.println (SCL);
|
||||
Serial.print("SDA=");Serial.println (SDA);
|
||||
Serial.print("PWSCL=");Serial.println (PIN_WIRE_SCL);
|
||||
Serial.print("PWSDA=");Serial.println (PIN_WIRE_SDA);
|
||||
//Serial.print("PWSCL=");Serial.println (PIN_WIRE_SCL);
|
||||
//Serial.print("PWSDA=");Serial.println (PIN_WIRE_SDA);
|
||||
Serial.println ();
|
||||
Serial.println ("I2C scanner. Scanning ...");
|
||||
byte count = 0;
|
||||
|
||||
/*#define SCL 5
|
||||
#define SDA 4*/
|
||||
Wire.begin();
|
||||
Wire.begin(SDA, SCL);
|
||||
for (byte i = 8; i < 120; i++)
|
||||
{
|
||||
Wire.beginTransmission (i);
|
||||
|
||||
Reference in New Issue
Block a user