From 997e52caaba63c5f4459b1646af8024bb5d39975 Mon Sep 17 00:00:00 2001 From: seky2205 Date: Sun, 9 Sep 2018 18:40:24 +0200 Subject: [PATCH 01/11] compiling after spliting homecontrol_magic from pub_sub and hw specific calls --- examples/basic_OnOff/basic_OnOff.ino | 29 ++- src/HomeControlMagic.cpp | 220 +++++------------- src/HomeControlMagic.h | 34 +-- src/arduinoWrapper/ArduinoDebugLed.h | 55 +++++ src/arduinoWrapper/ArduinoEspWrapper.cpp | 152 ++++++++++++ src/arduinoWrapper/ArduinoEspWrapper.h | 17 ++ src/arduinoWrapper/ArduinoEthernetWrapper.cpp | 1 + src/arduinoWrapper/ArduinoEthernetWrapper.h | 1 + src/arduinoWrapper/ArduinoNetworkInterface.h | 1 + src/arduinoWrapper/ArduinoWrapper.cpp | 187 +++++++++++++++ src/arduinoWrapper/ArduinoWrapper.h | 17 ++ 11 files changed, 512 insertions(+), 202 deletions(-) create mode 100644 src/arduinoWrapper/ArduinoDebugLed.h create mode 100644 src/arduinoWrapper/ArduinoEspWrapper.cpp create mode 100644 src/arduinoWrapper/ArduinoEspWrapper.h create mode 100644 src/arduinoWrapper/ArduinoEthernetWrapper.cpp create mode 100644 src/arduinoWrapper/ArduinoEthernetWrapper.h create mode 100644 src/arduinoWrapper/ArduinoNetworkInterface.h create mode 100644 src/arduinoWrapper/ArduinoWrapper.cpp create mode 100644 src/arduinoWrapper/ArduinoWrapper.h diff --git a/examples/basic_OnOff/basic_OnOff.ino b/examples/basic_OnOff/basic_OnOff.ino index cd30c05..4851638 100644 --- a/examples/basic_OnOff/basic_OnOff.ino +++ b/examples/basic_OnOff/basic_OnOff.ino @@ -1,23 +1,26 @@ +#define ARDUINO +//#define SECURE // should we use SSL encryption? #include "HomeControlMagic.h" +#include "arduinoWrapper/ArduinoWrapper.h" +#include "arduinoWrapper/ArduinoNetworkInterface.h" #include "Endpoints/EndpointOnOff.h" -#define ESP_LOOP -#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 RECONNECTION_TIME 5 // network reconnection time in seconds - -static char* const GW_IP = "GW_IP"; // gateway IP address -static char* const deviceName = "ON_OFF_DEVICE"; // name of device +IPAddress gw_ip = {192, 168, 1, 2}; +static const char* const deviceName = "ON_OFF_DEVICE"; // name of device +static const char* const wifi_ssid = "ssid"; +static const char* const wifi_pass = "pass"; +static const char* const mqtt_username = "hc"; +static const char* const mqtt_password = "magic"; bool active_pin_state = false; // reverse pin state bool last_state = false; -HomeControlMagic hcm(GW_IP, deviceName, network); +HomeControlMagic hcm(deviceName); EndpointOnOff endpointOnOff(&hcm); void controlPin() @@ -47,7 +50,13 @@ void setup() Serial.println("Started serial"); #endif - network.setReconnectTime(RECONNECTION_TIME); + networkSetSsid(wifi_ssid); + networkSetPass(wifi_pass); + networkSetSecure(false); // this must be called before setServer + + wrapperSetServer(gw_ip); + wrapperSetUsernamePassword(mqtt_username, mqtt_password); + hcm.addEndpoint(&endpointOnOff); } diff --git a/src/HomeControlMagic.cpp b/src/HomeControlMagic.cpp index a72de66..35e76c8 100644 --- a/src/HomeControlMagic.cpp +++ b/src/HomeControlMagic.cpp @@ -4,16 +4,21 @@ #include "Endpoints/EndpointZero.h" +#ifdef ARDUINO +#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")); @@ -52,28 +57,21 @@ void callback(char* topic, byte* payload, unsigned int length) } } -HomeControlMagic::HomeControlMagic(char* server_ip, char* deviceName, NetworkObject& network_object, char* username, char* password) +HomeControlMagic::HomeControlMagic(const char* deviceName) : 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) + , 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, 1883); - m_mqtt_client.setCallback(callback); - - m_id = m_network_object.getUniqueId(); + m_id = getUniqueId(); strcat(m_base_topic, "d/"); strcat(m_base_topic, m_id); @@ -82,50 +80,44 @@ HomeControlMagic::HomeControlMagic(char* server_ip, char* deviceName, NetworkObj 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); + wrapperLoop(); - if(m_network_object.isConnected()) + if(wrapperIsMqttConnected()) { - mqttLoop(true); - - if(m_broker_connected) + if(!m_broker_was_connected) { - sendStatus(); + m_broker_was_connected = true; + announce(); } + sendStatus(); + } + else + { + 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); #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); + wrapperPublish(); } void HomeControlMagic::sendMessage(char* topic, bool message, char* endpoint_id) @@ -138,20 +130,18 @@ void HomeControlMagic::sendMessage(char* topic, bool message, char* endpoint_id) if(message) { - m_message_buffer[0] = '1'; + m_message_buffer_ptr[0] = '1'; } else { - m_message_buffer[0] = '0'; + m_message_buffer_ptr[0] = '0'; } #ifdef HCM_DEBUG - Serial.println(m_message_buffer); + 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, uint16_t message, char* endpoint_id) @@ -162,15 +152,13 @@ void HomeControlMagic::sendMessage(char* topic, uint16_t message, char* endpoint Serial.println(m_topic_buffer); #endif - itoa(message, m_message_buffer, 10); + itoa(message, m_message_buffer_ptr, 10); #ifdef HCM_DEBUG - Serial.println(m_message_buffer); + 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) @@ -181,115 +169,40 @@ void HomeControlMagic::sendMessage(char* topic, double message, char* endpoint_i Serial.println(m_topic_buffer); #endif - dtostrf(message, 4, 2, m_message_buffer); + dtostrf(message, 4, 2, m_message_buffer_ptr); #ifdef HCM_DEBUG - Serial.println(m_message_buffer); + 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); - strcat(m_message_buffer, "e:"); - strcat(m_message_buffer, config); - strcat(m_message_buffer, ";r="); + 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, buff); + 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); + 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() @@ -299,20 +212,6 @@ void HomeControlMagic::announce() sendFeedback(); } -void HomeControlMagic::subscribeNow() -{ - - strcat(m_topic_buffer, m_id); - strcat(m_topic_buffer, "/#"); - #ifdef HCM_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); -} - Endpoint* HomeControlMagic::getEndpoint(uint8_t number) { if(number >= m_number_of_endpoints) @@ -336,13 +235,13 @@ uint8_t HomeControlMagic::getNumberOfEndpoints() 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); + 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); #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() @@ -369,14 +268,9 @@ void HomeControlMagic::sendFeedback() } } -void HomeControlMagic::setReconnectTime(uint16_t seconds) -{ - m_reconnect_time = seconds * 1000; -} - char* HomeControlMagic::getMessageBufferPtr() { - return m_message_buffer; + return m_message_buffer_ptr; } diff --git a/src/HomeControlMagic.h b/src/HomeControlMagic.h index a132c2a..08248ae 100644 --- a/src/HomeControlMagic.h +++ b/src/HomeControlMagic.h @@ -1,13 +1,11 @@ #pragma once #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"); + HomeControlMagic(const char* deviceName); void doMagic(); Endpoint* getEndpoint(uint8_t number); @@ -29,39 +27,17 @@ 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); void setTopic(char* topic, char* endpoint_id); - char* m_name; - - NetworkObject& m_network_object; - PubSubClient& m_mqtt_client; - - uint8_t m_number_of_endpoints; - Endpoint* m_endpoints_pointers[10]; - - const uint16_t m_mqtt_port = 1883; + const char* m_name; 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; - + uint8_t m_number_of_endpoints; + Endpoint* m_endpoints_pointers[10]; char m_base_topic[20]; - bool m_broker_connected; - - bool m_start_done; + bool m_broker_was_connected; }; diff --git a/src/arduinoWrapper/ArduinoDebugLed.h b/src/arduinoWrapper/ArduinoDebugLed.h new file mode 100644 index 0000000..ce536dd --- /dev/null +++ b/src/arduinoWrapper/ArduinoDebugLed.h @@ -0,0 +1,55 @@ +#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..46a9d4d --- /dev/null +++ b/src/arduinoWrapper/ArduinoEspWrapper.cpp @@ -0,0 +1,152 @@ +#ifdef ESP8266 +#include "Arduino.h" +#include "ArduinoEspWrapper.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 + Serial.println(F("ESP settings not set!!!")); +#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()); + + // 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; +} + +#else // ESP8266 +#error "You did not choose ESP board" +#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..1dfb5a6 --- /dev/null +++ b/src/arduinoWrapper/ArduinoEspWrapper.h @@ -0,0 +1,17 @@ +#pragma once + +#include "ESP8266WiFi.h" + +void networkLoop(); +void networkSetup(); +void networkStart(); +void networkChipRestart(); +Client& networkGetClient(); + +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(); diff --git a/src/arduinoWrapper/ArduinoEthernetWrapper.cpp b/src/arduinoWrapper/ArduinoEthernetWrapper.cpp new file mode 100644 index 0000000..2da845f --- /dev/null +++ b/src/arduinoWrapper/ArduinoEthernetWrapper.cpp @@ -0,0 +1 @@ +#include "ArduinoEthernetWrapper.h" diff --git a/src/arduinoWrapper/ArduinoEthernetWrapper.h b/src/arduinoWrapper/ArduinoEthernetWrapper.h new file mode 100644 index 0000000..7b9637e --- /dev/null +++ b/src/arduinoWrapper/ArduinoEthernetWrapper.h @@ -0,0 +1 @@ +#pragma once \ No newline at end of file diff --git a/src/arduinoWrapper/ArduinoNetworkInterface.h b/src/arduinoWrapper/ArduinoNetworkInterface.h new file mode 100644 index 0000000..e73ecad --- /dev/null +++ b/src/arduinoWrapper/ArduinoNetworkInterface.h @@ -0,0 +1 @@ +#include "ArduinoEspWrapper.h" diff --git a/src/arduinoWrapper/ArduinoWrapper.cpp b/src/arduinoWrapper/ArduinoWrapper.cpp new file mode 100644 index 0000000..af7cabe --- /dev/null +++ b/src/arduinoWrapper/ArduinoWrapper.cpp @@ -0,0 +1,187 @@ +#include "Arduino.h" +#include "ArduinoWrapper.h" +#include "PubSubClient.h" +#include "ArduinoDebugLed.h" +#include "ArduinoNetworkInterface.h" +#include "helperFunctions.h" + +// 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(networkGetClient()); +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) +{ + networkLoop(); + + 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() +{ +} + +char* wrapperGetTopicBuffer() +{ + return m_topic_buffer; +} + +char* wrapperGetMessageBuffer() +{ + return m_message_buffer; +} + +void wrapperPublish() +{ + 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 HCM_DEBUG + Serial.println(F("Trying to reconnect to mqtt broker")); + #endif + // Attempt to connect + if(m_mqtt_client.connect(getUniqueId(), m_username, m_password)) + { + #ifdef HCM_DEBUG + Serial.println(F("Success")); + #endif + // ... and resubscribe + wrapperSubscribeNow(); + return true; + } + else + { + #ifdef HCM_DEBUG + Serial.print(F("failed, rc=")); + Serial.println(m_mqtt_client.state()); + #endif + return false; + } +} + +void wrapperSubscribeNow() +{ + + strcat(m_topic_buffer, getUniqueId()); + strcat(m_topic_buffer, "/#"); + #ifdef HCM_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 From 30db178ca99f22d3497db3700491e2b042ac0ba7 Mon Sep 17 00:00:00 2001 From: seky2205 Date: Sun, 9 Sep 2018 19:47:19 +0200 Subject: [PATCH 02/11] working esp connection, encrypted and unencrypted --- examples/basic_OnOff/basic_OnOff.ino | 22 +++++++++------- src/HomeControlMagic.cpp | 21 ++++++++++----- src/HomeControlMagic.h | 1 + src/arduinoWrapper/ArduinoEspWrapper.cpp | 11 +++++++- src/arduinoWrapper/ArduinoEspWrapper.h | 1 + src/arduinoWrapper/ArduinoWrapper.cpp | 33 +++++++++++++++++++----- 6 files changed, 67 insertions(+), 22 deletions(-) diff --git a/examples/basic_OnOff/basic_OnOff.ino b/examples/basic_OnOff/basic_OnOff.ino index 4851638..1a7640a 100644 --- a/examples/basic_OnOff/basic_OnOff.ino +++ b/examples/basic_OnOff/basic_OnOff.ino @@ -1,19 +1,17 @@ -#define ARDUINO -//#define SECURE // should we use SSL encryption? #include "HomeControlMagic.h" #include "arduinoWrapper/ArduinoWrapper.h" #include "arduinoWrapper/ArduinoNetworkInterface.h" #include "Endpoints/EndpointOnOff.h" -//#define DEBUG +#define DEBUG #define DEVICE_PIN LED_BUILTIN // GPIO pin to use, built in led as example -IPAddress gw_ip = {192, 168, 1, 2}; +IPAddress gw_ip = {192, 168, 5, 30}; static const char* const deviceName = "ON_OFF_DEVICE"; // name of device -static const char* const wifi_ssid = "ssid"; -static const char* const wifi_pass = "pass"; +static const char* const wifi_ssid = "ISKONOVAC-SEKI"; +static const char* const wifi_pass = "hrvoje22051994"; static const char* const mqtt_username = "hc"; static const char* const mqtt_password = "magic"; @@ -43,8 +41,6 @@ void controlPin() void setup() { - pinMode(DEVICE_PIN, OUTPUT); - #ifdef DEBUG Serial.begin(115200); Serial.println("Started serial"); @@ -52,11 +48,19 @@ void setup() networkSetSsid(wifi_ssid); networkSetPass(wifi_pass); - networkSetSecure(false); // this must be called before setServer + 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(&endpointOnOff); } diff --git a/src/HomeControlMagic.cpp b/src/HomeControlMagic.cpp index 35e76c8..135f925 100644 --- a/src/HomeControlMagic.cpp +++ b/src/HomeControlMagic.cpp @@ -11,7 +11,7 @@ #include "STMWrapper.h" #endif -//#define HCM_DEBUG +#define HCM_DEBUG static HomeControlMagic* hcm_ptr; @@ -70,16 +70,24 @@ HomeControlMagic::HomeControlMagic(const char* deviceName) EndpointZero* epZ = new EndpointZero(hcm_ptr); epZ->setId("0"); m_endpoints_pointers[m_number_of_endpoints++] = epZ; +} +void HomeControlMagic::setup() +{ m_id = getUniqueId(); strcat(m_base_topic, "d/"); strcat(m_base_topic, m_id); strcat(m_base_topic, "/"); + + wrapperSetCallback(callback); } void HomeControlMagic::doMagic() { + /* + On arduino this is calling loop for pubsubclient and network. Network is calling debugLed loop + */ wrapperLoop(); if(wrapperIsMqttConnected()) @@ -125,7 +133,7 @@ void HomeControlMagic::sendMessage(char* topic, bool message, char* endpoint_id) setTopic(topic, endpoint_id); #ifdef HCM_DEBUG - Serial.println(m_topic_buffer); + Serial.println(m_topic_buffer_ptr); #endif if(message) @@ -149,7 +157,7 @@ void HomeControlMagic::sendMessage(char* topic, uint16_t message, char* endpoint setTopic(topic, endpoint_id); #ifdef HCM_DEBUG - Serial.println(m_topic_buffer); + Serial.println(m_topic_buffer_ptr); #endif itoa(message, m_message_buffer_ptr, 10); @@ -166,7 +174,7 @@ void HomeControlMagic::sendMessage(char* topic, double message, char* endpoint_i setTopic(topic, endpoint_id); #ifdef HCM_DEBUG - Serial.println(m_topic_buffer); + Serial.println(m_topic_buffer_ptr); #endif dtostrf(message, 4, 2, m_message_buffer_ptr); @@ -207,7 +215,8 @@ Serial.println(m_message_buffer_ptr); void HomeControlMagic::announce() { - sendMessage("announce", m_name, "0"); + strcat(m_message_buffer_ptr, m_name); + sendStringMessage("announce", "0"); sendFeedback(); } @@ -238,7 +247,7 @@ void HomeControlMagic::addEndpoint(Endpoint* 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.println(m_message_buffer_ptr); #endif endpoint_ptr->setId(m_message_buffer_ptr); wrapperClearMessageBuffer(); diff --git a/src/HomeControlMagic.h b/src/HomeControlMagic.h index 08248ae..8a42f42 100644 --- a/src/HomeControlMagic.h +++ b/src/HomeControlMagic.h @@ -7,6 +7,7 @@ class HomeControlMagic public: HomeControlMagic(const char* deviceName); void doMagic(); + void setup(); Endpoint* getEndpoint(uint8_t number); void addEndpoint(Endpoint* endpoint_ptr); diff --git a/src/arduinoWrapper/ArduinoEspWrapper.cpp b/src/arduinoWrapper/ArduinoEspWrapper.cpp index 46a9d4d..4b9e51e 100644 --- a/src/arduinoWrapper/ArduinoEspWrapper.cpp +++ b/src/arduinoWrapper/ArduinoEspWrapper.cpp @@ -26,7 +26,12 @@ void networkLoop() if(m_client == nullptr || m_ssid == nullptr || m_password == nullptr) { #ifdef ESP_WRAPPER_DEBUG - Serial.println(F("ESP settings not set!!!")); + 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; } @@ -78,6 +83,10 @@ void networkSetup() } // 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); diff --git a/src/arduinoWrapper/ArduinoEspWrapper.h b/src/arduinoWrapper/ArduinoEspWrapper.h index 1dfb5a6..b08d6b4 100644 --- a/src/arduinoWrapper/ArduinoEspWrapper.h +++ b/src/arduinoWrapper/ArduinoEspWrapper.h @@ -7,6 +7,7 @@ void networkSetup(); void networkStart(); void networkChipRestart(); Client& networkGetClient(); +bool networkIsConnected(); void networkSetSecure(bool secure); bool networkIsSecure(); diff --git a/src/arduinoWrapper/ArduinoWrapper.cpp b/src/arduinoWrapper/ArduinoWrapper.cpp index af7cabe..598e5db 100644 --- a/src/arduinoWrapper/ArduinoWrapper.cpp +++ b/src/arduinoWrapper/ArduinoWrapper.cpp @@ -5,6 +5,8 @@ #include "ArduinoNetworkInterface.h" #include "helperFunctions.h" +#define ARDUINO_WRAPPER_DEBUG + // private vars: #define TOPIC_BUFFER_LENGTH 30 #define MESSAGE_BUFFER_LENGTH 50 @@ -13,7 +15,7 @@ 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(networkGetClient()); +static PubSubClient m_mqtt_client; const char* m_username = nullptr; const char* m_password = nullptr; @@ -27,6 +29,11 @@ void wrapperLoop(bool reconnect) { networkLoop(); + if(!networkIsConnected()) + { + return; + } + long current_time = millis(); if(!m_mqtt_client.connected() && (current_time - m_last_loop_time > 100)) { @@ -58,11 +65,13 @@ void wrapperLoop(bool reconnect) m_last_time_connected = current_time; m_mqtt_client.loop(); } + } void wrapperSetup() { + m_mqtt_client.setClient(networkGetClient()); } char* wrapperGetTopicBuffer() @@ -77,6 +86,12 @@ char* wrapperGetMessageBuffer() 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(); @@ -147,14 +162,20 @@ void wrapperSetUsernamePassword(const char* const username, const char* const pa // private functions: bool wrapperReconnectMqtt() { - #ifdef HCM_DEBUG + #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 HCM_DEBUG + #ifdef ARDUINO_WRAPPER_DEBUG Serial.println(F("Success")); + Serial.flush(); #endif // ... and resubscribe wrapperSubscribeNow(); @@ -162,9 +183,10 @@ bool wrapperReconnectMqtt() } else { - #ifdef HCM_DEBUG + #ifdef ARDUINO_WRAPPER_DEBUG Serial.print(F("failed, rc=")); Serial.println(m_mqtt_client.state()); + Serial.flush(); #endif return false; } @@ -172,10 +194,9 @@ bool wrapperReconnectMqtt() void wrapperSubscribeNow() { - strcat(m_topic_buffer, getUniqueId()); strcat(m_topic_buffer, "/#"); - #ifdef HCM_DEBUG + #ifdef ARDUINO_WRAPPER_DEBUG Serial.println(m_topic_buffer); #endif From 5e52e87612291aa85344eb70bcdb4ef803de2820 Mon Sep 17 00:00:00 2001 From: seky2205 Date: Sun, 23 Sep 2018 15:47:00 +0200 Subject: [PATCH 03/11] added support for ethernet chips --- .../basic_OnOff_ethernet.ino | 71 ++++++++ src/HomeControlMagic.cpp | 1 + src/arduinoWrapper/ArduinoConfig.h | 14 ++ src/arduinoWrapper/ArduinoEspWrapper.cpp | 2 - src/arduinoWrapper/ArduinoEspWrapper.h | 2 + src/arduinoWrapper/ArduinoEthernetWrapper.cpp | 161 ++++++++++++++++++ src/arduinoWrapper/ArduinoEthernetWrapper.h | 29 +++- src/arduinoWrapper/ArduinoNetworkInterface.h | 18 ++ src/arduinoWrapper/ArduinoWrapper.cpp | 2 + 9 files changed, 297 insertions(+), 3 deletions(-) create mode 100644 examples/basic_OnOff_ethernet/basic_OnOff_ethernet.ino create mode 100644 src/arduinoWrapper/ArduinoConfig.h 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..67e8d19 --- /dev/null +++ b/examples/basic_OnOff_ethernet/basic_OnOff_ethernet.ino @@ -0,0 +1,71 @@ +#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 DEBUG + +#define DEVICE_PIN LED_BUILTIN // GPIO pin to use, built in led as example + +IPAddress gw_ip = {192, 168, 5, 30}; +static const char* const deviceName = "ON_OFF_DEVICE"; // name of device +static const char* const mqtt_username = "hc"; +static const char* const mqtt_password = "magic"; + +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/src/HomeControlMagic.cpp b/src/HomeControlMagic.cpp index 135f925..107ad34 100644 --- a/src/HomeControlMagic.cpp +++ b/src/HomeControlMagic.cpp @@ -5,6 +5,7 @@ #include "Endpoints/EndpointZero.h" #ifdef ARDUINO +#include "arduinoWrapper/ArduinoConfig.h" #include "arduinoWrapper/ArduinoNetworkInterface.h" #include "arduinoWrapper/ArduinoWrapper.h" #elif defined STM diff --git a/src/arduinoWrapper/ArduinoConfig.h b/src/arduinoWrapper/ArduinoConfig.h new file mode 100644 index 0000000..42689ae --- /dev/null +++ b/src/arduinoWrapper/ArduinoConfig.h @@ -0,0 +1,14 @@ +#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/ArduinoEspWrapper.cpp b/src/arduinoWrapper/ArduinoEspWrapper.cpp index 4b9e51e..6963c29 100644 --- a/src/arduinoWrapper/ArduinoEspWrapper.cpp +++ b/src/arduinoWrapper/ArduinoEspWrapper.cpp @@ -156,6 +156,4 @@ char* getUniqueId() return m_uid; } -#else // ESP8266 -#error "You did not choose ESP board" #endif // ESP8266 \ No newline at end of file diff --git a/src/arduinoWrapper/ArduinoEspWrapper.h b/src/arduinoWrapper/ArduinoEspWrapper.h index b08d6b4..b6ed012 100644 --- a/src/arduinoWrapper/ArduinoEspWrapper.h +++ b/src/arduinoWrapper/ArduinoEspWrapper.h @@ -1,5 +1,6 @@ #pragma once +#ifdef ESP8266 #include "ESP8266WiFi.h" void networkLoop(); @@ -16,3 +17,4 @@ 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 index 2da845f..77f229f 100644 --- a/src/arduinoWrapper/ArduinoEthernetWrapper.cpp +++ b/src/arduinoWrapper/ArduinoEthernetWrapper.cpp @@ -1 +1,162 @@ +#include "ArduinoConfig.h" +#if defined(ETHERNET) || defined(ETHERNET2) + +#include "Arduino.h" #include "ArduinoEthernetWrapper.h" +#include "ArduinoDebugLed.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 index 7b9637e..9f23cbd 100644 --- a/src/arduinoWrapper/ArduinoEthernetWrapper.h +++ b/src/arduinoWrapper/ArduinoEthernetWrapper.h @@ -1 +1,28 @@ -#pragma once \ No newline at end of file +#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 index e73ecad..65c65f2 100644 --- a/src/arduinoWrapper/ArduinoNetworkInterface.h +++ b/src/arduinoWrapper/ArduinoNetworkInterface.h @@ -1 +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 index 598e5db..b545eba 100644 --- a/src/arduinoWrapper/ArduinoWrapper.cpp +++ b/src/arduinoWrapper/ArduinoWrapper.cpp @@ -2,6 +2,7 @@ #include "ArduinoWrapper.h" #include "PubSubClient.h" #include "ArduinoDebugLed.h" +#include "ArduinoConfig.h" #include "ArduinoNetworkInterface.h" #include "helperFunctions.h" @@ -27,6 +28,7 @@ static void wrapperSubscribeNow(); // function implementation: void wrapperLoop(bool reconnect) { + // wrapper loop will also call network loop networkLoop(); if(!networkIsConnected()) From 51e153b8067b9415579756eae2e8f9d8fe363c63 Mon Sep 17 00:00:00 2001 From: seky2205 Date: Sun, 23 Sep 2018 15:56:21 +0200 Subject: [PATCH 04/11] updated docs --- README.md | 14 +++++++++----- src/arduinoWrapper/README.md | 9 +++++++++ 2 files changed, 18 insertions(+), 5 deletions(-) create mode 100644 src/arduinoWrapper/README.md 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/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 From 1988ccc01a3d981e4db9bedf599fd30ce81beb54 Mon Sep 17 00:00:00 2001 From: seky2205 Date: Sun, 23 Sep 2018 15:57:06 +0200 Subject: [PATCH 05/11] turned off debug output --- src/HomeControlMagic.cpp | 2 +- src/arduinoWrapper/ArduinoEthernetWrapper.cpp | 2 +- src/arduinoWrapper/ArduinoWrapper.cpp | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/HomeControlMagic.cpp b/src/HomeControlMagic.cpp index 107ad34..b634d4f 100644 --- a/src/HomeControlMagic.cpp +++ b/src/HomeControlMagic.cpp @@ -12,7 +12,7 @@ #include "STMWrapper.h" #endif -#define HCM_DEBUG +//#define HCM_DEBUG static HomeControlMagic* hcm_ptr; diff --git a/src/arduinoWrapper/ArduinoEthernetWrapper.cpp b/src/arduinoWrapper/ArduinoEthernetWrapper.cpp index 77f229f..10799a1 100644 --- a/src/arduinoWrapper/ArduinoEthernetWrapper.cpp +++ b/src/arduinoWrapper/ArduinoEthernetWrapper.cpp @@ -9,7 +9,7 @@ #error "You need to define ETH_MAC with mac address. Make sure it is unique" #endif -#define ETHERNET_WRAPPER_DEBUG +//#define ETHERNET_WRAPPER_DEBUG static Client* m_client = nullptr; static char m_uid[20] = {'0'}; diff --git a/src/arduinoWrapper/ArduinoWrapper.cpp b/src/arduinoWrapper/ArduinoWrapper.cpp index b545eba..a3b25a1 100644 --- a/src/arduinoWrapper/ArduinoWrapper.cpp +++ b/src/arduinoWrapper/ArduinoWrapper.cpp @@ -6,7 +6,7 @@ #include "ArduinoNetworkInterface.h" #include "helperFunctions.h" -#define ARDUINO_WRAPPER_DEBUG +//#define ARDUINO_WRAPPER_DEBUG // private vars: #define TOPIC_BUFFER_LENGTH 30 From dec1c88957ff0a13d3faf507d2acef19bc6c9cad Mon Sep 17 00:00:00 2001 From: seky2205 Date: Sun, 23 Sep 2018 15:57:30 +0200 Subject: [PATCH 06/11] removed dependancy to NetworkLoops --- library.json | 4 ---- 1 file changed, 4 deletions(-) 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" - } ] } From 34fa2650ebf5dfecda4675ed7d76c674126d4eb5 Mon Sep 17 00:00:00 2001 From: seky2205 Date: Sun, 23 Sep 2018 16:15:39 +0200 Subject: [PATCH 07/11] fixed all examples with new changes --- examples/basic_Color/basic_Color.ino | 43 +++++++++++------ examples/basic_Level/basic_Level.ino | 43 +++++++++++------ examples/basic_Motion/basic_Motion.ino | 46 +++++++++++++------ examples/basic_OnOff/basic_OnOff.ino | 12 +++-- .../basic_OnOff_ethernet.ino | 4 +- .../basic_Temperature/basic_Temperature.ino | 38 +++++++++++---- .../basic_TemperatureTarget_OnOff.ino | 45 ++++++++++++------ .../basic_multipleEndpoints.ino | 35 ++++++++++---- src/arduinoWrapper/ArduinoConfig.h | 2 +- 9 files changed, 189 insertions(+), 79 deletions(-) diff --git a/examples/basic_Color/basic_Color.ino b/examples/basic_Color/basic_Color.ino index a4e455b..c4279f1 100644 --- a/examples/basic_Color/basic_Color.ino +++ b/examples/basic_Color/basic_Color.ino @@ -1,9 +1,11 @@ #include "HomeControlMagic.h" + +// in Config file define ethernet options +#include "arduinoWrapper/ArduinoConfig.h" +#include "arduinoWrapper/ArduinoWrapper.h" +#include "arduinoWrapper/ArduinoNetworkInterface.h" + #include "Endpoints/EndpointColor.h" -#define ESP_LOOP -#define WIFI_SSID "" // Wifi network name -#define WIFI_PASS "" // Wifi password -#include "NetworkLoops.hpp" //#define DEBUG @@ -11,10 +13,12 @@ #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 - -static char* const GW_IP = "GW_IP"; // gateway IP address +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 const char* const mqtt_username = "hc"; +static const char* const mqtt_password = "magic"; bool active_pin_state = true; // reverse pin state @@ -24,7 +28,7 @@ uint16_t last_color_R = 0; uint16_t last_color_G = 0; uint16_t last_color_B = 0; -HomeControlMagic hcm(GW_IP, deviceName, network); +HomeControlMagic hcm(deviceName); EndpointColor endpointColor(&hcm); uint16_t adjLevel(uint16_t color_X, uint16_t level) @@ -80,16 +84,29 @@ void controlPin() void setup() { - pinMode(R_PIN, OUTPUT); - pinMode(G_PIN, OUTPUT); - pinMode(B_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(); + + wrapperSetServer(gw_ip); + wrapperSetUsernamePassword(mqtt_username, mqtt_password); + wrapperSetup(); + + hcm.setup(); + + // DO NOT TOUCH ANYTHING BEFORE THIS LINE IN SETUP FUNCTION + + pinMode(R_PIN, OUTPUT); + pinMode(G_PIN, OUTPUT); + pinMode(B_PIN, OUTPUT); + hcm.addEndpoint(&endpointColor); } diff --git a/examples/basic_Level/basic_Level.ino b/examples/basic_Level/basic_Level.ino index 8b72587..7663f6d 100644 --- a/examples/basic_Level/basic_Level.ino +++ b/examples/basic_Level/basic_Level.ino @@ -1,25 +1,29 @@ #include "HomeControlMagic.h" + +// in Config file define ethernet options +#include "arduinoWrapper/ArduinoConfig.h" +#include "arduinoWrapper/ArduinoWrapper.h" +#include "arduinoWrapper/ArduinoNetworkInterface.h" + #include "Endpoints/EndpointLevel.h" -#define ESP_LOOP -#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 - -static char* const GW_IP = "GW_IP"; // gateway IP address +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 const char* const mqtt_username = "hc"; +static const char* const mqtt_password = "magic"; bool active_pin_state = false; // reverse pin state bool last_state = false; uint16_t last_level = 0; -HomeControlMagic hcm(GW_IP, deviceName, network); +HomeControlMagic hcm(deviceName); EndpointLevel endpointLevel(&hcm); void controlPin() @@ -52,14 +56,27 @@ void controlPin() void setup() { - pinMode(DEVICE_PIN, OUTPUT); - -#ifdef DEBUG + #ifdef DEBUG Serial.begin(115200); Serial.println("Started serial"); -#endif + #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(DEVICE_PIN, OUTPUT); - network.setReconnectTime(RECONNECTION_TIME); hcm.addEndpoint(&endpointLevel); } diff --git a/examples/basic_Motion/basic_Motion.ino b/examples/basic_Motion/basic_Motion.ino index c593963..44ed198 100644 --- a/examples/basic_Motion/basic_Motion.ino +++ b/examples/basic_Motion/basic_Motion.ino @@ -1,24 +1,29 @@ #include "HomeControlMagic.h" + +// in Config file define ethernet options +#include "arduinoWrapper/ArduinoConfig.h" +#include "arduinoWrapper/ArduinoWrapper.h" +#include "arduinoWrapper/ArduinoNetworkInterface.h" + #include "Endpoints/EndpointMotion.h" -#define ESP_LOOP -#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 - -static char* const GW_IP = "GW_IP"; // gateway IP address +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 const char* const mqtt_username = "hc"; +static const char* const mqtt_password = "magic"; bool active_pin_state = false; // reverse pin state bool last_motion = false; -HomeControlMagic hcm(GW_IP, deviceName, network); +HomeControlMagic hcm(deviceName); EndpointMotion endpointMotion(&hcm); void controlPin() @@ -43,17 +48,30 @@ void controlPin() 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); + 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); } diff --git a/examples/basic_OnOff/basic_OnOff.ino b/examples/basic_OnOff/basic_OnOff.ino index 1a7640a..73e9793 100644 --- a/examples/basic_OnOff/basic_OnOff.ino +++ b/examples/basic_OnOff/basic_OnOff.ino @@ -1,17 +1,21 @@ #include "HomeControlMagic.h" + +// in Config file define ethernet options +#include "arduinoWrapper/ArduinoConfig.h" #include "arduinoWrapper/ArduinoWrapper.h" #include "arduinoWrapper/ArduinoNetworkInterface.h" + #include "Endpoints/EndpointOnOff.h" -#define DEBUG +//#define DEBUG #define DEVICE_PIN LED_BUILTIN // GPIO pin to use, built in led as example -IPAddress gw_ip = {192, 168, 5, 30}; +IPAddress gw_ip = {192, 168, 1, 10}; static const char* const deviceName = "ON_OFF_DEVICE"; // name of device -static const char* const wifi_ssid = "ISKONOVAC-SEKI"; -static const char* const wifi_pass = "hrvoje22051994"; +static const char* const wifi_ssid = "WIFI-SSID"; +static const char* const wifi_pass = "WIFI-PASS"; static const char* const mqtt_username = "hc"; static const char* const mqtt_password = "magic"; diff --git a/examples/basic_OnOff_ethernet/basic_OnOff_ethernet.ino b/examples/basic_OnOff_ethernet/basic_OnOff_ethernet.ino index 67e8d19..613254c 100644 --- a/examples/basic_OnOff_ethernet/basic_OnOff_ethernet.ino +++ b/examples/basic_OnOff_ethernet/basic_OnOff_ethernet.ino @@ -2,9 +2,9 @@ // in Config file define ethernet options #include "arduinoWrapper/ArduinoConfig.h" - #include "arduinoWrapper/ArduinoNetworkInterface.h" #include "arduinoWrapper/ArduinoWrapper.h" + #include "Endpoints/EndpointOnOff.h" @@ -12,7 +12,7 @@ #define DEVICE_PIN LED_BUILTIN // GPIO pin to use, built in led as example -IPAddress gw_ip = {192, 168, 5, 30}; +IPAddress gw_ip = {192, 168, 1, 10}; static const char* const deviceName = "ON_OFF_DEVICE"; // name of device static const char* const mqtt_username = "hc"; static const char* const mqtt_password = "magic"; diff --git a/examples/basic_Temperature/basic_Temperature.ino b/examples/basic_Temperature/basic_Temperature.ino index 9fa6d14..4d3d0f0 100644 --- a/examples/basic_Temperature/basic_Temperature.ino +++ b/examples/basic_Temperature/basic_Temperature.ino @@ -1,35 +1,53 @@ #include "HomeControlMagic.h" + +// in Config file define ethernet options +#include "arduinoWrapper/ArduinoConfig.h" +#include "arduinoWrapper/ArduinoWrapper.h" +#include "arduinoWrapper/ArduinoNetworkInterface.h" + #include "Endpoints/EndpointTemperature.h" #include "DHT.h" -#define ESP_LOOP -#define WIFI_SSID "" // Wifi network name -#define WIFI_PASS "" // Wifi password -#include "NetworkLoops.hpp" //#define DEBUG #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 -static char* const GW_IP = "GW_IP"; // gateway IP address +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 const char* const mqtt_username = "hc"; +static const char* const mqtt_password = "magic"; -HomeControlMagic hcm(GW_IP, deviceName, network); +HomeControlMagic hcm(deviceName); EndpointTemperature endpointTemperature(&hcm); DHT dht(DHT_PIN, DHTTYPE); void setup() { -#ifdef DEBUG + #ifdef DEBUG Serial.begin(115200); Serial.println("Started serial"); - #endif + #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 - network.setReconnectTime(RECONNECTION_TIME); hcm.addEndpoint(&endpointTemperature); dht.begin(); diff --git a/examples/basic_TemperatureTarget_OnOff/basic_TemperatureTarget_OnOff.ino b/examples/basic_TemperatureTarget_OnOff/basic_TemperatureTarget_OnOff.ino index 708db89..edbbdcf 100644 --- a/examples/basic_TemperatureTarget_OnOff/basic_TemperatureTarget_OnOff.ino +++ b/examples/basic_TemperatureTarget_OnOff/basic_TemperatureTarget_OnOff.ino @@ -1,11 +1,14 @@ #include "HomeControlMagic.h" + +// in Config file define ethernet options +#include "arduinoWrapper/ArduinoConfig.h" +#include "arduinoWrapper/ArduinoWrapper.h" +#include "arduinoWrapper/ArduinoNetworkInterface.h" + #include "Endpoints/EndpointTemperatureTarget.h" #include "Endpoints/EndpointOnOff.h" #include "DHT.h" -#define ESP_LOOP -#define WIFI_SSID "" // Wifi network name -#define WIFI_PASS "" // Wifi password -#include "NetworkLoops.hpp" + //#define DEBUG @@ -16,13 +19,17 @@ #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 +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 const char* const mqtt_username = "hc"; +static const char* const mqtt_password = "magic"; bool active_pin_state = false; // reverse pin state bool last_state = false; -HomeControlMagic hcm(GW_IP, deviceName, network); +HomeControlMagic hcm(deviceName); EndpointTemperatureTarget endpointTemperatureTarget(&hcm); EndpointOnOff endpointOnOff(&hcm); @@ -49,22 +56,34 @@ void controlPin() void setup() { - pinMode(DEVICE_PIN, OUTPUT); - - network.setReconnectTime(RECONNECTION_TIME); - - double temperature = dht.readTemperature(); - endpointTemperatureTarget.setTemperatureTarget(temperature); - #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(DEVICE_PIN, OUTPUT); + hcm.addEndpoint(&endpointTemperatureTarget); hcm.addEndpoint(&endpointOnOff); dht.begin(); + + double temperature = dht.readTemperature(); + endpointTemperatureTarget.setTemperatureTarget(temperature); } void loop() diff --git a/examples/basic_multipleEndpoints/basic_multipleEndpoints.ino b/examples/basic_multipleEndpoints/basic_multipleEndpoints.ino index 79d72b0..f301a91 100644 --- a/examples/basic_multipleEndpoints/basic_multipleEndpoints.ino +++ b/examples/basic_multipleEndpoints/basic_multipleEndpoints.ino @@ -1,10 +1,12 @@ #include "HomeControlMagic.h" + +// in Config file define ethernet options +#include "arduinoWrapper/ArduinoConfig.h" + +#include "arduinoWrapper/ArduinoWrapper.h" +#include "arduinoWrapper/ArduinoNetworkInterface.h" #include "Endpoints/EndpointTemperature.h" #include "DHT.h" -#define ESP_LOOP -#define WIFI_SSID "" // Wifi network name -#define WIFI_PASS "" // Wifi password -#include "NetworkLoops.hpp" #define DEBUG @@ -15,12 +17,14 @@ #define DHTTYPE DHT22 // DHT type -#define RECONNECTION_TIME 5 // network reconnection time in seconds - -static char* const GW_IP = "GW_IP"; // gateway IP address +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 const char* const mqtt_username = "hc"; +static const char* const mqtt_password = "magic"; -HomeControlMagic hcm(GW_IP, deviceName, network); +HomeControlMagic hcm(deviceName); EndpointTemperature endpointTemperature_1(&hcm); EndpointTemperature endpointTemperature_2(&hcm); @@ -39,12 +43,25 @@ void setup() Serial.println("Started serial"); #endif + networkSetSsid(wifi_ssid); + networkSetPass(wifi_pass); + 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 + 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); diff --git a/src/arduinoWrapper/ArduinoConfig.h b/src/arduinoWrapper/ArduinoConfig.h index 42689ae..de3b573 100644 --- a/src/arduinoWrapper/ArduinoConfig.h +++ b/src/arduinoWrapper/ArduinoConfig.h @@ -1,7 +1,7 @@ #pragma once //#define ETHERNET // for WIZ5100 -#define ETHERNET2 // for WIZ5500 +//#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 From eeb853cf0da4b9e0c11541fb0b8c028bef6a78ce Mon Sep 17 00:00:00 2001 From: seky2205 Date: Sun, 23 Sep 2018 17:07:37 +0200 Subject: [PATCH 08/11] removed Arduino.h and changed byte to uint8_t --- src/Endpoint.h | 4 +++- src/Endpoints/EndpointColor.cpp | 2 +- src/Endpoints/EndpointColor.h | 2 +- src/Endpoints/EndpointIdentify.cpp | 2 +- src/Endpoints/EndpointIdentify.h | 2 +- src/Endpoints/EndpointLevel.cpp | 2 +- src/Endpoints/EndpointLevel.h | 2 +- src/Endpoints/EndpointMotion.cpp | 2 +- src/Endpoints/EndpointMotion.h | 2 +- src/Endpoints/EndpointOnOff.cpp | 2 +- src/Endpoints/EndpointOnOff.h | 2 +- src/Endpoints/EndpointTemperature.cpp | 2 +- src/Endpoints/EndpointTemperature.h | 2 +- src/Endpoints/EndpointTemperatureTarget.cpp | 2 +- src/Endpoints/EndpointTemperatureTarget.h | 2 +- src/Endpoints/EndpointZero.cpp | 2 +- src/Endpoints/EndpointZero.h | 2 +- src/HomeControlMagic.cpp | 1 - src/HomeControlMagic.h | 4 ++++ src/helperFunctions.cpp | 14 +++++++------- src/helperFunctions.h | 14 +++++++------- 21 files changed, 37 insertions(+), 32 deletions(-) diff --git a/src/Endpoint.h b/src/Endpoint.h index d45daec..34bdac5 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; @@ -21,7 +23,7 @@ class Endpoint 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; diff --git a/src/Endpoints/EndpointColor.cpp b/src/Endpoints/EndpointColor.cpp index b8e3301..8796f95 100644 --- a/src/Endpoints/EndpointColor.cpp +++ b/src/Endpoints/EndpointColor.cpp @@ -69,7 +69,7 @@ void EndpointColor::getRGBcharPtr(char* buffer) 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")); diff --git a/src/Endpoints/EndpointColor.h b/src/Endpoints/EndpointColor.h index 686b151..8ffef41 100644 --- a/src/Endpoints/EndpointColor.h +++ b/src/Endpoints/EndpointColor.h @@ -11,7 +11,7 @@ class EndpointColor : public Endpoint 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(); diff --git a/src/Endpoints/EndpointIdentify.cpp b/src/Endpoints/EndpointIdentify.cpp index f607020..06a34ca 100644 --- a/src/Endpoints/EndpointIdentify.cpp +++ b/src/Endpoints/EndpointIdentify.cpp @@ -14,7 +14,7 @@ EndpointIdentify::EndpointIdentify(HomeControlMagic* hcm_ptr, int8_t pin) 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")); diff --git a/src/Endpoints/EndpointIdentify.h b/src/Endpoints/EndpointIdentify.h index 0596054..da5c82d 100644 --- a/src/Endpoints/EndpointIdentify.h +++ b/src/Endpoints/EndpointIdentify.h @@ -7,7 +7,7 @@ class EndpointIdentify : public Endpoint 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: uint8_t m_pin; diff --git a/src/Endpoints/EndpointLevel.cpp b/src/Endpoints/EndpointLevel.cpp index f6be9e2..047c90b 100644 --- a/src/Endpoints/EndpointLevel.cpp +++ b/src/Endpoints/EndpointLevel.cpp @@ -38,7 +38,7 @@ uint16_t EndpointLevel::getLevel() 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")); diff --git a/src/Endpoints/EndpointLevel.h b/src/Endpoints/EndpointLevel.h index 3a1e203..6c12134 100644 --- a/src/Endpoints/EndpointLevel.h +++ b/src/Endpoints/EndpointLevel.h @@ -10,7 +10,7 @@ class EndpointLevel : public Endpoint 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(); diff --git a/src/Endpoints/EndpointMotion.cpp b/src/Endpoints/EndpointMotion.cpp index 5b22603..b3b0336 100644 --- a/src/Endpoints/EndpointMotion.cpp +++ b/src/Endpoints/EndpointMotion.cpp @@ -26,7 +26,7 @@ bool EndpointMotion::getState() 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")); diff --git a/src/Endpoints/EndpointMotion.h b/src/Endpoints/EndpointMotion.h index cf4cfb0..dfd3b65 100644 --- a/src/Endpoints/EndpointMotion.h +++ b/src/Endpoints/EndpointMotion.h @@ -10,7 +10,7 @@ class EndpointMotion : public Endpoint 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(); diff --git a/src/Endpoints/EndpointOnOff.cpp b/src/Endpoints/EndpointOnOff.cpp index 135c2a7..463802d 100644 --- a/src/Endpoints/EndpointOnOff.cpp +++ b/src/Endpoints/EndpointOnOff.cpp @@ -26,7 +26,7 @@ bool EndpointOnOff::getState() 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")); diff --git a/src/Endpoints/EndpointOnOff.h b/src/Endpoints/EndpointOnOff.h index 55619e1..f7bcbad 100644 --- a/src/Endpoints/EndpointOnOff.h +++ b/src/Endpoints/EndpointOnOff.h @@ -10,7 +10,7 @@ class EndpointOnOff : public Endpoint 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(); diff --git a/src/Endpoints/EndpointTemperature.cpp b/src/Endpoints/EndpointTemperature.cpp index 3f196f8..1d5666b 100644 --- a/src/Endpoints/EndpointTemperature.cpp +++ b/src/Endpoints/EndpointTemperature.cpp @@ -26,7 +26,7 @@ double EndpointTemperature::getTemperature() 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")); diff --git a/src/Endpoints/EndpointTemperature.h b/src/Endpoints/EndpointTemperature.h index 491ab7d..7eef5bf 100644 --- a/src/Endpoints/EndpointTemperature.h +++ b/src/Endpoints/EndpointTemperature.h @@ -11,7 +11,7 @@ class EndpointTemperature : public Endpoint 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(); diff --git a/src/Endpoints/EndpointTemperatureTarget.cpp b/src/Endpoints/EndpointTemperatureTarget.cpp index 098f3d9..a28f982 100644 --- a/src/Endpoints/EndpointTemperatureTarget.cpp +++ b/src/Endpoints/EndpointTemperatureTarget.cpp @@ -35,7 +35,7 @@ double EndpointTemperatureTarget::getTemperatureTarget() 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")); diff --git a/src/Endpoints/EndpointTemperatureTarget.h b/src/Endpoints/EndpointTemperatureTarget.h index c038441..ed4de98 100644 --- a/src/Endpoints/EndpointTemperatureTarget.h +++ b/src/Endpoints/EndpointTemperatureTarget.h @@ -10,7 +10,7 @@ class EndpointTemperatureTarget : public Endpoint 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); diff --git a/src/Endpoints/EndpointZero.cpp b/src/Endpoints/EndpointZero.cpp index 555251c..eeeda1f 100644 --- a/src/Endpoints/EndpointZero.cpp +++ b/src/Endpoints/EndpointZero.cpp @@ -24,7 +24,7 @@ void EndpointZero::sendFeedbackMessage() // 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")); diff --git a/src/Endpoints/EndpointZero.h b/src/Endpoints/EndpointZero.h index bf19a0e..cb13577 100644 --- a/src/Endpoints/EndpointZero.h +++ b/src/Endpoints/EndpointZero.h @@ -11,6 +11,6 @@ class EndpointZero : public Endpoint 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 b634d4f..c055b40 100644 --- a/src/HomeControlMagic.cpp +++ b/src/HomeControlMagic.cpp @@ -1,5 +1,4 @@ #include "HomeControlMagic.h" -#include "Arduino.h" #include "helperFunctions.h" #include "Endpoints/EndpointZero.h" diff --git a/src/HomeControlMagic.h b/src/HomeControlMagic.h index 8a42f42..1146dfe 100644 --- a/src/HomeControlMagic.h +++ b/src/HomeControlMagic.h @@ -1,5 +1,9 @@ #pragma once +#ifdef ARDUINO +#include "Arduino.h" +#endif + #include "Endpoint.h" class HomeControlMagic diff --git a/src/helperFunctions.cpp b/src/helperFunctions.cpp index 87b0b8d..77a683b 100644 --- a/src/helperFunctions.cpp +++ b/src/helperFunctions.cpp @@ -1,6 +1,6 @@ #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++) { @@ -47,7 +47,7 @@ int lineContains(const char *str, const char *sfind) 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, ".")) @@ -78,7 +78,7 @@ float extractFloat(byte* text, unsigned int 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, ".")) @@ -109,7 +109,7 @@ double extractDouble(byte* text, unsigned int length) return temp; } -int extractInteger(byte* text, unsigned int length) +int extractInteger(uint8_t* text, unsigned int length) { int temp = 0; @@ -126,7 +126,7 @@ int extractInteger(byte* text, unsigned int length) return temp; } -bool extractState(byte* text, unsigned int length) +bool extractState(uint8_t* text, unsigned int length) { bool temp = false; if (text[0] != '\0') @@ -145,7 +145,7 @@ bool extractState(byte* text, unsigned int length) } -bool extractBool(byte* text, unsigned int length) +bool extractBool(uint8_t* text, unsigned int length) { bool temp = false; if (text[0] != '\0') @@ -168,7 +168,7 @@ bool extractBool(byte* text, unsigned int length) return temp; } -RGB extractRGB(byte* text, unsigned int length) +RGB extractRGB(uint8_t* text, unsigned int length) { RGB rgb; if(text[length] != '\0') diff --git a/src/helperFunctions.h b/src/helperFunctions.h index d2f1758..f61d3bf 100644 --- a/src/helperFunctions.h +++ b/src/helperFunctions.h @@ -10,11 +10,11 @@ struct RGB }; void clearBuffer(char* text, uint8_t length); -void clearByte(byte* text, unsigned int length); +void clearByte(uint8_t* 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); +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); From 2e3bd2fbc1278e8c40441f74cf70851452ea49df Mon Sep 17 00:00:00 2001 From: Hrvoje Date: Sun, 23 Sep 2018 12:11:08 -0700 Subject: [PATCH 09/11] added formater and formated lib --- .clang-format | 32 ++ examples/basic_Color/basic_Color.ino | 135 ++++---- examples/basic_Level/basic_Level.ino | 88 ++--- examples/basic_Motion/basic_Motion.ino | 81 +++-- examples/basic_OnOff/basic_OnOff.ino | 69 ++-- .../basic_OnOff_ethernet.ino | 63 ++-- .../basic_Temperature/basic_Temperature.ino | 99 +++--- .../basic_TemperatureTarget_OnOff.ino | 148 ++++---- .../basic_multipleEndpoints.ino | 131 +++---- src/Endpoint.cpp | 5 +- src/Endpoint.h | 31 +- src/Endpoints/EndpointColor.cpp | 152 ++++----- src/Endpoints/EndpointColor.h | 4 +- src/Endpoints/EndpointIdentify.cpp | 36 +- src/Endpoints/EndpointIdentify.h | 4 +- src/Endpoints/EndpointLevel.cpp | 100 +++--- src/Endpoints/EndpointLevel.h | 4 +- src/Endpoints/EndpointMotion.cpp | 58 ++-- src/Endpoints/EndpointMotion.h | 4 +- src/Endpoints/EndpointOnOff.cpp | 66 ++-- src/Endpoints/EndpointOnOff.h | 4 +- src/Endpoints/EndpointTemperature.cpp | 58 ++-- src/Endpoints/EndpointTemperature.h | 14 +- src/Endpoints/EndpointTemperatureTarget.cpp | 87 +++-- src/Endpoints/EndpointTemperatureTarget.h | 5 +- src/Endpoints/EndpointZero.cpp | 28 +- src/Endpoints/EndpointZero.h | 3 +- src/HomeControlMagic.cpp | 320 +++++++++--------- src/HomeControlMagic.h | 7 +- src/arduinoWrapper/ArduinoConfig.h | 27 +- src/arduinoWrapper/ArduinoDebugLed.h | 4 +- src/arduinoWrapper/ArduinoEspWrapper.cpp | 12 +- src/arduinoWrapper/ArduinoEspWrapper.h | 2 +- src/arduinoWrapper/ArduinoEthernetWrapper.cpp | 40 ++- src/arduinoWrapper/ArduinoWrapper.cpp | 222 ++++++------ src/helperFunctions.cpp | 281 ++++++++------- src/helperFunctions.h | 8 +- 37 files changed, 1231 insertions(+), 1201 deletions(-) create mode 100644 .clang-format 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/examples/basic_Color/basic_Color.ino b/examples/basic_Color/basic_Color.ino index c4279f1..9af8380 100644 --- a/examples/basic_Color/basic_Color.ino +++ b/examples/basic_Color/basic_Color.ino @@ -2,25 +2,25 @@ // in Config file define ethernet options #include "arduinoWrapper/ArduinoConfig.h" -#include "arduinoWrapper/ArduinoWrapper.h" #include "arduinoWrapper/ArduinoNetworkInterface.h" +#include "arduinoWrapper/ArduinoWrapper.h" #include "Endpoints/EndpointColor.h" //#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 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) IPAddress gw_ip = {192, 168, 1, 10}; -static char* const deviceName = "COLOR_DEVICE"; // name of device +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 const char* const mqtt_username = "hc"; static const char* const mqtt_password = "magic"; -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; @@ -33,85 +33,86 @@ 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() { - #ifdef DEBUG - Serial.begin(115200); - Serial.println("Started serial"); - #endif +#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(); + 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(); + wrapperSetServer(gw_ip); + wrapperSetUsernamePassword(mqtt_username, mqtt_password); + wrapperSetup(); - hcm.setup(); + hcm.setup(); - // DO NOT TOUCH ANYTHING BEFORE THIS LINE IN SETUP FUNCTION + // DO NOT TOUCH ANYTHING BEFORE THIS LINE IN SETUP FUNCTION - pinMode(R_PIN, OUTPUT); - pinMode(G_PIN, OUTPUT); - pinMode(B_PIN, OUTPUT); + pinMode(R_PIN, OUTPUT); + pinMode(G_PIN, OUTPUT); + pinMode(B_PIN, OUTPUT); - 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 7663f6d..265b77f 100644 --- a/examples/basic_Level/basic_Level.ino +++ b/examples/basic_Level/basic_Level.ino @@ -2,23 +2,23 @@ // in Config file define ethernet options #include "arduinoWrapper/ArduinoConfig.h" -#include "arduinoWrapper/ArduinoWrapper.h" #include "arduinoWrapper/ArduinoNetworkInterface.h" +#include "arduinoWrapper/ArduinoWrapper.h" #include "Endpoints/EndpointLevel.h" //#define DEBUG -#define DEVICE_PIN LED_BUILTIN // GPIO pin, built in led as example +#define DEVICE_PIN LED_BUILTIN // GPIO pin, built in led as example IPAddress gw_ip = {192, 168, 1, 10}; -static char* const deviceName = "LEVEL_DEVICE"; // name of device +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 const char* const mqtt_username = "hc"; static const char* const mqtt_password = "magic"; -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; @@ -28,60 +28,60 @@ 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() { - #ifdef DEBUG - Serial.begin(115200); - Serial.println("Started serial"); - #endif +#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(); + 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(); + wrapperSetServer(gw_ip); + wrapperSetUsernamePassword(mqtt_username, mqtt_password); + wrapperSetup(); - hcm.setup(); + hcm.setup(); - // DO NOT TOUCH ANYTHING BEFORE THIS LINE IN SETUP FUNCTION + // DO NOT TOUCH ANYTHING BEFORE THIS LINE IN SETUP FUNCTION - pinMode(DEVICE_PIN, OUTPUT); + pinMode(DEVICE_PIN, OUTPUT); - hcm.addEndpoint(&endpointLevel); + hcm.addEndpoint(&endpointLevel); } void loop() { - controlPin(); - hcm.doMagic(); + controlPin(); + hcm.doMagic(); } \ No newline at end of file diff --git a/examples/basic_Motion/basic_Motion.ino b/examples/basic_Motion/basic_Motion.ino index 44ed198..a73fd02 100644 --- a/examples/basic_Motion/basic_Motion.ino +++ b/examples/basic_Motion/basic_Motion.ino @@ -2,25 +2,24 @@ // in Config file define ethernet options #include "arduinoWrapper/ArduinoConfig.h" -#include "arduinoWrapper/ArduinoWrapper.h" #include "arduinoWrapper/ArduinoNetworkInterface.h" +#include "arduinoWrapper/ArduinoWrapper.h" #include "Endpoints/EndpointMotion.h" - //#define DEBUG -#define PIR_PIN 4 // GPIO pin -#define DEVICE_PIN LED_BUILTIN // GPIO pin to use, built in led as example +#define PIR_PIN 4 // GPIO pin +#define DEVICE_PIN LED_BUILTIN // GPIO pin to use, built in led as example IPAddress gw_ip = {192, 168, 1, 10}; -static char* const deviceName = "MOTION_SENSOR"; // name of device +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 const char* const mqtt_username = "hc"; static const char* const mqtt_password = "magic"; -bool active_pin_state = false; // reverse pin state +bool active_pin_state = false; // reverse pin state bool last_motion = false; HomeControlMagic hcm(deviceName); @@ -28,55 +27,55 @@ EndpointMotion endpointMotion(&hcm); void controlPin() { - bool motion = digitalRead(PIR_PIN); - if(motion != last_motion) - { - last_motion = motion; - #ifdef DEVICE_PIN - if(motion) - { - digitalWrite(DEVICE_PIN, active_pin_state); - } - else + 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); } - #endif - endpointMotion.setState(motion); - } } void setup() { - #ifdef DEBUG - Serial.begin(115200); - Serial.println("Started serial"); - #endif +#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(); + 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(); + wrapperSetServer(gw_ip); + wrapperSetUsernamePassword(mqtt_username, mqtt_password); + wrapperSetup(); - hcm.setup(); + hcm.setup(); - // DO NOT TOUCH ANYTHING BEFORE THIS LINE IN SETUP FUNCTION + // DO NOT TOUCH ANYTHING BEFORE THIS LINE IN SETUP FUNCTION - pinMode(PIR_PIN, INPUT); - #ifdef DEVICE_PIN - pinMode(DEVICE_PIN, OUTPUT); - #endif + pinMode(PIR_PIN, INPUT); +#ifdef DEVICE_PIN + pinMode(DEVICE_PIN, OUTPUT); +#endif - hcm.addEndpoint(&endpointMotion); + hcm.addEndpoint(&endpointMotion); } void loop() { - controlPin(); - hcm.doMagic(); + controlPin(); + hcm.doMagic(); } \ No newline at end of file diff --git a/examples/basic_OnOff/basic_OnOff.ino b/examples/basic_OnOff/basic_OnOff.ino index 73e9793..36ecc0c 100644 --- a/examples/basic_OnOff/basic_OnOff.ino +++ b/examples/basic_OnOff/basic_OnOff.ino @@ -2,24 +2,23 @@ // in Config file define ethernet options #include "arduinoWrapper/ArduinoConfig.h" -#include "arduinoWrapper/ArduinoWrapper.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 +#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 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 const char* const mqtt_username = "hc"; static const char* const mqtt_password = "magic"; -bool active_pin_state = false; // reverse pin state +bool active_pin_state = false; // reverse pin state bool last_state = false; HomeControlMagic hcm(deviceName); @@ -27,49 +26,49 @@ 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() { - #ifdef DEBUG - Serial.begin(115200); - Serial.println("Started serial"); - #endif +#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(); + 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(); + wrapperSetServer(gw_ip); + wrapperSetUsernamePassword(mqtt_username, mqtt_password); + wrapperSetup(); - hcm.setup(); + hcm.setup(); - // DO NOT TOUCH ANYTHING BEFORE THIS LINE IN SETUP FUNCTION + // DO NOT TOUCH ANYTHING BEFORE THIS LINE IN SETUP FUNCTION - pinMode(DEVICE_PIN, OUTPUT); - hcm.addEndpoint(&endpointOnOff); + pinMode(DEVICE_PIN, OUTPUT); + hcm.addEndpoint(&endpointOnOff); } void loop() { - controlPin(); - hcm.doMagic(); + controlPin(); + hcm.doMagic(); } \ No newline at end of file diff --git a/examples/basic_OnOff_ethernet/basic_OnOff_ethernet.ino b/examples/basic_OnOff_ethernet/basic_OnOff_ethernet.ino index 613254c..fb12f7d 100644 --- a/examples/basic_OnOff_ethernet/basic_OnOff_ethernet.ino +++ b/examples/basic_OnOff_ethernet/basic_OnOff_ethernet.ino @@ -7,17 +7,16 @@ #include "Endpoints/EndpointOnOff.h" - #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 IPAddress gw_ip = {192, 168, 1, 10}; -static const char* const deviceName = "ON_OFF_DEVICE"; // name of device +static const char* const deviceName = "ON_OFF_DEVICE"; // name of device static const char* const mqtt_username = "hc"; static const char* const mqtt_password = "magic"; -bool active_pin_state = false; // reverse pin state +bool active_pin_state = false; // reverse pin state bool last_state = false; HomeControlMagic hcm(deviceName); @@ -25,47 +24,47 @@ 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() { - #ifdef DEBUG - Serial.begin(115200); - Serial.println("Started serial"); - #endif +#ifdef DEBUG + Serial.begin(115200); + Serial.println("Started serial"); +#endif - networkSetSecure(false); // this must be called before setServer and networkSetup - networkSetup(); - networkStart(); + networkSetSecure(false); // this must be called before setServer and networkSetup + networkSetup(); + networkStart(); - wrapperSetServer(gw_ip); - wrapperSetUsernamePassword(mqtt_username, mqtt_password); - wrapperSetup(); + wrapperSetServer(gw_ip); + wrapperSetUsernamePassword(mqtt_username, mqtt_password); + wrapperSetup(); - hcm.setup(); + hcm.setup(); - // DO NOT TOUCH ANYTHING BEFORE THIS LINE IN SETUP FUNCTION + // DO NOT TOUCH ANYTHING BEFORE THIS LINE IN SETUP FUNCTION - pinMode(DEVICE_PIN, OUTPUT); - hcm.addEndpoint(&endpointOnOff); + pinMode(DEVICE_PIN, OUTPUT); + hcm.addEndpoint(&endpointOnOff); } void loop() { - controlPin(); - hcm.doMagic(); + 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 4d3d0f0..3a37bc2 100644 --- a/examples/basic_Temperature/basic_Temperature.ino +++ b/examples/basic_Temperature/basic_Temperature.ino @@ -2,21 +2,21 @@ // in Config file define ethernet options #include "arduinoWrapper/ArduinoConfig.h" -#include "arduinoWrapper/ArduinoWrapper.h" #include "arduinoWrapper/ArduinoNetworkInterface.h" +#include "arduinoWrapper/ArduinoWrapper.h" -#include "Endpoints/EndpointTemperature.h" #include "DHT.h" +#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 READ_TIME 30 // sensor reading time in seconds +#define READ_TIME 30 // sensor reading time in seconds IPAddress gw_ip = {192, 168, 1, 10}; -static char* const deviceName = "TEMPERATURE_SENSOR"; // name of device +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 const char* const mqtt_username = "hc"; @@ -29,59 +29,60 @@ DHT dht(DHT_PIN, DHTTYPE); void setup() { - #ifdef DEBUG - Serial.begin(115200); - Serial.println("Started serial"); - #endif +#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(); + 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(); + wrapperSetServer(gw_ip); + wrapperSetUsernamePassword(mqtt_username, mqtt_password); + wrapperSetup(); - hcm.setup(); + hcm.setup(); - // DO NOT TOUCH ANYTHING BEFORE THIS LINE IN SETUP FUNCTION + // DO NOT TOUCH ANYTHING BEFORE THIS LINE IN SETUP FUNCTION - hcm.addEndpoint(&endpointTemperature); + hcm.addEndpoint(&endpointTemperature); - dht.begin(); + dht.begin(); } void loop() { - static int resend_time; - - if (millis() - resend_time > READ_TIME * 1000) - { - resend_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; + static int resend_time; + + if(millis() - resend_time > READ_TIME * 1000) + { + resend_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; + } + +#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); } - #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(); } \ No newline at end of file diff --git a/examples/basic_TemperatureTarget_OnOff/basic_TemperatureTarget_OnOff.ino b/examples/basic_TemperatureTarget_OnOff/basic_TemperatureTarget_OnOff.ino index edbbdcf..82b2946 100644 --- a/examples/basic_TemperatureTarget_OnOff/basic_TemperatureTarget_OnOff.ino +++ b/examples/basic_TemperatureTarget_OnOff/basic_TemperatureTarget_OnOff.ino @@ -2,31 +2,30 @@ // in Config file define ethernet options #include "arduinoWrapper/ArduinoConfig.h" -#include "arduinoWrapper/ArduinoWrapper.h" #include "arduinoWrapper/ArduinoNetworkInterface.h" +#include "arduinoWrapper/ArduinoWrapper.h" -#include "Endpoints/EndpointTemperatureTarget.h" -#include "Endpoints/EndpointOnOff.h" #include "DHT.h" - +#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 IPAddress gw_ip = {192, 168, 1, 10}; -static char* const deviceName = "TERMOSTAT"; // name of device +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 const char* const mqtt_username = "hc"; static const char* const mqtt_password = "magic"; -bool active_pin_state = false; // reverse pin state +bool active_pin_state = false; // reverse pin state bool last_state = false; HomeControlMagic hcm(deviceName); @@ -38,89 +37,90 @@ DHT dht(DHT_PIN, DHTTYPE); void controlPin() { - bool state = endpointOnOff.getState(); - if(state != last_state) - { - last_state = state; - if(state) + 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(); } - else - { - digitalWrite(DEVICE_PIN, !active_pin_state); - } - endpointOnOff.sendFeedbackMessage(); - } } void setup() { - #ifdef DEBUG - Serial.begin(115200); - Serial.println("Started serial"); - #endif +#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(); + 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(); + wrapperSetServer(gw_ip); + wrapperSetUsernamePassword(mqtt_username, mqtt_password); + wrapperSetup(); - hcm.setup(); + hcm.setup(); - // DO NOT TOUCH ANYTHING BEFORE THIS LINE IN SETUP FUNCTION + // DO NOT TOUCH ANYTHING BEFORE THIS LINE IN SETUP FUNCTION - pinMode(DEVICE_PIN, OUTPUT); + pinMode(DEVICE_PIN, OUTPUT); - hcm.addEndpoint(&endpointTemperatureTarget); - hcm.addEndpoint(&endpointOnOff); + hcm.addEndpoint(&endpointTemperatureTarget); + hcm.addEndpoint(&endpointOnOff); - dht.begin(); + dht.begin(); - double temperature = dht.readTemperature(); - endpointTemperatureTarget.setTemperatureTarget(temperature); + double temperature = dht.readTemperature(); + endpointTemperatureTarget.setTemperatureTarget(temperature); } void loop() { - static int read_time; - - if (millis() - read_time > READ_TIME * 1000) - { - read_time = millis(); + static int read_time; - 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 + 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(); } \ No newline at end of file diff --git a/examples/basic_multipleEndpoints/basic_multipleEndpoints.ino b/examples/basic_multipleEndpoints/basic_multipleEndpoints.ino index f301a91..cfef8b6 100644 --- a/examples/basic_multipleEndpoints/basic_multipleEndpoints.ino +++ b/examples/basic_multipleEndpoints/basic_multipleEndpoints.ino @@ -3,22 +3,22 @@ // in Config file define ethernet options #include "arduinoWrapper/ArduinoConfig.h" -#include "arduinoWrapper/ArduinoWrapper.h" -#include "arduinoWrapper/ArduinoNetworkInterface.h" -#include "Endpoints/EndpointTemperature.h" #include "DHT.h" +#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 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 DHTTYPE DHT22 // DHT type IPAddress gw_ip = {192, 168, 1, 10}; -static char* const deviceName = "TEMPERATURE_SENSORS"; // name of device +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 const char* const mqtt_username = "hc"; @@ -38,66 +38,67 @@ DHT dht_4(DHT_4_PIN, DHTTYPE); void setup() { - #ifdef DEBUG - Serial.begin(115200); - Serial.println("Started serial"); - #endif - - networkSetSsid(wifi_ssid); - networkSetPass(wifi_pass); - 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 - - 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(); +#ifdef DEBUG + Serial.begin(115200); + Serial.println("Started serial"); +#endif + + networkSetSsid(wifi_ssid); + networkSetPass(wifi_pass); + 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 + + 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(); } \ No newline at end of file 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 34bdac5..604d8e5 100644 --- a/src/Endpoint.h +++ b/src/Endpoint.h @@ -10,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, uint8_t* 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 8796f95..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, 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 8ffef41..0c48a1b 100644 --- a/src/Endpoints/EndpointColor.h +++ b/src/Endpoints/EndpointColor.h @@ -5,7 +5,7 @@ class EndpointColor : public Endpoint { - public: +public: EndpointColor(HomeControlMagic* hcm_ptr); virtual void sendStatusMessage(); @@ -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 06a34ca..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, 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 da5c82d..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, 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 047c90b..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, 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 6c12134..86ca5e8 100644 --- a/src/Endpoints/EndpointLevel.h +++ b/src/Endpoints/EndpointLevel.h @@ -4,7 +4,7 @@ class EndpointLevel : public Endpoint { - public: +public: EndpointLevel(HomeControlMagic* hcm_ptr); virtual void sendStatusMessage(); @@ -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 b3b0336..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, 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 dfd3b65..3727412 100644 --- a/src/Endpoints/EndpointMotion.h +++ b/src/Endpoints/EndpointMotion.h @@ -4,7 +4,7 @@ class EndpointMotion : public Endpoint { - public: +public: EndpointMotion(HomeControlMagic* hcm_ptr); virtual void sendStatusMessage(); @@ -15,6 +15,6 @@ class EndpointMotion : public Endpoint 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 463802d..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, 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 f7bcbad..8333a51 100644 --- a/src/Endpoints/EndpointOnOff.h +++ b/src/Endpoints/EndpointOnOff.h @@ -4,7 +4,7 @@ class EndpointOnOff : public Endpoint { - public: +public: EndpointOnOff(HomeControlMagic* hcm_ptr); virtual void sendStatusMessage(); @@ -15,6 +15,6 @@ class EndpointOnOff : public Endpoint 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 1d5666b..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, 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 7eef5bf..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, uint8_t* 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 a28f982..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, 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 ed4de98..2d8b125 100644 --- a/src/Endpoints/EndpointTemperatureTarget.h +++ b/src/Endpoints/EndpointTemperatureTarget.h @@ -4,7 +4,7 @@ class EndpointTemperatureTarget : public Endpoint { - public: +public: EndpointTemperatureTarget(HomeControlMagic* hcm_ptr); virtual void sendStatusMessage(); @@ -12,14 +12,13 @@ class EndpointTemperatureTarget : public Endpoint virtual void incomingMessage(char* topic, uint8_t* payload, unsigned int length); - virtual void setTemperature(double temperature); virtual void setTemperatureTarget(double temperature); 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 eeeda1f..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, 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 cb13577..13117c7 100644 --- a/src/Endpoints/EndpointZero.h +++ b/src/Endpoints/EndpointZero.h @@ -4,7 +4,7 @@ class EndpointZero : public Endpoint { - public: +public: EndpointZero(HomeControlMagic* hcm_ptr); void sendConfig(); @@ -13,4 +13,3 @@ class EndpointZero : public Endpoint void incomingMessage(char* topic, uint8_t* payload, unsigned int length); }; - diff --git a/src/HomeControlMagic.cpp b/src/HomeControlMagic.cpp index c055b40..0b9d896 100644 --- a/src/HomeControlMagic.cpp +++ b/src/HomeControlMagic.cpp @@ -21,265 +21,263 @@ static char* m_message_buffer_ptr; 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")) + { + 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++) { - hcm_ptr->announce(); - return; + 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); } - } - - // 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(const char* deviceName) - : m_number_of_endpoints(0) - , m_name(deviceName) - , m_broker_was_connected(false) + : 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; + // 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; } void HomeControlMagic::setup() { - m_id = getUniqueId(); + m_id = 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); + wrapperSetCallback(callback); } void HomeControlMagic::doMagic() { - /* - On arduino this is calling loop for pubsubclient and network. Network is calling debugLed loop - */ - wrapperLoop(); - - if(wrapperIsMqttConnected()) - { - if(!m_broker_was_connected) + /* + On arduino this is calling loop for pubsubclient and network. Network is calling debugLed loop + */ + wrapperLoop(); + + if(wrapperIsMqttConnected()) + { + if(!m_broker_was_connected) + { + m_broker_was_connected = true; + announce(); + } + sendStatus(); + } + else { - m_broker_was_connected = true; - announce(); + m_broker_was_connected = false; } - sendStatus(); - } - else - { - m_broker_was_connected = false; - } } void HomeControlMagic::setTopic(char* topic, char* endpoint_id) { - 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); + 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_ptr. 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_ptr); - Serial.println(m_message_buffer_ptr); - #endif +#ifdef HCM_DEBUG + Serial.println(m_topic_buffer_ptr); + Serial.println(m_message_buffer_ptr); +#endif - wrapperPublish(); + wrapperPublish(); } void HomeControlMagic::sendMessage(char* topic, bool message, char* endpoint_id) { - 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(); + 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_ptr); - #endif +#ifdef HCM_DEBUG + Serial.println(m_topic_buffer_ptr); +#endif - itoa(message, m_message_buffer_ptr, 10); + itoa(message, m_message_buffer_ptr, 10); - #ifdef HCM_DEBUG - Serial.println(m_message_buffer_ptr); - #endif +#ifdef HCM_DEBUG + Serial.println(m_message_buffer_ptr); +#endif - wrapperPublish(); + 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_ptr); - #endif +#ifdef HCM_DEBUG + Serial.println(m_topic_buffer_ptr); +#endif - dtostrf(message, 4, 2, m_message_buffer_ptr); + dtostrf(message, 4, 2, m_message_buffer_ptr); - #ifdef HCM_DEBUG - Serial.println(m_message_buffer_ptr); - #endif +#ifdef HCM_DEBUG + Serial.println(m_message_buffer_ptr); +#endif - wrapperPublish(); + 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_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); + 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_ptr, ";name="); - strcat(m_message_buffer_ptr, 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_ptr, ";"); + strcat(m_message_buffer_ptr, ";"); #ifdef HCM_DEBUG -Serial.println(m_topic_buffer_ptr); -Serial.println(m_message_buffer_ptr); + Serial.println(m_topic_buffer_ptr); + Serial.println(m_message_buffer_ptr); #endif - wrapperPublish(); + wrapperPublish(); } void HomeControlMagic::announce() { - strcat(m_message_buffer_ptr, m_name); - sendStringMessage("announce", "0"); + strcat(m_message_buffer_ptr, m_name); + sendStringMessage("announce", "0"); - sendFeedback(); + 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_ptr, 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_ptr); + Serial.print(F("Id to set: ")); + Serial.println(m_message_buffer_ptr); #endif - endpoint_ptr->setId(m_message_buffer_ptr); - wrapperClearMessageBuffer(); + 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(); - } + for(uint8_t i = 0; i < m_number_of_endpoints; i++) + { + m_endpoints_pointers[i]->sendFeedbackMessage(); + } } char* HomeControlMagic::getMessageBufferPtr() { - return m_message_buffer_ptr; + return m_message_buffer_ptr; } - - diff --git a/src/HomeControlMagic.h b/src/HomeControlMagic.h index 1146dfe..a9edaa5 100644 --- a/src/HomeControlMagic.h +++ b/src/HomeControlMagic.h @@ -8,7 +8,7 @@ class HomeControlMagic { - public: +public: HomeControlMagic(const char* deviceName); void doMagic(); void setup(); @@ -34,15 +34,14 @@ class HomeControlMagic char* getMessageBufferPtr(); - private: +private: void setTopic(char* topic, char* endpoint_id); const char* m_name; char* m_id; uint8_t m_number_of_endpoints; - Endpoint* m_endpoints_pointers[10]; + Endpoint* m_endpoints_pointers[10]; char m_base_topic[20]; bool m_broker_was_connected; }; - diff --git a/src/arduinoWrapper/ArduinoConfig.h b/src/arduinoWrapper/ArduinoConfig.h index de3b573..c02bde9 100644 --- a/src/arduinoWrapper/ArduinoConfig.h +++ b/src/arduinoWrapper/ArduinoConfig.h @@ -4,11 +4,24 @@ //#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) +#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 index ce536dd..b76d308 100644 --- a/src/arduinoWrapper/ArduinoDebugLed.h +++ b/src/arduinoWrapper/ArduinoDebugLed.h @@ -6,7 +6,9 @@ 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; +#define LED_PIN_CHECK() \ + if(m_led_pin == -1) \ + return; inline void DebugLedFlash(long half_period) { diff --git a/src/arduinoWrapper/ArduinoEspWrapper.cpp b/src/arduinoWrapper/ArduinoEspWrapper.cpp index 6963c29..3fd31e6 100644 --- a/src/arduinoWrapper/ArduinoEspWrapper.cpp +++ b/src/arduinoWrapper/ArduinoEspWrapper.cpp @@ -1,6 +1,6 @@ #ifdef ESP8266 -#include "Arduino.h" #include "ArduinoEspWrapper.h" +#include "Arduino.h" #include "ArduinoDebugLed.h" #define ESP_WRAPPER_DEBUG @@ -39,7 +39,7 @@ void networkLoop() unsigned long current_time = millis(); if(WiFi.status() == WL_CONNECTED) { - m_last_time_connected = current_time; + m_last_time_connected = current_time; } else { @@ -91,8 +91,7 @@ void networkSetup() // turn off access point WiFi.mode(WIFI_STA); - gotIpEventHandler = WiFi.onStationModeGotIP([](const WiFiEventStationModeGotIP& event) - { + gotIpEventHandler = WiFi.onStationModeGotIP([](const WiFiEventStationModeGotIP& event) { #ifdef ESP_WRAPPER_DEBUG Serial.print("Station connected, IP: "); Serial.println(WiFi.localIP()); @@ -100,8 +99,7 @@ void networkSetup() DebugLedState(false); }); - disconnectedEventHandler = WiFi.onStationModeDisconnected([](const WiFiEventStationModeDisconnected& event) - { + disconnectedEventHandler = WiFi.onStationModeDisconnected([](const WiFiEventStationModeDisconnected& event) { #ifdef ESP_WRAPPER_DEBUG Serial.println("Station disconnected"); #endif @@ -115,7 +113,7 @@ bool networkReconnect() void networkChipRestart() { - ESP.restart(); + ESP.restart(); } void networkStart() diff --git a/src/arduinoWrapper/ArduinoEspWrapper.h b/src/arduinoWrapper/ArduinoEspWrapper.h index b6ed012..ce85453 100644 --- a/src/arduinoWrapper/ArduinoEspWrapper.h +++ b/src/arduinoWrapper/ArduinoEspWrapper.h @@ -17,4 +17,4 @@ void networkSetSsid(const char* const ssid); void networkSetPass(const char* const pass); char* getUniqueId(); -#endif //ESP8266 +#endif // ESP8266 diff --git a/src/arduinoWrapper/ArduinoEthernetWrapper.cpp b/src/arduinoWrapper/ArduinoEthernetWrapper.cpp index 10799a1..9a9d7e0 100644 --- a/src/arduinoWrapper/ArduinoEthernetWrapper.cpp +++ b/src/arduinoWrapper/ArduinoEthernetWrapper.cpp @@ -2,8 +2,8 @@ #if defined(ETHERNET) || defined(ETHERNET2) #include "Arduino.h" -#include "ArduinoEthernetWrapper.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" @@ -24,50 +24,50 @@ void networkLoop() case 0: { m_connected = true; - m_last_time_connected = millis(); + m_last_time_connected = millis(); break; } case 1: { - #ifdef ETHERNET_WRAPPER_DEBUG +#ifdef ETHERNET_WRAPPER_DEBUG Serial.print(F("\n\rDHCP: Renew failed")); - #endif +#endif m_connected = false; DebugLedFlash(500); break; } case 2: { - #ifdef ETHERNET_WRAPPER_DEBUG +#ifdef ETHERNET_WRAPPER_DEBUG Serial.print(F("\n\rDHCP: Renew success")); - #endif +#endif m_connected = true; DebugLedState(false); break; } case 3: { - #ifdef ETHERNET_WRAPPER_DEBUG +#ifdef ETHERNET_WRAPPER_DEBUG Serial.print(F("\n\rDHCP: Rebind fail")); - #endif +#endif m_connected = false; DebugLedFlash(500); break; } case 4: { - #ifdef ETHERNET_WRAPPER_DEBUG +#ifdef ETHERNET_WRAPPER_DEBUG Serial.print(F("\n\rDHCP: Rebind success")); - #endif +#endif m_connected = true; DebugLedState(false); break; } default: - { - #ifdef ETHERNET_WRAPPER_DEBUG + { +#ifdef ETHERNET_WRAPPER_DEBUG Serial.print(F("\n\rDHCP: Unexpected number")); - #endif +#endif break; } } @@ -78,14 +78,13 @@ void networkLoop() void networkSetup() { - #ifdef ETHERNET_WRAPPER_DEBUG +#ifdef ETHERNET_WRAPPER_DEBUG Serial.println("In setup"); - #endif +#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]); + sprintf(m_uid, "%d%d%d%d%d%d", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); } void networkStart() @@ -111,10 +110,10 @@ void networkStart() } void networkChipRestart() { - // TODO: add restart - #ifdef ETHERNET_WRAPPER_DEBUG +// TODO: add restart +#ifdef ETHERNET_WRAPPER_DEBUG Serial.println("In restart"); - #endif +#endif } Client& networkGetClient() @@ -158,5 +157,4 @@ char* getUniqueId() return m_uid; } - #endif // defined(ETHERNET) || defined(ETHERNET2) \ No newline at end of file diff --git a/src/arduinoWrapper/ArduinoWrapper.cpp b/src/arduinoWrapper/ArduinoWrapper.cpp index a3b25a1..1cc04d7 100644 --- a/src/arduinoWrapper/ArduinoWrapper.cpp +++ b/src/arduinoWrapper/ArduinoWrapper.cpp @@ -1,9 +1,9 @@ -#include "Arduino.h" #include "ArduinoWrapper.h" -#include "PubSubClient.h" -#include "ArduinoDebugLed.h" +#include "Arduino.h" #include "ArduinoConfig.h" +#include "ArduinoDebugLed.h" #include "ArduinoNetworkInterface.h" +#include "PubSubClient.h" #include "helperFunctions.h" //#define ARDUINO_WRAPPER_DEBUG @@ -28,121 +28,119 @@ 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) + // wrapper loop will also call network loop + networkLoop(); + + if(!networkIsConnected()) { - // 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 + 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) { - //restart - networkChipRestart(); + // 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(); - } - - + else + { + m_last_time_connected = current_time; + m_mqtt_client.loop(); + } } void wrapperSetup() { - m_mqtt_client.setClient(networkGetClient()); + m_mqtt_client.setClient(networkGetClient()); } char* wrapperGetTopicBuffer() { - return m_topic_buffer; + return m_topic_buffer; } char* wrapperGetMessageBuffer() { - return m_message_buffer; + return m_message_buffer; } void wrapperPublish() { - #ifdef ARDUINO_WRAPPER_DEBUG +#ifdef ARDUINO_WRAPPER_DEBUG Serial.print("Publishing on topic: "); Serial.println(m_topic_buffer); Serial.println(m_message_buffer); - #endif +#endif - m_mqtt_client.publish(m_topic_buffer, m_message_buffer); - wrapperClearTopicBuffer(); - wrapperClearMessageBuffer(); + m_mqtt_client.publish(m_topic_buffer, m_message_buffer); + wrapperClearTopicBuffer(); + wrapperClearMessageBuffer(); } void wrapperClearTopicBuffer() { - clearBuffer(m_topic_buffer, TOPIC_BUFFER_LENGTH); + clearBuffer(m_topic_buffer, TOPIC_BUFFER_LENGTH); } void wrapperClearMessageBuffer() { - clearBuffer(m_message_buffer, MESSAGE_BUFFER_LENGTH); + 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); - } + 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); - } + 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); - } + if(networkIsSecure()) + { + m_mqtt_client.setServer(ip, 8883); + } + else + { + m_mqtt_client.setServer(ip, 1883); + } } void wrapperSetCallback(void (*callback)(char*, uint8_t*, unsigned int)) @@ -164,47 +162,45 @@ void wrapperSetUsernamePassword(const char* const username, const char* const pa // 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; - } +#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); -} - + 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/helperFunctions.cpp b/src/helperFunctions.cpp index 77a683b..e179047 100644 --- a/src/helperFunctions.cpp +++ b/src/helperFunctions.cpp @@ -2,197 +2,196 @@ 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(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(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(uint8_t* text, unsigned int length) { - int temp = 0; + int temp = 0; - if(text[length] != '\0') - { - text[length] = '\0'; - } + if(text[length] != '\0') + { + text[length] = '\0'; + } - if (text[0] != '\0') - { - temp = atoi((const char*)text); - } - clearByte(text, length); - return temp; + if(text[0] != '\0') + { + temp = atoi((const char*)text); + } + clearByte(text, length); + return temp; } bool extractState(uint8_t* text, unsigned int length) { - bool temp = false; - if (text[0] != '\0') - { - if(lineContains((const char*)text, "ON")) + bool temp = false; + if(text[0] != '\0') { - temp = true; - } - else if(lineContains((const char*)text, "OFF")) - { - temp = false; + if(lineContains((const char*)text, "ON")) + { + temp = true; + } + else if(lineContains((const char*)text, "OFF")) + { + temp = false; + } } - } - clearByte(text, length); - return temp; + clearByte(text, length); + return temp; } - bool extractBool(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(text[1] != '\0') + { + text[1] = '\0'; + } - if(lineContains((const char*)text, "1")) - { - temp = true; - } - else if(lineContains((const char*)text, "0")) - { - temp = false; + 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(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 f61d3bf..27e931b 100644 --- a/src/helperFunctions.h +++ b/src/helperFunctions.h @@ -4,14 +4,14 @@ struct RGB { - int r; - int g; - int b; + int r; + int g; + int b; }; void clearBuffer(char* text, uint8_t length); void clearByte(uint8_t* text, unsigned int length); -int lineContains(const char *str, const char *sfind); +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); From ab1f20abfdbc2afb17787e8ff200d55ad12130cd Mon Sep 17 00:00:00 2001 From: seky Date: Sun, 11 Nov 2018 12:35:37 +0100 Subject: [PATCH 10/11] fixed compile error after merge conflict and extended comment in ethernet example --- examples/basic_OnOff_ethernet/basic_OnOff_ethernet.ino | 2 +- src/HomeControlMagic.cpp | 8 -------- 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/examples/basic_OnOff_ethernet/basic_OnOff_ethernet.ino b/examples/basic_OnOff_ethernet/basic_OnOff_ethernet.ino index fb12f7d..1bba7a1 100644 --- a/examples/basic_OnOff_ethernet/basic_OnOff_ethernet.ino +++ b/examples/basic_OnOff_ethernet/basic_OnOff_ethernet.ino @@ -1,6 +1,6 @@ #include "HomeControlMagic.h" -// in Config file define ethernet options +// in Config file define ethernet options or this will not compile #include "arduinoWrapper/ArduinoConfig.h" #include "arduinoWrapper/ArduinoNetworkInterface.h" #include "arduinoWrapper/ArduinoWrapper.h" diff --git a/src/HomeControlMagic.cpp b/src/HomeControlMagic.cpp index 8b6d4e7..0b9d896 100644 --- a/src/HomeControlMagic.cpp +++ b/src/HomeControlMagic.cpp @@ -75,14 +75,6 @@ HomeControlMagic::HomeControlMagic(const char* deviceName) void HomeControlMagic::setup() { m_id = getUniqueId(); - uint16_t port = 1883; - if(m_network_object.isSecure()) - { - port = 8883; - } - - m_mqtt_client.setServer(server_ip, port); - m_mqtt_client.setCallback(callback); strcat(m_base_topic, "d/"); strcat(m_base_topic, m_id); From c015967496e915414b43f1e66f39663a017c0719 Mon Sep 17 00:00:00 2001 From: seky Date: Sun, 11 Nov 2018 12:45:20 +0100 Subject: [PATCH 11/11] added message to copy username and password from app --- examples/basic_Color/basic_Color.ino | 4 ++-- examples/basic_Level/basic_Level.ino | 4 ++-- examples/basic_Motion/basic_Motion.ino | 4 ++-- examples/basic_OnOff/basic_OnOff.ino | 4 ++-- examples/basic_OnOff_ethernet/basic_OnOff_ethernet.ino | 4 ++-- examples/basic_Temperature/basic_Temperature.ino | 4 ++-- .../basic_TemperatureTarget_OnOff.ino | 4 ++-- .../basic_multipleEndpoints/basic_multipleEndpoints.ino | 6 +++--- 8 files changed, 17 insertions(+), 17 deletions(-) diff --git a/examples/basic_Color/basic_Color.ino b/examples/basic_Color/basic_Color.ino index 9af8380..96b97df 100644 --- a/examples/basic_Color/basic_Color.ino +++ b/examples/basic_Color/basic_Color.ino @@ -17,8 +17,8 @@ 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 const char* const mqtt_username = "hc"; -static const char* const mqtt_password = "magic"; +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 diff --git a/examples/basic_Level/basic_Level.ino b/examples/basic_Level/basic_Level.ino index 7a6a0c1..ea6eb8b 100644 --- a/examples/basic_Level/basic_Level.ino +++ b/examples/basic_Level/basic_Level.ino @@ -15,8 +15,8 @@ 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 const char* const mqtt_username = "hc"; -static const char* const mqtt_password = "magic"; +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 diff --git a/examples/basic_Motion/basic_Motion.ino b/examples/basic_Motion/basic_Motion.ino index 4475612..3732257 100644 --- a/examples/basic_Motion/basic_Motion.ino +++ b/examples/basic_Motion/basic_Motion.ino @@ -16,8 +16,8 @@ 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 const char* const mqtt_username = "hc"; -static const char* const mqtt_password = "magic"; +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_motion = false; diff --git a/examples/basic_OnOff/basic_OnOff.ino b/examples/basic_OnOff/basic_OnOff.ino index 4ba0d9a..345f7f5 100644 --- a/examples/basic_OnOff/basic_OnOff.ino +++ b/examples/basic_OnOff/basic_OnOff.ino @@ -15,8 +15,8 @@ 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 const char* const mqtt_username = "hc"; -static const char* const mqtt_password = "magic"; +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; diff --git a/examples/basic_OnOff_ethernet/basic_OnOff_ethernet.ino b/examples/basic_OnOff_ethernet/basic_OnOff_ethernet.ino index 1bba7a1..6941016 100644 --- a/examples/basic_OnOff_ethernet/basic_OnOff_ethernet.ino +++ b/examples/basic_OnOff_ethernet/basic_OnOff_ethernet.ino @@ -13,8 +13,8 @@ IPAddress gw_ip = {192, 168, 1, 10}; static const char* const deviceName = "ON_OFF_DEVICE"; // name of device -static const char* const mqtt_username = "hc"; -static const char* const mqtt_password = "magic"; +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; diff --git a/examples/basic_Temperature/basic_Temperature.ino b/examples/basic_Temperature/basic_Temperature.ino index e9a95b2..a44faa6 100644 --- a/examples/basic_Temperature/basic_Temperature.ino +++ b/examples/basic_Temperature/basic_Temperature.ino @@ -19,8 +19,8 @@ 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 const char* const mqtt_username = "hc"; -static const char* const mqtt_password = "magic"; +static char* const mqtt_username = "hc"; // copy username from app +static char* const mqtt_password = ""; // copy password from app HomeControlMagic hcm(deviceName); EndpointTemperature endpointTemperature(&hcm); diff --git a/examples/basic_TemperatureTarget_OnOff/basic_TemperatureTarget_OnOff.ino b/examples/basic_TemperatureTarget_OnOff/basic_TemperatureTarget_OnOff.ino index ca60d12..483654a 100644 --- a/examples/basic_TemperatureTarget_OnOff/basic_TemperatureTarget_OnOff.ino +++ b/examples/basic_TemperatureTarget_OnOff/basic_TemperatureTarget_OnOff.ino @@ -22,8 +22,8 @@ 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 const char* const mqtt_username = "hc"; -static const char* const mqtt_password = "magic"; +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; diff --git a/examples/basic_multipleEndpoints/basic_multipleEndpoints.ino b/examples/basic_multipleEndpoints/basic_multipleEndpoints.ino index 98ec25e..ec14341 100644 --- a/examples/basic_multipleEndpoints/basic_multipleEndpoints.ino +++ b/examples/basic_multipleEndpoints/basic_multipleEndpoints.ino @@ -21,8 +21,8 @@ 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 const char* const mqtt_username = "hc"; -static const char* const mqtt_password = "magic"; +static char* const mqtt_username = "hc"; // copy username from app +static char* const mqtt_password = ""; // copy password from app HomeControlMagic hcm(deviceName); @@ -45,7 +45,7 @@ void setup() networkSetSsid(wifi_ssid); networkSetPass(wifi_pass); - networkSetSecure(false); // this must be called before setServer and networkSetup + networkSetSecure(true); // this must be called before setServer and networkSetup networkSetup(); networkStart();