diff --git a/libraries/MySensors/MyConfig.h b/libraries/MySensors/MyConfig.h index 9886789..851b808 100644 --- a/libraries/MySensors/MyConfig.h +++ b/libraries/MySensors/MyConfig.h @@ -133,15 +133,27 @@ #endif // The RX LED default pin #ifndef MY_DEFAULT_RX_LED_PIN -#define MY_DEFAULT_RX_LED_PIN 6 + #if defined(ARDUINO_ARCH_ESP8266) + #define MY_DEFAULT_RX_LED_PIN 8 + #else + #define MY_DEFAULT_RX_LED_PIN 6 + #endif #endif // The TX LED default pin #ifndef MY_DEFAULT_TX_LED_PIN -#define MY_DEFAULT_TX_LED_PIN 5 + #if defined(ARDUINO_ARCH_ESP8266) + #define MY_DEFAULT_TX_LED_PIN 9 + #else + #define MY_DEFAULT_TX_LED_PIN 5 + #endif #endif // The Error LED default pin #ifndef MY_DEFAULT_ERR_LED_PIN -#define MY_DEFAULT_ERR_LED_PIN 4 + #if defined(ARDUINO_ARCH_ESP8266) + #define MY_DEFAULT_ERR_LED_PIN 7 + #else + #define MY_DEFAULT_ERR_LED_PIN 4 + #endif #endif /********************************************** @@ -160,7 +172,11 @@ // The default input pin used for the inclusion mode button #ifndef MY_INCLUSION_MODE_BUTTON_PIN -#define MY_INCLUSION_MODE_BUTTON_PIN 3 + #if defined(ARDUINO_ARCH_ESP8266) + #define MY_INCLUSION_MODE_BUTTON_PIN 5 + #else + #define MY_INCLUSION_MODE_BUTTON_PIN 3 + #endif #endif // Number of seconds (default one minute) inclusion mode should be enabled #ifndef MY_INCLUSION_MODE_DURATION @@ -220,17 +236,26 @@ // Enables RF24 encryption (all nodes and gateway must have this enabled) //#define MY_RF24_ENABLE_ENCRYPTION -// Default encrytion key. Override in sketch if needed. +// Default encryption key. Override in sketch if needed. #ifndef MY_RF24_ENCRYPTKEY #define MY_RF24_ENCRYPTKEY 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x10,0x11,0x12,0x13,0x14,0x15,0x16 #endif // Default pin settings. Override in sketch if needed. #ifndef MY_RF24_CE_PIN -#define MY_RF24_CE_PIN 9 + #if defined(ARDUINO_ARCH_ESP8266) + #define MY_RF24_CE_PIN 4 + #else + #define MY_RF24_CE_PIN 9 + #endif #endif + #ifndef MY_RF24_CS_PIN -#define MY_RF24_CS_PIN 10 + #if defined(ARDUINO_ARCH_ESP8266) + #define MY_RF24_CS_PIN 15 + #else + #define MY_RF24_CS_PIN 10 + #endif #endif #ifndef MY_RF24_PA_LEVEL diff --git a/libraries/MySensors/MySensor.h b/libraries/MySensors/MySensor.h index 0f315d9..9426863 100644 --- a/libraries/MySensors/MySensor.h +++ b/libraries/MySensors/MySensor.h @@ -44,10 +44,11 @@ // HARDWARE #if defined(ARDUINO_ARCH_ESP8266) + // Remove PSTR macros from debug prints #undef PSTR #define PSTR(x) (x) - #include "drivers/ESP8266/EEPROM/EEPROM.cpp" - #include "drivers/ESP8266/SPI/SPI.cpp" + /*#include "drivers/ESP8266/EEPROM/EEPROM.cpp" + #include "drivers/ESP8266/SPI/SPI.cpp"*/ #include "core/MyHwESP8266.cpp" // For ESP8266, we always enable gateway feature #define MY_GATEWAY_ESP8266 @@ -230,33 +231,21 @@ #include "core/MySensorCore.cpp" extern void setup(); -//extern void loop(); - // Optional sketch functions called by MySensors library void receive(const MyMessage &message) __attribute__((weak)); void receiveTime(unsigned long) __attribute__((weak)); void presentation() __attribute__((weak)); extern "C" void setup() __attribute__((weak)); extern "C" void loop() __attribute__((weak)); - +extern "C" void loop2() __attribute__((weak)); #include -// Initialize library and handle sketch functions like we want to -int main(void) { - init(); // Init Arduino - #if defined(USBCON) - USBDevice.attach(); - #endif - _begin(); // Startup MySensors library - if (setup) setup(); // Call sketch setup - for(;;) { - _process(); // Process incoming data - if (loop) loop(); // Call sketch loop - #if !defined(ARDUINO_ARCH_ESP8266) - if (serialEventRun) serialEventRun(); - #endif - } - return 0; -} +#if defined(MY_GATEWAY_ESP8266) + #include "core/MyMainESP8266.cpp" +#else + #include "core/MyMainDefault.cpp" +#endif + + #endif diff --git a/libraries/MySensors/core/MyGatewayTransportEthernet.cpp b/libraries/MySensors/core/MyGatewayTransportEthernet.cpp index 2ec8123..f06dbae 100644 --- a/libraries/MySensors/core/MyGatewayTransportEthernet.cpp +++ b/libraries/MySensors/core/MyGatewayTransportEthernet.cpp @@ -86,6 +86,7 @@ bool gatewayTransportInit() { { delay(500); Serial.print("."); + hwWatchdogReset(); } Serial.println(PSTR("Connected to AP!")); Serial.print(PSTR("IP: ")); @@ -168,17 +169,47 @@ bool gatewayTransportAvailable() return protocolParse(_ethernetMsg, inputString[0]); } #else - // Loop over clients connect and read available data + // Go over list of clients and stop any that are no longer connected. + // If the server has a new client connection it will be assigned to a free slot. + bool allSlotsOccupied = true; + for (uint8_t i = 0; i < ARRAY_SIZE(clients); i++) { + if (!clients[i].connected()) { + if (clientsConnected[i]) { + Serial.print("Client "); + Serial.print(i); + Serial.println(" disconnected"); + clients[i].stop(); + } + //check if there are any new clients + if (_ethernetServer.hasClient()) { + clients[i] = _ethernetServer.available(); + inputString[i].idx = 0; + Serial.print("Client "); + Serial.print(i); + Serial.println(" connected"); + gatewayTransportSend(buildGw(_msg, I_GATEWAY_READY).set("Gateway startup complete.")); + } + } + bool connected = clients[i].connected(); + clientsConnected[i] = connected; + allSlotsOccupied &= connected; + } + if (allSlotsOccupied && _ethernetServer.hasClient()) { + //no free/disconnected spot so reject + Serial.println("No free slot available"); + EthernetClient c = _ethernetServer.available(); + c.stop(); + } + // Loop over clients connect and read available data for (uint8_t i = 0; i < ARRAY_SIZE(clients); i++) { while (clients[i].connected() && clients[i].available()) { char inChar = clients[i].read(); + Serial.print(inChar); if (inputString[i].idx < MY_GATEWAY_MAX_RECEIVE_LENGTH - 1) { // if newline then command is complete if (inChar == '\n') { // a command was issued by the client // we will now try to send it to the actuator - inputString[i].string[inputString[i].idx] = 0; - // echo the string to the serial port Serial.print(PSTR("Client ")); Serial.print(i); @@ -188,7 +219,11 @@ bool gatewayTransportAvailable() // clear the string: inputString[i].idx = 0; - return protocolParse(_ethernetMsg, inputString[i].string); + bool ret = protocolParse(_ethernetMsg, inputString[i].string); + inputString[i].string[inputString[i].idx] = 0; + if (ret) + return ret; + } else { // add it to the inputString: inputString[i].string[inputString[i].idx++] = inChar; diff --git a/libraries/MySensors/core/MySensorCore.cpp b/libraries/MySensors/core/MySensorCore.cpp index bda6917..ee3681d 100644 --- a/libraries/MySensors/core/MySensorCore.cpp +++ b/libraries/MySensors/core/MySensorCore.cpp @@ -64,6 +64,7 @@ void _begin() { hwInit(); #endif + debug(PSTR("Starting...\n")); #if defined(MY_RADIO_FEATURE) _failedTransmissions = 0; @@ -74,7 +75,6 @@ void _begin() { } #endif - #if defined(MY_GATEWAY_FEATURE) #if defined(MY_INCLUSION_BUTTON_FEATURE) inclusionInit(); @@ -147,7 +147,8 @@ void _begin() { #if defined(MY_RADIO_FEATURE) transportPresentNode(); #endif - + if (presentation) + presentation(); debug(PSTR("%s started, id=%d, parent=%d, distance=%d\n"), MY_NODE_TYPE, _nc.nodeId, _nc.parentNodeId, _nc.distance); } @@ -232,7 +233,11 @@ void _processInternalMessages() { hwWriteConfig(EEPROM_CONTROLLER_CONFIG_ADDRESS, isMetric); } else if (type == I_PRESENTATION) { // Re-send node presentation to controller - transportPresentNode(); + #if defined(MY_RADIO_FEATURE) + transportPresentNode(); + #endif + if (presentation) + presentation(); } else if (type == I_HEARTBEAT) { if (!mGetAck(_msg)) { // Send heartbeat ack message back to sender (with the same payload) diff --git a/libraries/MySensors/core/MyTransport.cpp b/libraries/MySensors/core/MyTransport.cpp index 788786b..92df222 100644 --- a/libraries/MySensors/core/MyTransport.cpp +++ b/libraries/MySensors/core/MyTransport.cpp @@ -540,8 +540,6 @@ void transportPresentNode() { #endif } #endif - if (presentation) - presentation(); } void transportFindParentNode() { diff --git a/libraries/MySensors/core/MyTransportNRF24.cpp b/libraries/MySensors/core/MyTransportNRF24.cpp index a051fb3..41cac2d 100644 --- a/libraries/MySensors/core/MyTransportNRF24.cpp +++ b/libraries/MySensors/core/MyTransportNRF24.cpp @@ -44,11 +44,16 @@ uint8_t _paLevel; bool transportInit() { // Start up the radio library + Serial.println("Radio1"); _rf24.begin(); + Serial.println("Radio2"); if (!_rf24.isPVariant()) { + Serial.println("Radio3"); + return false; } + Serial.println("Radio4"); _rf24.setAutoAck(1); _rf24.setAutoAck(BROADCAST_PIPE,false); // Turn off auto ack for broadcast _rf24.enableAckPayload(); @@ -58,9 +63,11 @@ bool transportInit() { _rf24.setRetries(5,15); _rf24.setCRCLength(RF24_CRC_16); _rf24.enableDynamicPayloads(); + Serial.println("Radio5"); // All nodes listen to broadcast pipe (for FIND_PARENT_RESPONSE messages) _rf24.openReadingPipe(BROADCAST_PIPE, TO_ADDR(BROADCAST_ADDRESS)); + Serial.println("Radio6"); #if defined(MY_RF24_ENABLE_ENCRYPTION) diff --git a/libraries/MySensors/examples/Esp8266Gateway/Esp8266Gateway.ino b/libraries/MySensors/examples/Esp8266Gateway/Esp8266Gateway.ino index d7b5510..913a873 100644 --- a/libraries/MySensors/examples/Esp8266Gateway/Esp8266Gateway.ino +++ b/libraries/MySensors/examples/Esp8266Gateway/Esp8266Gateway.ino @@ -65,11 +65,16 @@ * Make sure to fill in your ssid and WiFi password below for ssid & pass. */ +#include +#include #include // Enable debug prints to serial monitor #define MY_DEBUG +// Use a bit lower baudrate here than default +#define MY_BAUD_RATE 9600 + // Enable and select radio type (if attached) #define MY_RADIO_NRF24 //#define MY_RADIO_RFM69 @@ -80,6 +85,8 @@ #define MY_ESP8266_SSID "MySSID" #define MY_ESP8266_PASSWORD "MyVerySecretPassword" + + // Enable MY_IP_ADDRESS here if you want a static ip address (no DHCP) // #define MY_IP_ADDRESS 192,168,178,66 // If using static ip you need to define Gateway and Subnet address as well @@ -90,11 +97,12 @@ #define MY_PORT 5003 // How many clients should be able to connect to this gateway (default 1) -#define MY_GATEWAY_MAX_CLIENTS 5 +#define MY_GATEWAY_MAX_CLIENTS 2 // Controller ip address (enables client mode). Undefine this to act as sever. -//#define MY_CONTROLLER_IP_ADDRESS 192, 168, 178, 254 - + +// #define MY_CONTROLLER_IP_ADDRESS 192, 168, 178, 254 + /* // Flash leds on rx/tx/err #define MY_LEDS_BLINKING_FEATURE // Set blinking period @@ -112,12 +120,11 @@ #define MY_DEFAULT_ERR_LED_PIN 16 // Error led pin #define MY_DEFAULT_RX_LED_PIN 16 // Receive led pin #define MY_DEFAULT_TX_LED_PIN 16 // the PCB, on board LED - +*/ #include void setup() { - } void presentation() { @@ -126,7 +133,8 @@ void presentation() { void loop() { - + wait(500); + Serial.println("Hello"); }