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 index 297f3b7..11d586c --- a/dependencies.h +++ b/dependencies.h @@ -21,8 +21,8 @@ //#include // RFID // #include // RF 433Mhz // #include // ESP8266 - AT Firmwware -#include // ESP8266 - Standalone - +//#include // ESP8266 - Standalone +//#include // Enable MQTT //#if defined(EnableInterrupt_h) //#define EI_ARDUINO_INTERRUPTED_PIN diff --git a/examples/BluetoothConnection/BluetoothConnection.ino b/examples/BluetoothConnection/BluetoothConnection.ino old mode 100644 new mode 100755 index 44a1c07..429595e --- 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 old mode 100644 new mode 100755 index 22f3cd9..03f78fd --- 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/Controller595/Controller595.ino/Controller595.ino.ino b/examples/Controller595/Controller595.ino/Controller595.ino.ino new file mode 100755 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/examples/Controller595/Controller595.ino/Controller595.ino/Controller595.ino.ino b/examples/Controller595/Controller595.ino/Controller595.ino/Controller595.ino.ino new file mode 100755 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 index 39688e3..9a9ac56 --- 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,48 +14,26 @@ */ -//#include // to: AT Firmware +#include // Enable ESP8266 Embedded +// #include // Enable + MQTT #include -#include -const char* ssid = "OpenWrt"; -const char* password = ""; +const char* ssid = "--"; +const char* password = "--"; void setup() { ODev.name("ODev-Thing1"); - 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)); // to: 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 - Only for debug purpose -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/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/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/Expander595/Expander595.ino b/examples/Expander595/Expander595.ino new file mode 100755 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/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/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 index c700025..4ca8340 --- 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 } diff --git a/examples/UserCommands/UserCommands.ino b/examples/UserCommands/UserCommands.ino old mode 100644 new mode 100755 diff --git a/src/AbstractExpander.h b/src/AbstractExpander.h new file mode 100755 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/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 index b611c68..56b9f40 --- 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/Device.cpp b/src/Device.cpp index ba227f9..2a441b0 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; @@ -73,11 +74,11 @@ void Device::_init(uint8_t _id, uint8_t _pin, DeviceType _type, bool _sensor){ void Device::on(){ - setValue(HIGH); + setValue(HIGH, true); } void Device::off(){ - setValue(LOW); + setValue(LOW, true); } bool Device::setValue(unsigned long value, bool sync){ @@ -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..f4dea0f 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; @@ -77,12 +78,17 @@ 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(); void off(); + bool isON() { return currentValue == HIGH; } + + bool isOFF() { return currentValue == LOW; } + + /** * Get current value. */ @@ -94,6 +100,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. @@ -116,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(uint8_t iid, uint8_t ipin, Device::DeviceType type, bool sensor); - }; diff --git a/src/MQTTClient.cpp b/src/MQTTClient.cpp old mode 100644 new mode 100755 index 3600787..37fdbd6 --- 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() { 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 index 4052efb..b96467c --- 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 100755 index 0000000..a0bcd43 --- /dev/null +++ b/src/MQTTWifiConnection.cpp @@ -0,0 +1,84 @@ +/* + * MQTTWifiConnection.cpp + * + * Created on: May 14, 2016 + * Author: ricardo + */ + +#include "../dependencies.h" + +#ifdef PubSubClient_h + +#include + +namespace od { + +MQTTClient* MQTTWifiConnection::mqttClient; + +MQTTWifiConnection::MQTTWifiConnection(): mqtt(ethclient), mqttTimeout(5000) { + mqttClient = new MQTTClient(mqtt); +} + +MQTTWifiConnection::~MQTTWifiConnection() { +} + +void MQTTWifiConnection::begin(){ + WifiConnection::begin(); + Logger.debug("MQTT", "BEGIN"); + mqtt.setServer(Config.server, MQTT_PORT); + mqtt.setCallback(mqttCallback); + mqttConnect(); +} + +bool MQTTWifiConnection::checkDataAvalible(void){ + + // Reconnect MQTT if OFFLINE and not have Client (TcpServer) + if (!mqtt.connected() && mqttTimeout.expired() && !WifiConnection::client.connected()) { + mqttConnect(); + } + + if (mqtt.connected()){ + Config.keepAlive = false; // on MQTT is not required + mqtt.loop(); + mqttTimeout.disable(); + setStream(mqttClient); + return DeviceConnection::checkDataAvalible(); + }else{ + if(!mqttTimeout.isEnabled()) mqttTimeout.enable(); + Config.keepAlive = true; // on raw TCP is required + 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 { + mqttTimeout.reset(); + Serial.print("failed (#"); + Serial.print(mqtt.state()); + Serial.println(")"); + } + +} + +} /* namespace od */ + +#endif diff --git a/src/MQTTWifiConnection.h b/src/MQTTWifiConnection.h new file mode 100755 index 0000000..8060bb8 --- /dev/null +++ b/src/MQTTWifiConnection.h @@ -0,0 +1,79 @@ +/* + * MQTTWifiConnection.h + * + * Created on: May 14, 2016 + * Author: ricardo + */ + +#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 : 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: + + PubSubClient mqtt; + Timeout mqttTimeout; + +#if defined(_YUN_SERVER_H_) +YunClient ethclient; +#endif + +// ESP8266 Standalone +#if defined(ESP8266) +#if ENABLE_SSL + WiFiClientSecure ethclient; + #define MQTT_PORT 8883 +#else + WiFiClient ethclient; + #define MQTT_PORT 1883 +#endif +#endif + + void mqttConnect(); + +}; + + +} /* namespace od */ + +#endif /* LIBRARIES_OPENDEVICE_SRC_MQTTWIFICONNECTION_H_ */ diff --git a/src/OpenDevice.cpp b/src/OpenDevice.cpp index d9b5be7..c526ae7 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){ @@ -183,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; @@ -404,12 +407,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 +423,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 +453,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 +472,8 @@ 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); + devices[deviceLength]->setSyncListener(&(OpenDeviceClass::onDeviceChanged)); deviceLength++; return devices[deviceLength-1]; @@ -477,6 +482,23 @@ Device* OpenDeviceClass::addDevice(uint8_t pin, Device::DeviceType type, bool se } } +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 3ee3c16..5e3d039 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" @@ -39,10 +40,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__) @@ -50,12 +55,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 @@ -97,11 +103,11 @@ class OpenDeviceClass { Device* devices[MAX_DEVICE]; CommandCallback commands[MAX_COMMAND]; + AbstractExpander *expander; // 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; @@ -275,7 +281,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 +290,18 @@ void begin(ESP8266WiFiClass &wifi){ #endif +#if defined(ESP8266) && defined(PubSubClient_h) +void begin(ESP8266WiFiClass &wifi){ + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("#"); + } + + MQTTWifiConnection *conn = new MQTTWifiConnection(); + begin(*conn); +} +#endif + // TODO: Make compatible with Due // #ifdef _SAM3XA_ // #include // Arduino Due @@ -306,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)*/ @@ -321,18 +341,20 @@ 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); + 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/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 index b875e36..5b90ee2 --- 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) { @@ -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{ @@ -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(); diff --git a/src/WifiConnection.h b/src/WifiConnection.h old mode 100644 new mode 100755 index 30a413a..25eee48 --- a/src/WifiConnection.h +++ b/src/WifiConnection.h @@ -44,9 +44,10 @@ using namespace od; */ class WifiConnection : public BaseWifiConnection { -static WiFiClient client; - public: + + static WiFiClient client; // Current Client + WifiConnection(); virtual ~WifiConnection(); @@ -68,7 +69,7 @@ static WiFiClient client; virtual char* getIP(); -private: +protected: WiFiServer server; boolean isStartup; WiFiUDP Udp; // A UDP instance (discovery service) 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/config.h b/src/config.h index f030e32..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,6 +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 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 @@ -69,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 @@ -83,6 +85,9 @@ enum ConnectionMode{ namespace od { +// DYNAMIC CONFIGURATION + // ===================================== + typedef struct{ bool saved = false; char * moduleName = "OpenDevice"; @@ -95,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; diff --git a/src/devices/Expander595.cpp b/src/devices/Expander595.cpp new file mode 100755 index 0000000..9033a2e --- /dev/null +++ b/src/devices/Expander595.cpp @@ -0,0 +1,45 @@ +#include "Arduino.h" +#include "Expander595.h" + +Expander595::Expander595(int lacth,int cloc,int data,int chips){ + _pinClock = cloc; + _pinLacth = lacth; + _pinData = data; + _chips = chips; + _portas = new byte[_chips]; + pinMode(_pinClock, OUTPUT); + pinMode(_pinLacth, OUTPUT); + pinMode(_pinData, OUTPUT); + for(int i=0;i<_chips;i++){ + _portas[i] = 0b00000000; + } + syncData(); +} + +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 Expander595::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/Expander595.h b/src/devices/Expander595.h new file mode 100755 index 0000000..5892d27 --- /dev/null +++ b/src/devices/Expander595.h @@ -0,0 +1,25 @@ +/* + Expander595.h - Library for expansão de portas. + Created by Douglas B., Julho 2, 2016. + Released into the public domain. +*/ + +#ifndef Expander595_h +#define Expander595_h + +#include "Arduino.h" +#include "AbstractExpander.h" + +class Expander595 : public AbstractExpander{ + public: + Expander595(int lacth,int cloc,int data,int chips); + void begin(); + void digitalWrite(int indice,byte value); + private: + int _pinClock; + int _pinLacth; + int _pinData; + void syncData(); +}; + +#endif \ No newline at end of file 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/LogicalDevice.cpp b/src/devices/LogicalDevice.cpp new file mode 100755 index 0000000..fa96ea8 --- /dev/null +++ b/src/devices/LogicalDevice.cpp @@ -0,0 +1,25 @@ +#include "Arduino.h" +#include "LogicalDevice.h" + + + +LogicalDevice::LogicalDevice(int pin,AbstractExpander *controller){ + _pin = pin; + _controller = controller; +} + +LogicalDevice::LogicalDevice(int pin){ + _pin = pin; +} + +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 100755 index 0000000..e5c4a9f --- /dev/null +++ b/src/devices/LogicalDevice.h @@ -0,0 +1,24 @@ +/* + 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 "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 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