Bigroom PubSubClient, RGB LED
This commit is contained in:
@@ -11,7 +11,7 @@ extern "C" {
|
|||||||
#include "freertos/FreeRTOS.h"
|
#include "freertos/FreeRTOS.h"
|
||||||
#include "freertos/timers.h"
|
#include "freertos/timers.h"
|
||||||
}
|
}
|
||||||
#include <AsyncMqttClient.h>
|
//#include <AsyncMqttClient.h>
|
||||||
#include <leds.h>
|
#include <leds.h>
|
||||||
#include "MHZ19.h"
|
#include "MHZ19.h"
|
||||||
#include <Wire.h>
|
#include <Wire.h>
|
||||||
@@ -23,6 +23,9 @@ extern "C" {
|
|||||||
#include <ESPAsyncWebServer.h>
|
#include <ESPAsyncWebServer.h>
|
||||||
#include <WebSerial.h>
|
#include <WebSerial.h>
|
||||||
#include <AsyncElegantOTA.h>
|
#include <AsyncElegantOTA.h>
|
||||||
|
#include <SoftwareSerial.h>
|
||||||
|
#include <SerialTransfer.h>
|
||||||
|
#include <PubSubClient.h>
|
||||||
|
|
||||||
#define BAUDRATE 9600
|
#define BAUDRATE 9600
|
||||||
|
|
||||||
@@ -43,6 +46,7 @@ extern "C" {
|
|||||||
#define RECIVER 18
|
#define RECIVER 18
|
||||||
|
|
||||||
#define TOPIC "home/bigroom/"
|
#define TOPIC "home/bigroom/"
|
||||||
|
#define HNAME "BigRoom"
|
||||||
|
|
||||||
//WiFiMulti wifiMulti;
|
//WiFiMulti wifiMulti;
|
||||||
//const uint32_t connectTimeoutMs = 5000;
|
//const uint32_t connectTimeoutMs = 5000;
|
||||||
@@ -59,5 +63,31 @@ uint8_t statLamp;
|
|||||||
|
|
||||||
|
|
||||||
AsyncWebServer server(80);
|
AsyncWebServer server(80);
|
||||||
|
struct I2cTxStruct {
|
||||||
|
//byte mode, hue, saturation, bright;
|
||||||
|
byte mode, r, g, b;
|
||||||
|
};
|
||||||
|
|
||||||
|
#define SSRX 26
|
||||||
|
#define SSTX 25
|
||||||
|
|
||||||
|
I2cTxStruct txData = {0, 0x0F, 0x0F, 0x0F};
|
||||||
|
I2cTxStruct txDataN;
|
||||||
|
I2cTxStruct txDataF;
|
||||||
|
uint8_t mode = 0;
|
||||||
|
bool forceNight = false;
|
||||||
|
|
||||||
|
bool newTxData;
|
||||||
|
void transmitData();
|
||||||
|
|
||||||
|
EspSoftwareSerial::UART lampSerial;
|
||||||
|
SerialTransfer myTransfer;
|
||||||
|
|
||||||
|
long lastReconnectAttempt = 0;
|
||||||
|
WiFiClient espClient;
|
||||||
|
PubSubClient client(espClient);
|
||||||
|
|
||||||
|
boolean reconnect();
|
||||||
|
void callback(char* topic, byte* payload, unsigned int length);
|
||||||
|
|
||||||
#endif // __MAIN__
|
#endif // __MAIN__
|
||||||
|
|||||||
@@ -26,6 +26,9 @@ lib_deps =
|
|||||||
ottowinter/ESPAsyncWebServer-esphome @ ^2.1.0
|
ottowinter/ESPAsyncWebServer-esphome @ ^2.1.0
|
||||||
ayushsharma82/AsyncElegantOTA @ ^2.2.6
|
ayushsharma82/AsyncElegantOTA @ ^2.2.6
|
||||||
ayushsharma82/WebSerial @ ^1.3.0
|
ayushsharma82/WebSerial @ ^1.3.0
|
||||||
|
plerup/EspSoftwareSerial @ ^8.1.0
|
||||||
|
powerbroker2/SerialTransfer @ ^3.1.3
|
||||||
|
knolleary/PubSubClient @ ^2.8
|
||||||
;upload_protocol = espota
|
;upload_protocol = espota
|
||||||
;upload_port = 192.168.1.138
|
;upload_port = 192.168.1.138
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ MHZ19 myMHZ19;
|
|||||||
Adafruit_HTU21DF htu = Adafruit_HTU21DF();
|
Adafruit_HTU21DF htu = Adafruit_HTU21DF();
|
||||||
RunningMedian samples = RunningMedian(5 * sizeof(int));
|
RunningMedian samples = RunningMedian(5 * sizeof(int));
|
||||||
|
|
||||||
AsyncMqttClient mqttClient;
|
//AsyncMqttClient mqttClient;
|
||||||
TimerHandle_t mqttReconnectTimer;
|
TimerHandle_t mqttReconnectTimer;
|
||||||
TimerHandle_t wifiReconnectTimer;
|
TimerHandle_t wifiReconnectTimer;
|
||||||
|
|
||||||
@@ -17,24 +17,26 @@ const char* password2 = "499727479o";
|
|||||||
const char* mqtt_server = "192.168.1.111";
|
const char* mqtt_server = "192.168.1.111";
|
||||||
|
|
||||||
void connectToWifi();
|
void connectToWifi();
|
||||||
void connectToMqtt();
|
//void connectToMqtt();
|
||||||
void WiFiEvent(WiFiEvent_t event);
|
void WiFiEvent(WiFiEvent_t event);
|
||||||
void onMqttConnect(bool sessionPresent);
|
// void onMqttConnect(bool sessionPresent);
|
||||||
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason);
|
// void onMqttDisconnect(AsyncMqttClientDisconnectReason reason);
|
||||||
void onMqttSubscribe(uint16_t packetId, uint8_t qos);
|
// void onMqttSubscribe(uint16_t packetId, uint8_t qos);
|
||||||
void onMqttUnsubscribe(uint16_t packetId);
|
// void onMqttUnsubscribe(uint16_t packetId);
|
||||||
void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total);
|
// void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total);
|
||||||
void onMqttPublish(uint16_t packetId);
|
// void onMqttPublish(uint16_t packetId);
|
||||||
void trim(char *s);
|
void trim(char *s);
|
||||||
|
|
||||||
leds bled(LED_B, 300, true);
|
leds bled(LED_B, 300, true);
|
||||||
|
TwoWire MyI2C = TwoWire(8);
|
||||||
|
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
Serial.println("Booting"); // "Загрузка"
|
Serial.println("Booting"); // "Загрузка"
|
||||||
WiFi.mode(WIFI_STA);
|
WiFi.mode(WIFI_STA);
|
||||||
//WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE, INADDR_NONE);
|
//WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE, INADDR_NONE);
|
||||||
WiFi.setHostname("BigRoom");
|
WiFi.setHostname(HNAME);
|
||||||
//wifiMulti.addAP(ssid1, password1);
|
//wifiMulti.addAP(ssid1, password1);
|
||||||
//wifiMulti.addAP(ssid2, password2);
|
//wifiMulti.addAP(ssid2, password2);
|
||||||
// ArduinoOTA.onStart([]() {
|
// ArduinoOTA.onStart([]() {
|
||||||
@@ -62,19 +64,21 @@ void setup() {
|
|||||||
|
|
||||||
// ArduinoOTA.begin();
|
// ArduinoOTA.begin();
|
||||||
|
|
||||||
mqttReconnectTimer = xTimerCreate("mqttTimer", pdMS_TO_TICKS(2000), pdFALSE, (void*)0, reinterpret_cast<TimerCallbackFunction_t>(connectToMqtt));
|
//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));
|
wifiReconnectTimer = xTimerCreate("wifiTimer", pdMS_TO_TICKS(2000), pdFALSE, (void*)0, reinterpret_cast<TimerCallbackFunction_t>(connectToWifi));
|
||||||
|
|
||||||
WiFi.onEvent(WiFiEvent);
|
WiFi.onEvent(WiFiEvent);
|
||||||
|
|
||||||
mqttClient.onConnect(onMqttConnect);
|
// mqttClient.onConnect(onMqttConnect);
|
||||||
mqttClient.onDisconnect(onMqttDisconnect);
|
// mqttClient.onDisconnect(onMqttDisconnect);
|
||||||
mqttClient.onSubscribe(onMqttSubscribe);
|
// mqttClient.onSubscribe(onMqttSubscribe);
|
||||||
mqttClient.onUnsubscribe(onMqttUnsubscribe);
|
// mqttClient.onUnsubscribe(onMqttUnsubscribe);
|
||||||
mqttClient.onMessage(onMqttMessage);
|
// mqttClient.onMessage(onMqttMessage);
|
||||||
mqttClient.onPublish(onMqttPublish);
|
// mqttClient.onPublish(onMqttPublish);
|
||||||
mqttClient.setServer(mqtt_server, 1883);
|
// mqttClient.setServer(mqtt_server, 1883);
|
||||||
mqttClient.setClientId("BigRoom");
|
// mqttClient.setClientId("BigRoom");
|
||||||
|
client.setServer(mqtt_server, 1883);
|
||||||
|
|
||||||
|
|
||||||
pinMode(MOVE_SENS, INPUT_PULLUP);
|
pinMode(MOVE_SENS, INPUT_PULLUP);
|
||||||
pinMode(LED_R, OUTPUT);
|
pinMode(LED_R, OUTPUT);
|
||||||
@@ -83,15 +87,16 @@ void setup() {
|
|||||||
pinMode(POWER, OUTPUT);
|
pinMode(POWER, OUTPUT);
|
||||||
pinMode(RECIVER, OUTPUT);
|
pinMode(RECIVER, OUTPUT);
|
||||||
|
|
||||||
ledcSetup(PWM_WHITE, 5000, 8);
|
// ledcSetup(PWM_WHITE, 5000, 8);
|
||||||
ledcSetup(PWM_BLUE, 5000, 8);
|
// ledcSetup(PWM_BLUE, 5000, 8);
|
||||||
ledcAttachPin(LED_WHITE, PWM_WHITE);
|
// ledcAttachPin(LED_WHITE, PWM_WHITE);
|
||||||
ledcAttachPin(LED_BLUE, PWM_BLUE);
|
// ledcAttachPin(LED_BLUE, PWM_BLUE);
|
||||||
|
|
||||||
delay(200);
|
delay(200);
|
||||||
digitalWrite(LED_R, LOW);
|
digitalWrite(LED_R, LOW);
|
||||||
digitalWrite(LED_G, HIGH);
|
digitalWrite(LED_G, HIGH);
|
||||||
digitalWrite(LED_B, HIGH);
|
digitalWrite(LED_B, HIGH);
|
||||||
|
digitalWrite(POWER, HIGH);
|
||||||
Serial2.begin(9600);
|
Serial2.begin(9600);
|
||||||
myMHZ19.begin(Serial2);
|
myMHZ19.begin(Serial2);
|
||||||
myMHZ19.setRange(); // *Important, Pass your Stream reference here
|
myMHZ19.setRange(); // *Important, Pass your Stream reference here
|
||||||
@@ -127,46 +132,51 @@ void setup() {
|
|||||||
}
|
}
|
||||||
Serial.println("Sensor found!");
|
Serial.println("Sensor found!");
|
||||||
|
|
||||||
char s[7];
|
//char s[8];
|
||||||
EEPROM.begin(10);
|
EEPROM.begin(20);
|
||||||
EEPROM.get(0, periodMotion);
|
EEPROM.get(0, periodMotion);
|
||||||
itoa(periodMotion, s, 10);
|
|
||||||
mqttClient.publish(TOPIC"", 1, false, s);
|
|
||||||
//publish period
|
//publish period
|
||||||
EEPROM.get(1, spLight);
|
EEPROM.get(1, spLight);
|
||||||
itoa(spLight, s, 10);
|
|
||||||
mqttClient.publish(TOPIC"", 1, false, s);
|
|
||||||
//publish splight
|
//publish splight
|
||||||
EEPROM.get(3, dbLight);
|
EEPROM.get(3, dbLight);
|
||||||
itoa(dbLight, s, 10);
|
|
||||||
mqttClient.publish(TOPIC"", 1, false, s);
|
|
||||||
EEPROM.get(5, levelBlue);
|
EEPROM.get(5, levelBlue);
|
||||||
itoa(levelBlue, s, 10);
|
|
||||||
mqttClient.publish(TOPIC"", 1, false, s);
|
|
||||||
EEPROM.get(6, levelWhite);
|
EEPROM.get(6, levelWhite);
|
||||||
itoa(levelWhite, s, 10);
|
EEPROM.get(8, txDataF.r);
|
||||||
mqttClient.publish(TOPIC"", 1, false, s);
|
EEPROM.get(9, txDataF.g);
|
||||||
|
EEPROM.get(10, txDataF.b);
|
||||||
|
EEPROM.get(11, txDataN.r);
|
||||||
|
EEPROM.get(12, txDataN.g);
|
||||||
|
EEPROM.get(13, txDataN.b);
|
||||||
|
//EEPROM.get(16, mode);
|
||||||
|
|
||||||
|
bool w = false;
|
||||||
if(spLight == 65535) {
|
if(spLight == 65535) {
|
||||||
spLight = 1000;
|
spLight = 1000;
|
||||||
EEPROM.writeShort(1, spLight);
|
EEPROM.writeShort(1, spLight);
|
||||||
|
w = true;
|
||||||
}
|
}
|
||||||
if(dbLight == 65535) {
|
if(dbLight == 65535) {
|
||||||
dbLight = 1000;
|
dbLight = 1000;
|
||||||
EEPROM.writeShort(3, dbLight);
|
EEPROM.writeShort(3, dbLight);
|
||||||
|
w = true;
|
||||||
}
|
}
|
||||||
if(levelBlue == 255) {
|
if(levelBlue == 255) {
|
||||||
levelBlue = 100;
|
levelBlue = 100;
|
||||||
EEPROM.writeShort(5, levelBlue);
|
EEPROM.writeShort(5, levelBlue);
|
||||||
|
w = true;
|
||||||
}
|
}
|
||||||
if(levelWhite == 255) {
|
if(levelWhite == 255) {
|
||||||
levelWhite = 100;
|
levelWhite = 100;
|
||||||
EEPROM.writeByte(6, levelWhite);
|
EEPROM.writeByte(6, levelWhite);
|
||||||
|
w = true;
|
||||||
}
|
}
|
||||||
if(periodMotion == 255) {
|
if(periodMotion == 255) {
|
||||||
periodMotion = 60;
|
periodMotion = 60;
|
||||||
EEPROM.writeByte(0, periodMotion);
|
EEPROM.writeByte(0, periodMotion);
|
||||||
|
w = true;
|
||||||
}
|
}
|
||||||
EEPROM.commit();
|
if(w) EEPROM.commit();
|
||||||
|
|
||||||
Serial.print(F("PeriodMove:"));Serial.println(periodMotion);
|
Serial.print(F("PeriodMove:"));Serial.println(periodMotion);
|
||||||
Serial.print(F("SPLight:"));Serial.println(spLight);
|
Serial.print(F("SPLight:"));Serial.println(spLight);
|
||||||
Serial.print(F("DBLight:"));Serial.println(dbLight);
|
Serial.print(F("DBLight:"));Serial.println(dbLight);
|
||||||
@@ -178,14 +188,18 @@ void setup() {
|
|||||||
statLamp = 0;
|
statLamp = 0;
|
||||||
|
|
||||||
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
|
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
|
||||||
request->send(200, "text/plain", "Hi! I am ESP32.");
|
request->send(200, "text/plain", "Hi! I am ESP BIGROOM. Update - update, Webserial - webserial");
|
||||||
});
|
});
|
||||||
|
|
||||||
AsyncElegantOTA.setID("BigRoom");
|
AsyncElegantOTA.setID("BigRoom");
|
||||||
AsyncElegantOTA.begin(&server); // Start ElegantOTA
|
AsyncElegantOTA.begin(&server); // Start ElegantOTA
|
||||||
WebSerial.begin(&server);
|
WebSerial.begin(&server);
|
||||||
|
|
||||||
|
lampSerial.begin(19200, SWSERIAL_8N1, SSRX, SSTX);
|
||||||
|
myTransfer.begin(lampSerial);
|
||||||
|
|
||||||
connectToWifi();
|
connectToWifi();
|
||||||
|
Serial.println("Setup completed");
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
@@ -195,14 +209,30 @@ void loop() {
|
|||||||
static uint32_t levBlue = 0, levWhite = 0;
|
static uint32_t levBlue = 0, levWhite = 0;
|
||||||
static unsigned long levRunB = 0, levRunW = 0;
|
static unsigned long levRunB = 0, levRunW = 0;
|
||||||
char s[7];
|
char s[7];
|
||||||
|
|
||||||
//ArduinoOTA.handle();
|
//ArduinoOTA.handle();
|
||||||
bled.tick();
|
bled.tick();
|
||||||
|
|
||||||
|
if (!client.connected()) {
|
||||||
|
long now = millis();
|
||||||
|
if (now - lastReconnectAttempt > 5000) {
|
||||||
|
lastReconnectAttempt = now;
|
||||||
|
// Attempt to reconnect
|
||||||
|
if (reconnect()) {
|
||||||
|
lastReconnectAttempt = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Client connected
|
||||||
|
client.loop();
|
||||||
|
}
|
||||||
|
|
||||||
if(digitalRead(MOVE_SENS) > 0){
|
if(digitalRead(MOVE_SENS) > 0){
|
||||||
if(curDelay == -1){
|
if(curDelay == -1){
|
||||||
bled.start();
|
bled.start();
|
||||||
mqttClient.publish(TOPIC"move", 1, false, "1");
|
client.publish(TOPIC"move", "1");
|
||||||
//Serial.println("move");
|
//mqttClient.publish(TOPIC"move", 1, false, "1");
|
||||||
|
Serial.println("move");
|
||||||
|
WebSerial.println("move");
|
||||||
}
|
}
|
||||||
move = true;
|
move = true;
|
||||||
curDelay = periodMotion;
|
curDelay = periodMotion;
|
||||||
@@ -210,60 +240,86 @@ void loop() {
|
|||||||
uint16_t LightLev = analogRead(LIGHT_SENS);
|
uint16_t LightLev = analogRead(LIGHT_SENS);
|
||||||
samples.add(LightLev);
|
samples.add(LightLev);
|
||||||
LightLev = samples.getMedian();
|
LightLev = samples.getMedian();
|
||||||
if ((LightLev < spLight) && move && !bLamp){
|
//---------Night Mode
|
||||||
levBlue = levelBlue * 2.55;
|
if ((LightLev < spLight) && move && mode == 0){
|
||||||
ledcWrite(PWM_BLUE, levBlue);
|
mode = 1;
|
||||||
statLamp |= 0x01;
|
newTxData = true;
|
||||||
|
//Switch On Night Lamp
|
||||||
|
|
||||||
|
// levBlue = levelBlue * 2.55;
|
||||||
|
// ledcWrite(PWM_BLUE, levBlue);
|
||||||
|
// statLamp |= 0x01;
|
||||||
bLamp = true;
|
bLamp = true;
|
||||||
//Serial.println(F("BLamp On"));
|
|
||||||
//WebSerial.println(F("Blue Lamp On"));
|
Serial.println(F("BlueLamp On"));
|
||||||
|
WebSerial.println(F("Blue Lamp On"));
|
||||||
}
|
}
|
||||||
else if((LightLev > (spLight + dbLight)) || (move == 0)){
|
else if((LightLev > (spLight + dbLight)) || (move == 0)){
|
||||||
if((levRunB + 50 <= millis() && levBlue > 0)){
|
// if((levRunB + 50 <= millis() && levBlue > 0)){
|
||||||
levRunB = millis();
|
// levRunB = millis();
|
||||||
levBlue--;
|
// levBlue--;
|
||||||
|
// }
|
||||||
|
// ledcWrite(PWM_BLUE, levBlue);
|
||||||
|
// statLamp &= 0xFE;
|
||||||
|
|
||||||
|
// Switch Off Night Lamp
|
||||||
|
//WebSerial.println(F("Blue Lamp Off"));
|
||||||
|
if(mode == 1 && !forceNight){
|
||||||
|
Serial.println(F("BlueLamp Off"));
|
||||||
|
mode = 0;
|
||||||
|
newTxData = true;
|
||||||
|
bLamp = false;
|
||||||
}
|
}
|
||||||
ledcWrite(PWM_BLUE, levBlue);
|
|
||||||
statLamp &= 0xFE;
|
|
||||||
bLamp = false;
|
|
||||||
}
|
}
|
||||||
if(lightWhite && !wLamp){
|
//---------Night mode end
|
||||||
levWhite = levelWhite * 2.55;
|
// if(lightWhite && !wLamp){
|
||||||
ledcWrite(PWM_WHITE, levWhite);
|
// //Switch On Normal Lamp
|
||||||
statLamp |= 0x02;
|
|
||||||
wLamp = true;
|
// // levWhite = levelWhite * 2.55;
|
||||||
Serial.println(F("WLamp On"));
|
// // ledcWrite(PWM_WHITE, levWhite);
|
||||||
WebSerial.println(F("White Lamp On"));
|
// // statLamp |= 0x02;
|
||||||
}
|
// mode = 2;
|
||||||
else if (!lightWhite){
|
// //newTxData = true;
|
||||||
if((levRunW + 30 <= millis() && levWhite > 0)){
|
// wLamp = true;
|
||||||
//WebSerial.println(levWhite);
|
// Serial.println(F("WhiteLamp On"));
|
||||||
levRunW = millis();
|
// WebSerial.println(F("White Lamp On"));
|
||||||
levWhite--;
|
// }
|
||||||
}
|
// else if (!lightWhite && !bLamp && mode != 3 && mode != 0){
|
||||||
ledcWrite(PWM_WHITE, levWhite);
|
// // if((levRunW + 30 <= millis() && levWhite > 0)){
|
||||||
statLamp &= 0xFD;
|
// // //WebSerial.println(levWhite);
|
||||||
wLamp = false;
|
// // levRunW = millis();
|
||||||
}
|
// // levWhite--;
|
||||||
if(statLamp > 0) digitalWrite(POWER, HIGH);
|
// // }
|
||||||
else if((levBlue == 0) && (levWhite == 0)) digitalWrite(POWER, LOW);
|
// // ledcWrite(PWM_WHITE, levWhite);
|
||||||
|
// // statLamp &= 0xFD;
|
||||||
|
// //Switch Off Normal Lamp
|
||||||
|
// mode = 0;
|
||||||
|
// //newTxData = true;
|
||||||
|
// wLamp = false;
|
||||||
|
// }
|
||||||
|
transmitData();
|
||||||
|
// if(statLamp > 0) digitalWrite(POWER, HIGH);
|
||||||
|
// else if((levBlue == 0) && (levWhite == 0)) digitalWrite(POWER, LOW);
|
||||||
if((cRun + 1000) <= millis()){ //Once per second
|
if((cRun + 1000) <= millis()){ //Once per second
|
||||||
cRun = millis();
|
cRun = millis();
|
||||||
if(abs(LightLev - prevLLevel) > 100){
|
if(abs(LightLev - prevLLevel) > 100){
|
||||||
itoa(LightLev, s, 10);
|
itoa(LightLev, s, 10);
|
||||||
mqttClient.publish(TOPIC"lightlev", 1, false, s);
|
client.publish(TOPIC"lightlev", s);
|
||||||
|
// mqttClient.publish(TOPIC"lightlev", 1, false, s);
|
||||||
}
|
}
|
||||||
prevLLevel = LightLev;
|
prevLLevel = LightLev;
|
||||||
if(curDelay > -1){
|
if(curDelay > -1){
|
||||||
Serial.println("ADC: " + String(LightLev) + " MV:" + String(move) + " TMv:" + curDelay + " SL:" + String(statLamp) + " PS:" + digitalRead(POWER));
|
Serial.println("ADC: " + String(LightLev) + " MV:" + String(move) + " TMv:" + curDelay + " SL:" + String(statLamp) + " blamp:" + bLamp + " mode:" + mode);
|
||||||
Serial.println("SPL: " + String(spLight) + " DBL:" + String(dbLight)+ " BL:" + String(bLamp));
|
Serial.println("SPL: " + String(spLight) + " DBL:" + String(dbLight)+ " BL:" + String(bLamp));
|
||||||
}
|
}
|
||||||
sec++;
|
sec++;
|
||||||
if(curDelay > 0) curDelay--;
|
if(curDelay > 0) curDelay--;
|
||||||
if(curDelay == 0){
|
if(curDelay == 0){
|
||||||
mqttClient.publish(TOPIC"move", 1, false, "0");
|
client.publish(TOPIC"move", "0");
|
||||||
|
// mqttClient.publish(TOPIC"move", 1, false, "0");
|
||||||
bled.start();
|
bled.start();
|
||||||
Serial.println("no move");
|
Serial.println("no move");
|
||||||
|
WebSerial.println("no move");
|
||||||
move = false;
|
move = false;
|
||||||
curDelay = -1;
|
curDelay = -1;
|
||||||
}
|
}
|
||||||
@@ -272,8 +328,13 @@ void loop() {
|
|||||||
hum = htu.readHumidity();
|
hum = htu.readHumidity();
|
||||||
Serial.println("Temp: " + String(temp));
|
Serial.println("Temp: " + String(temp));
|
||||||
Serial.println("Hum: " + String(hum));
|
Serial.println("Hum: " + String(hum));
|
||||||
|
WebSerial.println("Temp: " + String(temp));
|
||||||
|
WebSerial.println("Hum: " + String(hum));
|
||||||
Serial.println(sec);
|
Serial.println(sec);
|
||||||
}
|
}
|
||||||
|
itoa(WiFi.RSSI(), s, 10);
|
||||||
|
client.publish(TOPIC"RSSI", s);
|
||||||
|
// mqttClient.publish(TOPIC"RSSI", 1, false, s);
|
||||||
if(sec == 59){
|
if(sec == 59){
|
||||||
//minuts++;
|
//minuts++;
|
||||||
sec = 0;
|
sec = 0;
|
||||||
@@ -287,7 +348,8 @@ void loop() {
|
|||||||
Serial.println(CO2);
|
Serial.println(CO2);
|
||||||
itoa(CO2, s, 10);
|
itoa(CO2, s, 10);
|
||||||
bled.start();
|
bled.start();
|
||||||
mqttClient.publish(TOPIC"co2", 1, false, s);
|
client.publish(TOPIC"co2", s);
|
||||||
|
// mqttClient.publish(TOPIC"co2", 1, false, s);
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
Serial.println("CO2err:" + String(myMHZ19.errorCode));
|
Serial.println("CO2err:" + String(myMHZ19.errorCode));
|
||||||
@@ -300,23 +362,25 @@ void loop() {
|
|||||||
dtostrf(temp, 6, 1, s);
|
dtostrf(temp, 6, 1, s);
|
||||||
trim(s);
|
trim(s);
|
||||||
bled.start();
|
bled.start();
|
||||||
mqttClient.publish(TOPIC"temp", 1, false, s);
|
client.publish(TOPIC"temp", s);
|
||||||
|
// mqttClient.publish(TOPIC"temp", 1, false, s);
|
||||||
}
|
}
|
||||||
if(hum != 0.0f){
|
if(hum != 0.0f){
|
||||||
dtostrf(hum, 6, 1, s);
|
dtostrf(hum, 6, 1, s);
|
||||||
trim(s);
|
trim(s);
|
||||||
mqttClient.publish(TOPIC"hum", 1, false, s);
|
client.publish(TOPIC"hum", s);
|
||||||
|
// mqttClient.publish(TOPIC"hum", 1, false, s);
|
||||||
//sendDataI(msgLight, LightLev);
|
//sendDataI(msgLight, LightLev);
|
||||||
//minuts = 0;
|
//minuts = 0;
|
||||||
}
|
}
|
||||||
bled.start();
|
bled.start();
|
||||||
dtostrf(cRun / 60000.0, 6, 1, s);
|
dtostrf(cRun / 60000.0, 6, 1, s);
|
||||||
trim(s);
|
trim(s);
|
||||||
mqttClient.publish(TOPIC"mins", 1, false, s);
|
client.publish(TOPIC"mins", s);
|
||||||
itoa(WiFi.RSSI(), s, 10);
|
// mqttClient.publish(TOPIC"mins", 1, false, s);
|
||||||
mqttClient.publish(TOPIC"RSSI", 1, false, s);
|
|
||||||
itoa(LightLev, s, 10);
|
itoa(LightLev, s, 10);
|
||||||
mqttClient.publish(TOPIC"lightlev", 1, false, s);
|
client.publish(TOPIC"lightlev", s);
|
||||||
|
// mqttClient.publish(TOPIC"lightlev", 1, false, s);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -327,10 +391,10 @@ void connectToWifi() {
|
|||||||
WiFi.begin(ssid, password);
|
WiFi.begin(ssid, password);
|
||||||
}
|
}
|
||||||
|
|
||||||
void connectToMqtt() {
|
// void connectToMqtt() {
|
||||||
Serial.println("Connecting to MQTT...");
|
// Serial.println("Connecting to MQTT...");
|
||||||
mqttClient.connect();
|
// mqttClient.connect();
|
||||||
}
|
// }
|
||||||
|
|
||||||
|
|
||||||
void WiFiEvent(WiFiEvent_t event) {
|
void WiFiEvent(WiFiEvent_t event) {
|
||||||
@@ -343,11 +407,11 @@ void WiFiEvent(WiFiEvent_t event) {
|
|||||||
digitalWrite(LED_R, HIGH);
|
digitalWrite(LED_R, HIGH);
|
||||||
server.begin();
|
server.begin();
|
||||||
Serial.println("HTTP server started");
|
Serial.println("HTTP server started");
|
||||||
connectToMqtt();
|
//connectToMqtt();
|
||||||
break;
|
break;
|
||||||
case SYSTEM_EVENT_STA_DISCONNECTED:
|
case SYSTEM_EVENT_STA_DISCONNECTED:
|
||||||
Serial.println("WiFi lost connection");
|
Serial.println("WiFi lost connection");
|
||||||
xTimerStop(mqttReconnectTimer, 0); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi
|
// xTimerStop(mqttReconnectTimer, 0); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi
|
||||||
xTimerStart(wifiReconnectTimer, 0);
|
xTimerStart(wifiReconnectTimer, 0);
|
||||||
server.end();
|
server.end();
|
||||||
digitalWrite(LED_R, LOW);
|
digitalWrite(LED_R, LOW);
|
||||||
@@ -358,138 +422,176 @@ void WiFiEvent(WiFiEvent_t event) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void onMqttConnect(bool sessionPresent) {
|
void onMqttConnect(bool sessionPresent) {
|
||||||
Serial.println("Connected to MQTT.");
|
// Serial.println("Connected to MQTT.");
|
||||||
Serial.print("Session present: ");
|
// Serial.print("Session present: ");
|
||||||
Serial.println(sessionPresent);
|
// Serial.println(sessionPresent);
|
||||||
//uint16_t packetIdSub =
|
// //uint16_t packetIdSub =
|
||||||
// Serial.print("Subscribing Lamp1, packetId: ");
|
// // Serial.print("Subscribing Lamp1, packetId: ");
|
||||||
// Serial.println(packetIdSub);
|
// // Serial.println(packetIdSub);
|
||||||
//packetIdSub =
|
// //packetIdSub =
|
||||||
|
|
||||||
char s[7];
|
// char s[20];
|
||||||
itoa(periodMotion, s, 10);
|
// itoa(periodMotion, s, 10);
|
||||||
mqttClient.publish(TOPIC"moveperiod", 1, false, s);
|
// mqttClient.publish(TOPIC"moveperiod", 1, false, s);
|
||||||
itoa(levelBlue, s, 10);
|
// itoa(levelBlue, s, 10);
|
||||||
mqttClient.publish(TOPIC"lampblevel", 1, false, s);
|
// mqttClient.publish(TOPIC"lampblevel", 1, false, s);
|
||||||
itoa(levelWhite, s, 10);
|
// itoa(levelWhite, s, 10);
|
||||||
mqttClient.publish(TOPIC"lampwlevel", 1, false, s);
|
// mqttClient.publish(TOPIC"lampwlevel", 1, false, s);
|
||||||
itoa(spLight, s, 10);
|
// itoa(spLight, s, 10);
|
||||||
mqttClient.publish(TOPIC"spLevel", 1, false, s);
|
// mqttClient.publish(TOPIC"spLevel", 1, false, s);
|
||||||
itoa(dbLight, s, 10);
|
// itoa(dbLight, s, 10);
|
||||||
mqttClient.publish(TOPIC"dbLevel", 1, false, s);
|
// mqttClient.publish(TOPIC"dbLevel", 1, false, s);
|
||||||
mqttClient.publish(TOPIC"reciever", 1, false, "0");
|
// mqttClient.publish(TOPIC"reciever", 1, false, "0");
|
||||||
mqttClient.publish(TOPIC"lampw", 1, false, lightWhite ? "1" : "0");
|
// mqttClient.publish(TOPIC"lampw", 1, false, lightWhite ? "1" : "0");
|
||||||
mqttClient.publish(TOPIC"lampw_set", 1, false, lightWhite ? "1" : "0");
|
// mqttClient.publish(TOPIC"lampw_set", 1, false, lightWhite ? "1" : "0");
|
||||||
|
|
||||||
mqttClient.subscribe(TOPIC"lampblevel", 1);
|
|
||||||
mqttClient.subscribe(TOPIC"lampwlevel", 1);
|
|
||||||
//mqttClient.subscribe(TOPIC"lampw", 1);
|
|
||||||
mqttClient.subscribe(TOPIC"lampw_set", 1);
|
|
||||||
mqttClient.subscribe(TOPIC"moveperiod", 1);
|
|
||||||
mqttClient.subscribe(TOPIC"spLevel", 1);
|
|
||||||
mqttClient.subscribe(TOPIC"dbLevel", 1);
|
|
||||||
mqttClient.subscribe(TOPIC"reciever", 1);
|
|
||||||
|
|
||||||
Serial.print(F("PeriodMove:"));Serial.println(periodMotion);
|
// mqttClient.subscribe(TOPIC"lampblevel", 1);
|
||||||
Serial.print(F("SPLight:"));Serial.println(spLight);
|
// mqttClient.subscribe(TOPIC"lampwlevel", 1);
|
||||||
Serial.print(F("DBLight:"));Serial.println(dbLight);
|
// //mqttClient.subscribe(TOPIC"lampw", 1);
|
||||||
Serial.print(F("BLevel:"));Serial.println(levelBlue);
|
// mqttClient.subscribe(TOPIC"lampw_set", 1);
|
||||||
Serial.print(F("WLevel:"));Serial.println(levelWhite);
|
// mqttClient.subscribe(TOPIC"moveperiod", 1);
|
||||||
//Serial.print("Subscribing Lamp2, packetId: ");
|
// mqttClient.subscribe(TOPIC"spLevel", 1);
|
||||||
//Serial.println(packetIdSub);
|
// mqttClient.subscribe(TOPIC"dbLevel", 1);
|
||||||
//Serial.println("Publishing at Lamp 1");
|
// mqttClient.subscribe(TOPIC"reciever", 1);
|
||||||
//mqttClient.publish("/home/kor/lamp2", 1, false, lStat2 ? "1" : "0");
|
|
||||||
//Serial.println("Publishing at Lamp 2");
|
// sprintf(s, "#%02X%02X%02X", txDataN.r, txDataN.g, txDataN.b);
|
||||||
digitalWrite(LED_G, LOW);
|
// mqttClient.publish(TOPIC"lamp/RGBN", 1, false, s);
|
||||||
|
// sprintf(s, "#%02X%02X%02X", txDataF.r, txDataF.g, txDataF.b);
|
||||||
|
// mqttClient.publish(TOPIC"lamp/RGBF", 1, false, s);
|
||||||
|
// itoa(mode, s, 10);
|
||||||
|
// mqttClient.publish(TOPIC"lamp/mode", 1, false, s);
|
||||||
|
|
||||||
|
// mqttClient.subscribe(TOPIC"lamp/RGBN", 1);
|
||||||
|
// mqttClient.subscribe(TOPIC"lamp/RGBF", 1);
|
||||||
|
// mqttClient.subscribe(TOPIC"lamp/mode", 1);
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
// //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) {
|
//void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
|
||||||
Serial.println("Disconnected from MQTT.");
|
// Serial.println("Disconnected from MQTT.");
|
||||||
digitalWrite(LED_R, HIGH);
|
// digitalWrite(LED_R, HIGH);
|
||||||
if (WiFi.isConnected()) {
|
// if (WiFi.isConnected()) {
|
||||||
xTimerStart(mqttReconnectTimer, 0);
|
// xTimerStart(mqttReconnectTimer, 0);
|
||||||
}
|
// }
|
||||||
}
|
//}
|
||||||
|
|
||||||
void onMqttSubscribe(uint16_t packetId, uint8_t qos) {
|
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) {
|
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) {
|
//void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
|
||||||
bool w = false;
|
// bool w = false;
|
||||||
char pl[20];
|
// char pl[20];
|
||||||
strncpy(pl, payload, len);
|
// strncpy(pl, payload, len);
|
||||||
pl[len] = 0;
|
// pl[len] = 0;
|
||||||
Serial.printf("New message - topic %s, payload %s\n", topic, pl);
|
// Serial.printf("New message - topic %s, payload %s\n", topic, pl);
|
||||||
WebSerial.print("New message - topic ");WebSerial.print(topic);WebSerial.print(", payload ");WebSerial.println(pl);
|
// WebSerial.print("New message - topic ");WebSerial.print(topic);WebSerial.print(", payload ");WebSerial.println(pl);
|
||||||
if(strcmp(topic, TOPIC"lampblevel") == 0){
|
// if(strcmp(topic, TOPIC"lampblevel") == 0){
|
||||||
levelBlue = atoi(pl);
|
// levelBlue = atoi(pl);
|
||||||
EEPROM.writeByte(5, levelBlue);
|
// EEPROM.writeByte(5, levelBlue);
|
||||||
Serial.printf("New LevelBlue: %d\n", levelBlue);
|
// Serial.printf("New LevelBlue: %d\n", levelBlue);
|
||||||
WebSerial.print("New LevelBlue: ");WebSerial.println(levelBlue);
|
// WebSerial.print("New LevelBlue: ");WebSerial.println(levelBlue);
|
||||||
w = true;
|
// w = true;
|
||||||
if(bLamp)
|
// if(bLamp)
|
||||||
ledcWrite(PWM_BLUE, (levelBlue) * 2.55);
|
// ledcWrite(PWM_BLUE, (levelBlue) * 2.55);
|
||||||
}
|
// }
|
||||||
if(strcmp(topic, TOPIC"lampwlevel") == 0){
|
// if(strcmp(topic, TOPIC"lampwlevel") == 0){
|
||||||
levelWhite = atoi(pl);
|
// levelWhite = atoi(pl);
|
||||||
Serial.printf("New LevelWite: %d\n", levelWhite);
|
// Serial.printf("New LevelWite: %d\n", levelWhite);
|
||||||
WebSerial.print("New LevelWhite: ");WebSerial.println(levelWhite);
|
// WebSerial.print("New LevelWhite: ");WebSerial.println(levelWhite);
|
||||||
EEPROM.writeByte(6, levelWhite);
|
// EEPROM.writeByte(6, levelWhite);
|
||||||
w = true;
|
// w = true;
|
||||||
if(lightWhite)
|
// if(lightWhite)
|
||||||
ledcWrite(PWM_WHITE, (levelWhite) * 2.55);
|
// ledcWrite(PWM_WHITE, (levelWhite) * 2.55);
|
||||||
}
|
// }
|
||||||
if(strcmp(topic, TOPIC"lampw_set") == 0){
|
// if(strcmp(topic, TOPIC"lampw_set") == 0){
|
||||||
lightWhite = atoi(pl);
|
// mode = 1;
|
||||||
Serial.printf("New LampWhite: %d\n", lightWhite);
|
// //lightWhite = atoi(pl);
|
||||||
WebSerial.print("New LampWhite: ");WebSerial.println(lightWhite);
|
// Serial.printf("New LampWhite: %d\n", mode);
|
||||||
mqttClient.publish(TOPIC"lampw", 1, false, lightWhite ? "1" : "0");
|
// WebSerial.print("New LampWhite: ");WebSerial.println(mode);
|
||||||
//EEPROM.put(0, levelBlue);
|
// mqttClient.publish(TOPIC"lampw", 1, false, lightWhite ? "1" : "0");
|
||||||
//w = true;
|
// //EEPROM.put(0, levelBlue);
|
||||||
}
|
// //w = true;
|
||||||
if(strcmp(topic, TOPIC"reciever") == 0){
|
// }
|
||||||
reciever = atoi(pl);
|
// if(strcmp(topic, TOPIC"reciever") == 0){
|
||||||
digitalWrite(RECIVER, reciever);
|
// reciever = atoi(pl);
|
||||||
Serial.printf("New Reciever: %d\n", lightWhite);
|
// digitalWrite(RECIVER, reciever);
|
||||||
WebSerial.print("New Reciever: ");WebSerial.println(lightWhite);
|
// Serial.printf("New Reciever: %d\n", lightWhite);
|
||||||
//mqttClient.publish(TOPIC"reciever", 1, false, reciever ? "1" : "0");
|
// WebSerial.print("New Reciever: ");WebSerial.println(lightWhite);
|
||||||
//EEPROM.put(0, levelBlue);
|
// //mqttClient.publish(TOPIC"reciever", 1, false, reciever ? "1" : "0");
|
||||||
//w = true;
|
// //EEPROM.put(0, levelBlue);
|
||||||
}
|
// //w = true;
|
||||||
if(strcmp(topic, TOPIC"moveperiod") == 0){
|
// }
|
||||||
periodMotion = atoi(pl);
|
// if(strcmp(topic, TOPIC"moveperiod") == 0){
|
||||||
Serial.printf("New MovePeriod: %d\n", periodMotion);
|
// periodMotion = atoi(pl);
|
||||||
WebSerial.print("New MovePeriod: ");WebSerial.println(periodMotion);
|
// Serial.printf("New MovePeriod: %d\n", periodMotion);
|
||||||
EEPROM.writeByte(0, periodMotion);
|
// WebSerial.print("New MovePeriod: ");WebSerial.println(periodMotion);
|
||||||
w = true;
|
// EEPROM.writeByte(0, periodMotion);
|
||||||
}
|
// w = true;
|
||||||
if(strcmp(topic, TOPIC"spLevel") == 0){
|
// }
|
||||||
spLight = atoi(pl);
|
// if(strcmp(topic, TOPIC"spLevel") == 0){
|
||||||
Serial.printf("New SPLevel: %d\n", spLight);
|
// spLight = atoi(pl);
|
||||||
WebSerial.print("New SPLevel: ");WebSerial.println(spLight);
|
// Serial.printf("New SPLevel: %d\n", spLight);
|
||||||
EEPROM.writeShort(1, spLight);
|
// WebSerial.print("New SPLevel: ");WebSerial.println(spLight);
|
||||||
w = true;
|
// EEPROM.writeShort(1, spLight);
|
||||||
}
|
// w = true;
|
||||||
if(strcmp(topic, TOPIC"dbLevel") == 0){
|
// }
|
||||||
dbLight = atoi(pl);
|
// if(strcmp(topic, TOPIC"dbLevel") == 0){
|
||||||
Serial.printf("New DBLevel: %d\n", dbLight);
|
// dbLight = atoi(pl);
|
||||||
WebSerial.print("New DBLevel: ");WebSerial.println(dbLight);
|
// Serial.printf("New DBLevel: %d\n", dbLight);
|
||||||
EEPROM.writeShort(3, dbLight);
|
// WebSerial.print("New DBLevel: ");WebSerial.println(dbLight);
|
||||||
w = true;
|
// EEPROM.writeShort(3, dbLight);
|
||||||
}
|
// w = true;
|
||||||
if(w) EEPROM.commit();
|
// }
|
||||||
}
|
|
||||||
|
// if(strcmp(topic, TOPIC"lamp/mode") == 0){
|
||||||
|
// mode = atoi(pl);
|
||||||
|
// if(mode == 1) forceNight = true;
|
||||||
|
// else forceNight = false;
|
||||||
|
// newTxData = true;
|
||||||
|
// Serial.printf("Mode %d\n", mode);
|
||||||
|
// //EEPROM.writeShort(16, mode);
|
||||||
|
// //w = true;
|
||||||
|
// }
|
||||||
|
// if(strcmp(topic, TOPIC"lamp/RGBF") == 0){
|
||||||
|
// String inRGB = pl;
|
||||||
|
// Serial.printf("Proc Light %s\n", inRGB.c_str());
|
||||||
|
// sscanf(inRGB.substring(1).c_str(), "%02x%02x%02x", &txDataF.r, &txDataF.g, &txDataF.b);
|
||||||
|
// Serial.printf("#%02X%02X%02X\n", txDataF.r, txDataF.g, txDataF.b);
|
||||||
|
// EEPROM.writeByte(8, txDataF.r);
|
||||||
|
// EEPROM.writeByte(9, txDataF.g);
|
||||||
|
// EEPROM.writeByte(10, txDataF.b);
|
||||||
|
// w = true;
|
||||||
|
// newTxData = true;
|
||||||
|
// }
|
||||||
|
// if(strcmp(topic, TOPIC"lamp/RGBN") == 0){
|
||||||
|
// String inRGB = pl;
|
||||||
|
// Serial.printf("Proc Night %s\n", inRGB.c_str());
|
||||||
|
// sscanf(inRGB.substring(1).c_str(), "%02x%02x%02x", &txDataN.r, &txDataN.g, &txDataN.b);
|
||||||
|
// Serial.printf("#%02X%02X%02X\n", txDataN.r, txDataN.g, txDataN.b);
|
||||||
|
// newTxData = true;
|
||||||
|
// EEPROM.writeByte(11, txDataN.r);
|
||||||
|
// EEPROM.writeByte(12, txDataN.g);
|
||||||
|
// EEPROM.writeByte(13, txDataN.b);
|
||||||
|
// w = true;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// if(w) EEPROM.commit();
|
||||||
|
//}
|
||||||
|
|
||||||
void onMqttPublish(uint16_t packetId) {
|
void onMqttPublish(uint16_t packetId) {
|
||||||
// Serial.println("Publish acknowledged.");
|
// Serial.println("Publish acknowledged.");
|
||||||
@@ -526,3 +628,197 @@ void trim(char *s)
|
|||||||
s[i+1]='\0';
|
s[i+1]='\0';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void transmitData() {
|
||||||
|
if (newTxData == true) {
|
||||||
|
switch (mode)
|
||||||
|
{
|
||||||
|
case 0:
|
||||||
|
txData.mode = 0;
|
||||||
|
Serial.println("Send OFF");
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
Serial.println("Send ON Night");
|
||||||
|
txData.mode = 1;
|
||||||
|
txData.r = txDataN.r;
|
||||||
|
txData.g = txDataN.g;
|
||||||
|
txData.b = txDataN.b;
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
Serial.println("Send ON Light");
|
||||||
|
txData.mode = 1;
|
||||||
|
txData.r = txDataF.r;
|
||||||
|
txData.g = txDataF.g;
|
||||||
|
txData.b = txDataF.b;
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
txData.mode = 2;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
Serial.printf("Send mode %d, R:0x%02X, G:0x%02X, B:0x%02X\n", txData.mode, txData.r, txData.g, txData.b);
|
||||||
|
myTransfer.sendDatum(txData);
|
||||||
|
Serial.println("Sent");
|
||||||
|
newTxData = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean reconnect() {
|
||||||
|
if (client.connect(HNAME)) {
|
||||||
|
Serial.println("Connected to MQTT.");
|
||||||
|
|
||||||
|
char s[20];
|
||||||
|
itoa(periodMotion, s, 10);
|
||||||
|
client.publish(TOPIC"moveperiod", s);
|
||||||
|
itoa(levelBlue, s, 10);
|
||||||
|
client.publish(TOPIC"lampblevel", s);
|
||||||
|
itoa(levelWhite, s, 10);
|
||||||
|
client.publish(TOPIC"lampwlevel", s);
|
||||||
|
itoa(spLight, s, 10);
|
||||||
|
client.publish(TOPIC"splevel", s);
|
||||||
|
itoa(dbLight, s, 10);
|
||||||
|
client.publish(TOPIC"splevel", s);
|
||||||
|
client.publish(TOPIC"dbLevel", s);
|
||||||
|
client.publish(TOPIC"reciever", s);
|
||||||
|
client.publish(TOPIC"lampw", lightWhite ? "1" : "0");
|
||||||
|
client.publish(TOPIC"lampw_set", lightWhite ? "1" : "0");
|
||||||
|
|
||||||
|
client.subscribe(TOPIC"lampblevel", 1);
|
||||||
|
client.subscribe(TOPIC"lampblevel", 1);
|
||||||
|
client.subscribe(TOPIC"lampwlevel", 1);
|
||||||
|
//mqttClient.subscribe(TOPIC"lampw", 1);
|
||||||
|
client.subscribe(TOPIC"lampw_set", 1);
|
||||||
|
client.subscribe(TOPIC"moveperiod", 1);
|
||||||
|
client.subscribe(TOPIC"spLevel", 1);
|
||||||
|
client.subscribe(TOPIC"dbLevel", 1);
|
||||||
|
client.subscribe(TOPIC"reciever", 1);
|
||||||
|
|
||||||
|
sprintf(s, "#%02X%02X%02X", txDataN.r, txDataN.g, txDataN.b);
|
||||||
|
client.publish(TOPIC"lamp/RGBN", s);
|
||||||
|
sprintf(s, "#%02X%02X%02X", txDataF.r, txDataF.g, txDataF.b);
|
||||||
|
client.publish(TOPIC"lamp/RGBF", s);
|
||||||
|
itoa(mode, s, 10);
|
||||||
|
client.publish(TOPIC"lamp/mode", s);
|
||||||
|
|
||||||
|
client.subscribe(TOPIC"lamp/RGBN", 1);
|
||||||
|
client.subscribe(TOPIC"lamp/RGBF", 1);
|
||||||
|
client.subscribe(TOPIC"lamp/mode", 1);
|
||||||
|
|
||||||
|
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);
|
||||||
|
digitalWrite(LED_G, LOW);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
digitalWrite(LED_G, HIGH);
|
||||||
|
return client.connected();
|
||||||
|
}
|
||||||
|
|
||||||
|
void callback(char* topic, byte* payload, unsigned int length) {
|
||||||
|
bool w = false;
|
||||||
|
char pl[20];
|
||||||
|
|
||||||
|
Serial.print("Message arrived [");
|
||||||
|
Serial.print(topic);
|
||||||
|
Serial.print("] ");
|
||||||
|
strncpy(pl, (char*)payload, length);
|
||||||
|
pl[length] = 0;
|
||||||
|
|
||||||
|
Serial.printf("New message - topic %s, payload %s\n", topic, pl);
|
||||||
|
WebSerial.print("New message - topic ");WebSerial.print(topic);WebSerial.print(", payload ");WebSerial.println(pl);
|
||||||
|
if(strcmp(topic, TOPIC"lampblevel") == 0){
|
||||||
|
levelBlue = atoi(pl);
|
||||||
|
EEPROM.writeByte(5, levelBlue);
|
||||||
|
Serial.printf("New LevelBlue: %d\n", levelBlue);
|
||||||
|
WebSerial.print("New LevelBlue: ");WebSerial.println(levelBlue);
|
||||||
|
w = true;
|
||||||
|
if(bLamp)
|
||||||
|
ledcWrite(PWM_BLUE, (levelBlue) * 2.55);
|
||||||
|
}
|
||||||
|
if(strcmp(topic, TOPIC"lampwlevel") == 0){
|
||||||
|
levelWhite = atoi(pl);
|
||||||
|
Serial.printf("New LevelWite: %d\n", levelWhite);
|
||||||
|
WebSerial.print("New LevelWhite: ");WebSerial.println(levelWhite);
|
||||||
|
EEPROM.writeByte(6, levelWhite);
|
||||||
|
w = true;
|
||||||
|
if(lightWhite)
|
||||||
|
ledcWrite(PWM_WHITE, (levelWhite) * 2.55);
|
||||||
|
}
|
||||||
|
if(strcmp(topic, TOPIC"lampw_set") == 0){
|
||||||
|
mode = 1;
|
||||||
|
//lightWhite = atoi(pl);
|
||||||
|
Serial.printf("New LampWhite: %d\n", mode);
|
||||||
|
WebSerial.print("New LampWhite: ");WebSerial.println(mode);
|
||||||
|
//mqttClient.publish(TOPIC"lampw", 1, false, lightWhite ? "1" : "0");
|
||||||
|
//EEPROM.put(0, levelBlue);
|
||||||
|
//w = true;
|
||||||
|
}
|
||||||
|
if(strcmp(topic, TOPIC"reciever") == 0){
|
||||||
|
reciever = atoi(pl);
|
||||||
|
digitalWrite(RECIVER, reciever);
|
||||||
|
Serial.printf("New Reciever: %d\n", lightWhite);
|
||||||
|
WebSerial.print("New Reciever: ");WebSerial.println(lightWhite);
|
||||||
|
//mqttClient.publish(TOPIC"reciever", 1, false, reciever ? "1" : "0");
|
||||||
|
//EEPROM.put(0, levelBlue);
|
||||||
|
//w = true;
|
||||||
|
}
|
||||||
|
if(strcmp(topic, TOPIC"moveperiod") == 0){
|
||||||
|
periodMotion = atoi(pl);
|
||||||
|
Serial.printf("New MovePeriod: %d\n", periodMotion);
|
||||||
|
WebSerial.print("New MovePeriod: ");WebSerial.println(periodMotion);
|
||||||
|
EEPROM.writeByte(0, periodMotion);
|
||||||
|
w = true;
|
||||||
|
}
|
||||||
|
if(strcmp(topic, TOPIC"spLevel") == 0){
|
||||||
|
spLight = atoi(pl);
|
||||||
|
Serial.printf("New SPLevel: %d\n", spLight);
|
||||||
|
WebSerial.print("New SPLevel: ");WebSerial.println(spLight);
|
||||||
|
EEPROM.writeShort(1, spLight);
|
||||||
|
w = true;
|
||||||
|
}
|
||||||
|
if(strcmp(topic, TOPIC"dbLevel") == 0){
|
||||||
|
dbLight = atoi(pl);
|
||||||
|
Serial.printf("New DBLevel: %d\n", dbLight);
|
||||||
|
WebSerial.print("New DBLevel: ");WebSerial.println(dbLight);
|
||||||
|
EEPROM.writeShort(3, dbLight);
|
||||||
|
w = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(strcmp(topic, TOPIC"lamp/mode") == 0){
|
||||||
|
mode = atoi(pl);
|
||||||
|
if(mode == 1) forceNight = true;
|
||||||
|
else forceNight = false;
|
||||||
|
newTxData = true;
|
||||||
|
Serial.printf("Mode %d\n", mode);
|
||||||
|
//EEPROM.writeShort(16, mode);
|
||||||
|
//w = true;
|
||||||
|
}
|
||||||
|
if(strcmp(topic, TOPIC"lamp/RGBF") == 0){
|
||||||
|
String inRGB = pl;
|
||||||
|
Serial.printf("Proc Light %s\n", inRGB.c_str());
|
||||||
|
sscanf(inRGB.substring(1).c_str(), "%02x%02x%02x", &txDataF.r, &txDataF.g, &txDataF.b);
|
||||||
|
Serial.printf("#%02X%02X%02X\n", txDataF.r, txDataF.g, txDataF.b);
|
||||||
|
EEPROM.writeByte(8, txDataF.r);
|
||||||
|
EEPROM.writeByte(9, txDataF.g);
|
||||||
|
EEPROM.writeByte(10, txDataF.b);
|
||||||
|
w = true;
|
||||||
|
newTxData = true;
|
||||||
|
}
|
||||||
|
if(strcmp(topic, TOPIC"lamp/RGBN") == 0){
|
||||||
|
String inRGB = pl;
|
||||||
|
Serial.printf("Proc Night %s\n", inRGB.c_str());
|
||||||
|
sscanf(inRGB.substring(1).c_str(), "%02x%02x%02x", &txDataN.r, &txDataN.g, &txDataN.b);
|
||||||
|
Serial.printf("#%02X%02X%02X\n", txDataN.r, txDataN.g, txDataN.b);
|
||||||
|
newTxData = true;
|
||||||
|
EEPROM.writeByte(11, txDataN.r);
|
||||||
|
EEPROM.writeByte(12, txDataN.g);
|
||||||
|
EEPROM.writeByte(13, txDataN.b);
|
||||||
|
w = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(w) EEPROM.commit();
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user