diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..43dc468 --- /dev/null +++ b/.clang-format @@ -0,0 +1,32 @@ +DisableFormat: false +SpaceBeforeParens: Never +AllowShortFunctionsOnASingleLine: None +BasedOnStyle: LLVM +BreakConstructorInitializersBeforeComma: true +BreakBeforeBraces: Custom +BraceWrapping: + AfterControlStatement: true + AfterNamespace: true + AfterClass: true + AfterFunction: true + AfterStruct: true + BeforeElse: true + BeforeCatch: true + AfterEnum: true +ColumnLimit: 120 +AllowAllParametersOfDeclarationOnNextLine: false +BinPackParameters: false +BinPackArguments: false +TabWidth: 4 +IndentWidth: 4 +UseTab: Never +PointerAlignment: Left +IndentCaseLabels: true +AccessModifierOffset: -4 +AlwaysBreakTemplateDeclarations: true +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakAfterReturnType: None +PenaltyReturnTypeOnItsOwnLine: 500 +PenaltyBreakBeforeFirstCallParameter: 10000 +PenaltyExcessCharacter: 10 +FixNamespaceComments: true diff --git a/README.md b/README.md index a96d342..49a6662 100644 --- a/README.md +++ b/README.md @@ -11,14 +11,11 @@ HomeControl provides an open source Arduino library for integration with Your sm MQTT is a machine-to-machine (M2M)/"Internet of Things" open source connectivity protocol. It was designed as an extremely lightweight publish/subscribe messaging transport. It is useful for connections with remote locations where a small code footprint is required and/or network bandwidth is at a premium. # Getting started: +## Arduino The Arduino Software (IDE) allows you to write examples and upload them to your board. IDE can be downloaded [here](https://www.arduino.cc/en/Main/Software). If you have a reliable internet connection, you can use the [online IDE](https://create.arduino.cc/). It will allow you to save your sketches in the cloud, having them available from any device and backed up. To download HomeControl open source Arduino library, click the DOWNLOAD button in the top right corner and uncompress the folder. Place the homecontrol library folder to your /libraries/folder. You may need to create the libraries subfolder if this is your first library. Restart the IDE. -Repeat the procedure with following Arduino libraries to use this class: -- [Loops](https://github.com/seky2205/NetworkLoops) -- [PubSubClient](https://github.com/knolleary/pubsubclient) - You can open one of the HomeControl example sketch: File > Examples > homecontrol-mqtt Select the board You are using (Tools > Board) and correct serial port of the board (Tools > Port). @@ -27,8 +24,12 @@ On Windows, port this should be something like COM2 (or some other number), on t Push the reset button on the board then click the Upload button in the IDE. Wait a few seconds. If successful, the message "Done uploading." will appear in the status bar. +## Other platforms +This will be updated as we add platform + # Examples: -HomeControl is providing You six examples composed of specific endpoints for each application: +## Arduino +HomeControl is providing You examples composed of specific endpoints for each application: - basic_OnOff - with this example, it is possible to control and have a feedback with any existing device which use a digital control signal (on/off). - basic_Level - example Level allows you to manage and feedback any existing device which use analog control signal. This example also uses the functionality of the basic_OnOff example. - basic_Color - allows you to turn ON or OFF, set color and brightness of RGB LED strip. @@ -38,3 +39,6 @@ HomeControl is providing You six examples composed of specific endpoints for eac Your smart devices can be a combination of multiple endpoints from this library. On that way can be implemented devices for virtually every user need. +## Other platforms +This platforms will have examples in their own subfolders + diff --git a/examples/basic_Color/basic_Color.ino b/examples/basic_Color/basic_Color.ino index 0a90177..96b97df 100644 --- a/examples/basic_Color/basic_Color.ino +++ b/examples/basic_Color/basic_Color.ino @@ -1,25 +1,26 @@ #include "HomeControlMagic.h" + +// in Config file define ethernet options +#include "arduinoWrapper/ArduinoConfig.h" +#include "arduinoWrapper/ArduinoNetworkInterface.h" +#include "arduinoWrapper/ArduinoWrapper.h" + #include "Endpoints/EndpointColor.h" -#define ESP_LOOP -#define SECURE -#define WIFI_SSID "" // Wifi network name -#define WIFI_PASS "" // Wifi password -#include "NetworkLoops.hpp" //#define DEBUG -#define R_PIN 5 // GPIO pin, as example R color - (D1) -#define G_PIN 4 // GPIO pin, as example G color - (D2) -#define B_PIN 0 // GPIO pin, as example B color - (D3) - -#define RECONNECTION_TIME 5 // network reconnection time in seconds +#define R_PIN 5 // GPIO pin, as example R color - (D1) +#define G_PIN 4 // GPIO pin, as example G color - (D2) +#define B_PIN 0 // GPIO pin, as example B color - (D3) -static char* const GW_IP = "GW_IP"; // gateway IP address -static char* const deviceName = "COLOR_DEVICE"; // name of device -static char* const username = "hc"; // copy username from app -static char* const password = ""; // copy password from app +IPAddress gw_ip = {192, 168, 1, 10}; +static char* const deviceName = "COLOR_DEVICE"; // name of device +static const char* const wifi_ssid = "WIFI-SSID"; +static const char* const wifi_pass = "WIFI-PASS"; +static char* const mqtt_username = "hc"; // copy username from app +static char* const mqtt_password = ""; // copy password from app -bool active_pin_state = true; // reverse pin state +bool active_pin_state = true; // reverse pin state bool last_state = false; uint16_t last_level = 0; @@ -27,77 +28,91 @@ uint16_t last_color_R = 0; uint16_t last_color_G = 0; uint16_t last_color_B = 0; -HomeControlMagic hcm(GW_IP, deviceName, network, username, password); +HomeControlMagic hcm(deviceName); EndpointColor endpointColor(&hcm); uint16_t adjLevel(uint16_t color_X, uint16_t level) { - if(active_pin_state) - { - // level 0-1000 - return (int)((color_X / 10.) * ((double)level / 10000)); - } - else - { - // level 1000-0 - return (int)((color_X / 10.) * ((double)(10000 - level) / 10000)); - } + if(active_pin_state) + { + // level 0-1000 + return (int)((color_X / 10.) * ((double)level / 10000)); + } + else + { + // level 1000-0 + return (int)((color_X / 10.) * ((double)(10000 - level) / 10000)); + } } void controlPin() { - // state 0/1 - bool state = endpointColor.getState(); - - // level 0-10000 - uint16_t level = endpointColor.getLevel(); - - // RGB color 0-10000 - uint16_t color_R = endpointColor.getColorR(); - uint16_t color_G = endpointColor.getColorG(); - uint16_t color_B = endpointColor.getColorB(); - - if((state != last_state) || (last_level != level) || (color_R != last_color_R) || (color_G != last_color_G) || (color_B != last_color_B)) - { - last_state = state; - last_level = level; - last_color_R = color_R; - last_color_G = color_G; - last_color_B = color_B; - - if(state) - { - analogWrite(R_PIN, adjLevel(color_R, level)); - analogWrite(G_PIN, adjLevel(color_G, level)); - analogWrite(B_PIN, adjLevel(color_B, level)); - } - else + // state 0/1 + bool state = endpointColor.getState(); + + // level 0-10000 + uint16_t level = endpointColor.getLevel(); + + // RGB color 0-10000 + uint16_t color_R = endpointColor.getColorR(); + uint16_t color_G = endpointColor.getColorG(); + uint16_t color_B = endpointColor.getColorB(); + + if((state != last_state) || (last_level != level) || (color_R != last_color_R) || (color_G != last_color_G) || + (color_B != last_color_B)) { - digitalWrite(R_PIN, !active_pin_state); - digitalWrite(G_PIN, !active_pin_state); - digitalWrite(B_PIN, !active_pin_state); + last_state = state; + last_level = level; + last_color_R = color_R; + last_color_G = color_G; + last_color_B = color_B; + + if(state) + { + analogWrite(R_PIN, adjLevel(color_R, level)); + analogWrite(G_PIN, adjLevel(color_G, level)); + analogWrite(B_PIN, adjLevel(color_B, level)); + } + else + { + digitalWrite(R_PIN, !active_pin_state); + digitalWrite(G_PIN, !active_pin_state); + digitalWrite(B_PIN, !active_pin_state); + } + endpointColor.sendFeedbackMessage(); } - endpointColor.sendFeedbackMessage(); - } } void setup() { - pinMode(R_PIN, OUTPUT); - pinMode(G_PIN, OUTPUT); - pinMode(B_PIN, OUTPUT); +#ifdef DEBUG + Serial.begin(115200); + Serial.println("Started serial"); +#endif + + networkSetSsid(wifi_ssid); + networkSetPass(wifi_pass); + networkSetSecure(true); // this must be called before setServer and networkSetup + networkSetup(); + networkStart(); + + wrapperSetServer(gw_ip); + wrapperSetUsernamePassword(mqtt_username, mqtt_password); + wrapperSetup(); + + hcm.setup(); + + // DO NOT TOUCH ANYTHING BEFORE THIS LINE IN SETUP FUNCTION - #ifdef DEBUG - Serial.begin(115200); - Serial.println("Started serial"); - #endif + pinMode(R_PIN, OUTPUT); + pinMode(G_PIN, OUTPUT); + pinMode(B_PIN, OUTPUT); - network.setReconnectTime(RECONNECTION_TIME); - hcm.addEndpoint(&endpointColor); + hcm.addEndpoint(&endpointColor); } void loop() { - hcm.doMagic(); - controlPin(); + hcm.doMagic(); + controlPin(); } diff --git a/examples/basic_Level/basic_Level.ino b/examples/basic_Level/basic_Level.ino index 3ead079..ea6eb8b 100644 --- a/examples/basic_Level/basic_Level.ino +++ b/examples/basic_Level/basic_Level.ino @@ -1,73 +1,87 @@ #include "HomeControlMagic.h" + +// in Config file define ethernet options +#include "arduinoWrapper/ArduinoConfig.h" +#include "arduinoWrapper/ArduinoNetworkInterface.h" +#include "arduinoWrapper/ArduinoWrapper.h" + #include "Endpoints/EndpointLevel.h" -#define ESP_LOOP -#define SECURE -#define WIFI_SSID "" // Wifi network name -#define WIFI_PASS "" // Wifi password -#include "NetworkLoops.hpp" //#define DEBUG -#define DEVICE_PIN LED_BUILTIN // GPIO pin, built in led as example - -#define RECONNECTION_TIME 5 // network reconnection time in seconds +#define DEVICE_PIN LED_BUILTIN // GPIO pin, built in led as example -static char* const GW_IP = "GW_IP"; // gateway IP address -static char* const deviceName = "LEVEL_DEVICE"; // name of device -static char* const username = "hc"; // copy username from app -static char* const password = ""; // copy password from app +IPAddress gw_ip = {192, 168, 1, 10}; +static char* const deviceName = "LEVEL_DEVICE"; // name of device +static const char* const wifi_ssid = "WIFI-SSID"; +static const char* const wifi_pass = "WIFI-PASS"; +static char* const mqtt_username = "hc"; // copy username from app +static char* const mqtt_password = ""; // copy password from app -bool active_pin_state = false; // reverse pin state +bool active_pin_state = false; // reverse pin state bool last_state = false; uint16_t last_level = 0; -HomeControlMagic hcm(GW_IP, deviceName, network, username, password); +HomeControlMagic hcm(deviceName); EndpointLevel endpointLevel(&hcm); void controlPin() { - bool state = endpointLevel.getState(); - uint16_t level = endpointLevel.getLevel(); - - if((state != last_state) || (level != last_level)) - { - last_state = state; - last_level = level; - if(state) - { - if(active_pin_state) - { - analogWrite(DEVICE_PIN, level / 10); - } - else - { - analogWrite(DEVICE_PIN, (10000 - level) / 10); - } - } - else + bool state = endpointLevel.getState(); + uint16_t level = endpointLevel.getLevel(); + + if((state != last_state) || (level != last_level)) { - digitalWrite(DEVICE_PIN, !active_pin_state); + last_state = state; + last_level = level; + if(state) + { + if(active_pin_state) + { + analogWrite(DEVICE_PIN, level / 10); + } + else + { + analogWrite(DEVICE_PIN, (10000 - level) / 10); + } + } + else + { + digitalWrite(DEVICE_PIN, !active_pin_state); + } + endpointLevel.sendFeedbackMessage(); } - endpointLevel.sendFeedbackMessage(); - } } void setup() { - pinMode(DEVICE_PIN, OUTPUT); - #ifdef DEBUG - Serial.begin(115200); - Serial.println("Started serial"); + Serial.begin(115200); + Serial.println("Started serial"); #endif - network.setReconnectTime(RECONNECTION_TIME); - hcm.addEndpoint(&endpointLevel); + networkSetSsid(wifi_ssid); + networkSetPass(wifi_pass); + networkSetSecure(true); // this must be called before setServer and networkSetup + networkSetup(); + networkStart(); + + wrapperSetServer(gw_ip); + wrapperSetUsernamePassword(mqtt_username, mqtt_password); + wrapperSetup(); + + hcm.setup(); + + // DO NOT TOUCH ANYTHING BEFORE THIS LINE IN SETUP FUNCTION + + pinMode(DEVICE_PIN, OUTPUT); + + hcm.addEndpoint(&endpointLevel); } void loop() { - controlPin(); - hcm.doMagic(); + controlPin(); + hcm.doMagic(); } diff --git a/examples/basic_Motion/basic_Motion.ino b/examples/basic_Motion/basic_Motion.ino index eea0fef..3732257 100644 --- a/examples/basic_Motion/basic_Motion.ino +++ b/examples/basic_Motion/basic_Motion.ino @@ -1,67 +1,81 @@ #include "HomeControlMagic.h" + +// in Config file define ethernet options +#include "arduinoWrapper/ArduinoConfig.h" +#include "arduinoWrapper/ArduinoNetworkInterface.h" +#include "arduinoWrapper/ArduinoWrapper.h" + #include "Endpoints/EndpointMotion.h" -#define ESP_LOOP -#define SECURE -#define WIFI_SSID "" // Wifi network name -#define WIFI_PASS "" // Wifi password -#include "NetworkLoops.hpp" //#define DEBUG -#define PIR_PIN 4 // GPIO pin -#define DEVICE_PIN LED_BUILTIN // GPIO pin to use, built in led as example - -#define RECONNECTION_TIME 5 // network reconnection time in seconds +#define PIR_PIN 4 // GPIO pin +#define DEVICE_PIN LED_BUILTIN // GPIO pin to use, built in led as example -static char* const GW_IP = "GW_IP"; // gateway IP address -static char* const deviceName = "MOTION_SENSOR"; // name of device -static char* const username = "hc"; // copy username from app -static char* const password = ""; // copy password from app +IPAddress gw_ip = {192, 168, 1, 10}; +static char* const deviceName = "MOTION_SENSOR"; // name of device +static const char* const wifi_ssid = "WIFI-SSID"; +static const char* const wifi_pass = "WIFI-PASS"; +static char* const mqtt_username = "hc"; // copy username from app +static char* const mqtt_password = ""; // copy password from app -bool active_pin_state = false; // reverse pin state +bool active_pin_state = false; // reverse pin state bool last_motion = false; -HomeControlMagic hcm(GW_IP, deviceName, network, username, password); +HomeControlMagic hcm(deviceName); EndpointMotion endpointMotion(&hcm); void controlPin() { - bool motion = digitalRead(PIR_PIN); - if(motion != last_motion) - { - last_motion = motion; - #ifdef DEVICE_PIN - if(motion) + bool motion = digitalRead(PIR_PIN); + if(motion != last_motion) { - digitalWrite(DEVICE_PIN, active_pin_state); + last_motion = motion; +#ifdef DEVICE_PIN + if(motion) + { + digitalWrite(DEVICE_PIN, active_pin_state); + } + else + { + digitalWrite(DEVICE_PIN, !active_pin_state); + } +#endif + endpointMotion.setState(motion); } - else - { - digitalWrite(DEVICE_PIN, !active_pin_state); - } - #endif - endpointMotion.setState(motion); - } } void setup() { - pinMode(PIR_PIN, INPUT); - #ifdef DEVICE_PIN - pinMode(DEVICE_PIN, OUTPUT); - #endif - - #ifdef DEBUG - Serial.begin(115200); - Serial.println("Started serial"); - #endif - - network.setReconnectTime(RECONNECTION_TIME); - hcm.addEndpoint(&endpointMotion); +#ifdef DEBUG + Serial.begin(115200); + Serial.println("Started serial"); +#endif + + networkSetSsid(wifi_ssid); + networkSetPass(wifi_pass); + networkSetSecure(true); // this must be called before setServer and networkSetup + networkSetup(); + networkStart(); + + wrapperSetServer(gw_ip); + wrapperSetUsernamePassword(mqtt_username, mqtt_password); + wrapperSetup(); + + hcm.setup(); + + // DO NOT TOUCH ANYTHING BEFORE THIS LINE IN SETUP FUNCTION + + pinMode(PIR_PIN, INPUT); +#ifdef DEVICE_PIN + pinMode(DEVICE_PIN, OUTPUT); +#endif + + hcm.addEndpoint(&endpointMotion); } void loop() { - controlPin(); - hcm.doMagic(); + controlPin(); + hcm.doMagic(); } diff --git a/examples/basic_OnOff/basic_OnOff.ino b/examples/basic_OnOff/basic_OnOff.ino index 9ea64ed..345f7f5 100644 --- a/examples/basic_OnOff/basic_OnOff.ino +++ b/examples/basic_OnOff/basic_OnOff.ino @@ -1,61 +1,74 @@ #include "HomeControlMagic.h" + +// in Config file define ethernet options +#include "arduinoWrapper/ArduinoConfig.h" +#include "arduinoWrapper/ArduinoNetworkInterface.h" +#include "arduinoWrapper/ArduinoWrapper.h" + #include "Endpoints/EndpointOnOff.h" -#define ESP_LOOP -#define SECURE -#define WIFI_SSID "" // Wifi network name -#define WIFI_PASS "" // Wifi password -#include "NetworkLoops.hpp" //#define DEBUG -#define DEVICE_PIN LED_BUILTIN // GPIO pin to use, built in led as example +#define DEVICE_PIN LED_BUILTIN // GPIO pin to use, built in led as example -#define RECONNECTION_TIME 5 // network reconnection time in seconds +IPAddress gw_ip = {192, 168, 1, 10}; +static const char* const deviceName = "ON_OFF_DEVICE"; // name of device +static const char* const wifi_ssid = "WIFI-SSID"; +static const char* const wifi_pass = "WIFI-PASS"; +static char* const mqtt_username = "hc"; // copy username from app +static char* const mqtt_password = ""; // copy password from app -static char* const GW_IP = "GW_IP"; // gateway IP address -static char* const deviceName = "ON_OFF_DEVICE"; // name of device -static char* const username = "hc"; // copy username from app -static char* const password = ""; // copy password from app - -bool active_pin_state = false; // reverse pin state +bool active_pin_state = false; // reverse pin state bool last_state = false; -HomeControlMagic hcm(GW_IP, deviceName, network, username, password); +HomeControlMagic hcm(deviceName); EndpointOnOff endpointOnOff(&hcm); void controlPin() { - bool state = endpointOnOff.getState(); - if(state != last_state) - { - last_state = state; - if(state) - { - digitalWrite(DEVICE_PIN, active_pin_state); - } - else + bool state = endpointOnOff.getState(); + if(state != last_state) { - digitalWrite(DEVICE_PIN, !active_pin_state); + last_state = state; + if(state) + { + digitalWrite(DEVICE_PIN, active_pin_state); + } + else + { + digitalWrite(DEVICE_PIN, !active_pin_state); + } + endpointOnOff.sendFeedbackMessage(); } - endpointOnOff.sendFeedbackMessage(); - } } void setup() { - pinMode(DEVICE_PIN, OUTPUT); +#ifdef DEBUG + Serial.begin(115200); + Serial.println("Started serial"); +#endif + + networkSetSsid(wifi_ssid); + networkSetPass(wifi_pass); + networkSetSecure(true); // this must be called before setServer and networkSetup + networkSetup(); + networkStart(); + + wrapperSetServer(gw_ip); + wrapperSetUsernamePassword(mqtt_username, mqtt_password); + wrapperSetup(); + + hcm.setup(); - #ifdef DEBUG - Serial.begin(115200); - Serial.println("Started serial"); - #endif + // DO NOT TOUCH ANYTHING BEFORE THIS LINE IN SETUP FUNCTION - network.setReconnectTime(RECONNECTION_TIME); - hcm.addEndpoint(&endpointOnOff); + pinMode(DEVICE_PIN, OUTPUT); + hcm.addEndpoint(&endpointOnOff); } void loop() { - controlPin(); - hcm.doMagic(); + controlPin(); + hcm.doMagic(); } diff --git a/examples/basic_OnOff_ethernet/basic_OnOff_ethernet.ino b/examples/basic_OnOff_ethernet/basic_OnOff_ethernet.ino new file mode 100644 index 0000000..6941016 --- /dev/null +++ b/examples/basic_OnOff_ethernet/basic_OnOff_ethernet.ino @@ -0,0 +1,70 @@ +#include "HomeControlMagic.h" + +// in Config file define ethernet options or this will not compile +#include "arduinoWrapper/ArduinoConfig.h" +#include "arduinoWrapper/ArduinoNetworkInterface.h" +#include "arduinoWrapper/ArduinoWrapper.h" + +#include "Endpoints/EndpointOnOff.h" + +#define DEBUG + +#define DEVICE_PIN LED_BUILTIN // GPIO pin to use, built in led as example + +IPAddress gw_ip = {192, 168, 1, 10}; +static const char* const deviceName = "ON_OFF_DEVICE"; // name of device +static char* const mqtt_username = "hc"; // copy username from app +static char* const mqtt_password = ""; // copy password from app + +bool active_pin_state = false; // reverse pin state +bool last_state = false; + +HomeControlMagic hcm(deviceName); +EndpointOnOff endpointOnOff(&hcm); + +void controlPin() +{ + bool state = endpointOnOff.getState(); + if(state != last_state) + { + last_state = state; + if(state) + { + digitalWrite(DEVICE_PIN, active_pin_state); + } + else + { + digitalWrite(DEVICE_PIN, !active_pin_state); + } + endpointOnOff.sendFeedbackMessage(); + } +} + +void setup() +{ +#ifdef DEBUG + Serial.begin(115200); + Serial.println("Started serial"); +#endif + + networkSetSecure(false); // this must be called before setServer and networkSetup + networkSetup(); + networkStart(); + + wrapperSetServer(gw_ip); + wrapperSetUsernamePassword(mqtt_username, mqtt_password); + wrapperSetup(); + + hcm.setup(); + + // DO NOT TOUCH ANYTHING BEFORE THIS LINE IN SETUP FUNCTION + + pinMode(DEVICE_PIN, OUTPUT); + hcm.addEndpoint(&endpointOnOff); +} + +void loop() +{ + controlPin(); + hcm.doMagic(); +} \ No newline at end of file diff --git a/examples/basic_Temperature/basic_Temperature.ino b/examples/basic_Temperature/basic_Temperature.ino index 88aed8b..a44faa6 100644 --- a/examples/basic_Temperature/basic_Temperature.ino +++ b/examples/basic_Temperature/basic_Temperature.ino @@ -1,26 +1,28 @@ #include "HomeControlMagic.h" -#include "Endpoints/EndpointTemperature.h" + +// in Config file define ethernet options +#include "arduinoWrapper/ArduinoConfig.h" +#include "arduinoWrapper/ArduinoNetworkInterface.h" +#include "arduinoWrapper/ArduinoWrapper.h" + #include "DHT.h" -#define ESP_LOOP -#define SECURE -#define WIFI_SSID "" // Wifi network name -#define WIFI_PASS "" // Wifi password -#include "NetworkLoops.hpp" +#include "Endpoints/EndpointTemperature.h" //#define DEBUG -#define DHT_PIN 4 // GPIO pin to use (D2). -#define DHTTYPE DHT22 // DHT type +#define DHT_PIN 4 // GPIO pin to use (D2). +#define DHTTYPE DHT22 // DHT type -#define RECONNECTION_TIME 5 // network reconnection time in seconds -#define READ_TIME 30 // sensor reading time in seconds +#define READ_TIME 30 // sensor reading time in seconds -static char* const GW_IP = "GW_IP"; // gateway IP address -static char* const deviceName = "TEMPERATURE_SENSOR"; // name of device -static char* const username = "hc"; // copy username from app -static char* const password = ""; // copy password from app +IPAddress gw_ip = {192, 168, 1, 10}; +static char* const deviceName = "TEMPERATURE_SENSOR"; // name of device +static const char* const wifi_ssid = "WIFI-SSID"; +static const char* const wifi_pass = "WIFI-PASS"; +static char* const mqtt_username = "hc"; // copy username from app +static char* const mqtt_password = ""; // copy password from app -HomeControlMagic hcm(GW_IP, deviceName, network, username, password); +HomeControlMagic hcm(deviceName); EndpointTemperature endpointTemperature(&hcm); DHT dht(DHT_PIN, DHTTYPE); @@ -28,45 +30,59 @@ DHT dht(DHT_PIN, DHTTYPE); void setup() { #ifdef DEBUG - Serial.begin(115200); - Serial.println("Started serial"); - #endif + Serial.begin(115200); + Serial.println("Started serial"); +#endif + + networkSetSsid(wifi_ssid); + networkSetPass(wifi_pass); + networkSetSecure(true); // this must be called before setServer and networkSetup + networkSetup(); + networkStart(); + + wrapperSetServer(gw_ip); + wrapperSetUsernamePassword(mqtt_username, mqtt_password); + wrapperSetup(); + + hcm.setup(); - network.setReconnectTime(RECONNECTION_TIME); - hcm.addEndpoint(&endpointTemperature); + // DO NOT TOUCH ANYTHING BEFORE THIS LINE IN SETUP FUNCTION - dht.begin(); + hcm.addEndpoint(&endpointTemperature); + + dht.begin(); } void loop() { - static int resend_time; - - if (millis() - resend_time > READ_TIME * 1000) - { - resend_time = millis(); + static int resend_time; - double temperature = dht.readTemperature(); + if(millis() - resend_time > READ_TIME * 1000) + { + resend_time = millis(); - // Check if any reads failed and exit early (to try again). - if (isnan(temperature)) { - #ifdef DEBUG - Serial.println("Failed to read from DHT sensor!"); - #endif - return; - } + double temperature = dht.readTemperature(); - #ifdef DEBUG - Serial.print("Temperature: "); - Serial.print(temperature); - Serial.print(" *C "); - Serial.println(); - #endif + // Check if any reads failed and exit early (to try again). + if(isnan(temperature)) + { +#ifdef DEBUG + Serial.println("Failed to read from DHT sensor!"); +#endif + return; + } - // this example read/set temperature every 30s and update system every 60 - // use enpointTemperature.sendFeedback() if is neccessary to update system after specific event - endpointTemperature.setTemperature(temperature); - } +#ifdef DEBUG + Serial.print("Temperature: "); + Serial.print(temperature); + Serial.print(" *C "); + Serial.println(); +#endif + + // this example read/set temperature every 30s and update system every 60 + // use enpointTemperature.sendFeedback() if is neccessary to update system after specific event + endpointTemperature.setTemperature(temperature); + } - hcm.doMagic(); + hcm.doMagic(); } diff --git a/examples/basic_TemperatureTarget_OnOff/basic_TemperatureTarget_OnOff.ino b/examples/basic_TemperatureTarget_OnOff/basic_TemperatureTarget_OnOff.ino index 5f7a0fb..483654a 100644 --- a/examples/basic_TemperatureTarget_OnOff/basic_TemperatureTarget_OnOff.ino +++ b/examples/basic_TemperatureTarget_OnOff/basic_TemperatureTarget_OnOff.ino @@ -1,31 +1,34 @@ #include "HomeControlMagic.h" -#include "Endpoints/EndpointTemperatureTarget.h" -#include "Endpoints/EndpointOnOff.h" + +// in Config file define ethernet options +#include "arduinoWrapper/ArduinoConfig.h" +#include "arduinoWrapper/ArduinoNetworkInterface.h" +#include "arduinoWrapper/ArduinoWrapper.h" + #include "DHT.h" -#define ESP_LOOP -#define SECURE -#define WIFI_SSID "" // Wifi network name -#define WIFI_PASS "" // Wifi password -#include "NetworkLoops.hpp" +#include "Endpoints/EndpointOnOff.h" +#include "Endpoints/EndpointTemperatureTarget.h" //#define DEBUG -#define DHT_PIN 4 // GPIO pin to use as example -#define DHTTYPE DHT22 // DHT type -#define DEVICE_PIN LED_BUILTIN // connected heater or cooler device, built in led as example +#define DHT_PIN 4 // GPIO pin to use as example +#define DHTTYPE DHT22 // DHT type +#define DEVICE_PIN LED_BUILTIN // connected heater or cooler device, built in led as example -#define RECONNECTION_TIME 5 // network reconnection time in seconds -#define READ_TIME 30 // sensor reading time in seconds +#define RECONNECTION_TIME 5 // network reconnection time in seconds +#define READ_TIME 30 // sensor reading time in seconds -static char* const GW_IP = "GW_IP"; // gateway IP address -static char* const deviceName = "TERMOSTAT"; // name of device -static char* const username = "hc"; // copy username from app -static char* const password = ""; // copy password from app +IPAddress gw_ip = {192, 168, 1, 10}; +static char* const deviceName = "TERMOSTAT"; // name of device +static const char* const wifi_ssid = "WIFI-SSID"; +static const char* const wifi_pass = "WIFI-PASS"; +static char* const mqtt_username = "hc"; // copy username from app +static char* const mqtt_password = ""; // copy password from app -bool active_pin_state = false; // reverse pin state +bool active_pin_state = false; // reverse pin state bool last_state = false; -HomeControlMagic hcm(GW_IP, deviceName, network, username, password); +HomeControlMagic hcm(deviceName); EndpointTemperatureTarget endpointTemperatureTarget(&hcm); EndpointOnOff endpointOnOff(&hcm); @@ -34,77 +37,90 @@ DHT dht(DHT_PIN, DHTTYPE); void controlPin() { - bool state = endpointOnOff.getState(); - if(state != last_state) - { - last_state = state; - if(state) - { - digitalWrite(DEVICE_PIN, active_pin_state); - } - else + bool state = endpointOnOff.getState(); + if(state != last_state) { - digitalWrite(DEVICE_PIN, !active_pin_state); + last_state = state; + if(state) + { + digitalWrite(DEVICE_PIN, active_pin_state); + } + else + { + digitalWrite(DEVICE_PIN, !active_pin_state); + } + endpointOnOff.sendFeedbackMessage(); } - endpointOnOff.sendFeedbackMessage(); - } } void setup() { - pinMode(DEVICE_PIN, OUTPUT); +#ifdef DEBUG + Serial.begin(115200); + Serial.println("Started serial"); +#endif - network.setReconnectTime(RECONNECTION_TIME); + networkSetSsid(wifi_ssid); + networkSetPass(wifi_pass); + networkSetSecure(true); // this must be called before setServer and networkSetup + networkSetup(); + networkStart(); - double temperature = dht.readTemperature(); - endpointTemperatureTarget.setTemperatureTarget(temperature); + wrapperSetServer(gw_ip); + wrapperSetUsernamePassword(mqtt_username, mqtt_password); + wrapperSetup(); - #ifdef DEBUG - Serial.begin(115200); - Serial.println("Started serial"); - #endif + hcm.setup(); - hcm.addEndpoint(&endpointTemperatureTarget); - hcm.addEndpoint(&endpointOnOff); + // DO NOT TOUCH ANYTHING BEFORE THIS LINE IN SETUP FUNCTION - dht.begin(); -} + pinMode(DEVICE_PIN, OUTPUT); -void loop() -{ - static int read_time; + hcm.addEndpoint(&endpointTemperatureTarget); + hcm.addEndpoint(&endpointOnOff); - if (millis() - read_time > READ_TIME * 1000) - { - read_time = millis(); + dht.begin(); double temperature = dht.readTemperature(); + endpointTemperatureTarget.setTemperatureTarget(temperature); +} - // Check if any reads failed and exit early (to try again). - if (isnan(temperature)) { - #ifdef DEBUG - Serial.println("Failed to read from DHT sensor!"); - #endif - return; - } - else +void loop() +{ + static int read_time; + + if(millis() - read_time > READ_TIME * 1000) { - endpointTemperatureTarget.setTemperature(temperature); + read_time = millis(); + + double temperature = dht.readTemperature(); + + // Check if any reads failed and exit early (to try again). + if(isnan(temperature)) + { +#ifdef DEBUG + Serial.println("Failed to read from DHT sensor!"); +#endif + return; + } + else + { + endpointTemperatureTarget.setTemperature(temperature); + } + +#ifdef DEBUG + Serial.print("Temperature from sensor: "); + Serial.print(endpointTemperatureTarget.getTemperature()); + Serial.print(" *C "); + Serial.println(); + + Serial.print("Temperature target: "); + Serial.print(endpointTemperatureTarget.getTemperatureTarget()); + Serial.print(" *C "); + Serial.println(); +#endif } - #ifdef DEBUG - Serial.print("Temperature from sensor: "); - Serial.print(endpointTemperatureTarget.getTemperature()); - Serial.print(" *C "); - Serial.println(); - - Serial.print("Temperature target: "); - Serial.print(endpointTemperatureTarget.getTemperatureTarget()); - Serial.print(" *C "); - Serial.println(); - #endif - } - - controlPin(); - hcm.doMagic(); + controlPin(); + hcm.doMagic(); } diff --git a/examples/basic_multipleEndpoints/basic_multipleEndpoints.ino b/examples/basic_multipleEndpoints/basic_multipleEndpoints.ino index 41f4d63..ec14341 100644 --- a/examples/basic_multipleEndpoints/basic_multipleEndpoints.ino +++ b/examples/basic_multipleEndpoints/basic_multipleEndpoints.ino @@ -1,29 +1,30 @@ #include "HomeControlMagic.h" -#include "Endpoints/EndpointTemperature.h" + +// in Config file define ethernet options +#include "arduinoWrapper/ArduinoConfig.h" + #include "DHT.h" -#define ESP_LOOP -#define SECURE -#define WIFI_SSID "" // Wifi network name -#define WIFI_PASS "" // Wifi password -#include "NetworkLoops.hpp" +#include "Endpoints/EndpointTemperature.h" +#include "arduinoWrapper/ArduinoNetworkInterface.h" +#include "arduinoWrapper/ArduinoWrapper.h" #define DEBUG -#define DHT_1_PIN 4 // GPIO pin -#define DHT_2_PIN 5 // GPIO pin -#define DHT_3_PIN 12 // GPIO pin -#define DHT_4_PIN 14 // GPIO pin - -#define DHTTYPE DHT22 // DHT type +#define DHT_1_PIN 4 // GPIO pin +#define DHT_2_PIN 5 // GPIO pin +#define DHT_3_PIN 12 // GPIO pin +#define DHT_4_PIN 14 // GPIO pin -#define RECONNECTION_TIME 5 // network reconnection time in seconds +#define DHTTYPE DHT22 // DHT type -static char* const GW_IP = "GW_IP"; // gateway IP address -static char* const deviceName = "TEMPERATURE_SENSORS"; // name of device -static char* const username = "hc"; // copy username from app -static char* const password = ""; // copy password from app +IPAddress gw_ip = {192, 168, 1, 10}; +static char* const deviceName = "TEMPERATURE_SENSORS"; // name of device +static const char* const wifi_ssid = "WIFI-SSID"; +static const char* const wifi_pass = "WIFI-PASS"; +static char* const mqtt_username = "hc"; // copy username from app +static char* const mqtt_password = ""; // copy password from app -HomeControlMagic hcm(GW_IP, deviceName, network, username, password); +HomeControlMagic hcm(deviceName); EndpointTemperature endpointTemperature_1(&hcm); EndpointTemperature endpointTemperature_2(&hcm); @@ -37,53 +38,67 @@ DHT dht_4(DHT_4_PIN, DHTTYPE); void setup() { - #ifdef DEBUG - Serial.begin(115200); - Serial.println("Started serial"); - #endif - - endpointTemperature_1.setEndpointName("NAME_1"); - endpointTemperature_2.setEndpointName("NAME_2"); - endpointTemperature_3.setEndpointName("NAME_3"); - endpointTemperature_4.setEndpointName("NAME_4"); - - network.setReconnectTime(RECONNECTION_TIME); - hcm.addEndpoint(&endpointTemperature_1); - hcm.addEndpoint(&endpointTemperature_2); - hcm.addEndpoint(&endpointTemperature_3); - hcm.addEndpoint(&endpointTemperature_4); - - dht_1.begin(); - dht_2.begin(); - dht_3.begin(); - dht_4.begin(); +#ifdef DEBUG + Serial.begin(115200); + Serial.println("Started serial"); +#endif + + networkSetSsid(wifi_ssid); + networkSetPass(wifi_pass); + networkSetSecure(true); // this must be called before setServer and networkSetup + networkSetup(); + networkStart(); + + wrapperSetServer(gw_ip); + wrapperSetUsernamePassword(mqtt_username, mqtt_password); + wrapperSetup(); + + hcm.setup(); + + // DO NOT TOUCH ANYTHING BEFORE THIS LINE IN SETUP FUNCTION + + endpointTemperature_1.setEndpointName("NAME_1"); + endpointTemperature_2.setEndpointName("NAME_2"); + endpointTemperature_3.setEndpointName("NAME_3"); + endpointTemperature_4.setEndpointName("NAME_4"); + + hcm.addEndpoint(&endpointTemperature_1); + hcm.addEndpoint(&endpointTemperature_2); + hcm.addEndpoint(&endpointTemperature_3); + hcm.addEndpoint(&endpointTemperature_4); + + dht_1.begin(); + dht_2.begin(); + dht_3.begin(); + dht_4.begin(); } void loop() { - static int resend_time; - - if (millis() - resend_time > 60000) - { - resend_time = millis(); - - double temp_1 = dht_1.readTemperature(); - double temp_2 = dht_2.readTemperature(); - double temp_3 = dht_3.readTemperature(); - double temp_4 = dht_4.readTemperature(); - - if (isnan(temp_1) || isnan(temp_2) || isnan(temp_3) || isnan(temp_4)) { - #ifdef DEBUG - Serial.println("Failed to read from DHT sensor!"); - #endif - return; + static int resend_time; + + if(millis() - resend_time > 60000) + { + resend_time = millis(); + + double temp_1 = dht_1.readTemperature(); + double temp_2 = dht_2.readTemperature(); + double temp_3 = dht_3.readTemperature(); + double temp_4 = dht_4.readTemperature(); + + if(isnan(temp_1) || isnan(temp_2) || isnan(temp_3) || isnan(temp_4)) + { +#ifdef DEBUG + Serial.println("Failed to read from DHT sensor!"); +#endif + return; + } + + endpointTemperature_1.setTemperature(temp_1); + endpointTemperature_2.setTemperature(temp_2); + endpointTemperature_3.setTemperature(temp_3); + endpointTemperature_4.setTemperature(temp_4); } - endpointTemperature_1.setTemperature(temp_1); - endpointTemperature_2.setTemperature(temp_2); - endpointTemperature_3.setTemperature(temp_3); - endpointTemperature_4.setTemperature(temp_4); - } - - hcm.doMagic(); + hcm.doMagic(); } diff --git a/library.json b/library.json index 40ce406..7d9705d 100644 --- a/library.json +++ b/library.json @@ -14,9 +14,5 @@ ], "dependencies": [ - { - "name": "NetworkLoops", - "version": "https://github.com/seky2205/NetworkLoops#0.1" - } ] } diff --git a/src/Endpoint.cpp b/src/Endpoint.cpp index e89a1a7..0451ccb 100644 --- a/src/Endpoint.cpp +++ b/src/Endpoint.cpp @@ -8,11 +8,10 @@ Endpoint::Endpoint(HomeControlMagic* hcm_ptr) { } - void Endpoint::setId(char* id) { uint8_t i = 0; - while (*(id + i) != '\0') + while(*(id + i) != '\0') { *(m_id + i) = *(id + i); i++; @@ -22,7 +21,7 @@ void Endpoint::setId(char* id) void Endpoint::setStatusTime(int status_time) { // not allowed to set report status time under 2 seconds - if (status_time < MIN_STATUS_TIME) + if(status_time < MIN_STATUS_TIME) { m_resend_status_time = MIN_STATUS_TIME; } diff --git a/src/Endpoint.h b/src/Endpoint.h index d45daec..604d8e5 100644 --- a/src/Endpoint.h +++ b/src/Endpoint.h @@ -1,6 +1,8 @@ #pragma once +#ifdef ARDUINO #include "Arduino.h" +#endif #define MIN_STATUS_TIME 2 class HomeControlMagic; @@ -8,27 +10,26 @@ class HomeControlMagic; class Endpoint { public: - Endpoint(HomeControlMagic* hcm_ptr); + Endpoint(HomeControlMagic* hcm_ptr); - void setStatusTime(int status_time); - void setId(char* id); + void setStatusTime(int status_time); + void setId(char* id); - void setEndpointName(char* name_endpoint); - char* getEndpointName(); + void setEndpointName(char* name_endpoint); + char* getEndpointName(); - virtual void sendConfig(); + virtual void sendConfig(); - virtual void sendStatusMessage() = 0; - virtual void sendFeedbackMessage() = 0; + virtual void sendStatusMessage() = 0; + virtual void sendFeedbackMessage() = 0; - virtual void incomingMessage(char* topic, byte* payload, unsigned int length) = 0; + virtual void incomingMessage(char* topic, uint8_t* payload, unsigned int length) = 0; protected: - HomeControlMagic* m_owner; - char m_id[4] = {0}; - long m_last_send_time; - uint8_t m_resend_status_time; - char* m_endpoint_name = nullptr; - char* m_config = nullptr; + HomeControlMagic* m_owner; + char m_id[4] = {0}; + long m_last_send_time; + uint8_t m_resend_status_time; + char* m_endpoint_name = nullptr; + char* m_config = nullptr; }; - diff --git a/src/Endpoints/EndpointColor.cpp b/src/Endpoints/EndpointColor.cpp index b8e3301..977b72b 100644 --- a/src/Endpoints/EndpointColor.cpp +++ b/src/Endpoints/EndpointColor.cpp @@ -7,136 +7,136 @@ static char* const CONFIG = "col"; EndpointColor::EndpointColor(HomeControlMagic* hcm_ptr) - : Endpoint(hcm_ptr) - , m_level(0) - , m_state(false) + : Endpoint(hcm_ptr) + , m_level(0) + , m_state(false) { - m_last_send_time = millis(); - m_resend_status_time = 30; - m_config = CONFIG; + m_last_send_time = millis(); + m_resend_status_time = 30; + m_config = CONFIG; } void EndpointColor::setState(bool state) { - m_state = state; - m_owner->sendMessage("sp", m_state, m_id); + m_state = state; + m_owner->sendMessage("sp", m_state, m_id); } bool EndpointColor::getState() { - return m_state; + return m_state; } void EndpointColor::setLevel(uint16_t level) { - m_level = level; - m_owner->sendMessage("sl", m_level, m_id); + m_level = level; + m_owner->sendMessage("sl", m_level, m_id); } uint16_t EndpointColor::getLevel() { - return m_level; + return m_level; } int EndpointColor::getColorR() { - return m_rgb.r; + return m_rgb.r; } int EndpointColor::getColorG() { - return m_rgb.g; + return m_rgb.g; } int EndpointColor::getColorB() { - return m_rgb.b; + return m_rgb.b; } void EndpointColor::getRGBcharPtr(char* buffer) { - char buff[6]; + char buff[6]; - itoa(m_rgb.r, buff, 10); - strcat(buffer, buff); - strcat(buffer, ";"); + itoa(m_rgb.r, buff, 10); + strcat(buffer, buff); + strcat(buffer, ";"); - itoa(m_rgb.g, buff, 10); - strcat(buffer, buff); - strcat(buffer, ";"); + itoa(m_rgb.g, buff, 10); + strcat(buffer, buff); + strcat(buffer, ";"); - itoa(m_rgb.b, buff, 10); - strcat(buffer, buff); + itoa(m_rgb.b, buff, 10); + strcat(buffer, buff); } -void EndpointColor::incomingMessage(char* topic, byte* payload, unsigned int length) +void EndpointColor::incomingMessage(char* topic, uint8_t* payload, unsigned int length) { - #ifdef ENDPOINT_COLOR_DEBUG - Serial.println(F("incoming message, EndpointColor")); - - for(int i=0; i< length; i++) - { - Serial.print((char)payload[i]); - } - Serial.println(); - #endif - - if(lineContains(topic, "cl")) - { - m_level = extractInteger(payload, length); - } - - else if(lineContains(topic, "sl")) - { - m_owner->sendMessage("sl", m_level, m_id); - } +#ifdef ENDPOINT_COLOR_DEBUG + Serial.println(F("incoming message, EndpointColor")); - else if(lineContains(topic, "cp")) - { - m_state = extractBool(payload, length); - } + for(int i = 0; i < length; i++) + { + Serial.print((char)payload[i]); + } + Serial.println(); +#endif - else if(lineContains(topic, "sp")) - { - m_owner->sendMessage("sp", m_state, m_id); - } + if(lineContains(topic, "cl")) + { + m_level = extractInteger(payload, length); + } - else if(lineContains(topic, "cc")) - { - m_rgb = extractRGB(payload, length); - } + else if(lineContains(topic, "sl")) + { + m_owner->sendMessage("sl", m_level, m_id); + } - else if(lineContains(topic, "sc")) - { - getRGBcharPtr(m_owner->getMessageBufferPtr()); - m_owner->sendStringMessage("sc", m_id); - } + else if(lineContains(topic, "cp")) + { + m_state = extractBool(payload, length); + } + + else if(lineContains(topic, "sp")) + { + m_owner->sendMessage("sp", m_state, m_id); + } + + else if(lineContains(topic, "cc")) + { + m_rgb = extractRGB(payload, length); + } + + else if(lineContains(topic, "sc")) + { + getRGBcharPtr(m_owner->getMessageBufferPtr()); + m_owner->sendStringMessage("sc", m_id); + } } void EndpointColor::sendStatusMessage() { - if (millis() - m_last_send_time > m_resend_status_time * 1000) + if(millis() - m_last_send_time > m_resend_status_time * 1000) { - m_last_send_time = millis(); - #ifdef ENDPOINT_COLOR_DEBUG + m_last_send_time = millis(); +#ifdef ENDPOINT_COLOR_DEBUG Serial.println(F("sending status message, EndpointColor")); - #endif +#endif - m_owner->sendMessage("sp", m_state, m_id); - m_owner->sendMessage("sl", m_level, m_id); - getRGBcharPtr(m_owner->getMessageBufferPtr()); - m_owner->sendStringMessage("sc", m_id); + m_owner->sendMessage("sp", m_state, m_id); + m_owner->sendMessage("sl", m_level, m_id); + getRGBcharPtr(m_owner->getMessageBufferPtr()); + m_owner->sendStringMessage("sc", m_id); } } void EndpointColor::sendFeedbackMessage() { - #ifdef ENDPOINT_COLOR_DEBUG - Serial.println(F("sending feedback message, EndpointColor")); - #endif - - m_owner->sendMessage("sp", m_state, m_id); - m_owner->sendMessage("sl", m_level, m_id); - getRGBcharPtr(m_owner->getMessageBufferPtr()); - m_owner->sendStringMessage("sc", m_id); +#ifdef ENDPOINT_COLOR_DEBUG + Serial.println(F("sending feedback message, EndpointColor")); +#endif + + m_owner->sendMessage("sp", m_state, m_id); + m_owner->sendMessage("sl", m_level, m_id); + getRGBcharPtr(m_owner->getMessageBufferPtr()); + m_owner->sendStringMessage("sc", m_id); } \ No newline at end of file diff --git a/src/Endpoints/EndpointColor.h b/src/Endpoints/EndpointColor.h index 686b151..0c48a1b 100644 --- a/src/Endpoints/EndpointColor.h +++ b/src/Endpoints/EndpointColor.h @@ -5,13 +5,13 @@ class EndpointColor : public Endpoint { - public: +public: EndpointColor(HomeControlMagic* hcm_ptr); virtual void sendStatusMessage(); virtual void sendFeedbackMessage(); - virtual void incomingMessage(char* topic, byte* payload, unsigned int length); + virtual void incomingMessage(char* topic, uint8_t* payload, unsigned int length); virtual void setState(bool state); virtual bool getState(); @@ -25,7 +25,7 @@ class EndpointColor : public Endpoint virtual void getRGBcharPtr(char* buffer); - protected: +protected: struct RGB m_rgb; bool m_state; uint16_t m_level; diff --git a/src/Endpoints/EndpointIdentify.cpp b/src/Endpoints/EndpointIdentify.cpp index f607020..d79abfb 100644 --- a/src/Endpoints/EndpointIdentify.cpp +++ b/src/Endpoints/EndpointIdentify.cpp @@ -7,29 +7,29 @@ static char* const CONFIG = "id"; EndpointIdentify::EndpointIdentify(HomeControlMagic* hcm_ptr, int8_t pin) - : Endpoint(hcm_ptr) - , m_pin(pin) + : Endpoint(hcm_ptr) + , m_pin(pin) { - pinMode(m_pin, OUTPUT); - m_config = CONFIG; + pinMode(m_pin, OUTPUT); + m_config = CONFIG; } -void EndpointIdentify::incomingMessage(char* topic, byte* payload, unsigned int length) +void EndpointIdentify::incomingMessage(char* topic, uint8_t* payload, unsigned int length) { - #ifdef ENDPOINT_IDENTIFY_DEBUG - Serial.println(F("incoming message, EndpointIdentify")); +#ifdef ENDPOINT_IDENTIFY_DEBUG + Serial.println(F("incoming message, EndpointIdentify")); - for(int i=0; i< length; i++) - { - Serial.print((char)payload[i]); - } - Serial.println(); - #endif + for(int i = 0; i < length; i++) + { + Serial.print((char)payload[i]); + } + Serial.println(); +#endif - if(lineContains(topic, "ci")) - { - bool state = extractBool(payload, length); - digitalWrite(m_pin, state); - digitalWrite(m_pin, !state); - } + if(lineContains(topic, "ci")) + { + bool state = extractBool(payload, length); + digitalWrite(m_pin, state); + digitalWrite(m_pin, !state); + } } \ No newline at end of file diff --git a/src/Endpoints/EndpointIdentify.h b/src/Endpoints/EndpointIdentify.h index 0596054..0583aa6 100644 --- a/src/Endpoints/EndpointIdentify.h +++ b/src/Endpoints/EndpointIdentify.h @@ -4,11 +4,11 @@ class EndpointIdentify : public Endpoint { - public: +public: EndpointIdentify(HomeControlMagic* hcm_ptr, int8_t pin); - void incomingMessage(char* topic, byte* payload, unsigned int length); + void incomingMessage(char* topic, uint8_t* payload, unsigned int length); - private: +private: uint8_t m_pin; }; diff --git a/src/Endpoints/EndpointLevel.cpp b/src/Endpoints/EndpointLevel.cpp index f6be9e2..51851ed 100644 --- a/src/Endpoints/EndpointLevel.cpp +++ b/src/Endpoints/EndpointLevel.cpp @@ -7,90 +7,90 @@ static char* const CONFIG = "lev"; EndpointLevel::EndpointLevel(HomeControlMagic* hcm_ptr) - : Endpoint(hcm_ptr) - , m_level(0) - , m_state(false) + : Endpoint(hcm_ptr) + , m_level(0) + , m_state(false) { - m_last_send_time = millis(); - m_resend_status_time = 30; - m_config = CONFIG; + m_last_send_time = millis(); + m_resend_status_time = 30; + m_config = CONFIG; } void EndpointLevel::setState(bool state) { - m_state = state; - m_owner->sendMessage("sp", m_state, m_id); + m_state = state; + m_owner->sendMessage("sp", m_state, m_id); } bool EndpointLevel::getState() { - return m_state; + return m_state; } void EndpointLevel::setLevel(uint16_t level) { - m_level = level; - m_owner->sendMessage("sl", m_level, m_id); + m_level = level; + m_owner->sendMessage("sl", m_level, m_id); } uint16_t EndpointLevel::getLevel() { - return m_level; + return m_level; } -void EndpointLevel::incomingMessage(char* topic, byte* payload, unsigned int length) +void EndpointLevel::incomingMessage(char* topic, uint8_t* payload, unsigned int length) { - #ifdef ENDPOINT_LEVEL_DEBUG - Serial.println(F("incoming message, EndpointLevel")); - - for(int i=0; i< length; i++) - { - Serial.print((char)payload[i]); - } - Serial.println(); - #endif - - if(lineContains(topic, "cl")) - { - m_level = extractInteger(payload, length); - } - - else if(lineContains(topic, "sl")) - { - m_owner->sendMessage("sl", m_level, m_id); - } +#ifdef ENDPOINT_LEVEL_DEBUG + Serial.println(F("incoming message, EndpointLevel")); + + for(int i = 0; i < length; i++) + { + Serial.print((char)payload[i]); + } + Serial.println(); +#endif - else if(lineContains(topic, "cp")) - { - m_state = extractBool(payload, length); - } + if(lineContains(topic, "cl")) + { + m_level = extractInteger(payload, length); + } - else if(lineContains(topic, "sp")) - { - m_owner->sendMessage("sp", m_state, m_id); - } + else if(lineContains(topic, "sl")) + { + m_owner->sendMessage("sl", m_level, m_id); + } + + else if(lineContains(topic, "cp")) + { + m_state = extractBool(payload, length); + } + + else if(lineContains(topic, "sp")) + { + m_owner->sendMessage("sp", m_state, m_id); + } } void EndpointLevel::sendStatusMessage() { - if (millis() - m_last_send_time > m_resend_status_time * 1000) + if(millis() - m_last_send_time > m_resend_status_time * 1000) { - m_last_send_time = millis(); - #ifdef ENDPOINT_LEVEL_DEBUG + m_last_send_time = millis(); +#ifdef ENDPOINT_LEVEL_DEBUG Serial.println(F("sending status message, EndpointLevel")); - #endif +#endif - m_owner->sendMessage("sp", m_state, m_id); - m_owner->sendMessage("sl", m_level, m_id); + m_owner->sendMessage("sp", m_state, m_id); + m_owner->sendMessage("sl", m_level, m_id); } } void EndpointLevel::sendFeedbackMessage() { - #ifdef ENDPOINT_LEVEL_DEBUG - Serial.println(F("sending feedback message, EndpointLevel")); - #endif +#ifdef ENDPOINT_LEVEL_DEBUG + Serial.println(F("sending feedback message, EndpointLevel")); +#endif - m_owner->sendMessage("sp", m_state, m_id); - m_owner->sendMessage("sl", m_level, m_id); + m_owner->sendMessage("sp", m_state, m_id); + m_owner->sendMessage("sl", m_level, m_id); } \ No newline at end of file diff --git a/src/Endpoints/EndpointLevel.h b/src/Endpoints/EndpointLevel.h index 3a1e203..86ca5e8 100644 --- a/src/Endpoints/EndpointLevel.h +++ b/src/Endpoints/EndpointLevel.h @@ -4,13 +4,13 @@ class EndpointLevel : public Endpoint { - public: +public: EndpointLevel(HomeControlMagic* hcm_ptr); virtual void sendStatusMessage(); virtual void sendFeedbackMessage(); - virtual void incomingMessage(char* topic, byte* payload, unsigned int length); + virtual void incomingMessage(char* topic, uint8_t* payload, unsigned int length); virtual void setState(bool state); virtual bool getState(); @@ -18,7 +18,7 @@ class EndpointLevel : public Endpoint virtual void setLevel(uint16_t state); virtual uint16_t getLevel(); - protected: +protected: uint16_t m_level; bool m_state; }; \ No newline at end of file diff --git a/src/Endpoints/EndpointMotion.cpp b/src/Endpoints/EndpointMotion.cpp index 5b22603..1b15afa 100644 --- a/src/Endpoints/EndpointMotion.cpp +++ b/src/Endpoints/EndpointMotion.cpp @@ -7,60 +7,60 @@ static char* const CONFIG = "mot"; EndpointMotion::EndpointMotion(HomeControlMagic* hcm_ptr) - : Endpoint(hcm_ptr) - , m_state(false) + : Endpoint(hcm_ptr) + , m_state(false) { - m_last_send_time = millis(); - m_resend_status_time = 60; - m_config = CONFIG; + m_last_send_time = millis(); + m_resend_status_time = 60; + m_config = CONFIG; } void EndpointMotion::setState(bool state) { - m_state = state; - m_owner->sendMessage("sm", m_state, m_id); + m_state = state; + m_owner->sendMessage("sm", m_state, m_id); } bool EndpointMotion::getState() { - return m_state; + return m_state; } -void EndpointMotion::incomingMessage(char* topic, byte* payload, unsigned int length) +void EndpointMotion::incomingMessage(char* topic, uint8_t* payload, unsigned int length) { - #ifdef ENDPOINT_MOTION_DEBUG - Serial.println(F("incoming message, EndpointMotion")); - for(int i=0; i< length; i++) - { - Serial.print((char)payload[i]); - } - Serial.println(); - #endif +#ifdef ENDPOINT_MOTION_DEBUG + Serial.println(F("incoming message, EndpointMotion")); + for(int i = 0; i < length; i++) + { + Serial.print((char)payload[i]); + } + Serial.println(); +#endif - if(lineContains(topic, "sm")) - { - m_owner->sendMessage("sm", m_state, m_id); - } + if(lineContains(topic, "sm")) + { + m_owner->sendMessage("sm", m_state, m_id); + } } void EndpointMotion::sendStatusMessage() { - if (millis() - m_last_send_time > m_resend_status_time * 1000) + if(millis() - m_last_send_time > m_resend_status_time * 1000) { - m_last_send_time = millis(); - #ifdef ENDPOINT_MOTION_DEBUG + m_last_send_time = millis(); +#ifdef ENDPOINT_MOTION_DEBUG Serial.println(F("sending status message, EndpointMotion")); - #endif +#endif - m_owner->sendMessage("sm", m_state, m_id); + m_owner->sendMessage("sm", m_state, m_id); } } void EndpointMotion::sendFeedbackMessage() { - #ifdef ENDPOINT_MOTION_DEBUG - Serial.println(F("sending feedback message, EndpointMotion")); - #endif +#ifdef ENDPOINT_MOTION_DEBUG + Serial.println(F("sending feedback message, EndpointMotion")); +#endif - m_owner->sendMessage("sm", m_state, m_id); + m_owner->sendMessage("sm", m_state, m_id); } \ No newline at end of file diff --git a/src/Endpoints/EndpointMotion.h b/src/Endpoints/EndpointMotion.h index cf4cfb0..3727412 100644 --- a/src/Endpoints/EndpointMotion.h +++ b/src/Endpoints/EndpointMotion.h @@ -4,17 +4,17 @@ class EndpointMotion : public Endpoint { - public: +public: EndpointMotion(HomeControlMagic* hcm_ptr); virtual void sendStatusMessage(); virtual void sendFeedbackMessage(); - virtual void incomingMessage(char* topic, byte* payload, unsigned int length); + virtual void incomingMessage(char* topic, uint8_t* payload, unsigned int length); virtual void setState(bool state); virtual bool getState(); - protected: +protected: bool m_state; }; diff --git a/src/Endpoints/EndpointOnOff.cpp b/src/Endpoints/EndpointOnOff.cpp index 135c2a7..2b89e48 100644 --- a/src/Endpoints/EndpointOnOff.cpp +++ b/src/Endpoints/EndpointOnOff.cpp @@ -7,64 +7,64 @@ static char* const CONFIG = "pwr"; EndpointOnOff::EndpointOnOff(HomeControlMagic* hcm_ptr) - : Endpoint(hcm_ptr) - , m_state(false) + : Endpoint(hcm_ptr) + , m_state(false) { - m_last_send_time = millis(); - m_resend_status_time = 30; - m_config = CONFIG; + m_last_send_time = millis(); + m_resend_status_time = 30; + m_config = CONFIG; } void EndpointOnOff::setState(bool state) { - m_state = state; - m_owner->sendMessage("sp", m_state, m_id); + m_state = state; + m_owner->sendMessage("sp", m_state, m_id); } bool EndpointOnOff::getState() { - return m_state; + return m_state; } -void EndpointOnOff::incomingMessage(char* topic, byte* payload, unsigned int length) +void EndpointOnOff::incomingMessage(char* topic, uint8_t* payload, unsigned int length) { - #ifdef ENDPOINT_ON_OFF_DEBUG - Serial.println(F("incoming message, EndpointOnOff")); - for(int i=0; i< length; i++) - { - Serial.print((char)payload[i]); - } - Serial.println(); - #endif +#ifdef ENDPOINT_ON_OFF_DEBUG + Serial.println(F("incoming message, EndpointOnOff")); + for(int i = 0; i < length; i++) + { + Serial.print((char)payload[i]); + } + Serial.println(); +#endif - if(lineContains(topic, "cp")) - { - m_state = extractBool(payload, length); - } - else if(lineContains(topic, "sp")) - { - m_owner->sendMessage("sp", m_state, m_id); - } + if(lineContains(topic, "cp")) + { + m_state = extractBool(payload, length); + } + else if(lineContains(topic, "sp")) + { + m_owner->sendMessage("sp", m_state, m_id); + } } void EndpointOnOff::sendStatusMessage() { - if (millis() - m_last_send_time > m_resend_status_time * 1000) + if(millis() - m_last_send_time > m_resend_status_time * 1000) { - m_last_send_time = millis(); - #ifdef ENDPOINT_ON_OFF_DEBUG + m_last_send_time = millis(); +#ifdef ENDPOINT_ON_OFF_DEBUG Serial.println(F("sending status message, EndpointOnOff")); - #endif +#endif - m_owner->sendMessage("sp", m_state, m_id); + m_owner->sendMessage("sp", m_state, m_id); } } void EndpointOnOff::sendFeedbackMessage() { - #ifdef ENDPOINT_ON_OFF_DEBUG - Serial.println(F("sending feedback message, EndpointOnOff")); - #endif +#ifdef ENDPOINT_ON_OFF_DEBUG + Serial.println(F("sending feedback message, EndpointOnOff")); +#endif - m_owner->sendMessage("sp", m_state, m_id); + m_owner->sendMessage("sp", m_state, m_id); } \ No newline at end of file diff --git a/src/Endpoints/EndpointOnOff.h b/src/Endpoints/EndpointOnOff.h index 55619e1..8333a51 100644 --- a/src/Endpoints/EndpointOnOff.h +++ b/src/Endpoints/EndpointOnOff.h @@ -4,17 +4,17 @@ class EndpointOnOff : public Endpoint { - public: +public: EndpointOnOff(HomeControlMagic* hcm_ptr); virtual void sendStatusMessage(); virtual void sendFeedbackMessage(); - virtual void incomingMessage(char* topic, byte* payload, unsigned int length); + virtual void incomingMessage(char* topic, uint8_t* payload, unsigned int length); virtual void setState(bool state); virtual bool getState(); - protected: +protected: bool m_state; }; diff --git a/src/Endpoints/EndpointTemperature.cpp b/src/Endpoints/EndpointTemperature.cpp index 3f196f8..81b83af 100644 --- a/src/Endpoints/EndpointTemperature.cpp +++ b/src/Endpoints/EndpointTemperature.cpp @@ -7,61 +7,61 @@ static char* const CONFIG = "tmp"; EndpointTemperature::EndpointTemperature(HomeControlMagic* hcm_ptr) - : Endpoint(hcm_ptr) - , m_temperature(0) + : Endpoint(hcm_ptr) + , m_temperature(0) { - m_last_send_time = millis(); - m_resend_status_time = 60; - m_config = CONFIG; + m_last_send_time = millis(); + m_resend_status_time = 60; + m_config = CONFIG; } void EndpointTemperature::setTemperature(double temperature) { - m_temperature = temperature; - m_owner->sendMessage("st", m_temperature, m_id); + m_temperature = temperature; + m_owner->sendMessage("st", m_temperature, m_id); } double EndpointTemperature::getTemperature() { - return m_temperature; + return m_temperature; } -void EndpointTemperature::incomingMessage(char* topic, byte* payload, unsigned int length) +void EndpointTemperature::incomingMessage(char* topic, uint8_t* payload, unsigned int length) { - #ifdef ENDPOINT_TEMPERATURE_DEBUG - Serial.println(F("incoming message, EndpointTemperature")); +#ifdef ENDPOINT_TEMPERATURE_DEBUG + Serial.println(F("incoming message, EndpointTemperature")); - for(int i=0; i< length; i++) - { - Serial.print((char)payload[i]); - } - Serial.println(); - #endif + for(int i = 0; i < length; i++) + { + Serial.print((char)payload[i]); + } + Serial.println(); +#endif - if(lineContains(topic, "st")) - { - m_owner->sendMessage("st", m_temperature, m_id); - } + if(lineContains(topic, "st")) + { + m_owner->sendMessage("st", m_temperature, m_id); + } } void EndpointTemperature::sendStatusMessage() { - if (millis() - m_last_send_time > m_resend_status_time * 1000) + if(millis() - m_last_send_time > m_resend_status_time * 1000) { - m_last_send_time = millis(); - #ifdef ENDPOINT_TEMPERATURE_DEBUG + m_last_send_time = millis(); +#ifdef ENDPOINT_TEMPERATURE_DEBUG Serial.println(F("sending status message, EndpointTemperature")); - #endif +#endif - m_owner->sendMessage("st", m_temperature, m_id); + m_owner->sendMessage("st", m_temperature, m_id); } } void EndpointTemperature::sendFeedbackMessage() { - #ifdef ENDPOINT_TEMPERATURE_DEBUG - Serial.println(F("sending feedback message, EndpointTemperature")); - #endif +#ifdef ENDPOINT_TEMPERATURE_DEBUG + Serial.println(F("sending feedback message, EndpointTemperature")); +#endif - m_owner->sendMessage("st", m_temperature, m_id); + m_owner->sendMessage("st", m_temperature, m_id); } \ No newline at end of file diff --git a/src/Endpoints/EndpointTemperature.h b/src/Endpoints/EndpointTemperature.h index 491ab7d..840b416 100644 --- a/src/Endpoints/EndpointTemperature.h +++ b/src/Endpoints/EndpointTemperature.h @@ -5,17 +5,17 @@ class EndpointTemperature : public Endpoint { public: - EndpointTemperature(HomeControlMagic* hcm_ptr); + EndpointTemperature(HomeControlMagic* hcm_ptr); - virtual void sendStatusMessage(); + virtual void sendStatusMessage(); - virtual void sendFeedbackMessage(); + virtual void sendFeedbackMessage(); - virtual void incomingMessage(char* topic, byte* payload, unsigned int length); + virtual void incomingMessage(char* topic, uint8_t* payload, unsigned int length); - virtual void setTemperature(double temperature); - virtual double getTemperature(); + virtual void setTemperature(double temperature); + virtual double getTemperature(); protected: - double m_temperature; + double m_temperature; }; diff --git a/src/Endpoints/EndpointTemperatureTarget.cpp b/src/Endpoints/EndpointTemperatureTarget.cpp index 098f3d9..2f173b8 100644 --- a/src/Endpoints/EndpointTemperatureTarget.cpp +++ b/src/Endpoints/EndpointTemperatureTarget.cpp @@ -7,83 +7,82 @@ static char* const CONFIG = "temp_tar"; EndpointTemperatureTarget::EndpointTemperatureTarget(HomeControlMagic* hcm_ptr) - : Endpoint(hcm_ptr) - , m_temperature(0) + : Endpoint(hcm_ptr) + , m_temperature(0) { - m_last_send_time = millis(); - m_resend_status_time = 60; - m_config = CONFIG; + m_last_send_time = millis(); + m_resend_status_time = 60; + m_config = CONFIG; } void EndpointTemperatureTarget::setTemperature(double temperature) { - m_temperature = temperature; + m_temperature = temperature; } double EndpointTemperatureTarget::getTemperature() { - return m_temperature; + return m_temperature; } void EndpointTemperatureTarget::setTemperatureTarget(double temperature) { - m_temperature_target = temperature; + m_temperature_target = temperature; } double EndpointTemperatureTarget::getTemperatureTarget() { - return m_temperature_target; + return m_temperature_target; } -void EndpointTemperatureTarget::incomingMessage(char* topic, byte* payload, unsigned int length) +void EndpointTemperatureTarget::incomingMessage(char* topic, uint8_t* payload, unsigned int length) { - #ifdef ENDPOINT_TEMPERATURE_TARGET_DEBUG - Serial.println(F("incoming message, EndpointTemperatureTarget")); - - for(int i=0; i< length; i++) - { - Serial.print((char)payload[i]); - } - Serial.println(); - #endif - - if(lineContains(topic, "ctt")) - { - m_temperature_target = extractDouble(payload, length); - } - - else if(lineContains(topic, "stt")) - { - m_owner->sendMessage("stt", m_temperature_target, m_id); - } +#ifdef ENDPOINT_TEMPERATURE_TARGET_DEBUG + Serial.println(F("incoming message, EndpointTemperatureTarget")); - else if(lineContains(topic, "st")) - { - m_owner->sendMessage("st", m_temperature, m_id); - } + for(int i = 0; i < length; i++) + { + Serial.print((char)payload[i]); + } + Serial.println(); +#endif + + if(lineContains(topic, "ctt")) + { + m_temperature_target = extractDouble(payload, length); + } + + else if(lineContains(topic, "stt")) + { + m_owner->sendMessage("stt", m_temperature_target, m_id); + } + + else if(lineContains(topic, "st")) + { + m_owner->sendMessage("st", m_temperature, m_id); + } } void EndpointTemperatureTarget::sendStatusMessage() { - if (millis() - m_last_send_time > m_resend_status_time * 1000) + if(millis() - m_last_send_time > m_resend_status_time * 1000) { - m_last_send_time = millis(); - #ifdef ENDPOINT_TEMPERATURE_TARGET_DEBUG + m_last_send_time = millis(); +#ifdef ENDPOINT_TEMPERATURE_TARGET_DEBUG Serial.println(F("sending status message, EndpointTemperatureTarget")); - #endif +#endif - m_owner->sendMessage("st", m_temperature, m_id); - m_owner->sendMessage("stt", m_temperature_target, m_id); + m_owner->sendMessage("st", m_temperature, m_id); + m_owner->sendMessage("stt", m_temperature_target, m_id); } } void EndpointTemperatureTarget::sendFeedbackMessage() { - #ifdef ENDPOINT_TEMPERATURE_TARGET_DEBUG - Serial.println(F("sending feedback message, EndpointTemperatureTarget")); - #endif - - m_owner->sendMessage("st", m_temperature, m_id); - m_owner->sendMessage("stt", m_temperature_target, m_id); +#ifdef ENDPOINT_TEMPERATURE_TARGET_DEBUG + Serial.println(F("sending feedback message, EndpointTemperatureTarget")); +#endif + m_owner->sendMessage("st", m_temperature, m_id); + m_owner->sendMessage("stt", m_temperature_target, m_id); } \ No newline at end of file diff --git a/src/Endpoints/EndpointTemperatureTarget.h b/src/Endpoints/EndpointTemperatureTarget.h index c038441..2d8b125 100644 --- a/src/Endpoints/EndpointTemperatureTarget.h +++ b/src/Endpoints/EndpointTemperatureTarget.h @@ -4,14 +4,13 @@ class EndpointTemperatureTarget : public Endpoint { - public: +public: EndpointTemperatureTarget(HomeControlMagic* hcm_ptr); virtual void sendStatusMessage(); virtual void sendFeedbackMessage(); - virtual void incomingMessage(char* topic, byte* payload, unsigned int length); - + virtual void incomingMessage(char* topic, uint8_t* payload, unsigned int length); virtual void setTemperature(double temperature); virtual void setTemperatureTarget(double temperature); @@ -19,7 +18,7 @@ class EndpointTemperatureTarget : public Endpoint virtual double getTemperature(); virtual double getTemperatureTarget(); - protected: +protected: double m_temperature; double m_temperature_target; }; \ No newline at end of file diff --git a/src/Endpoints/EndpointZero.cpp b/src/Endpoints/EndpointZero.cpp index 555251c..eb34dec 100644 --- a/src/Endpoints/EndpointZero.cpp +++ b/src/Endpoints/EndpointZero.cpp @@ -4,40 +4,40 @@ //#define ENDPOINT_ZERO_DEBUG EndpointZero::EndpointZero(HomeControlMagic* hcm_ptr) - : Endpoint(hcm_ptr) + : Endpoint(hcm_ptr) { } void EndpointZero::sendConfig() { - // nothing +// nothing #ifdef ENDPOINT_ZERO_DEBUG - Serial.println("handling endpoint zero config"); + Serial.println("handling endpoint zero config"); #endif } void EndpointZero::sendStatusMessage() { - // nothing + // nothing } void EndpointZero::sendFeedbackMessage() { - // nothing + // nothing } -void EndpointZero::incomingMessage(char* topic, byte* payload, unsigned int length) +void EndpointZero::incomingMessage(char* topic, uint8_t* payload, unsigned int length) { #ifdef ENDPOINT_ZERO_DEBUG - Serial.println(F("incoming message, EndpointZero")); + Serial.println(F("incoming message, EndpointZero")); - for(int i=0; i< length; i++) - { - Serial.print((char)payload[i]); - } - Serial.println(); + for(int i = 0; i < length; i++) + { + Serial.print((char)payload[i]); + } + Serial.println(); #endif - uint16_t ep_num = (m_owner->getNumberOfEndpoints() - 1); + uint16_t ep_num = (m_owner->getNumberOfEndpoints() - 1); - m_owner->sendMessage("conf", ep_num, m_id); - m_owner->sendConfigs(); + m_owner->sendMessage("conf", ep_num, m_id); + m_owner->sendConfigs(); } diff --git a/src/Endpoints/EndpointZero.h b/src/Endpoints/EndpointZero.h index bf19a0e..13117c7 100644 --- a/src/Endpoints/EndpointZero.h +++ b/src/Endpoints/EndpointZero.h @@ -4,13 +4,12 @@ class EndpointZero : public Endpoint { - public: +public: EndpointZero(HomeControlMagic* hcm_ptr); void sendConfig(); void sendStatusMessage(); void sendFeedbackMessage(); - void incomingMessage(char* topic, byte* payload, unsigned int length); + void incomingMessage(char* topic, uint8_t* payload, unsigned int length); }; - diff --git a/src/HomeControlMagic.cpp b/src/HomeControlMagic.cpp index cf58264..0b9d896 100644 --- a/src/HomeControlMagic.cpp +++ b/src/HomeControlMagic.cpp @@ -1,389 +1,283 @@ #include "HomeControlMagic.h" -#include "Arduino.h" #include "helperFunctions.h" #include "Endpoints/EndpointZero.h" +#ifdef ARDUINO +#include "arduinoWrapper/ArduinoConfig.h" +#include "arduinoWrapper/ArduinoNetworkInterface.h" +#include "arduinoWrapper/ArduinoWrapper.h" +#elif defined STM +#include "STMWrapper.h" +#endif + //#define HCM_DEBUG static HomeControlMagic* hcm_ptr; -#define TOPIC_BUFFER_LENGTH 30 -#define MESSAGE_BUFFER_LENGTH 50 -static char m_topic_buffer[TOPIC_BUFFER_LENGTH]; -static char m_message_buffer[MESSAGE_BUFFER_LENGTH]; +static char* m_topic_buffer_ptr; +static char* m_message_buffer_ptr; -void callback(char* topic, byte* payload, unsigned int length) +void callback(char* topic, uint8_t* payload, unsigned int length) { #ifdef HCM_DEBUG - Serial.println(F("got in callback")); - Serial.println(topic); - for(uint8_t i = 0; i < length; i++) - { - Serial.print((char)payload[i]); - } - Serial.println(); + Serial.println(F("got in callback")); + Serial.println(topic); + for(uint8_t i = 0; i < length; i++) + { + Serial.print((char)payload[i]); + } + Serial.println(); #endif - // check for server announce - if(lineContains(topic, "broadcast")) - { - if(lineContains((char*)payload, "serverannounce")) + // check for server announce + if(lineContains(topic, "broadcast")) { - hcm_ptr->announce(); - return; + if(lineContains((char*)payload, "serverannounce")) + { + hcm_ptr->announce(); + return; + } } - } - - // it is not server announce - uint8_t start_position = lineContains(topic, "/"); - uint8_t end_position = lineContains(topic + start_position, "/") + start_position - 1; - uint8_t diff = end_position - start_position; - - uint8_t endpoint_id = 0; - for(int i = 0; i < diff; i++) - { - endpoint_id += (topic[start_position + i] - 48) * pow(10, diff - 1 - i); - } - - Endpoint* end_ptr = hcm_ptr->getEndpoint(endpoint_id); - if(end_ptr != NULL) - { - end_ptr->incomingMessage(topic, payload, length); - } -} -HomeControlMagic::HomeControlMagic(char* server_ip, char* deviceName, NetworkObject& network_object, char* username, char* password) - : m_number_of_endpoints(0) - , m_name(deviceName) - , m_network_object(network_object) - , m_mqtt_client(*(new PubSubClient(*network_object.getClientPtr()))) - , m_led_time(0) - , m_last_loop_time(0) - , m_username(username) - , m_password(password) - , m_start_done(false) -{ - // pointer that is used from callback to set messages - hcm_ptr = this; + // it is not server announce + uint8_t start_position = lineContains(topic, "/"); + uint8_t end_position = lineContains(topic + start_position, "/") + start_position - 1; + uint8_t diff = end_position - start_position; + + uint8_t endpoint_id = 0; + for(int i = 0; i < diff; i++) + { + endpoint_id += (topic[start_position + i] - 48) * pow(10, diff - 1 - i); + } - EndpointZero* epZ = new EndpointZero(hcm_ptr); - epZ->setId("0"); - m_endpoints_pointers[m_number_of_endpoints++] = epZ; + Endpoint* end_ptr = hcm_ptr->getEndpoint(endpoint_id); + if(end_ptr != NULL) + { + end_ptr->incomingMessage(topic, payload, length); + } +} - uint16_t port = 1883; - if(m_network_object.isSecure()) - { - port = 8883; - } +HomeControlMagic::HomeControlMagic(const char* deviceName) + : m_number_of_endpoints(0) + , m_name(deviceName) + , m_broker_was_connected(false) +{ + // pointer that is used from callback to set messages + hcm_ptr = this; + m_topic_buffer_ptr = wrapperGetTopicBuffer(); + m_message_buffer_ptr = wrapperGetMessageBuffer(); + + EndpointZero* epZ = new EndpointZero(hcm_ptr); + epZ->setId("0"); + m_endpoints_pointers[m_number_of_endpoints++] = epZ; +} - m_mqtt_client.setServer(server_ip, port); - m_mqtt_client.setCallback(callback); +void HomeControlMagic::setup() +{ + m_id = getUniqueId(); - m_id = m_network_object.getUniqueId(); + strcat(m_base_topic, "d/"); + strcat(m_base_topic, m_id); + strcat(m_base_topic, "/"); - strcat(m_base_topic, "d/"); - strcat(m_base_topic, m_id); - strcat(m_base_topic, "/"); + wrapperSetCallback(callback); } void HomeControlMagic::doMagic() { - // this will run only once to setup the controller - if(!m_start_done) - { - m_network_object.start(); - m_start_done = true; - return; - } - - m_network_object.loop(true); + /* + On arduino this is calling loop for pubsubclient and network. Network is calling debugLed loop + */ + wrapperLoop(); - if(m_network_object.isConnected()) - { - mqttLoop(true); - - if(m_broker_connected) + if(wrapperIsMqttConnected()) + { + if(!m_broker_was_connected) + { + m_broker_was_connected = true; + announce(); + } + sendStatus(); + } + else { - sendStatus(); + m_broker_was_connected = false; } - } } void HomeControlMagic::setTopic(char* topic, char* endpoint_id) { - strcat(m_topic_buffer, m_base_topic); - strcat(m_topic_buffer, endpoint_id); - strcat(m_topic_buffer, "/"); - strcat(m_topic_buffer, topic); + strcat(m_topic_buffer_ptr, m_base_topic); + strcat(m_topic_buffer_ptr, endpoint_id); + strcat(m_topic_buffer_ptr, "/"); + strcat(m_topic_buffer_ptr, topic); } /* -* Use with m_message_buffer. Get it by calling getMessageBufferPtr() -*/ + * Use with m_message_buffer_ptr. Get it by calling getMessageBufferPtr() + */ void HomeControlMagic::sendStringMessage(char* topic, char* endpoint_id) { - setTopic(topic, endpoint_id); + setTopic(topic, endpoint_id); - #ifdef HCM_DEBUG - Serial.println(m_topic_buffer); - Serial.println(m_message_buffer); - #endif +#ifdef HCM_DEBUG + Serial.println(m_topic_buffer_ptr); + Serial.println(m_message_buffer_ptr); +#endif - m_mqtt_client.publish(m_topic_buffer, m_message_buffer); - clearBuffer(m_topic_buffer, TOPIC_BUFFER_LENGTH); - clearBuffer(m_message_buffer, MESSAGE_BUFFER_LENGTH); + wrapperPublish(); } void HomeControlMagic::sendMessage(char* topic, bool message, char* endpoint_id) { - setTopic(topic, endpoint_id); - - #ifdef HCM_DEBUG - Serial.println(m_topic_buffer); - #endif - - if(message) - { - m_message_buffer[0] = '1'; - } - else - { - m_message_buffer[0] = '0'; - } - - #ifdef HCM_DEBUG - Serial.println(m_message_buffer); - #endif - - m_mqtt_client.publish(m_topic_buffer, m_message_buffer); - clearBuffer(m_topic_buffer, TOPIC_BUFFER_LENGTH); - clearBuffer(m_message_buffer, MESSAGE_BUFFER_LENGTH); + setTopic(topic, endpoint_id); + +#ifdef HCM_DEBUG + Serial.println(m_topic_buffer_ptr); +#endif + + if(message) + { + m_message_buffer_ptr[0] = '1'; + } + else + { + m_message_buffer_ptr[0] = '0'; + } + +#ifdef HCM_DEBUG + Serial.println(m_message_buffer_ptr); +#endif + + wrapperPublish(); } void HomeControlMagic::sendMessage(char* topic, uint16_t message, char* endpoint_id) { - setTopic(topic, endpoint_id); + setTopic(topic, endpoint_id); - #ifdef HCM_DEBUG - Serial.println(m_topic_buffer); - #endif +#ifdef HCM_DEBUG + Serial.println(m_topic_buffer_ptr); +#endif - itoa(message, m_message_buffer, 10); + itoa(message, m_message_buffer_ptr, 10); - #ifdef HCM_DEBUG - Serial.println(m_message_buffer); - #endif +#ifdef HCM_DEBUG + Serial.println(m_message_buffer_ptr); +#endif - m_mqtt_client.publish(m_topic_buffer, m_message_buffer); - clearBuffer(m_topic_buffer, TOPIC_BUFFER_LENGTH); - clearBuffer(m_message_buffer, MESSAGE_BUFFER_LENGTH); + wrapperPublish(); } void HomeControlMagic::sendMessage(char* topic, double message, char* endpoint_id) { - setTopic(topic, endpoint_id); + setTopic(topic, endpoint_id); - #ifdef HCM_DEBUG - Serial.println(m_topic_buffer); - #endif +#ifdef HCM_DEBUG + Serial.println(m_topic_buffer_ptr); +#endif - dtostrf(message, 4, 2, m_message_buffer); + dtostrf(message, 4, 2, m_message_buffer_ptr); - #ifdef HCM_DEBUG - Serial.println(m_message_buffer); - #endif +#ifdef HCM_DEBUG + Serial.println(m_message_buffer_ptr); +#endif - m_mqtt_client.publish(m_topic_buffer, m_message_buffer); - clearBuffer(m_topic_buffer, TOPIC_BUFFER_LENGTH); - clearBuffer(m_message_buffer, MESSAGE_BUFFER_LENGTH); + wrapperPublish(); } void HomeControlMagic::sendConfig(char* config, uint8_t resend_time, char* endpoint_name, char* endpoint_id) { - setTopic("conf", endpoint_id); + setTopic("conf", endpoint_id); - strcat(m_message_buffer, "e:"); - strcat(m_message_buffer, config); - strcat(m_message_buffer, ";r="); - char buff[5]; - itoa(resend_time, buff, 10); - strcat(m_message_buffer, buff); + strcat(m_message_buffer_ptr, "e:"); + strcat(m_message_buffer_ptr, config); + strcat(m_message_buffer_ptr, ";r="); + char buff[5]; + itoa(resend_time, buff, 10); + strcat(m_message_buffer_ptr, buff); - if(endpoint_name != nullptr) - { - // TODO: replace name with just n - strcat(m_message_buffer, ";name="); - strcat(m_message_buffer, endpoint_name); - } + if(endpoint_name != nullptr) + { + // TODO: replace name with just n + strcat(m_message_buffer_ptr, ";name="); + strcat(m_message_buffer_ptr, endpoint_name); + } - strcat(m_message_buffer, ";"); + strcat(m_message_buffer_ptr, ";"); #ifdef HCM_DEBUG -Serial.println(m_topic_buffer); -Serial.println(m_message_buffer); + Serial.println(m_topic_buffer_ptr); + Serial.println(m_message_buffer_ptr); #endif - m_mqtt_client.publish(m_topic_buffer, m_message_buffer); - clearBuffer(m_topic_buffer, TOPIC_BUFFER_LENGTH); - clearBuffer(m_message_buffer, MESSAGE_BUFFER_LENGTH); -} - -/* - * - */ -void HomeControlMagic::mqttLoop(bool reconnect) -{ - long current_time = millis(); - if(!m_mqtt_client.connected() && (current_time - m_last_loop_time > 100)) - { - m_last_loop_time = current_time; - // flash led for visual feedback - if(current_time - m_led_time > 2000) - { - m_led_time = current_time; - m_network_object.toggleLed(); - } - if(current_time - m_last_reconnect_attempt > m_reconnect_time && reconnect) - { - // Attempt to reconnect - if(reconnectMqtt()) - { - m_last_reconnect_attempt = 0; - m_network_object.controlLed(false); - } - else - { - // reconnectMqtt takes few seconds if it is failing so just read new time - m_last_reconnect_attempt = millis(); - if(current_time - m_last_time_connected > 300000) //5 mins - { - //restart - m_network_object.restart(); - } - } - } - } - else - { - m_last_time_connected = current_time; - m_mqtt_client.loop(); - } -} - -bool HomeControlMagic::reconnectMqtt() -{ - #ifdef HCM_DEBUG - Serial.println(F("Trying to reconnect to mqtt broker")); - #endif - // Attempt to connect - if(m_mqtt_client.connect(m_id, m_username, m_password)) - { - #ifdef HCM_DEBUG - Serial.println(F("Success")); - #endif - // ... and resubscribe - m_mqtt_client.setCallback(callback); - subscribeNow(); - announce(); - m_broker_connected = true; - return true; - } - else - { - #ifdef HCM_DEBUG - Serial.print(F("failed, rc=")); - Serial.println(m_mqtt_client.state()); - #endif - m_broker_connected = false; - return false; - } + wrapperPublish(); } void HomeControlMagic::announce() { - strcat(m_message_buffer, m_name); - sendStringMessage("announce", "0"); - - sendFeedback(); -} - -void HomeControlMagic::subscribeNow() -{ - - strcat(m_topic_buffer, m_id); - strcat(m_topic_buffer, "/#"); - #ifdef HCM_DEBUG - Serial.println(m_topic_buffer); - #endif + strcat(m_message_buffer_ptr, m_name); + sendStringMessage("announce", "0"); - m_mqtt_client.subscribe(m_topic_buffer); - m_mqtt_client.subscribe("broadcast"); - clearBuffer(m_topic_buffer, TOPIC_BUFFER_LENGTH); + sendFeedback(); } Endpoint* HomeControlMagic::getEndpoint(uint8_t number) { - if(number >= m_number_of_endpoints) - { - return NULL; - } + if(number >= m_number_of_endpoints) + { + return NULL; + } - return m_endpoints_pointers[number]; + return m_endpoints_pointers[number]; } char* HomeControlMagic::getId() { - return m_id; + return m_id; } uint8_t HomeControlMagic::getNumberOfEndpoints() { - return m_number_of_endpoints; + return m_number_of_endpoints; } void HomeControlMagic::addEndpoint(Endpoint* endpoint_ptr) { - m_endpoints_pointers[m_number_of_endpoints++] = endpoint_ptr; - itoa(m_number_of_endpoints - 1, m_message_buffer, 10); + m_endpoints_pointers[m_number_of_endpoints++] = endpoint_ptr; + itoa(m_number_of_endpoints - 1, m_message_buffer_ptr, 10); #ifdef HCM_DEBUG - Serial.print(F("Id to set: ")); - Serial.println(m_message_buffer); + Serial.print(F("Id to set: ")); + Serial.println(m_message_buffer_ptr); #endif - endpoint_ptr->setId(m_message_buffer); - clearBuffer(m_message_buffer, MESSAGE_BUFFER_LENGTH); + endpoint_ptr->setId(m_message_buffer_ptr); + wrapperClearMessageBuffer(); } void HomeControlMagic::sendConfigs() { - for(uint8_t i = 0; isendConfig(); - } + for(uint8_t i = 0; i < m_number_of_endpoints; i++) + { + m_endpoints_pointers[i]->sendConfig(); + } } void HomeControlMagic::sendStatus() { - for(uint8_t i = 0; isendStatusMessage(); - } + for(uint8_t i = 0; i < m_number_of_endpoints; i++) + { + m_endpoints_pointers[i]->sendStatusMessage(); + } } void HomeControlMagic::sendFeedback() { - for(uint8_t i = 0; isendFeedbackMessage(); - } -} - -void HomeControlMagic::setReconnectTime(uint16_t seconds) -{ - m_reconnect_time = seconds * 1000; + for(uint8_t i = 0; i < m_number_of_endpoints; i++) + { + m_endpoints_pointers[i]->sendFeedbackMessage(); + } } char* HomeControlMagic::getMessageBufferPtr() { - return m_message_buffer; + return m_message_buffer_ptr; } - - diff --git a/src/HomeControlMagic.h b/src/HomeControlMagic.h index a132c2a..a9edaa5 100644 --- a/src/HomeControlMagic.h +++ b/src/HomeControlMagic.h @@ -1,14 +1,17 @@ #pragma once +#ifdef ARDUINO +#include "Arduino.h" +#endif + #include "Endpoint.h" -#include "NetworkObject.hpp" -#include "PubSubClient.h" class HomeControlMagic { - public: - HomeControlMagic(char* server_ip, char* deviceName, NetworkObject& network_object, char* username = "hc", char* password = "magic"); +public: + HomeControlMagic(const char* deviceName); void doMagic(); + void setup(); Endpoint* getEndpoint(uint8_t number); void addEndpoint(Endpoint* endpoint_ptr); @@ -29,39 +32,16 @@ class HomeControlMagic void sendConfig(char* config, uint8_t, char* endpoint_name, char* endpoint_id); - void setReconnectTime(uint16_t seconds); - char* getMessageBufferPtr(); - private: - bool reconnectMqtt(); - void subscribeNow(); - void mqttLoop(bool reconnect); +private: void setTopic(char* topic, char* endpoint_id); - char* m_name; - - NetworkObject& m_network_object; - PubSubClient& m_mqtt_client; + const char* m_name; + char* m_id; uint8_t m_number_of_endpoints; Endpoint* m_endpoints_pointers[10]; - - const uint16_t m_mqtt_port = 1883; - char* m_id; - - int m_reconnect_time = 5000; - long m_last_time_connected = 0; - long m_last_reconnect_attempt = 0; - long m_led_time; - long m_last_loop_time; - - char* m_username; - char* m_password; - char m_base_topic[20]; - bool m_broker_connected; - - bool m_start_done; + bool m_broker_was_connected; }; - diff --git a/src/arduinoWrapper/ArduinoConfig.h b/src/arduinoWrapper/ArduinoConfig.h new file mode 100644 index 0000000..c02bde9 --- /dev/null +++ b/src/arduinoWrapper/ArduinoConfig.h @@ -0,0 +1,27 @@ +#pragma once + +//#define ETHERNET // for WIZ5100 +//#define ETHERNET2 // for WIZ5500 + +#if defined(ETHERNET) || defined(ETHERNET2) +#define ETH_MAC \ + { \ + 0x11, 0x11, 0x22, 0x33, 0x44, 0x55 \ + } // make sure that MAC is unique per device +#define ETH_IP \ + { \ + 192, 168, 5, 40 \ + } // device will use this IP if it can not get ip automatically +#define ETH_DNS \ + { \ + 192, 168, 5, 1 \ + } // IP of DNS server (router) +#define ETH_GATEWAY \ + { \ + 192, 168, 5, 1 \ + } // IP of internet gateway (router) +#define ETH_SUBNET \ + { \ + 255, 255, 255, 0 \ + } // subnet (if you don't know what this is leave it like this) +#endif // defined(ETHERNET) || defined(ETHERNET2) diff --git a/src/arduinoWrapper/ArduinoDebugLed.h b/src/arduinoWrapper/ArduinoDebugLed.h new file mode 100644 index 0000000..b76d308 --- /dev/null +++ b/src/arduinoWrapper/ArduinoDebugLed.h @@ -0,0 +1,57 @@ +#pragma once +#include "Arduino.h" + +static unsigned long m_flash_period = 0; +static unsigned long m_last_flash = 0; +static bool m_led_is_on_when = false; +static int8_t m_led_pin = -1; + +#define LED_PIN_CHECK() \ + if(m_led_pin == -1) \ + return; + +inline void DebugLedFlash(long half_period) +{ + LED_PIN_CHECK(); + m_flash_period = half_period; +} + +inline void DebugLedToggle() +{ + LED_PIN_CHECK(); + digitalWrite(m_led_pin, !digitalRead(m_led_pin)); +} + +inline void DebugLedState(bool state) +{ + LED_PIN_CHECK(); + if(state) + { + m_flash_period = 0; + digitalWrite(m_led_pin, m_led_is_on_when); + } + else + { + m_flash_period = 0; + digitalWrite(m_led_pin, !m_led_is_on_when); + } +} + +inline void DebugLedLoop() +{ + LED_PIN_CHECK(); + if(m_flash_period > 0 && millis() - m_last_flash > m_flash_period) + { + DebugLedToggle(); + m_last_flash = millis(); + } +} + +inline void DebugLedPinSet(int8_t led_pin) +{ + m_led_pin = led_pin; + if(m_led_pin != -1) + { + pinMode(m_led_pin, OUTPUT); + } +} \ No newline at end of file diff --git a/src/arduinoWrapper/ArduinoEspWrapper.cpp b/src/arduinoWrapper/ArduinoEspWrapper.cpp new file mode 100644 index 0000000..3fd31e6 --- /dev/null +++ b/src/arduinoWrapper/ArduinoEspWrapper.cpp @@ -0,0 +1,157 @@ +#ifdef ESP8266 +#include "ArduinoEspWrapper.h" +#include "Arduino.h" +#include "ArduinoDebugLed.h" + +#define ESP_WRAPPER_DEBUG + +#define RESTART_TIME 1800000L // 30 minutes + +// private vars +static Client* m_client = nullptr; +static char m_uid[20] = {'0'}; +static const char* m_ssid = nullptr; +static const char* m_password = nullptr; +static unsigned long m_last_time_connected = 0; +static bool m_is_secure = false; + +WiFiEventHandler gotIpEventHandler, disconnectedEventHandler; + +// private function declaration: +static bool networkReconnect(); + +// implementation: +void networkLoop() +{ + if(m_client == nullptr || m_ssid == nullptr || m_password == nullptr) + { +#ifdef ESP_WRAPPER_DEBUG + if(m_client == nullptr) + Serial.println(F("m_client is null!!!")); + if(m_ssid == nullptr) + Serial.println(F("m_ssid is null!!!")); + if(m_password == nullptr) + Serial.println(F("m_password is null!!!")); +#endif + return; + } + + unsigned long current_time = millis(); + if(WiFi.status() == WL_CONNECTED) + { + m_last_time_connected = current_time; + } + else + { + DebugLedFlash(500); + } + + if(current_time - m_last_time_connected > RESTART_TIME) + { + networkChipRestart(); + } + + // network loop will also keep debugLed running + DebugLedLoop(); +} + +void networkSetSecure(bool secure) +{ + m_is_secure = secure; +} + +bool networkIsConnected() +{ + return WiFi.status() == WL_CONNECTED; +} + +void networkSetup() +{ + DebugLedPinSet(LED_BUILTIN); + // if we are using that small module for ESP make sure we killoff debug led +#ifdef ARDUINO_ESP8266_ESP01 + DebugLedPinSet(-1); +#endif + + if(m_is_secure) + { + m_client = new WiFiClientSecure(); + } + else + { + m_client = new WiFiClient(); + } + // TODO: remove sprintf + sprintf(m_uid, "%d", ESP.getChipId()); +#ifdef ESP_WRAPPER_DEBUG + Serial.print(F("unique id: ")); + Serial.println(m_uid); +#endif + + // turn off access point + WiFi.mode(WIFI_STA); + + gotIpEventHandler = WiFi.onStationModeGotIP([](const WiFiEventStationModeGotIP& event) { +#ifdef ESP_WRAPPER_DEBUG + Serial.print("Station connected, IP: "); + Serial.println(WiFi.localIP()); +#endif + DebugLedState(false); + }); + + disconnectedEventHandler = WiFi.onStationModeDisconnected([](const WiFiEventStationModeDisconnected& event) { +#ifdef ESP_WRAPPER_DEBUG + Serial.println("Station disconnected"); +#endif + DebugLedFlash(500); + }); +} + +bool networkReconnect() +{ +} + +void networkChipRestart() +{ + ESP.restart(); +} + +void networkStart() +{ + if(m_client == nullptr || m_ssid == nullptr || m_password == nullptr) + { +#ifdef ESP_WRAPPER_DEBUG + Serial.println(F("ESP settings not set!!!")); +#endif + return; + } + + WiFi.begin(m_ssid, m_password); +} + +void networkSetSsid(const char* const ssid) +{ + m_ssid = ssid; +} + +void networkSetPass(const char* const pass) +{ + m_password = pass; +} + +bool networkIsSecure() +{ + return m_is_secure; +} + +Client& networkGetClient() +{ + return *m_client; +} + +char* getUniqueId() +{ + return m_uid; +} + +#endif // ESP8266 \ No newline at end of file diff --git a/src/arduinoWrapper/ArduinoEspWrapper.h b/src/arduinoWrapper/ArduinoEspWrapper.h new file mode 100644 index 0000000..ce85453 --- /dev/null +++ b/src/arduinoWrapper/ArduinoEspWrapper.h @@ -0,0 +1,20 @@ +#pragma once + +#ifdef ESP8266 +#include "ESP8266WiFi.h" + +void networkLoop(); +void networkSetup(); +void networkStart(); +void networkChipRestart(); +Client& networkGetClient(); +bool networkIsConnected(); + +void networkSetSecure(bool secure); +bool networkIsSecure(); +void setReconnectTime(unsigned long reconnect_time); +void networkSetSsid(const char* const ssid); +void networkSetPass(const char* const pass); + +char* getUniqueId(); +#endif // ESP8266 diff --git a/src/arduinoWrapper/ArduinoEthernetWrapper.cpp b/src/arduinoWrapper/ArduinoEthernetWrapper.cpp new file mode 100644 index 0000000..9a9d7e0 --- /dev/null +++ b/src/arduinoWrapper/ArduinoEthernetWrapper.cpp @@ -0,0 +1,160 @@ +#include "ArduinoConfig.h" +#if defined(ETHERNET) || defined(ETHERNET2) + +#include "Arduino.h" +#include "ArduinoDebugLed.h" +#include "ArduinoEthernetWrapper.h" + +#ifndef ETH_MAC +#error "You need to define ETH_MAC with mac address. Make sure it is unique" +#endif + +//#define ETHERNET_WRAPPER_DEBUG + +static Client* m_client = nullptr; +static char m_uid[20] = {'0'}; +static unsigned long m_last_time_connected = 0; +static bool m_connected = false; +static byte mac[] = ETH_MAC; + +void networkLoop() +{ + switch(Ethernet.maintain()) + { + case 0: + { + m_connected = true; + m_last_time_connected = millis(); + break; + } + case 1: + { +#ifdef ETHERNET_WRAPPER_DEBUG + Serial.print(F("\n\rDHCP: Renew failed")); +#endif + m_connected = false; + DebugLedFlash(500); + break; + } + case 2: + { +#ifdef ETHERNET_WRAPPER_DEBUG + Serial.print(F("\n\rDHCP: Renew success")); +#endif + m_connected = true; + DebugLedState(false); + break; + } + case 3: + { +#ifdef ETHERNET_WRAPPER_DEBUG + Serial.print(F("\n\rDHCP: Rebind fail")); +#endif + m_connected = false; + DebugLedFlash(500); + break; + } + case 4: + { +#ifdef ETHERNET_WRAPPER_DEBUG + Serial.print(F("\n\rDHCP: Rebind success")); +#endif + m_connected = true; + DebugLedState(false); + break; + } + default: + { +#ifdef ETHERNET_WRAPPER_DEBUG + Serial.print(F("\n\rDHCP: Unexpected number")); +#endif + break; + } + } + + // network loop will also keep debugLed running + DebugLedLoop(); +} + +void networkSetup() +{ +#ifdef ETHERNET_WRAPPER_DEBUG + Serial.println("In setup"); +#endif + m_client = new EthernetClient(); + + // TODO: remove sprintf + sprintf(m_uid, "%d%d%d%d%d%d", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); +} + +void networkStart() +{ +#ifdef ETHERNET_WRAPPER_DEBUG + Serial.println("In start"); +#endif + +#if defined(ETH_IP) && defined(ETH_DNS) && defined(ETH_GATEWAY) && defined(ETH_SUBNET) + Ethernet.begin(mac, ETH_IP, ETH_DNS, ETH_GATEWAY, ETH_SUBNET); +#elif defined(ETH_IP) && defined(ETH_DNS) && defined(ETH_GATEWAY) + Ethernet.begin(mac, ETH_IP, ETH_DNS, ETH_GATEWAY); +#elif defined(ETH_IP) && defined(ETH_DNS) + Ethernet.begin(mac, ETH_IP, ETH_DNS); +#elif defined(ETH_IP) + Ethernet.begin(mac, ETH_IP); +#else + Ethernet.begin(mac); +#endif + + // Allow the hardware to sort itself out + delay(1500); +} +void networkChipRestart() +{ +// TODO: add restart +#ifdef ETHERNET_WRAPPER_DEBUG + Serial.println("In restart"); +#endif +} + +Client& networkGetClient() +{ + return *m_client; +} + +bool networkIsConnected() +{ + return m_connected; +} + +void networkSetSecure(bool secure) +{ + // atmega chips are not supporting SSL so this will always be false +} + +bool networkIsSecure() +{ + // atmega chips are not supporting SSL so this will always be false + return false; +} + +void setReconnectTime(unsigned long reconnect_time) +{ + // this is here to keep interface same for wifi +} + +void networkSetSsid(const char* const ssid) +{ + // this is here to keep interface same for wifi +} + +void networkSetPass(const char* const pass) +{ + // this is here to keep interface same for wifi +} + +char* getUniqueId() +{ + return m_uid; +} + +#endif // defined(ETHERNET) || defined(ETHERNET2) \ No newline at end of file diff --git a/src/arduinoWrapper/ArduinoEthernetWrapper.h b/src/arduinoWrapper/ArduinoEthernetWrapper.h new file mode 100644 index 0000000..9f23cbd --- /dev/null +++ b/src/arduinoWrapper/ArduinoEthernetWrapper.h @@ -0,0 +1,28 @@ +#pragma once + +#include "ArduinoConfig.h" + +#if defined(ETHERNET) || defined(ETHERNET2) + +#ifdef ETHERNET +#include "Ethernet.h" +#elif defined(ETHERNET2) +#include "Ethernet2.h" +#endif + +void networkLoop(); +void networkSetup(); +void networkStart(); +void networkChipRestart(); +Client& networkGetClient(); +bool networkIsConnected(); + +void networkSetSecure(bool secure); +bool networkIsSecure(); +void setReconnectTime(unsigned long reconnect_time); +void networkSetSsid(const char* const ssid); +void networkSetPass(const char* const pass); + +char* getUniqueId(); + +#endif // defined(ETHERNET) || defined(ETHERNET2) \ No newline at end of file diff --git a/src/arduinoWrapper/ArduinoNetworkInterface.h b/src/arduinoWrapper/ArduinoNetworkInterface.h new file mode 100644 index 0000000..65c65f2 --- /dev/null +++ b/src/arduinoWrapper/ArduinoNetworkInterface.h @@ -0,0 +1,19 @@ +#pragma once + +#include "ArduinoConfig.h" + +// guard from defining both Boards +#if defined(ESP8266) && (defined(ETHERNET) || defined(ETHERNET2)) +#error "You need to choose ESP or ETHERNET" +#elif defined(ETHERNET) && defined(ETHERNET2) +#error "You can not define both ethernet and ethernet2" +#endif + +// ESP8266 is automatically defined when you select board with that chip +#ifdef ESP8266 +#include "ArduinoEspWrapper.h" +#elif defined(ETHERNET) || defined(ETHERNET2) +#include "ArduinoEthernetWrapper.h" +#else +#error "You need to choose ESP, ETHERNET or ETHERNET2" +#endif \ No newline at end of file diff --git a/src/arduinoWrapper/ArduinoWrapper.cpp b/src/arduinoWrapper/ArduinoWrapper.cpp new file mode 100644 index 0000000..1cc04d7 --- /dev/null +++ b/src/arduinoWrapper/ArduinoWrapper.cpp @@ -0,0 +1,206 @@ +#include "ArduinoWrapper.h" +#include "Arduino.h" +#include "ArduinoConfig.h" +#include "ArduinoDebugLed.h" +#include "ArduinoNetworkInterface.h" +#include "PubSubClient.h" +#include "helperFunctions.h" + +//#define ARDUINO_WRAPPER_DEBUG + +// private vars: +#define TOPIC_BUFFER_LENGTH 30 +#define MESSAGE_BUFFER_LENGTH 50 +static char m_topic_buffer[TOPIC_BUFFER_LENGTH]; +static char m_message_buffer[MESSAGE_BUFFER_LENGTH]; +uint32_t m_last_reconnect_attempt = 0; +uint32_t m_reconnect_time = 5000; // 5 seconds +uint32_t m_last_time_connected = 0; +static PubSubClient m_mqtt_client; +const char* m_username = nullptr; +const char* m_password = nullptr; + +uint32_t m_last_loop_time; +// private function declaration: +static bool wrapperReconnectMqtt(); +static void wrapperSubscribeNow(); + +// function implementation: +void wrapperLoop(bool reconnect) +{ + // wrapper loop will also call network loop + networkLoop(); + + if(!networkIsConnected()) + { + return; + } + + long current_time = millis(); + if(!m_mqtt_client.connected() && (current_time - m_last_loop_time > 100)) + { + m_last_loop_time = current_time; + // flash led for visual feedback + DebugLedFlash(1000); + if(current_time - m_last_reconnect_attempt > m_reconnect_time && reconnect) + { + // Attempt to reconnect + if(wrapperReconnectMqtt()) + { + m_last_reconnect_attempt = 0; + DebugLedState(false); + } + else + { + // reconnectMqtt takes few seconds if it is failing so just read new time + m_last_reconnect_attempt = millis(); + if(current_time - m_last_time_connected > 300000) // 5 mins + { + // restart + networkChipRestart(); + } + } + } + } + else + { + m_last_time_connected = current_time; + m_mqtt_client.loop(); + } +} + +void wrapperSetup() +{ + m_mqtt_client.setClient(networkGetClient()); +} + +char* wrapperGetTopicBuffer() +{ + return m_topic_buffer; +} + +char* wrapperGetMessageBuffer() +{ + return m_message_buffer; +} + +void wrapperPublish() +{ +#ifdef ARDUINO_WRAPPER_DEBUG + Serial.print("Publishing on topic: "); + Serial.println(m_topic_buffer); + Serial.println(m_message_buffer); +#endif + + m_mqtt_client.publish(m_topic_buffer, m_message_buffer); + wrapperClearTopicBuffer(); + wrapperClearMessageBuffer(); +} + +void wrapperClearTopicBuffer() +{ + clearBuffer(m_topic_buffer, TOPIC_BUFFER_LENGTH); +} + +void wrapperClearMessageBuffer() +{ + clearBuffer(m_message_buffer, MESSAGE_BUFFER_LENGTH); +} + +void wrapperSetServer(IPAddress ip) +{ + if(networkIsSecure()) + { + m_mqtt_client.setServer(ip, 8883); + } + else + { + m_mqtt_client.setServer(ip, 1883); + } +} + +void wrapperSetServer(uint8_t* ip) +{ + if(networkIsSecure()) + { + m_mqtt_client.setServer(ip, 8883); + } + else + { + m_mqtt_client.setServer(ip, 1883); + } +} + +void wrapperSetServer(char* ip) +{ + if(networkIsSecure()) + { + m_mqtt_client.setServer(ip, 8883); + } + else + { + m_mqtt_client.setServer(ip, 1883); + } +} + +void wrapperSetCallback(void (*callback)(char*, uint8_t*, unsigned int)) +{ + m_mqtt_client.setCallback(callback); +} + +bool wrapperIsMqttConnected() +{ + return m_mqtt_client.connected(); +} + +void wrapperSetUsernamePassword(const char* const username, const char* const password) +{ + m_username = username; + m_password = password; +} + +// private functions: +bool wrapperReconnectMqtt() +{ +#ifdef ARDUINO_WRAPPER_DEBUG + if(m_username == nullptr) + Serial.println(F("mqtt username is null")); + if(m_password == nullptr) + Serial.println(F("mqtt pass is null")); + + Serial.println(F("Trying to reconnect to mqtt broker")); +#endif + // Attempt to connect + if(m_mqtt_client.connect(getUniqueId(), m_username, m_password)) + { +#ifdef ARDUINO_WRAPPER_DEBUG + Serial.println(F("Success")); + Serial.flush(); +#endif + // ... and resubscribe + wrapperSubscribeNow(); + return true; + } + else + { +#ifdef ARDUINO_WRAPPER_DEBUG + Serial.print(F("failed, rc=")); + Serial.println(m_mqtt_client.state()); + Serial.flush(); +#endif + return false; + } +} + +void wrapperSubscribeNow() +{ + strcat(m_topic_buffer, getUniqueId()); + strcat(m_topic_buffer, "/#"); +#ifdef ARDUINO_WRAPPER_DEBUG + Serial.println(m_topic_buffer); +#endif + + m_mqtt_client.subscribe(m_topic_buffer); + m_mqtt_client.subscribe("broadcast"); + clearBuffer(m_topic_buffer, TOPIC_BUFFER_LENGTH); +} diff --git a/src/arduinoWrapper/ArduinoWrapper.h b/src/arduinoWrapper/ArduinoWrapper.h new file mode 100644 index 0000000..ab1ab56 --- /dev/null +++ b/src/arduinoWrapper/ArduinoWrapper.h @@ -0,0 +1,17 @@ +#pragma once + +#include "IPAddress.h" + +void wrapperLoop(bool reconnect = true); +void wrapperSetup(); +void wrapperSetServer(IPAddress ip); +void wrapperSetServer(uint8_t* ip); +void wrapperSetServer(char* ip); +void wrapperSetCallback(void (*callback)(char*, uint8_t*, unsigned int)); +bool wrapperIsMqttConnected(); +void wrapperSetUsernamePassword(const char* const username, const char* const password); +void wrapperClearMessageBuffer(); +void wrapperClearTopicBuffer(); +char* wrapperGetTopicBuffer(); +char* wrapperGetMessageBuffer(); +void wrapperPublish(); \ No newline at end of file diff --git a/src/arduinoWrapper/README.md b/src/arduinoWrapper/README.md new file mode 100644 index 0000000..7367c42 --- /dev/null +++ b/src/arduinoWrapper/README.md @@ -0,0 +1,9 @@ +# How to select chips +## ESP +For esp comment both ETHERNET and ETHERNET2 macros in ArduinoConfig.h +ESP8266 macro will be defined once you select board from boards menu with that chip (this includes NodeMCU and so on) + +## WIZ chips +Go to ArduinoConfig.h and uncomment macro that you want: +* ETHERNET for WIZ5100 +* ETHERNET2 for WIZ5500 \ No newline at end of file diff --git a/src/helperFunctions.cpp b/src/helperFunctions.cpp index 87b0b8d..e179047 100644 --- a/src/helperFunctions.cpp +++ b/src/helperFunctions.cpp @@ -1,198 +1,197 @@ #include "helperFunctions.h" -void clearByte(byte* text, unsigned int length) +void clearByte(uint8_t* text, unsigned int length) { - for(int i = 0; i < length; i++) - { - text[i] = 0; - } + for(int i = 0; i < length; i++) + { + text[i] = 0; + } } void clearBuffer(char* text, uint8_t length) { - for(uint8_t i = 0; i < length; i++) - { - text[i] = 0; - } + for(uint8_t i = 0; i < length; i++) + { + text[i] = 0; + } } -int lineContains(const char *str, const char *sfind) +int lineContains(const char* str, const char* sfind) { - int found = 0; - int index = 0; - int len, slen; + int found = 0; + int index = 0; + int len, slen; - len = strlen(str); - slen = strlen(sfind); - if(slen > len) - { - return 0; - } - while(index < len) - { - if(str[index] == sfind[found]) + len = strlen(str); + slen = strlen(sfind); + if(slen > len) { - found++; - if(slen == found) - { - return (index + 1); - } + return 0; } - else + while(index < len) { - found = 0; + if(str[index] == sfind[found]) + { + found++; + if(slen == found) + { + return (index + 1); + } + } + else + { + found = 0; + } + index++; } - index++; - } - return 0; + return 0; } -float extractFloat(byte* text, unsigned int length) +float extractFloat(uint8_t* text, unsigned int length) { - float temp = 0; - if(int n = lineContains((const char*)text, ".")) - { - //it is float - if(length > n) - { - for(int i = n; i < length; i++) - { - temp += (text[i] - '0') * pow(10, ((-1) - (i - n))); - } - } - n--; - for(int i = 0; i < n; i++) + float temp = 0; + if(int n = lineContains((const char*)text, ".")) { - temp += (text[i] - '0') * pow(10, (n - 1 - i)); + // it is float + if(length > n) + { + for(int i = n; i < length; i++) + { + temp += (text[i] - '0') * pow(10, ((-1) - (i - n))); + } + } + n--; + for(int i = 0; i < n; i++) + { + temp += (text[i] - '0') * pow(10, (n - 1 - i)); + } } - } - else - { - //not float - for(int i = 0; i < length; i++) + else { - temp += (text[i] - '0') * pow(10, ((length - 1) - i)); + // not float + for(int i = 0; i < length; i++) + { + temp += (text[i] - '0') * pow(10, ((length - 1) - i)); + } } - } - clearByte(text, length); - return temp; + clearByte(text, length); + return temp; } -double extractDouble(byte* text, unsigned int length) +double extractDouble(uint8_t* text, unsigned int length) { - double temp = 0; - if(int n = lineContains((const char*)text, ".")) - { - //it is double - if(length > n) + double temp = 0; + if(int n = lineContains((const char*)text, ".")) { - for(int i = n; i < length; i++) - { - temp += (text[i] - '0') * pow(10, ((-1) - (i - n))); - } - } - n--; - for(int i = 0; i < n; i++) - { - temp += (text[i] - '0') * pow(10, (n - 1 - i)); + // it is double + if(length > n) + { + for(int i = n; i < length; i++) + { + temp += (text[i] - '0') * pow(10, ((-1) - (i - n))); + } + } + n--; + for(int i = 0; i < n; i++) + { + temp += (text[i] - '0') * pow(10, (n - 1 - i)); + } } - } - else - { - //not double - for(int i = 0; i < length; i++) + else { - temp += (text[i] - '0') * pow(10, ((length - 1) - i)); + // not double + for(int i = 0; i < length; i++) + { + temp += (text[i] - '0') * pow(10, ((length - 1) - i)); + } } - } - clearByte(text, length); - return temp; + clearByte(text, length); + return temp; } -int extractInteger(byte* text, unsigned int length) +int extractInteger(uint8_t* text, unsigned int length) { - int temp = 0; - - if(text[length] != '\0') - { - text[length] = '\0'; - } + int temp = 0; - if (text[0] != '\0') - { - temp = atoi((const char*)text); - } - clearByte(text, length); - return temp; -} - -bool extractState(byte* text, unsigned int length) -{ - bool temp = false; - if (text[0] != '\0') - { - if(lineContains((const char*)text, "ON")) + if(text[length] != '\0') { - temp = true; + text[length] = '\0'; } - else if(lineContains((const char*)text, "OFF")) + + if(text[0] != '\0') { - temp = false; + temp = atoi((const char*)text); } - } - clearByte(text, length); - return temp; + clearByte(text, length); + return temp; } - -bool extractBool(byte* text, unsigned int length) +bool extractState(uint8_t* text, unsigned int length) { - bool temp = false; - if (text[0] != '\0') - { - if(text[1] != '\0') + bool temp = false; + if(text[0] != '\0') { - text[1] = '\0'; + if(lineContains((const char*)text, "ON")) + { + temp = true; + } + else if(lineContains((const char*)text, "OFF")) + { + temp = false; + } } + clearByte(text, length); + return temp; +} - if(lineContains((const char*)text, "1")) - { - temp = true; - } - else if(lineContains((const char*)text, "0")) +bool extractBool(uint8_t* text, unsigned int length) +{ + bool temp = false; + if(text[0] != '\0') { - temp = false; + if(text[1] != '\0') + { + text[1] = '\0'; + } + + if(lineContains((const char*)text, "1")) + { + temp = true; + } + else if(lineContains((const char*)text, "0")) + { + temp = false; + } } - } - clearByte(text, length); - return temp; + clearByte(text, length); + return temp; } -RGB extractRGB(byte* text, unsigned int length) +RGB extractRGB(uint8_t* text, unsigned int length) +{ + RGB rgb; + if(text[length] != '\0') { - RGB rgb; - if(text[length] != '\0') - { - text[length] = '\0'; - } - int values[3] = {0}; - for(int i = 2; i >= 0; i--) + text[length] = '\0'; + } + int values[3] = {0}; + for(int i = 2; i >= 0; i--) + { + char* k1 = strrchr((const char*)text, ';'); + int k = k1 - (const char*)text + 1; + if(k < 0) + k = 0; + char temp[3]; + for(int j = k, j1 = 0; j < length; j++, j1++) { - char* k1 = strrchr((const char*)text, ';'); - int k = k1 - (const char*)text + 1; - if(k < 0) - k = 0; - char temp[3]; - for(int j = k, j1 = 0; j < length; j++, j1++) - { - temp[j1] = text[j]; - } - text[k - 1] = '\0'; - values[i] = atoi(temp); + temp[j1] = text[j]; } - rgb.r = values[0]; - rgb.g = values[1]; - rgb.b = values[2]; - clearByte(text, length); - return rgb; - } \ No newline at end of file + text[k - 1] = '\0'; + values[i] = atoi(temp); + } + rgb.r = values[0]; + rgb.g = values[1]; + rgb.b = values[2]; + clearByte(text, length); + return rgb; +} \ No newline at end of file diff --git a/src/helperFunctions.h b/src/helperFunctions.h index d2f1758..27e931b 100644 --- a/src/helperFunctions.h +++ b/src/helperFunctions.h @@ -4,17 +4,17 @@ struct RGB { - int r; - int g; - int b; + int r; + int g; + int b; }; void clearBuffer(char* text, uint8_t length); -void clearByte(byte* text, unsigned int length); -int lineContains(const char *str, const char *sfind); -float extractFloat(byte* text, unsigned int length); -int extractInteger(byte* text, unsigned int length); -double extractDouble(byte* text, unsigned int length); -bool extractState(byte* text, unsigned int length); -bool extractBool(byte* text, unsigned int length); -RGB extractRGB(byte* text, unsigned int length); +void clearByte(uint8_t* text, unsigned int length); +int lineContains(const char* str, const char* sfind); +float extractFloat(uint8_t* text, unsigned int length); +int extractInteger(uint8_t* text, unsigned int length); +double extractDouble(uint8_t* text, unsigned int length); +bool extractState(uint8_t* text, unsigned int length); +bool extractBool(uint8_t* text, unsigned int length); +RGB extractRGB(uint8_t* text, unsigned int length);