Change KorMYS LED Inv

This commit is contained in:
2021-06-02 16:08:06 +03:00
parent df47c18ed3
commit a7641761fd
2 changed files with 28 additions and 22 deletions

View File

@@ -12,7 +12,7 @@
// Set blinking period // Set blinking period
#define MY_DEFAULT_LED_BLINK_PERIOD 300 #define MY_DEFAULT_LED_BLINK_PERIOD 300
// Inverses the behavior of leds // Inverses the behavior of leds
#define MY_WITH_LEDS_BLINKING_INVERSE //#define MY_WITH_LEDS_BLINKING_INVERSE
// Flash leds on rx/tx/err // Flash leds on rx/tx/err
#define MY_DEFAULT_ERR_LED_PIN A3 // Error led pin #define MY_DEFAULT_ERR_LED_PIN A3 // Error led pin
#define MY_DEFAULT_TX_LED_PIN A4 // the PCB, on board LED #define MY_DEFAULT_TX_LED_PIN A4 // the PCB, on board LED
@@ -30,6 +30,7 @@ bool lamp;
#define RX_PIN 2 #define RX_PIN 2
#define TX_PIN 3 #define TX_PIN 3
#define LAMP_OUT 5 #define LAMP_OUT 5
#define PIN_MOVE 7
SoftwareSerial sSerial(RX_PIN, TX_PIN); SoftwareSerial sSerial(RX_PIN, TX_PIN);
SDS011 sds; SDS011 sds;
@@ -58,7 +59,7 @@ void setup()
//Serial.println(sds.setCustomWorkingPeriod(3).toString()); // sensor sends data every 3 minutes //Serial.println(sds.setCustomWorkingPeriod(3).toString()); // sensor sends data every 3 minutes
//sds.setActiveReportingMode(); //sds.setActiveReportingMode();
//sds.setCustomWorkingPeriod(1); //sds.setCustomWorkingPeriod(1);
pinMode(A1, INPUT_PULLUP); pinMode(PIN_MOVE, INPUT_PULLUP);
pinMode(LAMP_OUT, OUTPUT); pinMode(LAMP_OUT, OUTPUT);
//digitalWrite(7, HIGH); //digitalWrite(7, HIGH);
//saveState(6, 2); //saveState(6, 2);
@@ -98,9 +99,8 @@ void loop()
{ {
float p25, p10; float p25, p10;
uint32_t curTime; uint32_t curTime;
//move = digitalRead(A1);
if(digitalRead(A1) > 0){ if(digitalRead(PIN_MOVE) > 0){
if(curDelay == -1) sendData(msgMove, true); if(curDelay == -1) sendData(msgMove, true);
move = true; move = true;
curDelay = timeDelay; curDelay = timeDelay;
@@ -127,8 +127,8 @@ void loop()
} }
} }
else if((adc > (minLight + minLightDB)) || (move == 0)){ else if((adc > (minLight + minLightDB)) || (move == 0)){
//analogWrite(LAMP_OUT, 0); analogWrite(LAMP_OUT, 0);
curDelay = 2; //curDelay = 2;
if(lamp == true){ if(lamp == true){
Serial.println("Lamp OFF"); Serial.println("Lamp OFF");
Serial.print("ADC: ");Serial.print(adc); Serial.print("ADC: ");Serial.print(adc);
@@ -169,14 +169,14 @@ void loop()
send(msgMillis.set(ms)); send(msgMillis.set(ms));
} }
} }
if(lamp && curTime > millis()){ /*if(lamp && curTime > millis()){
uint8_t ll = int(LightInt - LightInt * (1 - curTime - millis() / float(fadeTime * 1000))); uint8_t ll = int(LightInt - LightInt * (1 - curTime - millis() / float(fadeTime * 1000)));
analogWrite(LAMP_OUT, ll); analogWrite(LAMP_OUT, ll);
if(curTime - millis() < 10){ if(curTime - millis() < 10){
lamp = false; lamp = false;
analogWrite(LAMP_OUT, 0); analogWrite(LAMP_OUT, 0);
} }
} }*/
} }
void receive(const MyMessage &message) void receive(const MyMessage &message)

View File

