Move from MySensors KOR
This commit is contained in:
@@ -14,4 +14,9 @@ board = nodemcuv2
|
||||
framework = arduino
|
||||
monitor_speed = 9600
|
||||
upload_protocol = espota
|
||||
upload_port = 192.168.1.6
|
||||
upload_port = 192.168.1.156
|
||||
lib_deps =
|
||||
jwrw/ESP_EEPROM @ ^2.0.0
|
||||
robtillaart/RunningMedian @ ^0.3.3
|
||||
ayushsharma82/WebSerial @ ^1.3.0
|
||||
ottowinter/ESPAsyncWebServer-esphome @ ^3.0.0
|
||||
|
||||
@@ -3,21 +3,38 @@
|
||||
#include <Ticker.h>
|
||||
#include <AsyncMqttClient.h>
|
||||
#include "SdsDustSensor.h"
|
||||
#include <ESP_EEPROM.h>
|
||||
#include <RunningMedian.h>
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <ESPAsyncTCP.h>
|
||||
#include <ESPAsyncWebServer.h>
|
||||
#include <WebSerial.h>
|
||||
|
||||
#define WIFI_SSID "wf-home"
|
||||
#define WIFI_PASSWORD "0ndthnrf"
|
||||
#define MQTT_SERV "192.168.1.250"
|
||||
#define MQTT_SERV "192.168.1.111"
|
||||
#define TOPIC "home/kor/"
|
||||
|
||||
#define HOSTNAME "ESP_Kor"
|
||||
|
||||
#define LED_WF (D0)
|
||||
#define LED_MQ (D3)
|
||||
#define LED_WRK (D4)
|
||||
#define LED_MQ (D4)
|
||||
#define LED_WRK (D3)
|
||||
|
||||
int rxPin = D1;
|
||||
int txPin = D2;
|
||||
#define LAMP_OUT (D5)
|
||||
#define PIN_MOVE (D6)
|
||||
|
||||
int rxPin = D7;
|
||||
int txPin = D8;
|
||||
SdsDustSensor sds(rxPin, txPin);
|
||||
|
||||
int cSec, adc, move, oldmov, minLight, minLightDB, LightInt, timeDelay, curDelay, fadeTime;
|
||||
uint8_t sdsPeriod;
|
||||
bool lamp;
|
||||
float mins;
|
||||
|
||||
RunningMedian samples = RunningMedian(5 * sizeof(int));
|
||||
|
||||
AsyncMqttClient mqttClient;
|
||||
Ticker mqttReconnectTimer;
|
||||
|
||||
@@ -37,7 +54,9 @@ void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties
|
||||
void onMqttPublish(uint16_t packetId);
|
||||
|
||||
unsigned long cRun;
|
||||
int cSec = 0;
|
||||
unsigned long stled;
|
||||
|
||||
AsyncWebServer server(80);
|
||||
|
||||
void setup()
|
||||
{
|
||||
@@ -80,43 +99,121 @@ void setup()
|
||||
mqttClient.onPublish(onMqttPublish);
|
||||
mqttClient.setServer(MQTT_SERV, 1883);
|
||||
|
||||
sds.begin();
|
||||
|
||||
// Serial.println(sds.queryFirmwareVersion().toString()); // prints firmware version
|
||||
// Serial.println(sds.setActiveReportingMode().toString()); // ensures sensor is in 'active' reporting mode
|
||||
// Serial.println(sds.setCustomWorkingPeriod(3).toString()); // sensor sends data every 3 minutes
|
||||
sds.setActiveReportingMode();
|
||||
sds.setCustomWorkingPeriod(1);
|
||||
|
||||
pinMode(LED_WF, OUTPUT);
|
||||
pinMode(LED_MQ, OUTPUT);
|
||||
pinMode(LED_WRK, OUTPUT);
|
||||
digitalWrite(LED_WF, HIGH);
|
||||
pinMode(LAMP_OUT, OUTPUT);
|
||||
pinMode(PIN_MOVE, INPUT);
|
||||
|
||||
digitalWrite(LED_WF, LOW);
|
||||
digitalWrite(LED_MQ, HIGH);
|
||||
digitalWrite(LED_WRK, LOW);
|
||||
|
||||
EEPROM.begin(20);
|
||||
EEPROM.get(0, timeDelay);
|
||||
EEPROM.get(4, minLight);
|
||||
EEPROM.get(8, minLightDB);
|
||||
EEPROM.get(12, sdsPeriod);
|
||||
EEPROM.get(16, LightInt);
|
||||
Serial.printf("timeDelay=%d\n", timeDelay);
|
||||
Serial.printf("minLight=%d\n", minLight);
|
||||
Serial.printf("minLightDB=%d\n", minLightDB);
|
||||
Serial.printf("sdsPeriod=%d\n", sdsPeriod);
|
||||
Serial.printf("LightInt=%d\n", LightInt);
|
||||
//mvDelaySet = 90;
|
||||
curDelay = -1;
|
||||
|
||||
sds.begin();
|
||||
Serial.println(sds.queryFirmwareVersion().toString()); // prints firmware version
|
||||
sds.setActiveReportingMode();
|
||||
sds.setCustomWorkingPeriod(sdsPeriod);
|
||||
connectToWifi();
|
||||
|
||||
cRun = millis();
|
||||
stled = millis();
|
||||
mins = 0;
|
||||
WebSerial.begin(&server);
|
||||
/* Attach Message Callback */
|
||||
server.begin();
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
//static bool lwork = false;
|
||||
static unsigned long stled = millis();
|
||||
char v[6];
|
||||
ArduinoOTA.handle();
|
||||
|
||||
if(digitalRead(PIN_MOVE) > 0){
|
||||
if(curDelay == -1) mqttClient.publish(TOPIC"move", 1, false, "1");
|
||||
move = true;
|
||||
curDelay = timeDelay;
|
||||
}
|
||||
|
||||
adc = analogRead(A0);
|
||||
samples.add(adc);
|
||||
adc = samples.getMedian();
|
||||
|
||||
if ((adc <= minLight) && (move == 1)){
|
||||
analogWrite(LAMP_OUT, LightInt);
|
||||
if(lamp == false){
|
||||
Serial.println("Lamp ON");
|
||||
Serial.print("ADC: ");Serial.print(adc);
|
||||
Serial.print(", Move: ");Serial.println(move);
|
||||
mqttClient.publish(TOPIC"adc", 1, false, "adc");
|
||||
mqttClient.publish(TOPIC"lamp", 1, false, "1");
|
||||
lamp = true;
|
||||
}
|
||||
}
|
||||
else if((adc > (minLight + minLightDB)) || (move == 0)){
|
||||
analogWrite(LAMP_OUT, 0);
|
||||
//curDelay = 2;
|
||||
if(lamp == true){
|
||||
Serial.println("Lamp OFF");
|
||||
Serial.print("ADC: ");Serial.print(adc);
|
||||
Serial.print(", Move: ");Serial.println(move);
|
||||
mqttClient.publish(TOPIC"adc", 1, false, "adc");
|
||||
mqttClient.publish(TOPIC"lamp", 1, false, "0");
|
||||
lamp = false;
|
||||
}
|
||||
}
|
||||
if(stled + 300 < millis()){
|
||||
digitalWrite(LED_WRK, HIGH);
|
||||
}
|
||||
if(cRun + 999 < millis()){
|
||||
cRun = millis();
|
||||
PmResult pm = sds.readPm();
|
||||
if((stled + 300) < millis()) digitalWrite(LED_WRK, LOW);
|
||||
//lwork = !lwork;
|
||||
cSec++;
|
||||
mins += 1 / 60.0;
|
||||
if(cSec == 10){
|
||||
sprintf(v, "%.1f", mins);
|
||||
mqttClient.publish(TOPIC"minsmain", 1, false, v);
|
||||
itoa(adc, v, 10);
|
||||
mqttClient.publish(TOPIC"adc", 1, false, v);
|
||||
cSec = 0;
|
||||
}
|
||||
if(curDelay == 0) {
|
||||
mqttClient.publish(TOPIC"move", 1, false, "0");
|
||||
move = false;
|
||||
curDelay = -1;
|
||||
}
|
||||
if(curDelay > 0){
|
||||
curDelay--;
|
||||
WebSerial.print("Timer light:");
|
||||
WebSerial.print(curDelay);
|
||||
WebSerial.print(", ADC:");
|
||||
WebSerial.println(adc);
|
||||
}
|
||||
|
||||
PmResult pm = sds.readPm();
|
||||
//if((stled + 300) < millis()) digitalWrite(LED_WRK, LOW);
|
||||
//lwork = !lwork;
|
||||
if (pm.isOk()) {
|
||||
if(mqttClient.connected()){
|
||||
digitalWrite(LED_WRK, HIGH);
|
||||
stled = millis();
|
||||
char v[6];
|
||||
|
||||
sprintf(v, "%.1f", pm.pm25);
|
||||
mqttClient.publish("/home/kor/pm25", 1, false, v);
|
||||
mqttClient.publish(TOPIC"pm25", 1, false, v);
|
||||
sprintf(v, "%.1f", pm.pm10);
|
||||
mqttClient.publish("/home/kor/pm10", 1, false, v);
|
||||
mqttClient.publish(TOPIC"pm10", 1, false, v);
|
||||
}
|
||||
Serial.print(cRun); Serial.print("\t");
|
||||
Serial.print("PM2.5 = ");
|
||||
@@ -124,7 +221,6 @@ void loop()
|
||||
Serial.print(", PM10 = ");
|
||||
Serial.println(pm.pm10);
|
||||
|
||||
cSec = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -144,20 +240,39 @@ void onWifiConnect(const WiFiEventStationModeGotIP& event) {
|
||||
Serial.print("IP: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
connectToMqtt();
|
||||
digitalWrite(LED_WF, LOW);
|
||||
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, HIGH);
|
||||
digitalWrite(LED_WF, LOW);
|
||||
}
|
||||
|
||||
void onMqttConnect(bool sessionPresent) {
|
||||
Serial.println("Connected to MQTT.");
|
||||
Serial.print("Session present: ");
|
||||
Serial.println(sessionPresent);
|
||||
char v[10];
|
||||
|
||||
itoa(timeDelay, v, 10);
|
||||
mqttClient.publish(TOPIC"mvdelay", 1, false, v);
|
||||
itoa(minLight, v, 10);
|
||||
mqttClient.publish(TOPIC"lightsp", 1, false, v);
|
||||
itoa(minLightDB, v, 10);
|
||||
mqttClient.publish(TOPIC"lightdb", 1, false, v);
|
||||
itoa(sdsPeriod, v, 10);
|
||||
mqttClient.publish(TOPIC"sdsperiod", 1, false, v);
|
||||
itoa(LightInt, v, 10);
|
||||
mqttClient.publish(TOPIC"lightlev", 1, false, v);
|
||||
|
||||
mqttClient.subscribe(TOPIC"mvdelay", 1);
|
||||
mqttClient.subscribe(TOPIC"lightsp", 1);
|
||||
mqttClient.subscribe(TOPIC"lightdb", 1);
|
||||
mqttClient.subscribe(TOPIC"sdsperiod", 1);
|
||||
mqttClient.subscribe(TOPIC"lightlev", 1);
|
||||
|
||||
digitalWrite(LED_MQ, LOW);
|
||||
}
|
||||
|
||||
@@ -185,10 +300,48 @@ void onMqttUnsubscribe(uint16_t packetId) {
|
||||
}
|
||||
|
||||
void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
|
||||
bool chd = false;
|
||||
if(strcmp(topic, TOPIC"mvdelay") == 0){
|
||||
timeDelay = atoi(payload);
|
||||
Serial.printf("timeDelay=%d\n", timeDelay);
|
||||
EEPROM.put(0, timeDelay);
|
||||
chd = true;
|
||||
}
|
||||
if(strcmp(topic, TOPIC"lightsp") == 0){
|
||||
minLight = atoi(payload);
|
||||
Serial.printf("minLight=%d\n", minLight);
|
||||
EEPROM.put(4, minLight);
|
||||
chd = true;
|
||||
}
|
||||
if(strcmp(topic, TOPIC"lightdb") == 0){
|
||||
minLightDB = atoi(payload);
|
||||
Serial.printf("minLightDB=%d\n", minLightDB);
|
||||
EEPROM.put(8, minLightDB);
|
||||
chd = true;
|
||||
}
|
||||
if(strcmp(topic, TOPIC"sdsperiod") == 0){
|
||||
sdsPeriod = atoi(payload);
|
||||
EEPROM.put(12, sdsPeriod);
|
||||
Serial.printf("sdsPeriod=%d\n", sdsPeriod);
|
||||
sds.setCustomWorkingPeriod(sdsPeriod);
|
||||
chd = true;
|
||||
}
|
||||
if(strcmp(topic, TOPIC"lightlev") == 0){
|
||||
LightInt = atoi(payload);
|
||||
Serial.printf("LightLev=%d\n", LightInt);
|
||||
EEPROM.put(16, LightInt);
|
||||
chd = true;
|
||||
}
|
||||
if(chd){
|
||||
EEPROM.commit();
|
||||
Serial.println("EEPROM commit");
|
||||
}
|
||||
}
|
||||
|
||||
void onMqttPublish(uint16_t packetId) {
|
||||
Serial.println("Publish acknowledged.");
|
||||
Serial.print(" packetId: ");
|
||||
Serial.println(packetId);
|
||||
digitalWrite(LED_WRK, LOW);
|
||||
stled = millis();
|
||||
}
|
||||
|
||||
3
KorMYS/.vscode/extensions.json
vendored
3
KorMYS/.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"
|
||||
]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user