From 4acce987de7df6b7cec5866867af48e7840c54bb Mon Sep 17 00:00:00 2001 From: Emanuele Trabattoni Date: Thu, 26 Jun 2025 12:44:20 +0200 Subject: [PATCH] Removed example files (can be recovered later) Now it compiles with test main code --- lib/ETH/WS_ETH.cpp | 18 +- lib/GPIO/TCA9554PWR_Driver.cpp | 2 +- lib/GPIO/WS_DIN.cpp | 151 ---------- lib/GPIO/WS_DIN.h | 18 -- lib/GPIO/WS_GPIO.cpp | 166 ----------- lib/GPIO/WS_GPIO.h | 44 --- lib/GPIO/WS_Relay.cpp | 273 ------------------- lib/GPIO/WS_Relay.h | 56 ---- lib/RS485/{WS_RS485.cpp => RS485_Driver.cpp} | 19 +- lib/RS485/{WS_RS485.h => RS485_Driver.h} | 8 - platformio.ini | 4 + src/MAIN_WIFI_MQTT.cpp | 37 --- src/WS_Bluetooth.cpp | 152 ----------- src/WS_Bluetooth.h | 24 -- src/WS_Information.h | 18 -- src/WS_MQTT.cpp | 248 ----------------- src/WS_MQTT.h | 25 -- src/WS_SD.cpp | 113 -------- src/WS_SD.h | 18 -- src/WS_Serial.cpp | 8 - src/WS_Serial.h | 8 - src/main.cpp | 38 +++ 22 files changed, 63 insertions(+), 1385 deletions(-) delete mode 100644 lib/GPIO/WS_DIN.cpp delete mode 100644 lib/GPIO/WS_DIN.h delete mode 100644 lib/GPIO/WS_GPIO.cpp delete mode 100644 lib/GPIO/WS_GPIO.h delete mode 100644 lib/GPIO/WS_Relay.cpp delete mode 100644 lib/GPIO/WS_Relay.h rename lib/RS485/{WS_RS485.cpp => RS485_Driver.cpp} (96%) rename lib/RS485/{WS_RS485.h => RS485_Driver.h} (90%) delete mode 100644 src/MAIN_WIFI_MQTT.cpp delete mode 100644 src/WS_Bluetooth.cpp delete mode 100644 src/WS_Bluetooth.h delete mode 100644 src/WS_Information.h delete mode 100644 src/WS_MQTT.cpp delete mode 100644 src/WS_MQTT.h delete mode 100644 src/WS_SD.cpp delete mode 100644 src/WS_SD.h delete mode 100644 src/WS_Serial.cpp delete mode 100644 src/WS_Serial.h create mode 100644 src/main.cpp diff --git a/lib/ETH/WS_ETH.cpp b/lib/ETH/WS_ETH.cpp index a2a1c11..794ce5f 100644 --- a/lib/ETH/WS_ETH.cpp +++ b/lib/ETH/WS_ETH.cpp @@ -84,7 +84,7 @@ void EthernetTask(void *parameter) { while(1){ if (eth_connected && !eth_connected_Old) { eth_connected_Old = eth_connected; - RGB_Open_Time(0, 60, 0,1000, 0); + //RGB_Open_Time(0, 60, 0,1000, 0); printf("Network port connected!\r\n"); Acquisition_time(); } @@ -108,13 +108,13 @@ void Acquisition_time(void) { // Get the netwo printf("ETH - Online clock error!!!\r\n"); } struct tm *localTime = localtime(¤tTime); - static datetime_t PCF85063_Time = {0}; - PCF85063_Time.year = localTime->tm_year + 1900; - PCF85063_Time.month = localTime->tm_mon + 1; - PCF85063_Time.day = localTime->tm_mday; - PCF85063_Time.dotw = localTime->tm_wday; - PCF85063_Time.hour = localTime->tm_hour; - PCF85063_Time.minute = localTime->tm_min; - PCF85063_Time.second = localTime->tm_sec; + //static datetime_t PCF85063_Time = {0}; + //PCF85063_Time.year = localTime->tm_year + 1900; + //PCF85063_Time.month = localTime->tm_mon + 1; + //PCF85063_Time.day = localTime->tm_mday; + //PCF85063_Time.dotw = localTime->tm_wday; + //PCF85063_Time.hour = localTime->tm_hour; + //PCF85063_Time.minute = localTime->tm_min; + //PCF85063_Time.second = localTime->tm_sec; //PCF85063_Set_All(PCF85063_Time); } diff --git a/lib/GPIO/TCA9554PWR_Driver.cpp b/lib/GPIO/TCA9554PWR_Driver.cpp index e9ade32..1ec881a 100644 --- a/lib/GPIO/TCA9554PWR_Driver.cpp +++ b/lib/GPIO/TCA9554PWR_Driver.cpp @@ -1,4 +1,4 @@ -#include "WS_TCA9554PWR.h" +#include "TCA9554PWR_Driver.h" namespace drivers { diff --git a/lib/GPIO/WS_DIN.cpp b/lib/GPIO/WS_DIN.cpp deleted file mode 100644 index 00f3799..0000000 --- a/lib/GPIO/WS_DIN.cpp +++ /dev/null @@ -1,151 +0,0 @@ -#include "WS_DIN.h" - -bool DIN_Flag[8] = {0}; // DIN current status flag -uint8_t DIN_Data = 0; -bool Relay_Immediate_Enable = Relay_Immediate_Default; - -bool DIN_Read_CH1(void){ - DIN_Flag[0] = digitalRead(DIN_PIN_CH1); - if(DIN_Flag[0]){ - DIN_Data |= (1 << 0); - return 1; - } - else{ - DIN_Data &= (~(1 << 0)); - return 0; - } -} -bool DIN_Read_CH2(void){ - DIN_Flag[1] = digitalRead(DIN_PIN_CH2); - if(DIN_Flag[1]){ - DIN_Data |= (1 << 1); - return 1; - } - else{ - DIN_Data &= (~(1 << 1)); - return 0; - } -} -bool DIN_Read_CH3(void){ - DIN_Flag[2] = digitalRead(DIN_PIN_CH3); - if(DIN_Flag[2]){ - DIN_Data |= (1 << 2); - return 1; - } - else{ - DIN_Data &= (~(1 << 2)); - return 0; - } -} -bool DIN_Read_CH4(void){ - DIN_Flag[3] = digitalRead(DIN_PIN_CH4); - if(DIN_Flag[3]){ - DIN_Data |= (1 << 3); - return 1; - } - else{ - DIN_Data &= (~(1 << 3)); - return 0; - } -} -bool DIN_Read_CH5(void){ - DIN_Flag[4] = digitalRead(DIN_PIN_CH5); - if(DIN_Flag[4]){ - DIN_Data |= (1 << 4); - return 1; - } - else{ - DIN_Data &= (~(1 << 4)); - return 0; - } -} -bool DIN_Read_CH6(void){ - DIN_Flag[5] = digitalRead(DIN_PIN_CH6); - if(DIN_Flag[5]){ - DIN_Data |= (1 << 5); - return 1; - } - else{ - DIN_Data &= (~(1 << 5)); - return 0; - } -} -bool DIN_Read_CH7(void){ - DIN_Flag[6] = digitalRead(DIN_PIN_CH7); - if(DIN_Flag[6]){ - DIN_Data |= (1 << 6); - return 1; - } - else{ - DIN_Data &= (~(1 << 6)); - return 0; - } -} -bool DIN_Read_CH8(void){ - DIN_Flag[7] = digitalRead(DIN_PIN_CH8); - if(DIN_Flag[7]){ - DIN_Data |= (1 << 7); - return 1; - } - else{ - DIN_Data &= (~(1 << 7)); - return 0; - } -} -uint8_t DIN_Read_CHxs(){ - DIN_Read_CH1(); - DIN_Read_CH2(); - DIN_Read_CH3(); - DIN_Read_CH4(); - DIN_Read_CH5(); - DIN_Read_CH6(); - DIN_Read_CH7(); - DIN_Read_CH8(); - return DIN_Data; -} - -static uint8_t DIN_Data_Old = 0; -void DINTask(void *parameter) { - while(1){ - if(Relay_Immediate_Enable){ - DIN_Read_CHxs(); - if(DIN_Data_Old != DIN_Data){ - if(DIN_Inverse_Enable) - Relay_Immediate_CHxs(~DIN_Data , DIN_Mode); - else - Relay_Immediate_CHxs(DIN_Data , DIN_Mode); - DIN_Data_Old = DIN_Data; - } - } - vTaskDelay(pdMS_TO_TICKS(20)); - } - vTaskDelete(NULL); -} - -void DIN_Init(void) -{ - pinMode(DIN_PIN_CH1, INPUT_PULLUP); - pinMode(DIN_PIN_CH2, INPUT_PULLUP); - pinMode(DIN_PIN_CH3, INPUT_PULLUP); - pinMode(DIN_PIN_CH4, INPUT_PULLUP); - pinMode(DIN_PIN_CH5, INPUT_PULLUP); - pinMode(DIN_PIN_CH6, INPUT_PULLUP); - pinMode(DIN_PIN_CH7, INPUT_PULLUP); - pinMode(DIN_PIN_CH8, INPUT_PULLUP); - - DIN_Read_CHxs(); - if(DIN_Inverse_Enable) - DIN_Data_Old = 0xFF; - else - DIN_Data_Old = 0x00; - - xTaskCreatePinnedToCore( - DINTask, - "DINTask", - 4096, - NULL, - 4, - NULL, - 0 - ); -} diff --git a/lib/GPIO/WS_DIN.h b/lib/GPIO/WS_DIN.h deleted file mode 100644 index 13180a6..0000000 --- a/lib/GPIO/WS_DIN.h +++ /dev/null @@ -1,18 +0,0 @@ -#pragma once - -#include "WS_GPIO.h" -#include "WS_Relay.h" -/************************************************************* I/O *************************************************************/ -#define DIN_PIN_CH1 4 // DIN CH1 GPIO -#define DIN_PIN_CH2 5 // DIN CH2 GPIO -#define DIN_PIN_CH3 6 // DIN CH3 GPIO -#define DIN_PIN_CH4 7 // DIN CH4 GPIO -#define DIN_PIN_CH5 8 // DIN CH5 GPIO -#define DIN_PIN_CH6 9 // DIN CH6 GPIO -#define DIN_PIN_CH7 10 // DIN CH7 GPIO -#define DIN_PIN_CH8 11 // DIN CH8 GPIO - -#define Relay_Immediate_Default 1 // Enable the input control relay -#define DIN_Inverse_Enable 1 // Input is reversed from control - -void DIN_Init(void); diff --git a/lib/GPIO/WS_GPIO.cpp b/lib/GPIO/WS_GPIO.cpp deleted file mode 100644 index 5cb7b41..0000000 --- a/lib/GPIO/WS_GPIO.cpp +++ /dev/null @@ -1,166 +0,0 @@ -#include "WS_GPIO.h" - -/************************************************************* I/O Init *************************************************************/ -void GPIO_Init() { - pinMode(GPIO_PIN_RGB, OUTPUT); // Initialize the control GPIO of RGB - pinMode(GPIO_PIN_Buzzer, OUTPUT); // Initialize the control GPIO of Buzzer - - // TODO: Re enable this - //ledcAttach(GPIO_PIN_Buzzer, Frequency, Resolution); - Set_Dutyfactor(0); //0~100 - - xTaskCreatePinnedToCore( - RGBTask, - "RelayFailTask", - 4096, - NULL, - 2, - NULL, - 0 - ); - xTaskCreatePinnedToCore( - BuzzerTask, - "RelayFailTask", - 4096, - NULL, - 2, - NULL, - 0 - ); -} - -/************************************************************* RGB *************************************************************/ -void RGB_Light(uint8_t red_val, uint8_t green_val, uint8_t blue_val) { - rgbLedWrite(GPIO_PIN_RGB, green_val, red_val, blue_val); // RGB color adjustment -} -RGB_Indicate RGB_indicate[RGB_Indicate_Number]; -static uint8_t RGB_indicate_Num = 0; -void RGB_Open_Time(uint8_t red_val, uint8_t green_val, uint8_t blue_val, uint16_t Time, uint16_t flicker_time) { - - if(RGB_indicate_Num + 1 >= RGB_Indicate_Number) - { - printf("Note : The RGB indicates that the cache is full and has been ignored\r\n"); - } - else{ - RGB_indicate[RGB_indicate_Num].Red = red_val; - RGB_indicate[RGB_indicate_Num].Green = green_val; - RGB_indicate[RGB_indicate_Num].Blue = blue_val; - RGB_indicate[RGB_indicate_Num].RGB_Time = Time; - if(flicker_time<51) - flicker_time = 0; // If the blinking interval is less than 50ms, the blinking is ignored - RGB_indicate[RGB_indicate_Num].RGB_Flicker = flicker_time; - RGB_indicate_Num ++; - } -} -void RGBTask(void *parameter) { - bool RGB_Flag = 0; - while(1){ - if(RGB_indicate[0].RGB_Time) - { - RGB_Flag = 1; - RGB_Light(RGB_indicate[0].Red, RGB_indicate[0].Green, RGB_indicate[0].Blue); - if(RGB_indicate[0].RGB_Flicker){ - vTaskDelay(pdMS_TO_TICKS(RGB_indicate[0].RGB_Flicker)); - RGB_Light(0, 0, 0); - vTaskDelay(pdMS_TO_TICKS(RGB_indicate[0].RGB_Flicker)); - } - if(RGB_indicate[0].RGB_Time > (RGB_indicate[0].RGB_Flicker * 2 +50)) - RGB_indicate[0].RGB_Time = RGB_indicate[0].RGB_Time -(RGB_indicate[0].RGB_Flicker * 2 +50); - else - RGB_indicate[0].RGB_Time = 0; - } - else if(RGB_Flag && !RGB_indicate[0].RGB_Time){ - RGB_Light(0, 0, 0); - RGB_Flag = 0; - RGB_indicate[0].Red = 0; - RGB_indicate[0].Green = 0; - RGB_indicate[0].Blue = 0; - RGB_indicate[0].RGB_Time = 0; - RGB_indicate[0].RGB_Flicker = 0; - if(RGB_indicate_Num > 0){ - for (int i = 1; i < RGB_Indicate_Number; i++) { - RGB_indicate[i-1] = RGB_indicate[i]; - } - RGB_indicate[RGB_Indicate_Number -1].Red = 0; - RGB_indicate[RGB_Indicate_Number -1].Green = 0; - RGB_indicate[RGB_Indicate_Number -1].Blue = 0; - RGB_indicate[RGB_Indicate_Number -1].RGB_Time = 0; - RGB_indicate[RGB_Indicate_Number -1].RGB_Flicker = 0; - RGB_indicate_Num --; - vTaskDelay(pdMS_TO_TICKS(RGB_Indicating_interval)); - } - } - vTaskDelay(pdMS_TO_TICKS(50)); - } - vTaskDelete(NULL); -} - - -/************************************************************* Buzzer *************************************************************/ -void Set_Dutyfactor(uint16_t dutyfactor) -{ - if(dutyfactor > Dutyfactor_MAX || dutyfactor < 0) - printf("Set Backlight parameters in the range of 0 to %d \r\n",Dutyfactor_MAX); - else{ - ledcWrite(GPIO_PIN_Buzzer, dutyfactor); - } -} -void Buzzer_Open(void) -{ - Set_Dutyfactor(Dutyfactor); -} -void Buzzer_Closs(void) -{ - Set_Dutyfactor(0); -} -Buzzer_Indicate Buzzer_indicate[Buzzer_Indicate_Number]; -static uint8_t Buzzer_indicate_Num = 0; -void Buzzer_Open_Time(uint16_t Time, uint16_t flicker_time) -{ - if(Buzzer_indicate_Num + 1 >= Buzzer_Indicate_Number) - { - printf("Note : The buzzer indicates that the cache is full and has been ignored\r\n"); - } - else{ - Buzzer_indicate[Buzzer_indicate_Num].Buzzer_Time = Time; - if(flicker_time<51) - flicker_time = 0; // If the blinking interval is less than 50ms, the blinking is ignored - Buzzer_indicate[Buzzer_indicate_Num].Buzzer_Flicker = flicker_time; - Buzzer_indicate_Num ++; - } -} -void BuzzerTask(void *parameter) { - bool Buzzer_Flag = 0; - while(1){ - if(Buzzer_indicate[0].Buzzer_Time) - { - Buzzer_Flag = 1; - Buzzer_Open(); - if(Buzzer_indicate[0].Buzzer_Flicker){ - vTaskDelay(pdMS_TO_TICKS(Buzzer_indicate[0].Buzzer_Flicker)); - Buzzer_Closs(); - vTaskDelay(pdMS_TO_TICKS(Buzzer_indicate[0].Buzzer_Flicker)); - } - if(Buzzer_indicate[0].Buzzer_Time > (Buzzer_indicate[0].Buzzer_Flicker * 2 +50)) - Buzzer_indicate[0].Buzzer_Time = Buzzer_indicate[0].Buzzer_Time -(Buzzer_indicate[0].Buzzer_Flicker * 2 +50); - else - Buzzer_indicate[0].Buzzer_Time = 0; - } - else if(Buzzer_Flag && !Buzzer_indicate[0].Buzzer_Time){ - Buzzer_Closs(); - Buzzer_Flag = 0; - Buzzer_indicate[0].Buzzer_Time = 0; - Buzzer_indicate[0].Buzzer_Flicker = 0; - if(Buzzer_indicate_Num > 0){ - for (int i = 1; i < Buzzer_indicate_Num; i++) { - Buzzer_indicate[i-1] = Buzzer_indicate[i]; - } - Buzzer_indicate[Buzzer_indicate_Num - 1].Buzzer_Time = 0; - Buzzer_indicate[Buzzer_indicate_Num - 1].Buzzer_Flicker = 0; - Buzzer_indicate_Num --; - } - } - vTaskDelay(pdMS_TO_TICKS(50)); - } - vTaskDelete(NULL); -} \ No newline at end of file diff --git a/lib/GPIO/WS_GPIO.h b/lib/GPIO/WS_GPIO.h deleted file mode 100644 index 8f517d4..0000000 --- a/lib/GPIO/WS_GPIO.h +++ /dev/null @@ -1,44 +0,0 @@ -#pragma once -#include -#include // Reference the ESP32 built-in serial port library - -/************************************************************* I/O *************************************************************/ -#define TXD1 17 //The TXD of UART1 corresponds to GPIO RS485/CAN -#define RXD1 18 //The RXD of UART1 corresponds to GPIO RS485/CAN -#define GPIO_PIN_RGB 38 // RGB Control GPIO - -/*********************************************************** Buzzer ***********************************************************/ -#define GPIO_PIN_Buzzer 46 // Buzzer Control GPIO -#define PWM_Channel 1 // PWM Channel -#define Frequency 1000 // PWM frequencyconst -#define Resolution 8 // PWM resolution ratio -#define Dutyfactor 200 // PWM Dutyfactor -#define Dutyfactor_MAX 255 - - -#define RGB_Indicate_Number 10 // Number of saved RGB indicator signals -#define RGB_Indicating_interval 500 // Time interval of each indication signal(unit: ms) -typedef struct { - uint8_t Red = 0; - uint8_t Green = 0; - uint8_t Blue = 0; - uint16_t RGB_Time = 0; // RGB lighting duration - uint16_t RGB_Flicker = 0; // RGB flicker interval -} RGB_Indicate; - -#define Buzzer_Indicate_Number 10 // Number of saved RGB indicator signals -typedef struct { - uint16_t Buzzer_Time = 0; // Buzzer duration - uint16_t Buzzer_Flicker = 0; // Buzzer interval duration -} Buzzer_Indicate; -/************************************************************* I/O *************************************************************/ -void GPIO_Init(); -void RGB_Light(uint8_t red_val, uint8_t green_val, uint8_t blue_val); -void RGB_Open_Time(uint8_t red_val, uint8_t green_val, uint8_t blue_val, uint16_t Time, uint16_t flicker_time); -void RGBTask(void *parameter); - -void Set_Dutyfactor(uint16_t dutyfactor); -void Buzzer_Open(void); -void Buzzer_Closs(void); -void Buzzer_Open_Time(uint16_t Time, uint16_t flicker_time); -void BuzzerTask(void *parameter); diff --git a/lib/GPIO/WS_Relay.cpp b/lib/GPIO/WS_Relay.cpp deleted file mode 100644 index 1f8ab07..0000000 --- a/lib/GPIO/WS_Relay.cpp +++ /dev/null @@ -1,273 +0,0 @@ -#include "WS_Relay.h" - -bool Failure_Flag = 0; -/************************************************************* Relay I/O *************************************************************/ -bool Relay_Open(uint8_t CHx) -{ - if(!Set_EXIO(CHx, true)){ - printf("Failed to Open CH%d!!!\r\n", CHx); - Failure_Flag = 1; - return 0; - } - return 1; -} -bool Relay_Close(uint8_t CHx) -{ - if(!Set_EXIO(CHx, false)){ - printf("Failed to Closs CH%d!!!\r\n", CHx); - Failure_Flag = 1; - return 0; - } - return 1; -} -bool Relay_CHx_Toggle(uint8_t CHx) -{ - if(!Set_Toggle(CHx)){ - printf("Failed to Toggle CH%d!!!\r\n", CHx); - Failure_Flag = 1; - return 0; - } - return 1; -} -bool Relay_CHx(uint8_t CHx, bool State) -{ - bool result = 0; - if(State) - result = Relay_Open(CHx); - else - result = Relay_Close(CHx); - if(!result) - Failure_Flag = 1; - return result; -} -bool Relay_CHxs_PinState(uint8_t PinState) -{ - if(!Set_EXIOS(PinState)){ - printf("Failed to set the relay status!!!\r\n"); - Failure_Flag = 1; - return 0; - } - return 1; -} - -void RelayFailTask(void *parameter) { - while(1){ - if(Failure_Flag) - { - Failure_Flag = 0; - printf("Error: Relay control failed!!!\r\n"); - RGB_Open_Time(60,0,0,5000,500); - Buzzer_Open_Time(5000, 500); - } - vTaskDelay(pdMS_TO_TICKS(50)); - } - vTaskDelete(NULL); -} -void Relay_Init(void) -{ - TCA9554PWR_Init(0x00); - xTaskCreatePinnedToCore( - RelayFailTask, - "RelayFailTask", - 4096, - NULL, - 3, - NULL, - 0 - ); -} - -/******************************************************** Data Analysis ********************************************************/ -bool Relay_Flag[8] = {0}; // Relay current status flag -void Relay_Analysis(uint8_t *buf,uint8_t Mode_Flag) -{ - uint8_t ret = 0; - if(Mode_Flag == Bluetooth_Mode) - printf("Bluetooth Data :\r\n"); - else if(Mode_Flag == WIFI_Mode) - printf("WIFI Data :\r\n"); - else if(Mode_Flag == MQTT_Mode) - printf("MQTT Data :\r\n"); - else if(Mode_Flag == RS485_Mode) - printf("RS485 Data :\r\n"); - switch(buf[0]) - { - case CH1: - ret = Relay_CHx_Toggle(GPIO_PIN_CH1); //Toggle the level status of the GPIO_PIN_CH1 pin - if(ret){ - Relay_Flag[0] =! Relay_Flag[0]; - Buzzer_Open_Time(200, 0); - if(Relay_Flag[0]) - printf("|*** Relay CH1 on ***|\r\n"); - else - printf("|*** Relay CH1 off ***|\r\n"); - } - break; - case CH2: - ret = Relay_CHx_Toggle(GPIO_PIN_CH2); //Toggle the level status of the GPIO_PIN_CH2 pin - if(ret){ - Relay_Flag[1] =! Relay_Flag[1]; - Buzzer_Open_Time(200, 0); - if(Relay_Flag[1]) - printf("|*** Relay CH2 on ***|\r\n"); - else - printf("|*** Relay CH2 off ***|\r\n"); - } - break; - case CH3: - ret = Relay_CHx_Toggle(GPIO_PIN_CH3); //Toggle the level status of the GPIO_PIN_CH3 pin - if(ret){ - Relay_Flag[2] =! Relay_Flag[2]; - Buzzer_Open_Time(200, 0); - if(Relay_Flag[2]) - printf("|*** Relay CH3 on ***|\r\n"); - else - printf("|*** Relay CH3 off ***|\r\n"); - } - break; - case CH4: - ret = Relay_CHx_Toggle(GPIO_PIN_CH4); //Toggle the level status of the GPIO_PIN_CH4 pin - if(ret){ - Relay_Flag[3] =! Relay_Flag[3]; - Buzzer_Open_Time(200, 0); - if(Relay_Flag[3]) - printf("|*** Relay CH4 on ***|\r\n"); - else - printf("|*** Relay CH4 off ***|\r\n"); - } - break; - case CH5: - ret = Relay_CHx_Toggle(GPIO_PIN_CH5); //Toggle the level status of the GPIO_PIN_CH5 pin - if(ret){ - Relay_Flag[4] =! Relay_Flag[4]; - Buzzer_Open_Time(200, 0); - if(Relay_Flag[4]) - printf("|*** Relay CH5 on ***|\r\n"); - else - printf("|*** Relay CH5 off ***|\r\n"); - } - break; - case CH6: - ret = Relay_CHx_Toggle(GPIO_PIN_CH6); //Toggle the level status of the GPIO_PIN_CH6 pin - if(ret){ - Relay_Flag[5] =! Relay_Flag[5]; - Buzzer_Open_Time(200, 0); - if(Relay_Flag[5]) - printf("|*** Relay CH6 on ***|\r\n"); - else - printf("|*** Relay CH6 off ***|\r\n"); - } - break; - case CH7: - ret = Relay_CHx_Toggle(GPIO_PIN_CH7); //Toggle the level status of the GPIO_PIN_CH6 pin - if(ret){ - Relay_Flag[6] =! Relay_Flag[6]; - Buzzer_Open_Time(200, 0); - if(Relay_Flag[6]) - printf("|*** Relay CH7 on ***|\r\n"); - else - printf("|*** Relay CH7 off ***|\r\n"); - } - break; - case CH8: - ret = Relay_CHx_Toggle(GPIO_PIN_CH8); //Toggle the level status of the GPIO_PIN_CH6 pin - if(ret){ - Relay_Flag[7] =! Relay_Flag[7]; - Buzzer_Open_Time(200, 0); - if(Relay_Flag[7]) - printf("|*** Relay CH8 on ***|\r\n"); - else - printf("|*** Relay CH8 off ***|\r\n"); - } - break; - case ALL_ON: - ret = Relay_CHxs_PinState(0xFF); // Turn on all relay - if(ret){ - memset(Relay_Flag,1, sizeof(Relay_Flag)); - printf("|*** Relay ALL on ***|\r\n"); - Buzzer_Open_Time(500, 0); - } - - break; - case ALL_OFF: - ret = Relay_CHxs_PinState(0x00); // Turn off all relay - if(ret){ - memset(Relay_Flag,0, sizeof(Relay_Flag)); - printf("|*** Relay ALL off ***|\r\n"); - Buzzer_Open_Time(500, 150); - } - break; - default: - printf("Note : Non-instruction data was received ! - %c\r\n", buf[0]); - } -} - -void Relay_Immediate(uint8_t CHx, bool State, uint8_t Mode_Flag) -{ - if(!CHx || CHx > 8){ - printf("Relay_Immediate(function): Incoming parameter error!!!!\r\n"); - Failure_Flag = 1; - } - else{ - uint8_t ret = 0; - if(Mode_Flag == DIN_Mode) - printf("DIN Data :\r\n"); - else if(Mode_Flag == RTC_Mode) - printf("RTC Data :\r\n"); - ret = Relay_CHx(CHx,State); - if(ret){ - Relay_Flag[CHx-1] = State; - Buzzer_Open_Time(200, 0); - if(Relay_Flag[0]) - printf("|*** Relay CH%d on ***|\r\n",CHx); - else - printf("|*** Relay CH%d off ***|\r\n",CHx); - } - } -} -void Relay_Immediate_CHxn(Status_adjustment * Relay_n, uint8_t Mode_Flag) -{ - uint8_t ret = 0; - if(Mode_Flag == DIN_Mode) - printf("DIN Data :\r\n"); - else if(Mode_Flag == RTC_Mode) - printf("RTC Data :\r\n"); - for (int i = 0; i < 8; i++) { - if(Relay_n[i] == STATE_Open || Relay_n[i] == STATE_Close){ - Relay_Flag[i] = (bool)Relay_n[i]; - ret = Relay_CHx(i+1,Relay_n[i]); - if(Relay_n[i] == STATE_Open) - printf("|*** Relay CH%d on ***|\r\n",i+1); - else if(Relay_n[i] == STATE_Close) - printf("|*** Relay CH%d off ***|\r\n",i+1); - } - } - Buzzer_Open_Time(200, 0); -} - -void Relay_Immediate_CHxs(uint8_t PinState, uint8_t Mode_Flag) -{ - uint8_t ret = 0; - if(Mode_Flag == DIN_Mode) - printf("DIN Data :\r\n"); - else if(Mode_Flag == RTC_Mode) - printf("RTC Data :\r\n"); - for (int i = 0; i < 8; i++) { - Relay_Flag[i] = (PinState >> i) & 0x01; // 提取每一位并赋值 - } - ret = Relay_CHxs_PinState(PinState); - if(ret){ - for (int j = 0; j < 8; j++) { - if(Relay_Flag[j]) - printf("|*** Relay CH%d on ***|\r\n",j+1); - else - printf("|*** Relay CH%d off ***|\r\n",j+1); - } - Buzzer_Open_Time(200, 0); - } - else - { - printf("Relay_Immediate_CHxs(function): Relay control failure!!!!\r\n"); - Failure_Flag = 1; - } -} \ No newline at end of file diff --git a/lib/GPIO/WS_Relay.h b/lib/GPIO/WS_Relay.h deleted file mode 100644 index ac8b742..0000000 --- a/lib/GPIO/WS_Relay.h +++ /dev/null @@ -1,56 +0,0 @@ -#pragma once - -#include "WS_TCA9554PWR.h" -#include // Reference the ESP32 built-in serial port library -#include "WS_GPIO.h" - - -/************************************************************* I/O *************************************************************/ -#define Relay_Number_MAX 8 -#define GPIO_PIN_CH1 EXIO_PIN1 // CH1 Control GPIO -#define GPIO_PIN_CH2 EXIO_PIN2 // CH2 Control GPIO -#define GPIO_PIN_CH3 EXIO_PIN3 // CH3 Control GPIO -#define GPIO_PIN_CH4 EXIO_PIN4 // CH4 Control GPIO -#define GPIO_PIN_CH5 EXIO_PIN5 // CH5 Control GPIO -#define GPIO_PIN_CH6 EXIO_PIN6 // CH6 Control GPIO -#define GPIO_PIN_CH7 EXIO_PIN7 // CH7 Control GPIO -#define GPIO_PIN_CH8 EXIO_PIN8 // CH8 Control GPIO - - -#define CH1 '1' // CH1 Enabled Instruction Hex : 0x31 -#define CH2 '2' // CH2 Enabled Instruction Hex : 0x32 -#define CH3 '3' // CH3 Enabled Instruction Hex : 0x33 -#define CH4 '4' // CH4 Enabled Instruction Hex : 0x34 -#define CH5 '5' // CH5 Enabled Instruction Hex : 0x35 -#define CH6 '6' // CH6 Enabled Instruction Hex : 0x36 -#define CH7 '7' // CH5 Enabled Instruction Hex : 0x37 -#define CH8 '8' // CH6 Enabled Instruction Hex : 0x38 -#define ALL_ON '9' // Start all channel instructions Hex : 0x39 -#define ALL_OFF '0' // Disable all channel instructions Hex : 0x30 - -#define DIN_Mode 1 -#define RS485_Mode 2 // Used to distinguish data sources -#define Bluetooth_Mode 3 -#define WIFI_Mode 4 -#define MQTT_Mode 5 -#define RTC_Mode 6 - -typedef enum { - STATE_Close = 0, // Closs Relay - STATE_Open = 1, // Open Relay - STATE_Retain = 2, // Stay in place -} Status_adjustment; - -extern bool Relay_Flag[8]; // Relay current status flag - -void Relay_Init(void); -bool Relay_Close(uint8_t CHx); -bool Relay_Open(uint8_t CHx); -bool Relay_CHx_Toggle(uint8_t CHx); -bool Relay_CHx(uint8_t CHx, bool State); -bool Relay_CHxs_PinState(uint8_t PinState); - -void Relay_Analysis(uint8_t *buf,uint8_t Mode_Flag); -void Relay_Immediate(uint8_t CHx, bool State, uint8_t Mode_Flag); -void Relay_Immediate_CHxs(uint8_t PinState, uint8_t Mode_Flag); -void Relay_Immediate_CHxn(Status_adjustment * Relay_n, uint8_t Mode_Flag); diff --git a/lib/RS485/WS_RS485.cpp b/lib/RS485/RS485_Driver.cpp similarity index 96% rename from lib/RS485/WS_RS485.cpp rename to lib/RS485/RS485_Driver.cpp index 7b3ceed..96e6889 100644 --- a/lib/RS485/WS_RS485.cpp +++ b/lib/RS485/RS485_Driver.cpp @@ -1,4 +1,4 @@ -#include "WS_RS485.h" +#include "RS485_driver.h" #include #include #include @@ -69,6 +69,7 @@ namespace drivers data.resize(avail); m_serial->readBytesUntil(ch, data.data(), avail); data.shrink_to_fit(); + return true; } //////////////////////////////// @@ -85,7 +86,7 @@ namespace drivers { constexpr uint8_t func = 0x01; log_d("Read coils: dev[%02x], reg[%04x], num[%d]", device, reg, num); - readBinary(func, device, reg, num, coils); + return readBinary(func, device, reg, num, coils); } // Func 0x02 @@ -93,7 +94,7 @@ namespace drivers { constexpr uint8_t func = 0x01; log_d("Read multi inputs: dev[%02x], reg[%04x], num[%d]", device, reg, num); - readBinary(func, device, reg, num, inputs); + return readBinary(func, device, reg, num, inputs); } // Func 0x03 @@ -101,7 +102,7 @@ namespace drivers { constexpr uint8_t func = 0x03; log_d("Read multi holding registers: dev[%02x], reg[%04x], num[%d]", device, reg, num); - readInteger(func, device, reg, num, values); + return readInteger(func, device, reg, num, values); } // Func 0x04 @@ -109,7 +110,7 @@ namespace drivers { constexpr uint8_t func = 0x04; log_d("Read multi input registers: dev[%02x], reg[%04x], num[%d]", device, reg, num); - readInteger(func, device, reg, num, values); + return readInteger(func, device, reg, num, values); } // Func 0x05 @@ -192,6 +193,7 @@ namespace drivers bitNum++; } } + return true; } const bool MODBUS::readInteger(const uint8_t func, const uint8_t device, const uint16_t reg, const uint16_t num, std::vector &out) @@ -233,6 +235,7 @@ namespace drivers const uint16_t val(0xFFFF & ((hi << 8) | lo)); out.push_back(be16toh(val)); } + return true; } const bool MODBUS::writeBinary(const uint8_t func, const uint8_t device, const uint16_t reg, const uint16_t bits, const std::vector &in) @@ -313,7 +316,7 @@ namespace drivers // compute crc for header + data m_crc.reset(); m_crc.add((uint8_t *)&header, headerBytes); // exclude last two bytes of crc - const uint16_t crc(htole16(m_crc.getCRC())); + const uint16_t crc(htole16(m_crc.calc())); std::vector dataOut(headerBytes + crcBytes, 0); std::memcpy(dataOut.data(), &header, headerBytes); @@ -344,7 +347,7 @@ namespace drivers m_crc.reset(); m_crc.add((uint8_t *)&header, headerBytes); // add the request excluding the CRC code m_crc.add((uint8_t *)dataBe.data(), dataBytes); - const uint16_t crc(htole16(m_crc.getCRC())); + const uint16_t crc(htole16(m_crc.calc())); std::vector dataOut; dataOut.resize(headerBytes + dataBytes + crcBytes); // header message + data values + crc code @@ -359,7 +362,7 @@ namespace drivers // compute crc of current message m_crc.reset(); m_crc.add(data.data(), data.size()); - const uint16_t computedCrc(m_crc.getCRC()); + const uint16_t computedCrc(m_crc.calc()); // extract crc from response const uint16_t size(data.size()); const uint8_t crcLo(data.at(size - 2)); diff --git a/lib/RS485/WS_RS485.h b/lib/RS485/RS485_Driver.h similarity index 90% rename from lib/RS485/WS_RS485.h rename to lib/RS485/RS485_Driver.h index 1c15577..1535bbc 100644 --- a/lib/RS485/WS_RS485.h +++ b/lib/RS485/RS485_Driver.h @@ -16,14 +16,6 @@ #define Extension_ALL_ON 9 // Expansion ALL ON #define Extension_ALL_OFF 10 // Expansion ALL OFF -void SetData(uint8_t *data, size_t length); // Send data from the RS485 -void ReadData(uint8_t *buf, uint8_t length); // Data is received over RS485 - -void RS485_Analysis(uint8_t *buf); // External relay control -void RS485_Init(); // Example Initialize the system serial port and RS485 -void RS485_Loop(); // Read RS485 data, parse and control relays -void RS485Task(void *parameter); - namespace drivers { class RS485 diff --git a/platformio.ini b/platformio.ini index 1cfe33d..193f5f4 100644 --- a/platformio.ini +++ b/platformio.ini @@ -17,3 +17,7 @@ lib_deps = arduino-libraries/NTPClient@^3.2.1 knolleary/PubSubClient@^2.8 robtillaart/CRC@^1.0.3 + +[env:native] +platform = native +test_framework = doctest diff --git a/src/MAIN_WIFI_MQTT.cpp b/src/MAIN_WIFI_MQTT.cpp deleted file mode 100644 index 69b102c..0000000 --- a/src/MAIN_WIFI_MQTT.cpp +++ /dev/null @@ -1,37 +0,0 @@ -#include -#include // Reference the ESP32 built-in serial port library -#include "WS_MQTT.h" -#include "WS_Bluetooth.h" -#include "WS_GPIO.h" -#include "WS_Serial.h" -#include "WS_RTC.h" -#include "WS_GPIO.h" -#include "WS_DIN.h" -#include "WS_SD.h" -#include "WS_ETH.h" - - -uint32_t Simulated_time=0; // Analog time counting - -/******************************************************** Initializing ********************************************************/ -void setup() { - Flash_test(); - GPIO_Init(); // RGB . Buzzer GPIO - //I2C_Init(); - //RTC_Init(); // RTC - SD_Init(); - Serial_Init(); // UART(RS485/CAN) - MQTT_Init();// MQTT - Bluetooth_Init();// Bluetooth - ETH_Init(); - - DIN_Init(); // If you don't want to control the relay through DIN, change Relay_Immediate_Default to 0 in WS_DIN.h and re-burn the program - Relay_Init(); - - printf("Connect to the WIFI network named \"ESP32-S3-POE-ETH-8DI-8RO\" and access the Internet using the connected IP address!!!\r\n"); -} - -/********************************************************** While **********************************************************/ -void loop() { - -} \ No newline at end of file diff --git a/src/WS_Bluetooth.cpp b/src/WS_Bluetooth.cpp deleted file mode 100644 index fdbe1a2..0000000 --- a/src/WS_Bluetooth.cpp +++ /dev/null @@ -1,152 +0,0 @@ -#include "WS_Bluetooth.h" - -BLEServer* pServer; // Used to represent a BLE server -BLECharacteristic* pTxCharacteristic; -BLECharacteristic* pRxCharacteristic; - -/********************************************************** Bluetooth *********************************************************/ - -class MyServerCallbacks : public BLEServerCallbacks { //By overriding the onConnect() and onDisconnect() functions - void onConnect(BLEServer* pServer) { // When the Device is connected, "Device connected" is printed. - Serial.println("Device connected"); - } - - void onDisconnect(BLEServer* pServer) { // "Device disconnected" will be printed when the device is disconnected - Serial.println("Device disconnected"); - - BLEAdvertising *pAdvertising = BLEDevice::getAdvertising(); // Re-broadcast so that the device can query - pAdvertising->addServiceUUID(SERVICE_UUID); // Re-broadcast so that the device can query - pAdvertising->setScanResponse(true); // Re-broadcast so that the device can query - pAdvertising->setMinPreferred(0x06); // Re-broadcast so that the device can query - pAdvertising->setMinPreferred(0x12); // Re-broadcast so that the device can query - BLEDevice::startAdvertising(); // Re-broadcast so that the device can query - pRxCharacteristic->notify(); // Re-broadcast so that the device can query - pAdvertising->start(); // Re-broadcast so that the device can query - } -}; -class MyRXCallback : public BLECharacteristicCallbacks { - void onWrite(BLECharacteristic* pCharacteristic) { // The onWrite function is called when the remote device sends data to your feature - String rxValue = String(pCharacteristic->getValue().c_str()); - if (!rxValue.isEmpty()) { - // The received data rxValue is processed here - if(rxValue.length() == 1) - { - printf("%s\n", rxValue.c_str()); // Print output through the serial port - uint8_t* valueBytes = reinterpret_cast(const_cast(rxValue.c_str())); // Convert value to uint8 t* - Relay_Analysis(valueBytes,Bluetooth_Mode); // pilot relay - } - else if(rxValue.length() == 2) - { - if(Extension_Enable) - { - printf("%s\n", rxValue.c_str()); // Print output through the serial port - uint8_t* valueBytes = reinterpret_cast(const_cast(rxValue.c_str())); // Convert value to uint8 t* - if(valueBytes[0] == 0x06) // Instruction check correct - RS485_Analysis(valueBytes); // Control external relay - else - printf("Note : Non-instruction data was received - Bluetooth !\r\n"); - } - else - printf("Note : Non-instruction data was received or external relays are not enabled - Bluetooth !\r\n"); - } - - else if(rxValue.length() == 14) - { - if(RTC_Event_Enable) - { - // printf("%s\n", rxValue.c_str()); // Print output through the serial port - uint8_t* valueBytes = reinterpret_cast(const_cast(rxValue.c_str())); - BLE_Set_RTC_Event(valueBytes); - } - else - printf("Note : Non-instruction data was received or RTC events were not enabled - Bluetooth !\r\n"); - } - else - { - printf("Note : Non-instruction data was received - Bluetooth !\r\n"); - } - pRxCharacteristic->setValue(""); // After data is read, set it to blank for next read - } - } -}; - -void BLE_Set_RTC_Event(uint8_t* valueBytes){ - if(valueBytes[0] == 0xA1 && valueBytes[6] == 0xAA && valueBytes[13] == 0xFF ){ - datetime_t Event_Time={0}; - Event_Time.year = (valueBytes[1]/16*10 + valueBytes[1] % 16) *100 + valueBytes[2]/16*10 + valueBytes[2] % 16; - Event_Time.month = valueBytes[3]/16*10 + valueBytes[3] % 16; - Event_Time.day = valueBytes[4]/16*10 + valueBytes[4] % 16; - Event_Time.dotw = valueBytes[5]/16*10 + valueBytes[5] % 16; - // valueBytes[6] == 0xAA; // check - Event_Time.hour = valueBytes[7]/16*10 + valueBytes[7] % 16; - Event_Time.minute = valueBytes[8]/16*10 + valueBytes[8] % 16; - Event_Time.second = valueBytes[9]/16*10 + valueBytes[9] % 16; - Repetition_event Repetition = (Repetition_event)valueBytes[12]; // cyclical indicators - if(valueBytes[11]){ // Whether to control all relays 1:Control all relays 0:Control a relay - uint8_t CHxs = valueBytes[10]; // relay control - TimerEvent_CHxs_Set(Event_Time, CHxs, Repetition); - } - else{ - uint8_t CHx = valueBytes[10]/16; - bool State = (valueBytes[10] % 16); - TimerEvent_CHx_Set(Event_Time,CHx, State, Repetition); - } - } -} -void Bluetooth_SendData(char* Data) { // Send data using Bluetooth - if (Data != nullptr && strlen(Data) > 0) { - if (pServer->getConnectedCount() > 0) { - String SendValue = String(Data); // Convert char* to String - pTxCharacteristic->setValue(SendValue.c_str()); // Set SendValue to the eigenvalue (String type) - pTxCharacteristic->notify(); // Sends a notification to all connected devices - } - } -} -void Bluetooth_Init() -{ - /************************************************************************* - Bluetooth - *************************************************************************/ - BLEDevice::init("ESP32-S3-POE-ETH-8DI-8RO"); // Initialize Bluetooth and start broadcasting - pServer = BLEDevice::createServer(); - pServer->setCallbacks(new MyServerCallbacks()); - BLEService* pService = pServer->createService(SERVICE_UUID); - pTxCharacteristic = pService->createCharacteristic( - TX_CHARACTERISTIC_UUID, - BLECharacteristic:: PROPERTY_READ); // The eigenvalues are readable and can be read by remote devices - pRxCharacteristic = pService->createCharacteristic( - RX_CHARACTERISTIC_UUID, - BLECharacteristic::PROPERTY_WRITE); // The eigenvalues are writable and can be written to by remote devices - pRxCharacteristic->setCallbacks(new MyRXCallback()); - - pRxCharacteristic->setValue("Successfully Connect To ESP32-S3-POE-ETH-8DI-8RO"); - pService->start(); - - BLEAdvertising *pAdvertising = BLEDevice::getAdvertising(); - pAdvertising->addServiceUUID(SERVICE_UUID); - pAdvertising->setScanResponse(true); - pAdvertising->setMinPreferred(0x06); - pAdvertising->setMinPreferred(0x12); - BLEDevice::startAdvertising(); - pRxCharacteristic->notify(); - pAdvertising->start(); - RGB_Open_Time(0, 0, 60,1000, 0); - printf("Now you can read it in your phone!\r\n"); - xTaskCreatePinnedToCore( - BLETask, - "BLETask", - 4096, - NULL, - 2, - NULL, - 0 - ); -} - -void BLETask(void *parameter) { - while(1){ - Bluetooth_SendData(ipStr); - vTaskDelay(pdMS_TO_TICKS(100)); - } - vTaskDelete(NULL); -} \ No newline at end of file diff --git a/src/WS_Bluetooth.h b/src/WS_Bluetooth.h deleted file mode 100644 index 7ee6bf2..0000000 --- a/src/WS_Bluetooth.h +++ /dev/null @@ -1,24 +0,0 @@ -#pragma once - -#include // Reference the ESP32 built-in serial port library -#include -#include -#include -#include "WS_GPIO.h" -#include "WS_Serial.h" -#include "WS_Information.h" -#include "WS_Relay.h" -#include "WS_MQTT.h" -#include "WS_RTC.h" - -#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b" // UUID of the server -#define RX_CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8" // UUID of the characteristic Tx -#define TX_CHARACTERISTIC_UUID "beb5484a-36e1-4688-b7f5-ea07361b26a8" // UUID of the characteristic Rx - -#define Bluetooth_Mode 2 - - -void Bluetooth_SendData(char * Data); -void Bluetooth_Init(); -void BLETask(void *parameter); -void BLE_Set_RTC_Event(uint8_t* valueBytes); \ No newline at end of file diff --git a/src/WS_Information.h b/src/WS_Information.h deleted file mode 100644 index ebfbe7d..0000000 --- a/src/WS_Information.h +++ /dev/null @@ -1,18 +0,0 @@ -#pragma once - -#define Extension_Enable 1 // Whether to extend the connection to external devices 1:Expansion device Modbus RTU Relay 0:No extend -#define RS485_CAN_Enable 1 // This item is configured according to product selection 1:Select RS485 0:Select CAN -#define RTC_Event_Enable 1 // Whether to enable RTC events (Bluetooth) 1:Enable 0:Disable - - - -// Name and password of the WiFi access point -#define STASSID "JSBPI" -#define STAPSK "waveshare0755" - -// Details about devices on the Waveshare cloud -#define MQTT_Server "mqtt.waveshare.cloud" -#define MQTT_Port 1883 -#define MQTT_ID "fc2d8db5" -#define MQTT_Pub "Pub/59/54/fc2d8db5" -#define MQTT_Sub "Sub/59/54/fc2d8db5" diff --git a/src/WS_MQTT.cpp b/src/WS_MQTT.cpp deleted file mode 100644 index 8ec5291..0000000 --- a/src/WS_MQTT.cpp +++ /dev/null @@ -1,248 +0,0 @@ -#include "WS_MQTT.h" - -// The name and password of the WiFi access point -const char* ssid = STASSID; -const char* password = STAPSK; -// Details about devices on the Waveshare cloud -const char* mqtt_server = MQTT_Server; -int PORT = MQTT_Port; -const char* ID = MQTT_ID; // Defining device ID -char pub[] = MQTT_Pub; // MQTT release topic -char sub[] = MQTT_Sub; // MQTT subscribe to topics - - -WiFiClient espClient; //MQTT initializes the contents -PubSubClient client(espClient); - -JsonDocument sendJson; -JsonDocument readJson; -unsigned long lastUpdateTime = 0; -char msg[MSG_BUFFER_SIZE]; -bool WIFI_Connection = 0; -bool WIFI_Connection_Old = 0; -char ipStr[16]; - -const unsigned long updateInterval = 5000; - -void WIFI_Init(void) -{ - xTaskCreatePinnedToCore( - WifiStaTask, - "WifiStaTask", - 4096, - NULL, - 3, - NULL, - 0 - ); -} - - -void WifiStaTask(void *parameter) { - uint8_t Count = 0; - WiFi.mode(WIFI_STA); - WiFi.setSleep(true); - WiFi.begin(ssid, password); // Connect to the specified Wi-Fi network - while(1){ - if(WiFi.status() != WL_CONNECTED) - { - WIFI_Connection = 0; - printf(".\n"); - RGB_Open_Time(50, 0, 0, 500, 0); - Count++; - if(Count >= 10){ - Count = 0; - printf("\r\n"); - WiFi.disconnect(); - vTaskDelay(pdMS_TO_TICKS(100)); - WiFi.mode(WIFI_OFF); - vTaskDelay(pdMS_TO_TICKS(100)); - WiFi.mode(WIFI_STA); - vTaskDelay(pdMS_TO_TICKS(100)); - WiFi.begin(ssid, password); - } - } - else{ - WIFI_Connection = 1; - IPAddress myIP = WiFi.localIP(); - printf("IP Address: "); - sprintf(ipStr, "%d.%d.%d.%d", myIP[0], myIP[1], myIP[2], myIP[3]); - printf("%s\r\n", ipStr); - RGB_Open_Time(0, 50, 0, 1000, 0); - - printf("WIFI connection is successful, relay control can be performed via Waveshare cloud.\r\n"); - - while (WiFi.status() == WL_CONNECTED){ - vTaskDelay(pdMS_TO_TICKS(100)); - } - } - vTaskDelay(pdMS_TO_TICKS(1000)); - } - vTaskDelete(NULL); -} - -// MQTT subscribes to callback functions for processing received messages -void callback(char* topic, byte* payload, unsigned int length) { - uint8_t CH_Flag = 0; - String inputString; - for (int i = 0; i < length; i++) { - inputString += (char)payload[i]; - } - printf("%s\r\n",inputString.c_str()); // Format of data sent back by the server {"data":{"CH1":1}} - int dataBegin = inputString.indexOf("\"data\""); // Finds if "data" is present in the string (quotes also) - if (dataBegin == -1) { - printf("Missing 'data' field in JSON. - MQTT\r\n"); - return; - } - int CH_Begin = -1; - if (inputString.indexOf("\"CH1\"", dataBegin) != -1){ - CH_Flag = 1; - CH_Begin = inputString.indexOf("\"CH1\"", dataBegin); - } - else if (inputString.indexOf("\"CH2\"", dataBegin) != -1){ - CH_Flag = 2; - CH_Begin = inputString.indexOf("\"CH2\"", dataBegin); - } - else if (inputString.indexOf("\"CH3\"", dataBegin) != -1){ - CH_Flag = 3; - CH_Begin = inputString.indexOf("\"CH3\"", dataBegin); - } - else if (inputString.indexOf("\"CH4\"", dataBegin) != -1){ - CH_Flag = 4; - CH_Begin = inputString.indexOf("\"CH4\"", dataBegin); - } - else if (inputString.indexOf("\"CH5\"", dataBegin) != -1){ - CH_Flag = 5; - CH_Begin = inputString.indexOf("\"CH5\"", dataBegin); - } - else if (inputString.indexOf("\"CH6\"", dataBegin) != -1){ - CH_Flag = 6; - CH_Begin = inputString.indexOf("\"CH6\"", dataBegin); - } - else if (inputString.indexOf("\"CH7\"", dataBegin) != -1){ - CH_Flag = 7; - CH_Begin = inputString.indexOf("\"CH7\"", dataBegin); - } - else if (inputString.indexOf("\"CH8\"", dataBegin) != -1){ - CH_Flag = 8; - CH_Begin = inputString.indexOf("\"CH8\"", dataBegin); - } - else if (inputString.indexOf("\"ALL\"", dataBegin) != -1){ - CH_Flag = 9; - CH_Begin = inputString.indexOf("\"ALL\"", dataBegin); - } - else{ - printf("Note : Non-instruction data was received - MQTT!\r\n"); - CH_Flag = 0; - return; - } - int valueBegin = inputString.indexOf(':', CH_Begin); - int valueEnd = inputString.indexOf('}', valueBegin); - if (valueBegin != -1 && valueEnd != -1) { - if(CH_Flag != 0) - { - String ValueStr = inputString.substring(valueBegin + 1, valueEnd); - int Value = ValueStr.toInt(); - if(CH_Flag < 9){ - if(Value == 1 && Relay_Flag[CH_Flag - 1] == 0){ - uint8_t Data[1]={CH_Flag+48}; - Relay_Analysis(Data,MQTT_Mode); - } - else if(Value == 0 && Relay_Flag[CH_Flag - 1] == 1){ - uint8_t Data[1]={CH_Flag+48}; - Relay_Analysis(Data,MQTT_Mode); - } - } - else if(CH_Flag == 9){ - if(Value == 1 && ((Relay_Flag[0] & Relay_Flag[1] & Relay_Flag[2] & Relay_Flag[3] & Relay_Flag[4] & Relay_Flag[5] & Relay_Flag[6] & Relay_Flag[7]) == 0)){ - uint8_t Data[1]={9+48}; - Relay_Analysis(Data,MQTT_Mode); - } - else if(Value == 0 && ((Relay_Flag[0] | Relay_Flag[1] | Relay_Flag[2] | Relay_Flag[3] | Relay_Flag[4] | Relay_Flag[5] | Relay_Flag[6] | Relay_Flag[7] )== 1)){ - uint8_t Data[1]={0+48}; - Relay_Analysis(Data,MQTT_Mode); - } - } - } - } -} - - -// Reconnect to the MQTT server -void reconnect(void) { - uint8_t Count = 0; - while (!client.connected()) { - Count++; - if (client.connect(ID)) { - client.subscribe(sub); - printf("Waveshare Cloud connection is successful and now you can use all features.\r\n"); - } - else{ - delay(500); - if(Count % 2 == 0 && Count != 0){ - printf("%d\r\n", client.state()); - RGB_Open_Time(50, 0, 50, 1000, 0); - } - if(Count % 10 == 0){ // 10 attempts failed to connect, cancel the connection, try again - client.disconnect(); - delay(100); - client.setServer(mqtt_server, PORT); - delay(100); - client.setCallback(callback); - delay(100); - } - if(Count > 32){ // connection fail - Count = 0; - printf("warning: Waveshare cloud connection fails. Currently, only Bluetooth control is available !!!\r\n"); - } - - } - } -} -// Send data in JSON format to MQTT server -void sendJsonData(void) { - sendJson["ID"] = ID; - String pubres; - serializeJson(sendJson, pubres); - int str_len = pubres.length() + 1; - char char_array[str_len]; - pubres.toCharArray(char_array, str_len); - client.publish(pub, char_array); -} - -void MQTTTask(void *parameter) { - bool WIFI_Connection_Old; - while(1){ - if(WIFI_Connection == 1) - { - if(!WIFI_Connection_Old){ - WIFI_Connection_Old = 1; - client.setServer(mqtt_server, PORT); - client.setCallback(callback); - } - if (!client.connected()) { - reconnect(); - } - client.loop(); - } - else{ - WIFI_Connection_Old = 0; - } - vTaskDelay(pdMS_TO_TICKS(10)); - } - vTaskDelete(NULL); -} -void MQTT_Init(void) -{ - WIFI_Init(); - xTaskCreatePinnedToCore( - MQTTTask, - "MQTTTask", - 4096, - NULL, - 3, - NULL, - 0 - ); -} - diff --git a/src/WS_MQTT.h b/src/WS_MQTT.h deleted file mode 100644 index 49a27de..0000000 --- a/src/WS_MQTT.h +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef _WS_MQTT_H_ -#define _WS_MQTT_H_ - -#include -#include -#include -#include -#include -#include "WS_GPIO.h" -#include "WS_Information.h" -#include "WS_Relay.h" - - -#define MSG_BUFFER_SIZE (50) - -extern char ipStr[16]; - -void WIFI_Init(void); -void WifiStaTask(void *parameter); -void callback(char* topic, byte* payload, unsigned int length); // MQTT subscribes to callback functions for processing received messages -void reconnect(void); // Reconnect to the MQTT server -void sendJsonData(void); // Send data in JSON format to MQTT server -void MQTT_Init(void); - -#endif \ No newline at end of file diff --git a/src/WS_SD.cpp b/src/WS_SD.cpp deleted file mode 100644 index 5088639..0000000 --- a/src/WS_SD.cpp +++ /dev/null @@ -1,113 +0,0 @@ -#include "WS_SD.h" - -bool SDCard_Flag = 0; -bool SDCard_Finish = 0; - -uint16_t SDCard_Size = 0; -uint16_t Flash_Size = 0; - -void SD_Init() { - // SD MMC - if(!SD_MMC.setPins(SD_CLK_PIN, SD_CMD_PIN, SD_D0_PIN,-1,-1,-1)){ - printf("SD MMC: Pin change failed!\r\n"); - return; - } - if (SD_MMC.begin("/sdcard", true, true)) { // "/sdcard", true, true or "/sdcard", true, false - printf("SD card initialization successful!\r\n"); - } else { - printf("SD card initialization failed!\r\n"); - } - uint8_t cardType = SD_MMC.cardType(); - if(cardType == CARD_NONE){ - printf("No SD card attached\r\n"); - return; - } - else{ - printf("SD Card Type: "); - if(cardType == CARD_MMC){ - printf("MMC\r\n"); - } else if(cardType == CARD_SD){ - printf("SDSC\r\n"); - } else if(cardType == CARD_SDHC){ - printf("SDHC\r\n"); - } else { - printf("UNKNOWN\r\n"); - } - uint64_t totalBytes = SD_MMC.totalBytes(); - uint64_t usedBytes = SD_MMC.usedBytes(); - SDCard_Size = totalBytes/(1024*1024); - printf("Total space: %llu\n", totalBytes); - printf("Used space: %llu\n", usedBytes); - printf("Free space: %llu\n", totalBytes - usedBytes); - } -} -bool File_Search(const char* directory, const char* fileName) -{ - File Path = SD_MMC.open(directory); - if (!Path) { - printf("Path: <%s> does not exist\r\n",directory); - return false; - } - File file = Path.openNextFile(); - while (file) { - if (strcmp(file.name(), fileName) == 0) { - if (strcmp(directory, "/") == 0) - printf("File '%s%s' found in root directory.\r\n",directory,fileName); - else - printf("File '%s/%s' found in root directory.\r\n",directory,fileName); - Path.close(); - return true; - } - file = Path.openNextFile(); - } - if (strcmp(directory, "/") == 0) - printf("File '%s%s' not found in root directory.\r\n",directory,fileName); - else - printf("File '%s/%s' not found in root directory.\r\n",directory,fileName); - Path.close(); - return false; -} -uint16_t Folder_retrieval(const char* directory, const char* fileExtension, char File_Name[][100],uint16_t maxFiles) -{ - File Path = SD_MMC.open(directory); - if (!Path) { - printf("Path: <%s> does not exist\r\n",directory); - return false; - } - - uint16_t fileCount = 0; - char filePath[100]; - File file = Path.openNextFile(); - while (file && fileCount < maxFiles) { - if (!file.isDirectory() && strstr(file.name(), fileExtension)) { - strncpy(File_Name[fileCount], file.name(), sizeof(File_Name[fileCount])); - if (strcmp(directory, "/") == 0) { - snprintf(filePath, 100, "%s%s", directory, file.name()); - } else { - snprintf(filePath, 100, "%s/%s", directory, file.name()); - } - printf("File found: %s\r\n", filePath); - fileCount++; - } - file = Path.openNextFile(); - } - Path.close(); - if (fileCount > 0) { - printf("Retrieved %d mp3 files\r\n",fileCount); - return fileCount; - } else { - printf("No files with extension '%s' found in directory: %s\r\n", fileExtension, directory); - return 0; - } -} - -void Flash_test() -{ - printf("/********** RAM Test**********/\r\n"); - // Get Flash size - uint32_t flashSize = ESP.getFlashChipSize(); - Flash_Size = flashSize/1024/1024; - printf("Flash size: %d MB \r\n", flashSize/1024/1024); - - printf("/******* RAM Test Over********/\r\n\r\n"); -} \ No newline at end of file diff --git a/src/WS_SD.h b/src/WS_SD.h deleted file mode 100644 index 65d103b..0000000 --- a/src/WS_SD.h +++ /dev/null @@ -1,18 +0,0 @@ -#pragma once -#include "Arduino.h" -#include -#include "FS.h" -#include "SD_MMC.h" - -#define SD_CLK_PIN 48 -#define SD_CMD_PIN 47 -#define SD_D0_PIN 45 - -extern uint16_t SDCard_Size; -extern uint16_t Flash_Size; - -void SD_Init(); -void Flash_test(); - -bool File_Search(const char* directory, const char* fileName); -uint16_t Folder_retrieval(const char* directory, const char* fileExtension, char File_Name[][100],uint16_t maxFiles); diff --git a/src/WS_Serial.cpp b/src/WS_Serial.cpp deleted file mode 100644 index 9d96fb5..0000000 --- a/src/WS_Serial.cpp +++ /dev/null @@ -1,8 +0,0 @@ -#include "WS_Serial.h" -void Serial_Init() -{ - if(RS485_CAN_Enable) - RS485_Init(); - //else - //CAN_Init(); -} diff --git a/src/WS_Serial.h b/src/WS_Serial.h deleted file mode 100644 index 6bcc71f..0000000 --- a/src/WS_Serial.h +++ /dev/null @@ -1,8 +0,0 @@ -#pragma once - -#include "WS_Information.h" -#include "WS_RS485.h" -//#include "WS_CAN.h" - -void Serial_Init(); // Example Initialize the system serial port and RS485 -void Serial_Loop(); // Read RS485 data, parse and control relays diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..87f36b8 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,38 @@ +#include +#include + +void setup() { + bool success = true; + auto bus = drivers::MODBUS(9600, SERIAL_8N1); + const uint8_t devAddress(0x01); + const uint8_t baseRegister(0x02); + + log_i("Write single coil"); + success &= bus.writeCoil(devAddress, baseRegister, true); + success &=bus.writeCoil(devAddress, baseRegister, false); + + log_i("Write multiple coils"); + const uint16_t coilsNum(32); + std::vector coilsValues(coilsNum, false); + bool v=true; + for (auto i(0); i < coilsNum; i++) { + coilsValues[i] = v; + v=~v; + } + success &=bus.writeCoils(devAddress, baseRegister, coilsValues); + + log_i("Write single register"); + success &=bus.writeRegister(devAddress, baseRegister, 0xAA); + + log_i("Write multiple registers"); + const uint16_t regNum(16); + std::vector regValues(regNum, 0); + for (uint16_t i(0); i < regNum; i++) { + regValues[i] = i*2; + } + +} + +void loop() { + +} \ No newline at end of file