@@ -50,6 +50,7 @@ void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties
void oneClick() void oneClick()
{ {
switchLight(R_LED, !digitalRead(R_LED), true); switchLight(R_LED, !digitalRead(R_LED), true);
//Serial.println("Press");
//lStat1 = !lStat1; //lStat1 = !lStat1;
} }
@@ -67,18 +68,18 @@ void longPress()
} }
void setup() { void setup() {
// Serial.begin(9600); //Serial.begin(115200);
// Serial.println("Booting"); // "Загрузка" //Serial.println("Booting"); // "Загрузка"
WiFi.mode(WIFI_STA); WiFi.mode(WIFI_STA);
WiFi.hostname("SW-Koridor"); WiFi.hostname("SW-Koridor");
ArduinoOTA.onStart([]() { ArduinoOTA.onStart([]() {
// Serial.println("Start"); // "Начало OTA-апдейта" //Serial.println("Start"); // "Начало OTA-апдейта"
}); });
ArduinoOTA.onEnd([]() { ArduinoOTA.onEnd([]() {
// Serial.println("\nEnd"); // "Завершение OTA-апдейта" //Serial.println("\nEnd"); // "Завершение OTA-апдейта"
}); });
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
// Serial.printf("Progress: %u%%\r", (progress / (total / 100))); //Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
}); });
ArduinoOTA.onError([](ota_error_t error) { ArduinoOTA.onError([](ota_error_t error) {
// Serial.printf("Error[%u]: ", error); // Serial.printf("Error[%u]: ", error);
@@ -93,19 +94,23 @@ void setup() {
// else if (error == OTA_END_ERROR) Serial.println("End Failed"); // else if (error == OTA_END_ERROR) Serial.println("End Failed");
// // "Ошибка при завершении OTA-апдейта" // // "Ошибка при завершении OTA-апдейта"
}); });
// Serial.println("OTA Begin");
ArduinoOTA.begin(); ArduinoOTA.begin();
// Serial.println("OTA Done");
//Serial.end();
pinMode(B_LED, FUNCTION_3); pinMode(B_LED, FUNCTION_3);
pinMode(B_LED, OUTPUT); pinMode(B_LED, OUTPUT);
digitalWrite(B_LED, HIGH); digitalWrite(B_LED, HIGH);
pinMode(R_LED, OUTPUT); pinMode(R_LED, OUTPUT);
pinMode(BUTT, INPUT_PULLUP); pinMode(BUTT, INPUT_PULLUP);
// Serial.println("EEPROM Begin");
EEPROM.begin(16); EEPROM.begin(16);
uint8_t lstat = 0; uint8_t lstat = 0;
EEPROM.get(0, lstat); EEPROM.get(0, lstat);
//oldLStat1 = lStat1; //oldLStat1 = lStat1;
// Serial.print("Lamp1: "); // Serial.print("Lamp1: ");
// Serial.println(lStat1); //Serial.println(lStat1);
digitalWrite(R_LED, lstat); digitalWrite(R_LED, lstat);
//oldLStat2 = lStat2; //oldLStat2 = lStat2;
@@ -127,6 +132,7 @@ void setup() {
//attachInterrupt(digitalPinToInterrupt(BUTT), sw_func, RISING); //attachInterrupt(digitalPinToInterrupt(BUTT), sw_func, RISING);
// Serial.println("WiFi Connect");
connectToWifi(); connectToWifi();
cRun = millis(); cRun = millis();
@@ -164,24 +170,24 @@ bool switchLight(uint8_t nLamp, int state, bool pub)
} }
void connectToWifi() { void connectToWifi() {
//Serial.println("Connecting to Wi-Fi..."); // Serial.println("Connecting to Wi-Fi...");
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 onWifiConnect(const WiFiEventStationModeGotIP& event) { void onWifiConnect(const WiFiEventStationModeGotIP& event) {
//Serial.println("Connected to Wi-Fi."); // Serial.println("Connected to Wi-Fi.");
//Serial.print("IP: "); // Serial.print("IP: ");
//Serial.println(WiFi.localIP()); // Serial.println(WiFi.localIP());
connectToMqtt(); connectToMqtt();
} }
void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) { void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) {
//Serial.println("Disconnected from Wi-Fi."); // Serial.println("Disconnected from Wi-Fi.");
mqttReconnectTimer.detach(); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi mqttReconnectTimer.detach(); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi
wifiReconnectTimer.once(2, connectToWifi); wifiReconnectTimer.once(2, connectToWifi);
} }
@@ -194,7 +200,7 @@ void onMqttConnect(bool sessionPresent) {
} }
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) { void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
//Serial.println("Disconnected from MQTT."); // Serial.println("Disconnected from MQTT.");
if (WiFi.isConnected()) { if (WiFi.isConnected()) {
mqttReconnectTimer.once(2, connectToMqtt); mqttReconnectTimer.once(2, connectToMqtt);