From 26021ee76be9005f960589ba88724ba58950b4eb Mon Sep 17 00:00:00 2001 From: "ricardo.jl.rufino@gmail.com" Date: Tue, 5 Jan 2016 01:13:42 -0300 Subject: [PATCH 01/15] move FuncSensor class --- examples/ESP8266WiFi_AT/ESP8266WiFi_AT.ino | 2 +- src/OpenDevice.h | 2 +- src/{ => devices}/FuncSensor.cpp | 0 src/{ => devices}/FuncSensor.h | 0 4 files changed, 2 insertions(+), 2 deletions(-) rename src/{ => devices}/FuncSensor.cpp (100%) rename src/{ => devices}/FuncSensor.h (100%) diff --git a/examples/ESP8266WiFi_AT/ESP8266WiFi_AT.ino b/examples/ESP8266WiFi_AT/ESP8266WiFi_AT.ino index 1f2adc7..0a1e026 100644 --- a/examples/ESP8266WiFi_AT/ESP8266WiFi_AT.ino +++ b/examples/ESP8266WiFi_AT/ESP8266WiFi_AT.ino @@ -38,7 +38,7 @@ void setup() { void loop() { - // Send using console + // Send AT using console if(Serial.available()){ readAndSendAT(); } diff --git a/src/OpenDevice.h b/src/OpenDevice.h index c527b18..51c2564 100755 --- a/src/OpenDevice.h +++ b/src/OpenDevice.h @@ -20,7 +20,7 @@ #include "Command.h" #include "DeviceConnection.h" #include "Device.h" -#include "FuncSensor.h" +#include "devices/FuncSensor.h" using namespace od; diff --git a/src/FuncSensor.cpp b/src/devices/FuncSensor.cpp similarity index 100% rename from src/FuncSensor.cpp rename to src/devices/FuncSensor.cpp diff --git a/src/FuncSensor.h b/src/devices/FuncSensor.h similarity index 100% rename from src/FuncSensor.h rename to src/devices/FuncSensor.h From dd6d1a39ad2bb8e5a698ab271282262e79425700 Mon Sep 17 00:00:00 2001 From: "ricardo.jl.rufino@gmail.com" Date: Sat, 14 May 2016 10:47:37 -0300 Subject: [PATCH 02/15] mqtt + esp8266 integration --- dependencies.h | 2 +- examples/ESP8266WiFi/ESP8266WiFi.ino | 15 ++++++---- src/MQTTConnection.h | 43 ++++++++++++++++++---------- src/MQTTWifiConnection.cpp | 21 ++++++++++++++ src/MQTTWifiConnection.h | 21 ++++++++++++++ src/OpenDevice.h | 21 ++++++++++++-- src/WifiConnection.cpp | 2 +- src/WifiConnection.h | 2 +- 8 files changed, 102 insertions(+), 25 deletions(-) create mode 100644 src/MQTTWifiConnection.cpp create mode 100644 src/MQTTWifiConnection.h diff --git a/dependencies.h b/dependencies.h index 297f3b7..294f2bd 100644 --- a/dependencies.h +++ b/dependencies.h @@ -22,7 +22,7 @@ // #include // RF 433Mhz // #include // ESP8266 - AT Firmwware #include // ESP8266 - Standalone - +#include // Enable MQTT //#if defined(EnableInterrupt_h) //#define EI_ARDUINO_INTERRUPTED_PIN diff --git a/examples/ESP8266WiFi/ESP8266WiFi.ino b/examples/ESP8266WiFi/ESP8266WiFi.ino index 39688e3..46da54b 100644 --- a/examples/ESP8266WiFi/ESP8266WiFi.ino +++ b/examples/ESP8266WiFi/ESP8266WiFi.ino @@ -4,6 +4,7 @@ * * The Open Device will create a TCP server on the default port and a softAP using ODev.name("..."). * + * * Links: * - Tutorial: https://opendevice.atlassian.net/wiki/display/DOC/WiFi+using+ESP8266 * - https://github.com/esp8266/arduino @@ -13,22 +14,26 @@ */ -//#include // to: AT Firmware +//#include // Enable: AT Firmware + +#include // Enable ESP8266 Embedded +#include // Enable + MQTT #include #include -const char* ssid = "OpenWrt"; -const char* password = ""; +const char* ssid = "ricardo-ap"; +const char* password = "abcdefghij"; void setup() { ODev.name("ODev-Thing1"); + Config.server = "192.168.3.105"; // Only for MQTT ODev.enableKeepAlive(false); ODev.enableDebug(); ODev.addDevice(2, Device::DIGITAL); - // WiFi.init(new ESP8266(Serial1, 115200)); // to: AT Firmware + // WiFi.init(new ESP8266(Serial1, 115200)); // Only for: AT Firmware WiFi.mode(WIFI_AP_STA); WiFi.begin(ssid, password); ODev.begin(WiFi); @@ -44,7 +49,7 @@ void loop() { ODev.loop(); } -// Optional - Only for debug purpose +// Optional - for debug purpose (Only for: AT Firmware) void readAndSendAT(){ #ifdef Serial1 while(Serial.available() > 0){ diff --git a/src/MQTTConnection.h b/src/MQTTConnection.h index 4052efb..b96467c 100644 --- a/src/MQTTConnection.h +++ b/src/MQTTConnection.h @@ -11,13 +11,12 @@ * ***************************************************************************** */ +#ifndef OPENDEVICE_MQTTConnection +#define OPENDEVICE_MQTTConnection + /* - * EthernetServerConnection.h - * This implementation is designed to run both the Ethernet Arduino native library when the "arduino_uip" used to work with the ENC28J60 module.
- * This include will be used automatically when the Ethernet or UIPEthernet library is declared in main sketch + * This MQTT implementation is designed to run both on Ethernet and Wifi (ESP8266) * - * Created on: 22/06/2014 - * Update: 23/02/2015 * Author: Ricardo JL Rufino */ #include @@ -27,6 +26,7 @@ // NOTE: Please do not include OpenDevice.h this will break the preprocessors / macros #include "config.h" +#include "utility/Logger.h" #include "DeviceConnection.h" #include "MQTTClient.h" @@ -35,11 +35,19 @@ using namespace od; #define USING_CUSTOM_CONNECTION 1 #define CUSTOM_CONNECTION_CLASS MQTTClient -YunClient ethclient; -PubSubClient mqtt(ethclient); -MQTTClient mqttClient(mqtt); +#if defined(_YUN_SERVER_H_) +static YunClient ethclient; +#endif + +// ESP8266 Standalone +#if defined(ESP8266) +static WiFiClient ethclient; +#endif -void pubSubClientReconnect() { +static PubSubClient mqtt(ethclient); +static MQTTClient mqttClient(mqtt); + +static void pubSubClientReconnect() { // Loop until we're reconnected while (!mqtt.connected()) { String clientID = String(Config.appID); @@ -50,35 +58,38 @@ void pubSubClientReconnect() { subscribe+= "/in/"; subscribe+= Config.moduleName; - Serial.print("Attempting MQTT connection..."); + Serial.print("Connecting..."); // Attempt to connect if (mqtt.connect(clientID.c_str())) { /// ---> NAME. Serial.println("connected"); mqtt.subscribe(subscribe.c_str()); } else { - Serial.print("failed, rc="); + Serial.print("failed (#"); Serial.print(mqtt.state()); - Serial.println(" try again in 5 seconds"); + Serial.println(")"); // Wait 5 seconds before retrying delay(5000); } } } -void pubSubClientCallback(char* topic, byte* payload, unsigned int length) { +static void pubSubClientCallback(char* topic, byte* payload, unsigned int length) { mqttClient.setData(payload, length); } /** This method is called automatically by the OpenDevice when run: ODev.begin() */ -void custom_connection_begin(){ +static void custom_connection_begin(){ + Logger.debug("MQTT", "BEGIN"); mqtt.setServer(Config.server, 1883); mqtt.setCallback(pubSubClientCallback); +#if defined(_YUN_SERVER_H_) Bridge.begin(); +#endif } /** This method is called automatically by the OpenDevice when run: ODev.loop() */ -MQTTClient custom_connection_loop(DeviceConnection *conn){ +static MQTTClient custom_connection_loop(DeviceConnection *conn){ if (!mqtt.connected()) { pubSubClientReconnect(); @@ -90,4 +101,6 @@ MQTTClient custom_connection_loop(DeviceConnection *conn){ } +#endif + diff --git a/src/MQTTWifiConnection.cpp b/src/MQTTWifiConnection.cpp new file mode 100644 index 0000000..dd4a0d3 --- /dev/null +++ b/src/MQTTWifiConnection.cpp @@ -0,0 +1,21 @@ +/* + * MQTTWifiConnection.cpp + * + * Created on: May 14, 2016 + * Author: ricardo + */ + +#include + +namespace od { + +MQTTWifiConnection::MQTTWifiConnection() { + // TODO Auto-generated constructor stub + +} + +MQTTWifiConnection::~MQTTWifiConnection() { + // TODO Auto-generated destructor stub +} + +} /* namespace od */ diff --git a/src/MQTTWifiConnection.h b/src/MQTTWifiConnection.h new file mode 100644 index 0000000..9896234 --- /dev/null +++ b/src/MQTTWifiConnection.h @@ -0,0 +1,21 @@ +/* + * MQTTWifiConnection.h + * + * Created on: May 14, 2016 + * Author: ricardo + */ + +#ifndef LIBRARIES_OPENDEVICE_SRC_MQTTWIFICONNECTION_H_ +#define LIBRARIES_OPENDEVICE_SRC_MQTTWIFICONNECTION_H_ + +namespace od { + +class MQTTWifiConnection { +public: + MQTTWifiConnection(); + virtual ~MQTTWifiConnection(); +}; + +} /* namespace od */ + +#endif /* LIBRARIES_OPENDEVICE_SRC_MQTTWIFICONNECTION_H_ */ diff --git a/src/OpenDevice.h b/src/OpenDevice.h index 3ee3c16..4bd9047 100755 --- a/src/OpenDevice.h +++ b/src/OpenDevice.h @@ -50,12 +50,13 @@ using namespace od; #endif // ESP8266 Standalone -#if defined(ESP8266) +#if defined(ESP8266) && !defined(PubSubClient_h) #include "stdlib_noniso.h" #include #include #endif + #if defined(MFRC522_h) #include #endif @@ -275,7 +276,7 @@ void begin(usb_serial_class &serial, unsigned long baud){ } #endif -#if defined(ESP8266) +#if defined(ESP8266) && !defined(PubSubClient_h) void begin(ESP8266WiFiClass &wifi){ WifiConnection *conn = new WifiConnection(); @@ -284,6 +285,22 @@ void begin(ESP8266WiFiClass &wifi){ #endif +#if defined(ESP8266) && defined(PubSubClient_h) +void begin(ESP8266WiFiClass &wifi){ + enableKeepAlive(false); // on MQTT is not required + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("#"); + } + + Serial.println(""); + Serial.println("WiFi connected"); + Serial.println("IP address: "); + Serial.println(WiFi.localIP()); + begin(); // custom connection, call begin +} +#endif + // TODO: Make compatible with Due // #ifdef _SAM3XA_ // #include // Arduino Due diff --git a/src/WifiConnection.cpp b/src/WifiConnection.cpp index b875e36..8ca7dd5 100644 --- a/src/WifiConnection.cpp +++ b/src/WifiConnection.cpp @@ -18,7 +18,7 @@ #include // TODO: use list: http://stackoverflow.com/a/27088552/955857 -WiFiClient WifiConnection::client; +WiFiClient WifiConnection::client; // Current Client WifiConnection::WifiConnection() : server(DEFAULT_SERVER_PORT) { diff --git a/src/WifiConnection.h b/src/WifiConnection.h index 30a413a..b29dbf3 100644 --- a/src/WifiConnection.h +++ b/src/WifiConnection.h @@ -44,7 +44,7 @@ using namespace od; */ class WifiConnection : public BaseWifiConnection { -static WiFiClient client; +static WiFiClient client; // Current Client public: WifiConnection(); From 0cca8ba8c82cdd2300313fa6bb7f79af84c5f867 Mon Sep 17 00:00:00 2001 From: "ricardo.jl.rufino@gmail.com" Date: Sat, 14 May 2016 12:41:12 -0300 Subject: [PATCH 03/15] mqtt + tcp server fallback --- src/BaseWifiConnection.h | 6 ++-- src/MQTTWifiConnection.cpp | 58 ++++++++++++++++++++++++++++++++++++-- src/MQTTWifiConnection.h | 54 ++++++++++++++++++++++++++++++++++- src/OpenDevice.h | 17 +++++------ src/WifiConnection.cpp | 10 +++---- 5 files changed, 125 insertions(+), 20 deletions(-) diff --git a/src/BaseWifiConnection.h b/src/BaseWifiConnection.h index b611c68..56b9f40 100644 --- a/src/BaseWifiConnection.h +++ b/src/BaseWifiConnection.h @@ -22,7 +22,8 @@ using namespace od; -#if !defined(WL_DEFINITIONS_H_) +#ifndef WL_DEFINITIONS_H_ + typedef enum { WL_NO_SHIELD = 255, // for compatibility with WiFi Shield library WL_IDLE_STATUS = 0, @@ -33,10 +34,9 @@ using namespace od; WL_CONNECTION_LOST = 5, WL_DISCONNECTED = 6 } wl_status_t; -#endif -#if !defined(WL_DEFINITIONS_H_) enum WiFiMode { WIFI_OFF = 0, WIFI_STA = 1, WIFI_AP = 2, WIFI_AP_STA = 3 }; + #endif /* diff --git a/src/MQTTWifiConnection.cpp b/src/MQTTWifiConnection.cpp index dd4a0d3..8bd6727 100644 --- a/src/MQTTWifiConnection.cpp +++ b/src/MQTTWifiConnection.cpp @@ -9,13 +9,65 @@ namespace od { -MQTTWifiConnection::MQTTWifiConnection() { - // TODO Auto-generated constructor stub +MQTTClient* MQTTWifiConnection::mqttClient; +MQTTWifiConnection::MQTTWifiConnection(): mqtt(ethclient), mqttTimeout(5000) { + mqttClient = new MQTTClient(mqtt); } MQTTWifiConnection::~MQTTWifiConnection() { - // TODO Auto-generated destructor stub +} + +void MQTTWifiConnection::begin(){ + WifiConnection::begin(); + Logger.debug("MQTT", "BEGIN"); + mqtt.setServer(Config.server, 1883); + mqtt.setCallback(mqttCallback); +} + +bool MQTTWifiConnection::checkDataAvalible(void){ + + if (!mqtt.connected() && mqttTimeout.expired()) { + mqttConnect(); + } + + if (mqtt.connected()){ + mqtt.loop(); + mqttTimeout.disable(); + setStream(mqttClient); + return DeviceConnection::checkDataAvalible(); + }else{ + if(!mqttTimeout.isEnabled()) mqttTimeout.enable(); + return WifiConnection::checkDataAvalible(); // TCP SERVER... + } + +} + +void MQTTWifiConnection::mqttCallback(char* topic, byte* payload, unsigned int length){ + mqttClient->setData(payload, length); +} + +void MQTTWifiConnection::mqttConnect(){ + + String clientID = String(Config.appID); + clientID+= "/"; + clientID+= Config.moduleName; + + String subscribe = String(Config.appID); + subscribe+= "/in/"; + subscribe+= Config.moduleName; + + Logger.debug("MQTT", "connecting... "); + // Attempt to connect + if (mqtt.connect(clientID.c_str())) { + Serial.println("[connected]"); + mqtt.subscribe(subscribe.c_str()); + } else { + Serial.print("failed (#"); + Serial.print(mqtt.state()); + Serial.println(")"); + } + } } /* namespace od */ diff --git a/src/MQTTWifiConnection.h b/src/MQTTWifiConnection.h index 9896234..36692ec 100644 --- a/src/MQTTWifiConnection.h +++ b/src/MQTTWifiConnection.h @@ -8,14 +8,66 @@ #ifndef LIBRARIES_OPENDEVICE_SRC_MQTTWIFICONNECTION_H_ #define LIBRARIES_OPENDEVICE_SRC_MQTTWIFICONNECTION_H_ + +#include +#include + +#ifdef ESP8266 + #include + #include + extern "C" { + #include "user_interface.h" + } +#endif + +#include + + +// NOTE: Please do not include OpenDevice.h this will break the preprocessors / macros +#include "config.h" +#include "utility/Logger.h" +#include "utility/Timeout.h" +#include "DeviceConnection.h" +#include "WifiConnection.h" +#include "MQTTClient.h" + + namespace od { -class MQTTWifiConnection { +class MQTTWifiConnection : public WifiConnection { + +static MQTTClient* mqttClient; + public: MQTTWifiConnection(); + virtual ~MQTTWifiConnection(); + + virtual void begin(void); + + virtual bool checkDataAvalible(void); + + static void mqttCallback(char* topic, byte* payload, unsigned int length); + +private: + +#if defined(_YUN_SERVER_H_) +YunClient ethclient; +#endif + +// ESP8266 Standalone +#if defined(ESP8266) +WiFiClient ethclient; +#endif + +PubSubClient mqtt; +Timeout mqttTimeout; + + void mqttConnect(); + }; + } /* namespace od */ #endif /* LIBRARIES_OPENDEVICE_SRC_MQTTWIFICONNECTION_H_ */ diff --git a/src/OpenDevice.h b/src/OpenDevice.h index 4bd9047..9b8e524 100755 --- a/src/OpenDevice.h +++ b/src/OpenDevice.h @@ -39,10 +39,14 @@ using namespace od; #endif -#if defined(PubSubClient_h) +#if defined(PubSubClient_h) && !defined(ESP8266) #include "MQTTConnection.h" #endif +#if defined(PubSubClient_h) && defined(ESP8266) +#include "MQTTWifiConnection.h" +#endif + // ESP8266 AT Command library #if defined(__ESP8266AT_H__) @@ -289,15 +293,12 @@ void begin(ESP8266WiFiClass &wifi){ void begin(ESP8266WiFiClass &wifi){ enableKeepAlive(false); // on MQTT is not required while (WiFi.status() != WL_CONNECTED) { - delay(500); - Serial.print("#"); + delay(500); + Serial.print("#"); } - Serial.println(""); - Serial.println("WiFi connected"); - Serial.println("IP address: "); - Serial.println(WiFi.localIP()); - begin(); // custom connection, call begin + MQTTWifiConnection *conn = new MQTTWifiConnection(); + begin(*conn); } #endif diff --git a/src/WifiConnection.cpp b/src/WifiConnection.cpp index 8ca7dd5..802aa7a 100644 --- a/src/WifiConnection.cpp +++ b/src/WifiConnection.cpp @@ -97,11 +97,11 @@ bool WifiConnection::checkDataAvalible(void){ Logger.debug("WifiClient", "connected"); client = newClient; setStream(&client); - - // wait to send data. - while (!newClient.available()) { - delay(1); - } +// +// // wait to send data. +// while (!newClient.available()) { +// delay(1); +// } }else{ int count = Udp.parsePacket(); From 1e9bfea5768c1062e67060a60c313575a7e6e56d Mon Sep 17 00:00:00 2001 From: "ricardo.jl.rufino@gmail.com" Date: Sat, 14 May 2016 13:52:10 -0300 Subject: [PATCH 04/15] fix mqtt topic name --- src/MQTTClient.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/MQTTClient.cpp b/src/MQTTClient.cpp index 3600787..37fdbd6 100644 --- a/src/MQTTClient.cpp +++ b/src/MQTTClient.cpp @@ -16,7 +16,8 @@ namespace od { MQTTClient::MQTTClient(PubSubClient& mqtt) : StreamBuffer(_buffer, DATA_BUFFER) { this->mqtt = &mqtt; topic = String(Config.appID); - topic += "/out"; + topic += "/out/"; + topic += Config.moduleName; } MQTTClient::~MQTTClient() { From 5c48f6ce8c3c777953cf4325eb5c647affb3774b Mon Sep 17 00:00:00 2001 From: "ricardo.jl.rufino@gmail.com" Date: Sat, 14 May 2016 13:52:45 -0300 Subject: [PATCH 05/15] Add name to Devices --- src/Device.cpp | 27 ++++++++++++++++++--------- src/Device.h | 7 ++++++- src/OpenDevice.cpp | 15 ++++++++------- src/OpenDevice.h | 8 ++++---- 4 files changed, 36 insertions(+), 21 deletions(-) diff --git a/src/Device.cpp b/src/Device.cpp index ba227f9..1592383 100755 --- a/src/Device.cpp +++ b/src/Device.cpp @@ -25,31 +25,31 @@ // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Device::Device(){ - _init(0, 0, DIGITAL, false); + _init(NULL, 0, 0, DIGITAL, false); } Device::Device(uint8_t ipin){ - _init(0, ipin, DIGITAL, false); + _init(NULL, 0, ipin, DIGITAL, false); } Device::Device(uint8_t ipin, DeviceType type){ - _init(0, ipin, type, false); + _init(NULL, 0, ipin, type, false); } Device::Device(uint8_t ipin, DeviceType type, bool sensor){ - _init(0, ipin, type, sensor); + _init(NULL, 0, ipin, type, sensor); } - Device::Device(uint8_t _id, uint8_t _pin, DeviceType _type){ - _init(_id, _pin, _type, false); + _init(NULL, _id, _pin, _type, false); } Device::Device(uint8_t _id, uint8_t _pin, DeviceType _type, bool _sensor){ - _init(_id, _pin, _type, _sensor); + _init(NULL, _id, _pin, _type, _sensor); } -void Device::_init(uint8_t _id, uint8_t _pin, DeviceType _type, bool _sensor){ + +void Device::_init(char* name, uint8_t _id, uint8_t _pin, DeviceType _type, bool _sensor){ id = _id; pin = _pin; @@ -61,6 +61,7 @@ void Device::_init(uint8_t _id, uint8_t _pin, DeviceType _type, bool _sensor){ interruptEnabled = false; interruptMode = CHANGE; changeListener = 0; + deviceName = name; //if(_sensor){ currentValue = LOW; @@ -185,6 +186,14 @@ Device* Device::invertedState(){ return this; } +void Device::name(char* name){ + deviceName = name; +} + +char* Device::name(){ + return deviceName; +} + void Device::onChange(DeviceListener listener){ changeListener = listener; } @@ -203,7 +212,7 @@ bool Device::notifyListeners(){ // [ID, PIN, VALUE, TARGET, SENSOR?, TYPE] int Device::toString(char buffer[]){ int itype = type; - return sprintf (buffer, "[%d,%d,%lu,%d,%d,%d]", id, pin, getValue(), targetID, (sensor ? 1 : 0), itype); + return sprintf (buffer, "[%s,%d,%d,%lu,%d,%d,%d]", (deviceName != NULL ? deviceName : ""), id, pin, getValue(), targetID, (sensor ? 1 : 0), itype); } diff --git a/src/Device.h b/src/Device.h index 956adf7..a5a33b8 100755 --- a/src/Device.h +++ b/src/Device.h @@ -54,6 +54,7 @@ class Device uint8_t pin; unsigned long currentValue; DeviceType type; + char* deviceName; bool sensor; @@ -94,6 +95,10 @@ class Device virtual bool hasChanged(); + void name(char* name); + + char* name(); + /** * Enable to read value using interruptions.
* NOTE: It is necessary to enable support in the general settings. @@ -120,7 +125,7 @@ class Device DeviceListener changeListener; DeviceListener syncListerner; - void _init(uint8_t iid, uint8_t ipin, Device::DeviceType type, bool sensor); + void _init(char* name, uint8_t iid, uint8_t ipin, Device::DeviceType type, bool sensor); }; diff --git a/src/OpenDevice.cpp b/src/OpenDevice.cpp index d9b5be7..b1a0a16 100755 --- a/src/OpenDevice.cpp +++ b/src/OpenDevice.cpp @@ -404,12 +404,12 @@ void OpenDeviceClass::debugChange(uint8_t id, unsigned long value){ // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -Device* OpenDeviceClass::addSensor(uint8_t pin, Device::DeviceType type){ - return addSensor(pin, type, 0); +Device* OpenDeviceClass::addSensor(char* name, uint8_t pin, Device::DeviceType type){ + return addSensor(name, pin, type, 0); } -Device* OpenDeviceClass::addSensor(uint8_t pin, Device::DeviceType type, uint8_t targetID){ - Device* v = addDevice(pin, type, true, 0); +Device* OpenDeviceClass::addSensor(char* name, uint8_t pin, Device::DeviceType type, uint8_t targetID){ + Device* v = addDevice(name, pin, type, true, 0); if(v) devices[deviceLength-1]->targetID = targetID; return v; } @@ -420,8 +420,8 @@ Device* OpenDeviceClass::addSensor(Device& sensor){ } -Device* OpenDeviceClass::addDevice(uint8_t pin, Device::DeviceType type){ - return addDevice(pin, type, false, 0); +Device* OpenDeviceClass::addDevice(char* name, uint8_t pin, Device::DeviceType type){ + return addDevice(name, pin, type, false, 0); } @@ -450,7 +450,7 @@ Device* OpenDeviceClass::addDevice(Device& device){ } -Device* OpenDeviceClass::addDevice(uint8_t pin, Device::DeviceType type, bool sensor, uint8_t id){ +Device* OpenDeviceClass::addDevice(char* name, uint8_t pin, Device::DeviceType type, bool sensor, uint8_t id){ if (deviceLength < MAX_DEVICE) { if (sensor) { @@ -469,6 +469,7 @@ Device* OpenDeviceClass::addDevice(uint8_t pin, Device::DeviceType type, bool se if (id == 0) id = (deviceLength + 1); devices[deviceLength] = new Device(id, pin, type, sensor); + devices[deviceLength]->name(name); deviceLength++; return devices[deviceLength-1]; diff --git a/src/OpenDevice.h b/src/OpenDevice.h index 9b8e524..8bac51b 100755 --- a/src/OpenDevice.h +++ b/src/OpenDevice.h @@ -339,16 +339,16 @@ void begin(ESP8266WiFiClass &wifi){ void debug(const String &s); #endif - Device* addSensor(uint8_t pin, Device::DeviceType type, uint8_t targetID); - Device* addSensor(uint8_t pin, Device::DeviceType type); + Device* addSensor(char* name, uint8_t pin, Device::DeviceType type, uint8_t targetID); + Device* addSensor(char* name, uint8_t pin, Device::DeviceType type); Device* addSensor(Device& sensor); Device* addSensor(unsigned long (*function)()){ FuncSensor* func = new FuncSensor(function); return addDevice(*func); } - Device* addDevice(uint8_t pin, Device::DeviceType type, bool sensor,uint8_t id); - Device* addDevice(uint8_t pin, Device::DeviceType type); + Device* addDevice(char* name, uint8_t pin, Device::DeviceType type, bool sensor,uint8_t id); + Device* addDevice(char* name, uint8_t pin, Device::DeviceType type); Device* addDevice(Device& device); bool addCommand(const char * name, void (*function)()); From 1bfd66d1146c6829926f3e051b2ea563ad2632e0 Mon Sep 17 00:00:00 2001 From: "ricardo.jl.rufino@gmail.com" Date: Mon, 16 May 2016 22:12:57 -0300 Subject: [PATCH 06/15] Fix mqtt reconnection and add mqtt/SSL support (fixed OpenDevice/issues/54) --- src/MQTTWifiConnection.cpp | 8 ++++++-- src/MQTTWifiConnection.h | 14 ++++++++++---- src/WifiConnection.cpp | 2 +- src/WifiConnection.h | 7 ++++--- src/config.h | 1 + 5 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/MQTTWifiConnection.cpp b/src/MQTTWifiConnection.cpp index 8bd6727..e6b2cbd 100644 --- a/src/MQTTWifiConnection.cpp +++ b/src/MQTTWifiConnection.cpp @@ -21,23 +21,26 @@ MQTTWifiConnection::~MQTTWifiConnection() { void MQTTWifiConnection::begin(){ WifiConnection::begin(); Logger.debug("MQTT", "BEGIN"); - mqtt.setServer(Config.server, 1883); + mqtt.setServer(Config.server, MQTT_PORT); mqtt.setCallback(mqttCallback); } bool MQTTWifiConnection::checkDataAvalible(void){ - if (!mqtt.connected() && mqttTimeout.expired()) { + // Reconnect MQTT if OFFLINE and not have Client (TcpServer) + if (!mqtt.connected() && mqttTimeout.expired() && !WifiConnection::client.connected()) { mqttConnect(); } if (mqtt.connected()){ + // enableKeepAlive(false); // on MQTT is not required mqtt.loop(); mqttTimeout.disable(); setStream(mqttClient); return DeviceConnection::checkDataAvalible(); }else{ if(!mqttTimeout.isEnabled()) mqttTimeout.enable(); + // enableKeepAlive(true); // on MQTT is not required return WifiConnection::checkDataAvalible(); // TCP SERVER... } @@ -63,6 +66,7 @@ void MQTTWifiConnection::mqttConnect(){ Serial.println("[connected]"); mqtt.subscribe(subscribe.c_str()); } else { + mqttTimeout.reset(); Serial.print("failed (#"); Serial.print(mqtt.state()); Serial.println(")"); diff --git a/src/MQTTWifiConnection.h b/src/MQTTWifiConnection.h index 36692ec..8060bb8 100644 --- a/src/MQTTWifiConnection.h +++ b/src/MQTTWifiConnection.h @@ -51,17 +51,23 @@ static MQTTClient* mqttClient; private: + PubSubClient mqtt; + Timeout mqttTimeout; + #if defined(_YUN_SERVER_H_) YunClient ethclient; #endif // ESP8266 Standalone #if defined(ESP8266) -WiFiClient ethclient; +#if ENABLE_SSL + WiFiClientSecure ethclient; + #define MQTT_PORT 8883 +#else + WiFiClient ethclient; + #define MQTT_PORT 1883 +#endif #endif - -PubSubClient mqtt; -Timeout mqttTimeout; void mqttConnect(); diff --git a/src/WifiConnection.cpp b/src/WifiConnection.cpp index 802aa7a..5b90ee2 100644 --- a/src/WifiConnection.cpp +++ b/src/WifiConnection.cpp @@ -75,7 +75,7 @@ bool WifiConnection::checkDataAvalible(void){ #if defined(ESP8266) // This is only to debug if(STATION_CONNECTING == wifi_station_get_connect_status()){ - Logger.debug("WifiConnection", "reconnecting..."); + Logger.debug("Wifi", "reconnecting..."); if(WiFi.waitForConnectResult() != WL_CONNECTED){ // need wait.. Logger.debug("STA Reconnect", "FAIL"); }else{ diff --git a/src/WifiConnection.h b/src/WifiConnection.h index b29dbf3..25eee48 100644 --- a/src/WifiConnection.h +++ b/src/WifiConnection.h @@ -44,9 +44,10 @@ using namespace od; */ class WifiConnection : public BaseWifiConnection { -static WiFiClient client; // Current Client - public: + + static WiFiClient client; // Current Client + WifiConnection(); virtual ~WifiConnection(); @@ -68,7 +69,7 @@ static WiFiClient client; // Current Client virtual char* getIP(); -private: +protected: WiFiServer server; boolean isStartup; WiFiUDP Udp; // A UDP instance (discovery service) diff --git a/src/config.h b/src/config.h index f030e32..4a4d131 100755 --- a/src/config.h +++ b/src/config.h @@ -41,6 +41,7 @@ #define KEEP_ALIVE_MAX_MISSING 3 #define ENABLE_DEVICE_INTERRUPTION 0 #define ENABLE_REMOTE_WIFI_SETUP 0 // disable to reduce flash usage +#define ENABLE_SSL 0 // disable to reduce flash/memory usage (tested only for MQTT/ESP8266) #ifndef ENABLE_DHCP #define ENABLE_DHCP 1 /* if you need save flash memory disable this From 1d65b716233bfa3de2b0f7221e0869d6c9c57ae5 Mon Sep 17 00:00:00 2001 From: "ricardo.jl.rufino@gmail.com" Date: Mon, 16 May 2016 22:13:52 -0300 Subject: [PATCH 07/15] fix on sync device local changes, usind addDevice method --- src/Device.cpp | 4 ++-- src/Device.h | 5 +++++ src/OpenDevice.cpp | 1 + src/OpenDevice.h | 1 - 4 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/Device.cpp b/src/Device.cpp index 1592383..2a441b0 100755 --- a/src/Device.cpp +++ b/src/Device.cpp @@ -74,11 +74,11 @@ void Device::_init(char* name, uint8_t _id, uint8_t _pin, DeviceType _type, bool void Device::on(){ - setValue(HIGH); + setValue(HIGH, true); } void Device::off(){ - setValue(LOW); + setValue(LOW, true); } bool Device::setValue(unsigned long value, bool sync){ diff --git a/src/Device.h b/src/Device.h index a5a33b8..0663146 100755 --- a/src/Device.h +++ b/src/Device.h @@ -84,6 +84,11 @@ class Device void off(); + bool isON() { return currentValue == HIGH; } + + bool isOFF() { return currentValue == LOW; } + + /** * Get current value. */ diff --git a/src/OpenDevice.cpp b/src/OpenDevice.cpp index b1a0a16..0ded951 100755 --- a/src/OpenDevice.cpp +++ b/src/OpenDevice.cpp @@ -470,6 +470,7 @@ Device* OpenDeviceClass::addDevice(char* name, uint8_t pin, Device::DeviceType t devices[deviceLength] = new Device(id, pin, type, sensor); devices[deviceLength]->name(name); + devices[deviceLength]->setSyncListener(&(OpenDeviceClass::onDeviceChanged)); deviceLength++; return devices[deviceLength-1]; diff --git a/src/OpenDevice.h b/src/OpenDevice.h index 8bac51b..5ee6e98 100755 --- a/src/OpenDevice.h +++ b/src/OpenDevice.h @@ -291,7 +291,6 @@ void begin(ESP8266WiFiClass &wifi){ #if defined(ESP8266) && defined(PubSubClient_h) void begin(ESP8266WiFiClass &wifi){ - enableKeepAlive(false); // on MQTT is not required while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("#"); From 1065741c32d5b345241d4778952238f1cae70a40 Mon Sep 17 00:00:00 2001 From: "ricardo.jl.rufino@gmail.com" Date: Tue, 17 May 2016 04:48:12 -0300 Subject: [PATCH 08/15] cleanup samples and fix api changes --- .../BluetoothConnection.ino | 4 +-- .../BluetoothConnectionSoft.ino | 2 +- examples/ESP8266WiFi/ESP8266WiFi.ino | 36 +++---------------- .../EthernetConnection/EthernetConnection.ino | 2 +- .../EthernetCustomCommands.ino | 2 +- examples/UsbConnection/UsbConnection.ino | 2 +- 6 files changed, 11 insertions(+), 37 deletions(-) diff --git a/examples/BluetoothConnection/BluetoothConnection.ino b/examples/BluetoothConnection/BluetoothConnection.ino index 44a1c07..429595e 100644 --- a/examples/BluetoothConnection/BluetoothConnection.ino +++ b/examples/BluetoothConnection/BluetoothConnection.ino @@ -9,11 +9,11 @@ void setup(){ ODev.enableDebug(); - ODev.addDevice(13, Device::DIGITAL); // ID:1 + ODev.addDevice("LED", 13, Device::DIGITAL); // ID:1 // ODev.begin(); // UNO ODev.begin(Serial1, 9600); //-- for Leonardo, Teensy (Rx:0,Tx:1) } void loop(){ ODev.loop(); -} \ No newline at end of file +} diff --git a/examples/BluetoothConnectionSoft/BluetoothConnectionSoft.ino b/examples/BluetoothConnectionSoft/BluetoothConnectionSoft.ino index 22f3cd9..03f78fd 100644 --- a/examples/BluetoothConnectionSoft/BluetoothConnectionSoft.ino +++ b/examples/BluetoothConnectionSoft/BluetoothConnectionSoft.ino @@ -16,7 +16,7 @@ SoftwareSerial mySerial(2, 5); // RX, TX void setup(){ // ODev.enableDebug(); - ODev.addDevice(13, Device::DIGITAL); // ID:1 + ODev.addDevice("LED", 13, Device::DIGITAL); // ID:1 mySerial.begin(9600); ODev.begin(mySerial); diff --git a/examples/ESP8266WiFi/ESP8266WiFi.ino b/examples/ESP8266WiFi/ESP8266WiFi.ino index 46da54b..9a9ac56 100644 --- a/examples/ESP8266WiFi/ESP8266WiFi.ino +++ b/examples/ESP8266WiFi/ESP8266WiFi.ino @@ -14,52 +14,26 @@ */ -//#include // Enable: AT Firmware - #include // Enable ESP8266 Embedded -#include // Enable + MQTT +// #include // Enable + MQTT #include -#include -const char* ssid = "ricardo-ap"; -const char* password = "abcdefghij"; +const char* ssid = "--"; +const char* password = "--"; void setup() { ODev.name("ODev-Thing1"); - Config.server = "192.168.3.105"; // Only for MQTT - ODev.enableKeepAlive(false); + // Config.server = "192.168.3.105"; // Only for MQTT ODev.enableDebug(); - ODev.addDevice(2, Device::DIGITAL); + ODev.addDevice("LED", 2, Device::DIGITAL); - // WiFi.init(new ESP8266(Serial1, 115200)); // Only for: AT Firmware WiFi.mode(WIFI_AP_STA); WiFi.begin(ssid, password); ODev.begin(WiFi); } void loop() { - - // Send using console - if(Serial.available()){ - readAndSendAT(); - } - ODev.loop(); } - -// Optional - for debug purpose (Only for: AT Firmware) -void readAndSendAT(){ - #ifdef Serial1 - while(Serial.available() > 0){ - Serial1.write(Serial.read()); - } - - // wait for response - String rec = WiFi.ESP->recvString("OK", "ERROR", 10000); - Serial.print(rec); - #endif -} - - diff --git a/examples/EthernetConnection/EthernetConnection.ino b/examples/EthernetConnection/EthernetConnection.ino index 70fef7e..055ba21 100755 --- a/examples/EthernetConnection/EthernetConnection.ino +++ b/examples/EthernetConnection/EthernetConnection.ino @@ -19,7 +19,7 @@ void setup(){ // ODev.ip(192,168,3,106); ODev.enableDebug(); - ODev.addDevice(13, Device::DIGITAL); + ODev.addDevice("LED", 13, Device::DIGITAL); ODev.addCommand("mycmd1", ledON); ODev.addCommand("mycmd2", ledOFF); ODev.begin(); diff --git a/examples/EthernetCustomCommands/EthernetCustomCommands.ino b/examples/EthernetCustomCommands/EthernetCustomCommands.ino index 7c0c533..528b04e 100755 --- a/examples/EthernetCustomCommands/EthernetCustomCommands.ino +++ b/examples/EthernetCustomCommands/EthernetCustomCommands.ino @@ -22,7 +22,7 @@ void setup(){ #endif ODev.enableDebug(); - ODev.addDevice(13, Device::DIGITAL); + ODev.addDevice("LED", 13, Device::DIGITAL); ODev.addCommand("mycmd1", ledON); ODev.addCommand("mycmd2", ledOFF); ODev.begin(); diff --git a/examples/UsbConnection/UsbConnection.ino b/examples/UsbConnection/UsbConnection.ino index c700025..4ca8340 100644 --- a/examples/UsbConnection/UsbConnection.ino +++ b/examples/UsbConnection/UsbConnection.ino @@ -8,7 +8,7 @@ void setup(){ ODev.enableDebug(); - ODev.addDevice(13, Device::DIGITAL); // ID:1 + ODev.addDevice("LED", 13, Device::DIGITAL); // ID:1 ODev.begin(); // by default call Serial.begin() and while(!Serial) on Leonardo } From 4c5105e91ec9c1515c7ddcacf0f2002c57bffcce Mon Sep 17 00:00:00 2001 From: "ricardo.jl.rufino@gmail.com" Date: Tue, 17 May 2016 04:49:54 -0300 Subject: [PATCH 09/15] move keepAlive flag do ConfigClass and disable it for MQTT --- src/MQTTWifiConnection.cpp | 11 +++++++++-- src/OpenDevice.cpp | 5 ++--- src/OpenDevice.h | 1 - src/config.h | 11 ++++++++--- 4 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/MQTTWifiConnection.cpp b/src/MQTTWifiConnection.cpp index e6b2cbd..a0bcd43 100644 --- a/src/MQTTWifiConnection.cpp +++ b/src/MQTTWifiConnection.cpp @@ -5,6 +5,10 @@ * Author: ricardo */ +#include "../dependencies.h" + +#ifdef PubSubClient_h + #include namespace od { @@ -23,6 +27,7 @@ void MQTTWifiConnection::begin(){ Logger.debug("MQTT", "BEGIN"); mqtt.setServer(Config.server, MQTT_PORT); mqtt.setCallback(mqttCallback); + mqttConnect(); } bool MQTTWifiConnection::checkDataAvalible(void){ @@ -33,14 +38,14 @@ bool MQTTWifiConnection::checkDataAvalible(void){ } if (mqtt.connected()){ - // enableKeepAlive(false); // on MQTT is not required + Config.keepAlive = false; // on MQTT is not required mqtt.loop(); mqttTimeout.disable(); setStream(mqttClient); return DeviceConnection::checkDataAvalible(); }else{ if(!mqttTimeout.isEnabled()) mqttTimeout.enable(); - // enableKeepAlive(true); // on MQTT is not required + Config.keepAlive = true; // on raw TCP is required return WifiConnection::checkDataAvalible(); // TCP SERVER... } @@ -75,3 +80,5 @@ void MQTTWifiConnection::mqttConnect(){ } } /* namespace od */ + +#endif diff --git a/src/OpenDevice.cpp b/src/OpenDevice.cpp index 0ded951..86e8dfc 100755 --- a/src/OpenDevice.cpp +++ b/src/OpenDevice.cpp @@ -25,7 +25,6 @@ volatile uint8_t* PIN_INTERRUPT = 0; OpenDeviceClass::OpenDeviceClass() { deviceConnection = NULL; autoControl = false; - keepAliveEnabled = true; connected = false; keepAliveTime = 0; keepAliveMiss = 0; @@ -157,7 +156,7 @@ void OpenDeviceClass::_loop() { checkSensorsStatus(); // Send PING/KeepAlive if enabled - if(connected && keepAliveEnabled){ + if(connected && Config.keepAlive){ unsigned long currentMillis = millis(); if(currentMillis - keepAliveTime > KEEP_ALIVE_INTERVAL) { keepAliveTime = currentMillis; @@ -174,7 +173,7 @@ void OpenDeviceClass::_loop() { } void OpenDeviceClass::enableKeepAlive(bool val){ - keepAliveEnabled = val; + Config.keepAlive = val; } void OpenDeviceClass::enableDebug(uint8_t _debugTarget){ diff --git a/src/OpenDevice.h b/src/OpenDevice.h index 5ee6e98..3defd63 100755 --- a/src/OpenDevice.h +++ b/src/OpenDevice.h @@ -106,7 +106,6 @@ class OpenDeviceClass { // Debouncing of normal pressing (for Sensor's) long time; bool autoControl; // Changes in the sensor should affect bonded devices.. - bool keepAliveEnabled; long keepAliveTime; long keepAliveMiss; bool connected; diff --git a/src/config.h b/src/config.h index 4a4d131..a1e26e8 100755 --- a/src/config.h +++ b/src/config.h @@ -24,7 +24,8 @@ #error A C++ compiler is required! #endif -// #include +// STATIC CONFIGURATION +// ===================================== #define DEBUG_SETUP 0 // set 1 to enable (receiving debug) #define DEBUG_CON 0 // set 1 to enable (receiving debug) @@ -41,7 +42,7 @@ #define KEEP_ALIVE_MAX_MISSING 3 #define ENABLE_DEVICE_INTERRUPTION 0 #define ENABLE_REMOTE_WIFI_SETUP 0 // disable to reduce flash usage -#define ENABLE_SSL 0 // disable to reduce flash/memory usage (tested only for MQTT/ESP8266) +#define ENABLE_SSL 1 // disable to reduce flash/memory usage (tested only for MQTT/ESP8266) #ifndef ENABLE_DHCP #define ENABLE_DHCP 1 /* if you need save flash memory disable this @@ -70,7 +71,7 @@ #endif -// May be better use: https://github.com/mrRobot62/Arduino-logging-library +// May be better use: https://github.com/mrRobot62/Arduino-logging-library OU -VDEBUG(see ESP8266) enum DebugTarget{ DEBUG_SERIAL, DEBUG_CURRENT @@ -84,6 +85,9 @@ enum ConnectionMode{ namespace od { +// DYNAMIC CONFIGURATION + // ===================================== + typedef struct{ bool saved = false; char * moduleName = "OpenDevice"; @@ -96,6 +100,7 @@ namespace od { int devicesEnd = 0; uint8_t pinReset = 2; bool debugMode = false; + bool keepAlive = true; uint8_t debugTarget = DEBUG_SERIAL; ConnectionMode connectionMode = CONNECTION_MODE_SERVER; From f66b4049d18f0caf0b10fdf454484ddef1debba2 Mon Sep 17 00:00:00 2001 From: douglas Date: Sun, 10 Jul 2016 07:23:02 -0300 Subject: [PATCH 10/15] adicionado modulo de controle de expansores --- dependencies.h | 4 +- .../Controller595.ino/Controller595.ino.ino | 27 +++++++++ src/AbstractExpander.h | 20 +++++++ src/Device.h | 7 +-- src/OpenDevice.h | 1 + src/devices/Controller595.cpp | 56 +++++++++++++++++++ src/devices/Controller595.h | 26 +++++++++ src/devices/LogicalDevice.cpp | 26 +++++++++ src/devices/LogicalDevice.h | 25 +++++++++ 9 files changed, 186 insertions(+), 6 deletions(-) create mode 100644 examples/Controller595/Controller595.ino/Controller595.ino.ino create mode 100644 src/AbstractExpander.h create mode 100644 src/devices/Controller595.cpp create mode 100644 src/devices/Controller595.h create mode 100644 src/devices/LogicalDevice.cpp create mode 100644 src/devices/LogicalDevice.h diff --git a/dependencies.h b/dependencies.h index 294f2bd..11d586c 100644 --- a/dependencies.h +++ b/dependencies.h @@ -21,8 +21,8 @@ //#include // RFID // #include // RF 433Mhz // #include // ESP8266 - AT Firmwware -#include // ESP8266 - Standalone -#include // Enable MQTT +//#include // ESP8266 - Standalone +//#include // Enable MQTT //#if defined(EnableInterrupt_h) //#define EI_ARDUINO_INTERRUPTED_PIN diff --git a/examples/Controller595/Controller595.ino/Controller595.ino.ino b/examples/Controller595/Controller595.ino/Controller595.ino.ino new file mode 100644 index 0000000..151f2ec --- /dev/null +++ b/examples/Controller595/Controller595.ino/Controller595.ino.ino @@ -0,0 +1,27 @@ +/* + * ***************************************************************************** + * See tutorial: https://opendevice.atlassian.net/wiki/display/DOC/A.+First+Steps+with+ODev + * This example code is in the public domain. + * ***************************************************************************** + */ +#include +#include + +Controller595 control(8,12,11,3); +LogicalDevice led(1); +LogicalDevice led1(9); +LogicalDevice led2(22); + +void setup(){ + control.begin(); + ODev.enableDebug(); + ODev.addDevice(led); // ID:1 + ODev.addDevice(led1); // ID:2 + ODev.addDevice(led2); // ID:3 + ODev.begin(); // by default call Serial.begin() and while(!Serial) on Leonardo +} + +void loop(){ + ODev.loop(); +} + diff --git a/src/AbstractExpander.h b/src/AbstractExpander.h new file mode 100644 index 0000000..2f74527 --- /dev/null +++ b/src/AbstractExpander.h @@ -0,0 +1,20 @@ +/* + Controller595.h - Library for expansão de portas. + Created by Douglas B., Julho 2, 2016. + Released into the public domain. +*/ + +#ifndef AbstractExpander_h +#define AbstractExpander_h + +class AbstractExpander{ + public: + virtual void begin(); + virtual void digitalWrite(int indice,byte value); + protected: + int _chips; + byte *_portas; + virtual void syncData(); +}; + +#endif \ No newline at end of file diff --git a/src/Device.h b/src/Device.h index 0663146..f4dea0f 100755 --- a/src/Device.h +++ b/src/Device.h @@ -78,7 +78,7 @@ class Device * Change value / state of Device * @param sync - sync with server */ - bool setValue(unsigned long value, bool sync = true); + virtual bool setValue(unsigned long value, bool sync = true); void on(); @@ -126,12 +126,11 @@ class Device int toString(char buffer[]); private: - + void _init(char* name, uint8_t iid, uint8_t ipin, Device::DeviceType type, bool sensor); +protected: DeviceListener changeListener; DeviceListener syncListerner; - void _init(char* name, uint8_t iid, uint8_t ipin, Device::DeviceType type, bool sensor); - }; diff --git a/src/OpenDevice.h b/src/OpenDevice.h index 3defd63..6b7ec37 100755 --- a/src/OpenDevice.h +++ b/src/OpenDevice.h @@ -20,6 +20,7 @@ #include "Command.h" #include "DeviceConnection.h" #include "Device.h" +#include "devices/LogicalDevice.h" #include "devices/FuncSensor.h" #include "../dependencies.h" diff --git a/src/devices/Controller595.cpp b/src/devices/Controller595.cpp new file mode 100644 index 0000000..f7fa264 --- /dev/null +++ b/src/devices/Controller595.cpp @@ -0,0 +1,56 @@ +#include "Arduino.h" +#include "Controller595.h" + +Controller595 *Controller595::controller=NULL; + +Controller595::Controller595(){ + +} + +Controller595::Controller595(int lacth,int cloc,int data,int chips){ + _pinClock = cloc; + _pinLacth = lacth; + _pinData = data; + _chips = chips; + _portas = new byte[_chips]; + Controller595::controller = (Controller595 *)this; +} + +Controller595::Controller595(int lacth,int cloc,int data,int chips,Controller595 *c){ + Controller595(lacth,cloc,data,chips); + Controller595::controller = c; +} + +void Controller595::begin(){ + pinMode(_pinClock, OUTPUT); + pinMode(_pinLacth, OUTPUT); + pinMode(_pinData, OUTPUT); + for(int i=0;i<_chips;i++){ + _portas[i] = 0b00000000; + } + syncData(); +} + +void Controller595::digitalWrite(int indice,byte value){ + int chipSelect = (int)indice/9; + int pinSelect = (indice-1)-(chipSelect*8); + bitWrite(_portas[chipSelect],pinSelect,value); + syncData(); +} + +void Controller595::syncData(){ + ::digitalWrite(_pinLacth, LOW); + + for (int i=_chips-1; i>=0; i--) { + shiftOut(_pinData,_pinClock, MSBFIRST, _portas[i]); + Serial.print(" -"); + Serial.print(_portas[i],BIN); + + } + + Serial.println(""); + + ::digitalWrite(_pinLacth, HIGH); +} + + diff --git a/src/devices/Controller595.h b/src/devices/Controller595.h new file mode 100644 index 0000000..d4c57b5 --- /dev/null +++ b/src/devices/Controller595.h @@ -0,0 +1,26 @@ +/* + Controller595.h - Library for expansão de portas. + Created by Douglas B., Julho 2, 2016. + Released into the public domain. +*/ + +#ifndef Controller595_h +#define Controller595_h + +#include "Arduino.h" +#include "AbstractExpander.h" + +class Controller595 : public AbstractExpander{ + public: + Controller595(int lacth,int cloc,int data,int chips); + void begin(); + void digitalWrite(int indice,byte value); + static Controller595 *controller; + private: + int _pinClock; + int _pinLacth; + int _pinData; + void syncData(); +}; + +#endif \ No newline at end of file diff --git a/src/devices/LogicalDevice.cpp b/src/devices/LogicalDevice.cpp new file mode 100644 index 0000000..64f8206 --- /dev/null +++ b/src/devices/LogicalDevice.cpp @@ -0,0 +1,26 @@ +#include "Arduino.h" +#include "LogicalDevice.h" +#include "Controller595.h" + + +LogicalDevice::LogicalDevice(int pin,AbstractExpander *controller){ + _pin = pin; + _controller = controller; +} + +LogicalDevice::LogicalDevice(int pin){ + _pin = pin; + _controller = Controller595::controller; +} + +bool LogicalDevice::setValue(unsigned long value, bool sync){ + _controller->digitalWrite(_pin,value); + notifyListeners(); + + if(sync){ + if(syncListerner) (*syncListerner)(id, currentValue); + } +} + + + diff --git a/src/devices/LogicalDevice.h b/src/devices/LogicalDevice.h new file mode 100644 index 0000000..6c582d2 --- /dev/null +++ b/src/devices/LogicalDevice.h @@ -0,0 +1,25 @@ +/* + LogicalDevice.h - Library for expansão de portas. + Created by Douglas B., Julho 2, 2016. + Released into the public domain. +*/ + +#ifndef LogicalDevice_h +#define LogicalDevice_h + +#include "Arduino.h" +#include "Device.h" +#include "Controller595.h" +#include "AbstractExpander.h" + +class LogicalDevice : public Device { + public: + LogicalDevice(int pin,AbstractExpander *controler); + LogicalDevice(int pin); + bool setValue(unsigned long value, bool sync); + private: + int _pin; + AbstractExpander *_controller; +}; + +#endif \ No newline at end of file From ed06d9c4a6980940dcd382f144c21025973cb530 Mon Sep 17 00:00:00 2001 From: douglas Date: Mon, 11 Jul 2016 18:20:50 -0300 Subject: [PATCH 11/15] examples de controller --- .../Controller595.ino/Controller595.ino.ino | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 examples/Controller595/Controller595.ino/Controller595.ino.ino diff --git a/examples/Controller595/Controller595.ino/Controller595.ino.ino b/examples/Controller595/Controller595.ino/Controller595.ino.ino new file mode 100644 index 0000000..151f2ec --- /dev/null +++ b/examples/Controller595/Controller595.ino/Controller595.ino.ino @@ -0,0 +1,27 @@ +/* + * ***************************************************************************** + * See tutorial: https://opendevice.atlassian.net/wiki/display/DOC/A.+First+Steps+with+ODev + * This example code is in the public domain. + * ***************************************************************************** + */ +#include +#include + +Controller595 control(8,12,11,3); +LogicalDevice led(1); +LogicalDevice led1(9); +LogicalDevice led2(22); + +void setup(){ + control.begin(); + ODev.enableDebug(); + ODev.addDevice(led); // ID:1 + ODev.addDevice(led1); // ID:2 + ODev.addDevice(led2); // ID:3 + ODev.begin(); // by default call Serial.begin() and while(!Serial) on Leonardo +} + +void loop(){ + ODev.loop(); +} + From 891ef69aabc57ac355152bfe0cbcda4b1cfeb3b7 Mon Sep 17 00:00:00 2001 From: douglas Date: Mon, 11 Jul 2016 18:27:18 -0300 Subject: [PATCH 12/15] tetet --- README.adoc | 0 dependencies.h | 0 .../BluetoothConnection.ino | 0 .../BluetoothConnectionSoft.ino | 0 .../Controller595.ino/Controller595.ino.ino | 0 .../Controller595.ino/Controller595.ino.ino | 27 +++++++++++++++++++ examples/CustomSensor/CustomSensor.ino | 0 .../DigisparkBluetoothLowLevel.ino | 0 examples/ESP8266WiFi/ESP8266WiFi.ino | 0 examples/ESP8266WiFi_AT/ESP8266WiFi_AT.ino | 0 .../EnergiaLaunchPadBasic.ino | 0 examples/RF433Sensor/RF433Sensor.ino | 0 examples/RFIDSensor/RFIDSensor.ino | 0 examples/UsbConnection/UsbConnection.ino | 0 examples/UserCommands/UserCommands.ino | 0 src/BaseWifiConnection.cpp | 0 src/BaseWifiConnection.h | 0 src/MQTTClient.cpp | 0 src/MQTTClient.h | 0 src/MQTTConnection.h | 0 src/OpenDeviceTiny.cpp | 0 src/OpenDeviceTiny.h | 0 src/WifiConnection.cpp | 0 src/WifiConnection.h | 0 src/WifiConnetionEspAT.cpp | 0 src/WifiConnetionEspAT.h | 0 src/YunServerConnection.h | 0 src/devices/FuncSensor.cpp | 0 src/devices/FuncSensor.h | 0 src/devices/IRDevice.cpp | 0 src/devices/IRDevice.h | 0 src/devices/IRSensor.cpp | 0 src/devices/IRSensor.h | 0 src/devices/RFIDSensor.cpp | 0 src/devices/RFIDSensor.h | 0 src/devices/RFSensor.cpp | 0 src/devices/RFSensor.h | 0 src/utility/DataUtils.cpp | 0 src/utility/DataUtils.h | 0 src/utility/Logger.cpp | 0 src/utility/Logger.h | 0 src/utility/StreamBuffer.cpp | 0 src/utility/StreamBuffer.h | 0 src/utility/Timeout.cpp | 0 src/utility/Timeout.h | 0 45 files changed, 27 insertions(+) mode change 100644 => 100755 README.adoc mode change 100644 => 100755 dependencies.h mode change 100644 => 100755 examples/BluetoothConnection/BluetoothConnection.ino mode change 100644 => 100755 examples/BluetoothConnectionSoft/BluetoothConnectionSoft.ino mode change 100644 => 100755 examples/Controller595/Controller595.ino/Controller595.ino.ino create mode 100644 examples/Controller595/Controller595.ino/Controller595.ino/Controller595.ino.ino mode change 100644 => 100755 examples/CustomSensor/CustomSensor.ino mode change 100644 => 100755 examples/DigisparkBluetoothLowLevel/DigisparkBluetoothLowLevel.ino mode change 100644 => 100755 examples/ESP8266WiFi/ESP8266WiFi.ino mode change 100644 => 100755 examples/ESP8266WiFi_AT/ESP8266WiFi_AT.ino mode change 100644 => 100755 examples/EnergiaLaunchPadBasic/EnergiaLaunchPadBasic.ino mode change 100644 => 100755 examples/RF433Sensor/RF433Sensor.ino mode change 100644 => 100755 examples/RFIDSensor/RFIDSensor.ino mode change 100644 => 100755 examples/UsbConnection/UsbConnection.ino mode change 100644 => 100755 examples/UserCommands/UserCommands.ino mode change 100644 => 100755 src/BaseWifiConnection.cpp mode change 100644 => 100755 src/BaseWifiConnection.h mode change 100644 => 100755 src/MQTTClient.cpp mode change 100644 => 100755 src/MQTTClient.h mode change 100644 => 100755 src/MQTTConnection.h mode change 100644 => 100755 src/OpenDeviceTiny.cpp mode change 100644 => 100755 src/OpenDeviceTiny.h mode change 100644 => 100755 src/WifiConnection.cpp mode change 100644 => 100755 src/WifiConnection.h mode change 100644 => 100755 src/WifiConnetionEspAT.cpp mode change 100644 => 100755 src/WifiConnetionEspAT.h mode change 100644 => 100755 src/YunServerConnection.h mode change 100644 => 100755 src/devices/FuncSensor.cpp mode change 100644 => 100755 src/devices/FuncSensor.h mode change 100644 => 100755 src/devices/IRDevice.cpp mode change 100644 => 100755 src/devices/IRDevice.h mode change 100644 => 100755 src/devices/IRSensor.cpp mode change 100644 => 100755 src/devices/IRSensor.h mode change 100644 => 100755 src/devices/RFIDSensor.cpp mode change 100644 => 100755 src/devices/RFIDSensor.h mode change 100644 => 100755 src/devices/RFSensor.cpp mode change 100644 => 100755 src/devices/RFSensor.h mode change 100644 => 100755 src/utility/DataUtils.cpp mode change 100644 => 100755 src/utility/DataUtils.h mode change 100644 => 100755 src/utility/Logger.cpp mode change 100644 => 100755 src/utility/Logger.h mode change 100644 => 100755 src/utility/StreamBuffer.cpp mode change 100644 => 100755 src/utility/StreamBuffer.h mode change 100644 => 100755 src/utility/Timeout.cpp mode change 100644 => 100755 src/utility/Timeout.h diff --git a/README.adoc b/README.adoc old mode 100644 new mode 100755 diff --git a/dependencies.h b/dependencies.h old mode 100644 new mode 100755 diff --git a/examples/BluetoothConnection/BluetoothConnection.ino b/examples/BluetoothConnection/BluetoothConnection.ino old mode 100644 new mode 100755 diff --git a/examples/BluetoothConnectionSoft/BluetoothConnectionSoft.ino b/examples/BluetoothConnectionSoft/BluetoothConnectionSoft.ino old mode 100644 new mode 100755 diff --git a/examples/Controller595/Controller595.ino/Controller595.ino.ino b/examples/Controller595/Controller595.ino/Controller595.ino.ino old mode 100644 new mode 100755 diff --git a/examples/Controller595/Controller595.ino/Controller595.ino/Controller595.ino.ino b/examples/Controller595/Controller595.ino/Controller595.ino/Controller595.ino.ino new file mode 100644 index 0000000..9e162e8 --- /dev/null +++ b/examples/Controller595/Controller595.ino/Controller595.ino/Controller595.ino.ino @@ -0,0 +1,27 @@ +/* + * ***************************************************************************** + * See tutorial: https://opendevice.atlassian.net/wiki/display/DOC/A.+First+Steps+with+ODev + * This example code is in the public domain. + * ***************************************************************************** + */ +#include +#include + +Controller595 control(8,12,11,3); +LogicalDevice led(1); +LogicalDevice led1(9); +LogicalDevice led2(22); + +void setup(){ + control.begin(); + ODev.enableDebug(); + ODev.addDevice(led); // ID:1 + ODev.addDevice(led1); // ID:2 + ODev.addDevice(led2); // ID:3 + ODev.begin(); // by default call Serial.begin() and while(!Serial) on Leonardo +} + +void loop(){ + ODev.loop(); +} + diff --git a/examples/CustomSensor/CustomSensor.ino b/examples/CustomSensor/CustomSensor.ino old mode 100644 new mode 100755 diff --git a/examples/DigisparkBluetoothLowLevel/DigisparkBluetoothLowLevel.ino b/examples/DigisparkBluetoothLowLevel/DigisparkBluetoothLowLevel.ino old mode 100644 new mode 100755 diff --git a/examples/ESP8266WiFi/ESP8266WiFi.ino b/examples/ESP8266WiFi/ESP8266WiFi.ino old mode 100644 new mode 100755 diff --git a/examples/ESP8266WiFi_AT/ESP8266WiFi_AT.ino b/examples/ESP8266WiFi_AT/ESP8266WiFi_AT.ino old mode 100644 new mode 100755 diff --git a/examples/EnergiaLaunchPadBasic/EnergiaLaunchPadBasic.ino b/examples/EnergiaLaunchPadBasic/EnergiaLaunchPadBasic.ino old mode 100644 new mode 100755 diff --git a/examples/RF433Sensor/RF433Sensor.ino b/examples/RF433Sensor/RF433Sensor.ino old mode 100644 new mode 100755 diff --git a/examples/RFIDSensor/RFIDSensor.ino b/examples/RFIDSensor/RFIDSensor.ino old mode 100644 new mode 100755 diff --git a/examples/UsbConnection/UsbConnection.ino b/examples/UsbConnection/UsbConnection.ino old mode 100644 new mode 100755 diff --git a/examples/UserCommands/UserCommands.ino b/examples/UserCommands/UserCommands.ino old mode 100644 new mode 100755 diff --git a/src/BaseWifiConnection.cpp b/src/BaseWifiConnection.cpp old mode 100644 new mode 100755 diff --git a/src/BaseWifiConnection.h b/src/BaseWifiConnection.h old mode 100644 new mode 100755 diff --git a/src/MQTTClient.cpp b/src/MQTTClient.cpp old mode 100644 new mode 100755 diff --git a/src/MQTTClient.h b/src/MQTTClient.h old mode 100644 new mode 100755 diff --git a/src/MQTTConnection.h b/src/MQTTConnection.h old mode 100644 new mode 100755 diff --git a/src/OpenDeviceTiny.cpp b/src/OpenDeviceTiny.cpp old mode 100644 new mode 100755 diff --git a/src/OpenDeviceTiny.h b/src/OpenDeviceTiny.h old mode 100644 new mode 100755 diff --git a/src/WifiConnection.cpp b/src/WifiConnection.cpp old mode 100644 new mode 100755 diff --git a/src/WifiConnection.h b/src/WifiConnection.h old mode 100644 new mode 100755 diff --git a/src/WifiConnetionEspAT.cpp b/src/WifiConnetionEspAT.cpp old mode 100644 new mode 100755 diff --git a/src/WifiConnetionEspAT.h b/src/WifiConnetionEspAT.h old mode 100644 new mode 100755 diff --git a/src/YunServerConnection.h b/src/YunServerConnection.h old mode 100644 new mode 100755 diff --git a/src/devices/FuncSensor.cpp b/src/devices/FuncSensor.cpp old mode 100644 new mode 100755 diff --git a/src/devices/FuncSensor.h b/src/devices/FuncSensor.h old mode 100644 new mode 100755 diff --git a/src/devices/IRDevice.cpp b/src/devices/IRDevice.cpp old mode 100644 new mode 100755 diff --git a/src/devices/IRDevice.h b/src/devices/IRDevice.h old mode 100644 new mode 100755 diff --git a/src/devices/IRSensor.cpp b/src/devices/IRSensor.cpp old mode 100644 new mode 100755 diff --git a/src/devices/IRSensor.h b/src/devices/IRSensor.h old mode 100644 new mode 100755 diff --git a/src/devices/RFIDSensor.cpp b/src/devices/RFIDSensor.cpp old mode 100644 new mode 100755 diff --git a/src/devices/RFIDSensor.h b/src/devices/RFIDSensor.h old mode 100644 new mode 100755 diff --git a/src/devices/RFSensor.cpp b/src/devices/RFSensor.cpp old mode 100644 new mode 100755 diff --git a/src/devices/RFSensor.h b/src/devices/RFSensor.h old mode 100644 new mode 100755 diff --git a/src/utility/DataUtils.cpp b/src/utility/DataUtils.cpp old mode 100644 new mode 100755 diff --git a/src/utility/DataUtils.h b/src/utility/DataUtils.h old mode 100644 new mode 100755 diff --git a/src/utility/Logger.cpp b/src/utility/Logger.cpp old mode 100644 new mode 100755 diff --git a/src/utility/Logger.h b/src/utility/Logger.h old mode 100644 new mode 100755 diff --git a/src/utility/StreamBuffer.cpp b/src/utility/StreamBuffer.cpp old mode 100644 new mode 100755 diff --git a/src/utility/StreamBuffer.h b/src/utility/StreamBuffer.h old mode 100644 new mode 100755 diff --git a/src/utility/Timeout.cpp b/src/utility/Timeout.cpp old mode 100644 new mode 100755 diff --git a/src/utility/Timeout.h b/src/utility/Timeout.h old mode 100644 new mode 100755 From cad12bdc4fa3cbdb073ef6ffc2274b3557764748 Mon Sep 17 00:00:00 2001 From: douglas Date: Mon, 11 Jul 2016 18:29:33 -0300 Subject: [PATCH 13/15] class controller de expansores --- .../Controller595.ino/Controller595.ino.ino | 27 +++++++++++++++++++ src/devices/Controller595.cpp | 9 ------- 2 files changed, 27 insertions(+), 9 deletions(-) create mode 100644 examples/Controller595/Controller595.ino/Controller595.ino/Controller595.ino.ino diff --git a/examples/Controller595/Controller595.ino/Controller595.ino/Controller595.ino.ino b/examples/Controller595/Controller595.ino/Controller595.ino/Controller595.ino.ino new file mode 100644 index 0000000..9e162e8 --- /dev/null +++ b/examples/Controller595/Controller595.ino/Controller595.ino/Controller595.ino.ino @@ -0,0 +1,27 @@ +/* + * ***************************************************************************** + * See tutorial: https://opendevice.atlassian.net/wiki/display/DOC/A.+First+Steps+with+ODev + * This example code is in the public domain. + * ***************************************************************************** + */ +#include +#include + +Controller595 control(8,12,11,3); +LogicalDevice led(1); +LogicalDevice led1(9); +LogicalDevice led2(22); + +void setup(){ + control.begin(); + ODev.enableDebug(); + ODev.addDevice(led); // ID:1 + ODev.addDevice(led1); // ID:2 + ODev.addDevice(led2); // ID:3 + ODev.begin(); // by default call Serial.begin() and while(!Serial) on Leonardo +} + +void loop(){ + ODev.loop(); +} + diff --git a/src/devices/Controller595.cpp b/src/devices/Controller595.cpp index f7fa264..be8c5a4 100644 --- a/src/devices/Controller595.cpp +++ b/src/devices/Controller595.cpp @@ -3,10 +3,6 @@ Controller595 *Controller595::controller=NULL; -Controller595::Controller595(){ - -} - Controller595::Controller595(int lacth,int cloc,int data,int chips){ _pinClock = cloc; _pinLacth = lacth; @@ -16,11 +12,6 @@ Controller595::Controller595(int lacth,int cloc,int data,int chips){ Controller595::controller = (Controller595 *)this; } -Controller595::Controller595(int lacth,int cloc,int data,int chips,Controller595 *c){ - Controller595(lacth,cloc,data,chips); - Controller595::controller = c; -} - void Controller595::begin(){ pinMode(_pinClock, OUTPUT); pinMode(_pinLacth, OUTPUT); From 04d5952ef415a839d41c7a02db7dde8bef9c2db0 Mon Sep 17 00:00:00 2001 From: douglas Date: Mon, 11 Jul 2016 19:45:37 -0300 Subject: [PATCH 14/15] =?UTF-8?q?Melhorando=20implementa=C3=A7=C3=A3o=20do?= =?UTF-8?q?=20expansores=20de=20portas=20e=20link=20do=20tutorial?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/Expander595/Expander595.ino | 23 +++++++++++++++++++ src/OpenDevice.cpp | 21 +++++++++++++++++ src/OpenDevice.h | 5 ++++ .../{Controller595.cpp => Expander595.cpp} | 18 +++++++-------- .../{Controller595.h => Expander595.h} | 11 ++++----- src/devices/LogicalDevice.cpp | 3 +-- src/devices/LogicalDevice.h | 1 - 7 files changed, 63 insertions(+), 19 deletions(-) create mode 100644 examples/Expander595/Expander595.ino rename src/devices/{Controller595.cpp => Expander595.cpp} (69%) rename src/devices/{Controller595.h => Expander595.h} (55%) diff --git a/examples/Expander595/Expander595.ino b/examples/Expander595/Expander595.ino new file mode 100644 index 0000000..e2634dd --- /dev/null +++ b/examples/Expander595/Expander595.ino @@ -0,0 +1,23 @@ +/* + * ***************************************************************************** + * See tutorial: https://opendevice.atlassian.net/wiki/display/DOC/A.+First+Steps+with+ODev + * This example code is in the public domain. + * ***************************************************************************** + */ +#include +#include + +void setup(){ + ODev.enableDebug(); + ODev.setIoExpander(new Expander595(8,12,11,3)); + ODev.addLogical("led",1); // ID:1 + ODev.addLogical("led1",2); // ID:2 + ODev.addLogical("led2",22); // ID:3 + ODev.addLogical("led2",24); // ID:3 + ODev.begin(); // by default call Serial.begin() and while(!Serial) on Leonardo +} + +void loop(){ + ODev.loop(); +} + diff --git a/src/OpenDevice.cpp b/src/OpenDevice.cpp index 86e8dfc..c526ae7 100755 --- a/src/OpenDevice.cpp +++ b/src/OpenDevice.cpp @@ -182,6 +182,10 @@ void OpenDeviceClass::enableDebug(uint8_t _debugTarget){ if(_debugTarget == DEBUG_SERIAL) Serial.begin(DEFAULT_BAUD); } +void OpenDeviceClass::setIoExpander(AbstractExpander *expander){ + this->expander = expander; +} + /** Called when a command is received by the connection */ void OpenDeviceClass::onMessageReceived(Command cmd) { ODev.lastCMD = cmd; @@ -478,6 +482,23 @@ Device* OpenDeviceClass::addDevice(char* name, uint8_t pin, Device::DeviceType t } } +Device* OpenDeviceClass::addLogical(char* name, uint8_t pin,AbstractExpander *expander, uint8_t id){ + if (deviceLength < MAX_DEVICE) { + if (id == 0) id = (deviceLength + 1); + if(expander==NULL) + devices[deviceLength] = new LogicalDevice(pin,this->expander); + else + devices[deviceLength] = new LogicalDevice(pin,expander); + devices[deviceLength]->name(name); + devices[deviceLength]->id=id; + devices[deviceLength]->setSyncListener(&(OpenDeviceClass::onDeviceChanged)); + deviceLength++; + return devices[deviceLength-1]; + } else{ + return false; + } +} + bool OpenDeviceClass::addCommand(const char * name, void (*function)()){ if (commandsLength < MAX_COMMAND) { diff --git a/src/OpenDevice.h b/src/OpenDevice.h index 6b7ec37..5e3d039 100755 --- a/src/OpenDevice.h +++ b/src/OpenDevice.h @@ -103,6 +103,7 @@ class OpenDeviceClass { Device* devices[MAX_DEVICE]; CommandCallback commands[MAX_COMMAND]; + AbstractExpander *expander; // Debouncing of normal pressing (for Sensor's) long time; @@ -323,6 +324,8 @@ void begin(ESP8266WiFiClass &wifi){ void enableDebug(uint8_t debugTarget = DEBUG_SERIAL); + void setIoExpander(AbstractExpander *expander); + void send(Command cmd); /** Create a simple command (using lastCMD buffer)*/ @@ -350,6 +353,8 @@ void begin(ESP8266WiFiClass &wifi){ Device* addDevice(char* name, uint8_t pin, Device::DeviceType type); Device* addDevice(Device& device); + Device* addLogical(char* name, uint8_t pin, AbstractExpander *expander=NULL, uint8_t id=0); + bool addCommand(const char * name, void (*function)()); #ifdef _TASKSCHEDULER_H_ diff --git a/src/devices/Controller595.cpp b/src/devices/Expander595.cpp similarity index 69% rename from src/devices/Controller595.cpp rename to src/devices/Expander595.cpp index be8c5a4..9033a2e 100644 --- a/src/devices/Controller595.cpp +++ b/src/devices/Expander595.cpp @@ -1,18 +1,12 @@ #include "Arduino.h" -#include "Controller595.h" +#include "Expander595.h" -Controller595 *Controller595::controller=NULL; - -Controller595::Controller595(int lacth,int cloc,int data,int chips){ +Expander595::Expander595(int lacth,int cloc,int data,int chips){ _pinClock = cloc; _pinLacth = lacth; _pinData = data; _chips = chips; _portas = new byte[_chips]; - Controller595::controller = (Controller595 *)this; -} - -void Controller595::begin(){ pinMode(_pinClock, OUTPUT); pinMode(_pinLacth, OUTPUT); pinMode(_pinData, OUTPUT); @@ -22,14 +16,18 @@ void Controller595::begin(){ syncData(); } -void Controller595::digitalWrite(int indice,byte value){ +void Expander595::begin(){ + +} + +void Expander595::digitalWrite(int indice,byte value){ int chipSelect = (int)indice/9; int pinSelect = (indice-1)-(chipSelect*8); bitWrite(_portas[chipSelect],pinSelect,value); syncData(); } -void Controller595::syncData(){ +void Expander595::syncData(){ ::digitalWrite(_pinLacth, LOW); for (int i=_chips-1; i>=0; i--) { diff --git a/src/devices/Controller595.h b/src/devices/Expander595.h similarity index 55% rename from src/devices/Controller595.h rename to src/devices/Expander595.h index d4c57b5..5892d27 100644 --- a/src/devices/Controller595.h +++ b/src/devices/Expander595.h @@ -1,21 +1,20 @@ /* - Controller595.h - Library for expansão de portas. + Expander595.h - Library for expansão de portas. Created by Douglas B., Julho 2, 2016. Released into the public domain. */ -#ifndef Controller595_h -#define Controller595_h +#ifndef Expander595_h +#define Expander595_h #include "Arduino.h" #include "AbstractExpander.h" -class Controller595 : public AbstractExpander{ +class Expander595 : public AbstractExpander{ public: - Controller595(int lacth,int cloc,int data,int chips); + Expander595(int lacth,int cloc,int data,int chips); void begin(); void digitalWrite(int indice,byte value); - static Controller595 *controller; private: int _pinClock; int _pinLacth; diff --git a/src/devices/LogicalDevice.cpp b/src/devices/LogicalDevice.cpp index 64f8206..fa96ea8 100644 --- a/src/devices/LogicalDevice.cpp +++ b/src/devices/LogicalDevice.cpp @@ -1,6 +1,6 @@ #include "Arduino.h" #include "LogicalDevice.h" -#include "Controller595.h" + LogicalDevice::LogicalDevice(int pin,AbstractExpander *controller){ @@ -10,7 +10,6 @@ LogicalDevice::LogicalDevice(int pin,AbstractExpander *controller){ LogicalDevice::LogicalDevice(int pin){ _pin = pin; - _controller = Controller595::controller; } bool LogicalDevice::setValue(unsigned long value, bool sync){ diff --git a/src/devices/LogicalDevice.h b/src/devices/LogicalDevice.h index 6c582d2..e5c4a9f 100644 --- a/src/devices/LogicalDevice.h +++ b/src/devices/LogicalDevice.h @@ -9,7 +9,6 @@ #include "Arduino.h" #include "Device.h" -#include "Controller595.h" #include "AbstractExpander.h" class LogicalDevice : public Device { From 62e8e673688611033f4382020b608f00d6601f88 Mon Sep 17 00:00:00 2001 From: douglas Date: Mon, 11 Jul 2016 19:47:18 -0300 Subject: [PATCH 15/15] exemplo com link do tutorial --- .../Controller595.ino/Controller595.ino.ino | 0 examples/Expander595/Expander595.ino | 0 examples/Expander595/Expander595.ino~ | 22 +++++++++++++++++++ src/AbstractExpander.h | 0 src/MQTTWifiConnection.cpp | 0 src/MQTTWifiConnection.h | 0 src/devices/Expander595.cpp | 0 src/devices/Expander595.h | 0 src/devices/LogicalDevice.cpp | 0 src/devices/LogicalDevice.h | 0 10 files changed, 22 insertions(+) mode change 100644 => 100755 examples/Controller595/Controller595.ino/Controller595.ino/Controller595.ino.ino mode change 100644 => 100755 examples/Expander595/Expander595.ino create mode 100755 examples/Expander595/Expander595.ino~ mode change 100644 => 100755 src/AbstractExpander.h mode change 100644 => 100755 src/MQTTWifiConnection.cpp mode change 100644 => 100755 src/MQTTWifiConnection.h mode change 100644 => 100755 src/devices/Expander595.cpp mode change 100644 => 100755 src/devices/Expander595.h mode change 100644 => 100755 src/devices/LogicalDevice.cpp mode change 100644 => 100755 src/devices/LogicalDevice.h diff --git a/examples/Controller595/Controller595.ino/Controller595.ino/Controller595.ino.ino b/examples/Controller595/Controller595.ino/Controller595.ino/Controller595.ino.ino old mode 100644 new mode 100755 diff --git a/examples/Expander595/Expander595.ino b/examples/Expander595/Expander595.ino old mode 100644 new mode 100755 diff --git a/examples/Expander595/Expander595.ino~ b/examples/Expander595/Expander595.ino~ new file mode 100755 index 0000000..74b7fa9 --- /dev/null +++ b/examples/Expander595/Expander595.ino~ @@ -0,0 +1,22 @@ +/* + * ***************************************************************************** + * See tutorial: https://opendevice.atlassian.net/wiki/display/DOC/Port+Expanders + * This example code is in the public domain. + * ***************************************************************************** + */ +#include +#include + +void setup(){ + ODev.enableDebug(); + ODev.setIoExpander(new Expander595(8,12,11,2));//pin load, pin clock, pin data, lenght of chip + ODev.addLogical("led 0",1); // ID:1 chip:1 Q0 + ODev.addLogical("led 1",2); // ID:2 chip:1 Q1 + ODev.addLogical("led 2",15); // ID:3 chip:2 Q6 + ODev.begin(); // by default call Serial.begin() and while(!Serial) on Leonardo +} + +void loop(){ + ODev.loop(); +} + diff --git a/src/AbstractExpander.h b/src/AbstractExpander.h old mode 100644 new mode 100755 diff --git a/src/MQTTWifiConnection.cpp b/src/MQTTWifiConnection.cpp old mode 100644 new mode 100755 diff --git a/src/MQTTWifiConnection.h b/src/MQTTWifiConnection.h old mode 100644 new mode 100755 diff --git a/src/devices/Expander595.cpp b/src/devices/Expander595.cpp old mode 100644 new mode 100755 diff --git a/src/devices/Expander595.h b/src/devices/Expander595.h old mode 100644 new mode 100755 diff --git a/src/devices/LogicalDevice.cpp b/src/devices/LogicalDevice.cpp old mode 100644 new mode 100755 diff --git a/src/devices/LogicalDevice.h b/src/devices/LogicalDevice.h old mode 100644 new mode 100755