ESPGateway work ongoing

This commit is contained in:
Henrik Ekblad
2015-10-01 20:38:41 +02:00
parent adc3f664fc
commit ca8ebdb3a4
7 changed files with 111 additions and 44 deletions
+32 -7
View File
@@ -133,15 +133,27 @@
#endif #endif
// The RX LED default pin // The RX LED default pin
#ifndef MY_DEFAULT_RX_LED_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 #endif
// The TX LED default pin // The TX LED default pin
#ifndef MY_DEFAULT_TX_LED_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 #endif
// The Error LED default pin // The Error LED default pin
#ifndef MY_DEFAULT_ERR_LED_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 #endif
/********************************************** /**********************************************
@@ -160,7 +172,11 @@
// The default input pin used for the inclusion mode button // The default input pin used for the inclusion mode button
#ifndef MY_INCLUSION_MODE_BUTTON_PIN #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 #endif
// Number of seconds (default one minute) inclusion mode should be enabled // Number of seconds (default one minute) inclusion mode should be enabled
#ifndef MY_INCLUSION_MODE_DURATION #ifndef MY_INCLUSION_MODE_DURATION
@@ -220,17 +236,26 @@
// Enables RF24 encryption (all nodes and gateway must have this enabled) // Enables RF24 encryption (all nodes and gateway must have this enabled)
//#define MY_RF24_ENABLE_ENCRYPTION //#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 #ifndef MY_RF24_ENCRYPTKEY
#define MY_RF24_ENCRYPTKEY 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x10,0x11,0x12,0x13,0x14,0x15,0x16 #define MY_RF24_ENCRYPTKEY 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x10,0x11,0x12,0x13,0x14,0x15,0x16
#endif #endif
// Default pin settings. Override in sketch if needed. // Default pin settings. Override in sketch if needed.
#ifndef MY_RF24_CE_PIN #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 #endif
#ifndef MY_RF24_CS_PIN #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 #endif
#ifndef MY_RF24_PA_LEVEL #ifndef MY_RF24_PA_LEVEL
+11 -22
View File
@@ -44,10 +44,11 @@
// HARDWARE // HARDWARE
#if defined(ARDUINO_ARCH_ESP8266) #if defined(ARDUINO_ARCH_ESP8266)
// Remove PSTR macros from debug prints
#undef PSTR #undef PSTR
#define PSTR(x) (x) #define PSTR(x) (x)
#include "drivers/ESP8266/EEPROM/EEPROM.cpp" /*#include "drivers/ESP8266/EEPROM/EEPROM.cpp"
#include "drivers/ESP8266/SPI/SPI.cpp" #include "drivers/ESP8266/SPI/SPI.cpp"*/
#include "core/MyHwESP8266.cpp" #include "core/MyHwESP8266.cpp"
// For ESP8266, we always enable gateway feature // For ESP8266, we always enable gateway feature
#define MY_GATEWAY_ESP8266 #define MY_GATEWAY_ESP8266
@@ -230,33 +231,21 @@
#include "core/MySensorCore.cpp" #include "core/MySensorCore.cpp"
extern void setup(); extern void setup();
//extern void loop();
// Optional sketch functions called by MySensors library // Optional sketch functions called by MySensors library
void receive(const MyMessage &message) __attribute__((weak)); void receive(const MyMessage &message) __attribute__((weak));
void receiveTime(unsigned long) __attribute__((weak)); void receiveTime(unsigned long) __attribute__((weak));
void presentation() __attribute__((weak)); void presentation() __attribute__((weak));
extern "C" void setup() __attribute__((weak)); extern "C" void setup() __attribute__((weak));
extern "C" void loop() __attribute__((weak)); extern "C" void loop() __attribute__((weak));
extern "C" void loop2() __attribute__((weak));
#include <Arduino.h> #include <Arduino.h>
// 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(;;) { #if defined(MY_GATEWAY_ESP8266)
_process(); // Process incoming data #include "core/MyMainESP8266.cpp"
if (loop) loop(); // Call sketch loop #else
#if !defined(ARDUINO_ARCH_ESP8266) #include "core/MyMainDefault.cpp"
if (serialEventRun) serialEventRun(); #endif
#endif
}
return 0;
}
#endif #endif
@@ -86,6 +86,7 @@ bool gatewayTransportInit() {
{ {
delay(500); delay(500);
Serial.print("."); Serial.print(".");
hwWatchdogReset();
} }
Serial.println(PSTR("Connected to AP!")); Serial.println(PSTR("Connected to AP!"));
Serial.print(PSTR("IP: ")); Serial.print(PSTR("IP: "));
@@ -168,17 +169,47 @@ bool gatewayTransportAvailable()
return protocolParse(_ethernetMsg, inputString[0]); return protocolParse(_ethernetMsg, inputString[0]);
} }
#else #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++) { for (uint8_t i = 0; i < ARRAY_SIZE(clients); i++) {
while (clients[i].connected() && clients[i].available()) { while (clients[i].connected() && clients[i].available()) {
char inChar = clients[i].read(); char inChar = clients[i].read();
Serial.print(inChar);
if (inputString[i].idx < MY_GATEWAY_MAX_RECEIVE_LENGTH - 1) { if (inputString[i].idx < MY_GATEWAY_MAX_RECEIVE_LENGTH - 1) {
// if newline then command is complete // if newline then command is complete
if (inChar == '\n') { if (inChar == '\n') {
// a command was issued by the client // a command was issued by the client
// we will now try to send it to the actuator // we will now try to send it to the actuator
inputString[i].string[inputString[i].idx] = 0;
// echo the string to the serial port // echo the string to the serial port
Serial.print(PSTR("Client ")); Serial.print(PSTR("Client "));
Serial.print(i); Serial.print(i);
@@ -188,7 +219,11 @@ bool gatewayTransportAvailable()
// clear the string: // clear the string:
inputString[i].idx = 0; 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 { } else {
// add it to the inputString: // add it to the inputString:
inputString[i].string[inputString[i].idx++] = inChar; inputString[i].string[inputString[i].idx++] = inChar;
+8 -3
View File
@@ -64,6 +64,7 @@ void _begin() {
hwInit(); hwInit();
#endif #endif
debug(PSTR("Starting...\n"));
#if defined(MY_RADIO_FEATURE) #if defined(MY_RADIO_FEATURE)
_failedTransmissions = 0; _failedTransmissions = 0;
@@ -74,7 +75,6 @@ void _begin() {
} }
#endif #endif
#if defined(MY_GATEWAY_FEATURE) #if defined(MY_GATEWAY_FEATURE)
#if defined(MY_INCLUSION_BUTTON_FEATURE) #if defined(MY_INCLUSION_BUTTON_FEATURE)
inclusionInit(); inclusionInit();
@@ -147,7 +147,8 @@ void _begin() {
#if defined(MY_RADIO_FEATURE) #if defined(MY_RADIO_FEATURE)
transportPresentNode(); transportPresentNode();
#endif #endif
if (presentation)
presentation();
debug(PSTR("%s started, id=%d, parent=%d, distance=%d\n"), MY_NODE_TYPE, _nc.nodeId, _nc.parentNodeId, _nc.distance); 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); hwWriteConfig(EEPROM_CONTROLLER_CONFIG_ADDRESS, isMetric);
} else if (type == I_PRESENTATION) { } else if (type == I_PRESENTATION) {
// Re-send node presentation to controller // Re-send node presentation to controller
transportPresentNode(); #if defined(MY_RADIO_FEATURE)
transportPresentNode();
#endif
if (presentation)
presentation();
} else if (type == I_HEARTBEAT) { } else if (type == I_HEARTBEAT) {
if (!mGetAck(_msg)) { if (!mGetAck(_msg)) {
// Send heartbeat ack message back to sender (with the same payload) // Send heartbeat ack message back to sender (with the same payload)
-2
View File
@@ -540,8 +540,6 @@ void transportPresentNode() {
#endif #endif
} }
#endif #endif
if (presentation)
presentation();
} }
void transportFindParentNode() { void transportFindParentNode() {
@@ -44,11 +44,16 @@ uint8_t _paLevel;
bool transportInit() { bool transportInit() {
// Start up the radio library // Start up the radio library
Serial.println("Radio1");
_rf24.begin(); _rf24.begin();
Serial.println("Radio2");
if (!_rf24.isPVariant()) { if (!_rf24.isPVariant()) {
Serial.println("Radio3");
return false; return false;
} }
Serial.println("Radio4");
_rf24.setAutoAck(1); _rf24.setAutoAck(1);
_rf24.setAutoAck(BROADCAST_PIPE,false); // Turn off auto ack for broadcast _rf24.setAutoAck(BROADCAST_PIPE,false); // Turn off auto ack for broadcast
_rf24.enableAckPayload(); _rf24.enableAckPayload();
@@ -58,9 +63,11 @@ bool transportInit() {
_rf24.setRetries(5,15); _rf24.setRetries(5,15);
_rf24.setCRCLength(RF24_CRC_16); _rf24.setCRCLength(RF24_CRC_16);
_rf24.enableDynamicPayloads(); _rf24.enableDynamicPayloads();
Serial.println("Radio5");
// All nodes listen to broadcast pipe (for FIND_PARENT_RESPONSE messages) // All nodes listen to broadcast pipe (for FIND_PARENT_RESPONSE messages)
_rf24.openReadingPipe(BROADCAST_PIPE, TO_ADDR(BROADCAST_ADDRESS)); _rf24.openReadingPipe(BROADCAST_PIPE, TO_ADDR(BROADCAST_ADDRESS));
Serial.println("Radio6");
#if defined(MY_RF24_ENABLE_ENCRYPTION) #if defined(MY_RF24_ENABLE_ENCRYPTION)
@@ -65,11 +65,16 @@
* Make sure to fill in your ssid and WiFi password below for ssid & pass. * Make sure to fill in your ssid and WiFi password below for ssid & pass.
*/ */
#include <EEPROM.h>
#include <SPI.h>
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
// Enable debug prints to serial monitor // Enable debug prints to serial monitor
#define MY_DEBUG #define MY_DEBUG
// Use a bit lower baudrate here than default
#define MY_BAUD_RATE 9600
// Enable and select radio type (if attached) // Enable and select radio type (if attached)
#define MY_RADIO_NRF24 #define MY_RADIO_NRF24
//#define MY_RADIO_RFM69 //#define MY_RADIO_RFM69
@@ -80,6 +85,8 @@
#define MY_ESP8266_SSID "MySSID" #define MY_ESP8266_SSID "MySSID"
#define MY_ESP8266_PASSWORD "MyVerySecretPassword" #define MY_ESP8266_PASSWORD "MyVerySecretPassword"
// Enable MY_IP_ADDRESS here if you want a static ip address (no DHCP) // Enable MY_IP_ADDRESS here if you want a static ip address (no DHCP)
// #define MY_IP_ADDRESS 192,168,178,66 // #define MY_IP_ADDRESS 192,168,178,66
// If using static ip you need to define Gateway and Subnet address as well // If using static ip you need to define Gateway and Subnet address as well
@@ -90,11 +97,12 @@
#define MY_PORT 5003 #define MY_PORT 5003
// How many clients should be able to connect to this gateway (default 1) // 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. // 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 // Flash leds on rx/tx/err
#define MY_LEDS_BLINKING_FEATURE #define MY_LEDS_BLINKING_FEATURE
// Set blinking period // Set blinking period
@@ -112,12 +120,11 @@
#define MY_DEFAULT_ERR_LED_PIN 16 // Error led pin #define MY_DEFAULT_ERR_LED_PIN 16 // Error led pin
#define MY_DEFAULT_RX_LED_PIN 16 // Receive led pin #define MY_DEFAULT_RX_LED_PIN 16 // Receive led pin
#define MY_DEFAULT_TX_LED_PIN 16 // the PCB, on board LED #define MY_DEFAULT_TX_LED_PIN 16 // the PCB, on board LED
*/
#include <MySensor.h> #include <MySensor.h>
void setup() { void setup() {
} }
void presentation() { void presentation() {
@@ -126,7 +133,8 @@ void presentation() {
void loop() { void loop() {
wait(500);
Serial.println("Hello");
} }