ESPGateway verified. DHCP/Static ip. TCP/UDP communication.

This commit is contained in:
Henrik Ekblad
2015-10-02 20:10:25 +02:00
parent 5a71855389
commit 0f57bce1e5
8 changed files with 57 additions and 37 deletions
+1 -4
View File
@@ -19,10 +19,6 @@
#ifndef MySensor_h
#define MySensor_h
/* { dg-warning "import is a deprecated GCC extension" "deprecated" {target *-*-* } 0 } */
#include "MyConfig.h"
#include "core/MySensorCore.h"
@@ -239,6 +235,7 @@ extern "C" void setup() __attribute__((weak));
extern "C" void loop() __attribute__((weak));
extern "C" void loop2() __attribute__((weak));
#include <Arduino.h>
#if defined(MY_GATEWAY_ESP8266)
@@ -44,6 +44,7 @@ typedef struct
// Some re-defines to make code more readable below
#define EthernetServer WiFiServer
#define EthernetClient WiFiClient
#define EthernetUDP WiFiUDP
#if defined(MY_IP_ADDRESS)
IPAddress gateway(MY_IP_GATEWAY_ADDRESS);
@@ -55,11 +56,7 @@ typedef struct
#if defined(MY_USE_UDP)
#if defined(MY_GATEWAY_ESP8266)
WiFiUDP _ethernetServer;
#else
EthernetUDP _ethernetServer;
#endif
EthernetUDP _ethernetServer;
#else
EthernetServer _ethernetServer(_ethernetGatewayPort);
uint8_t _ethernetInputPos;
@@ -165,8 +162,8 @@ bool gatewayTransportAvailable()
int packet_size = _ethernetServer.parsePacket();
if (_ethernetServer.available()) {
_ethernetServer.read(inputString[0], MY_GATEWAY_MAX_RECEIVE_LENGTH);
return protocolParse(_ethernetMsg, inputString[0]);
_ethernetServer.read(inputString[0].string, MY_GATEWAY_MAX_RECEIVE_LENGTH);
return protocolParse(_ethernetMsg, inputString[0].string);
}
#else
// Go over list of clients and stop any that are no longer connected.
+18 -4
View File
@@ -70,8 +70,13 @@ void _begin() {
// Setup radio
if (!transportInit()) {
debug(PSTR("radio init fail\n"));
while(1); // Nothing more we can do
debug(PSTR("Radio init fail\n"));
// Nothing more we can do
while(1) {
#if defined(MY_GATEWAY_ESP8266)
yield();
#endif
};
}
#endif
@@ -82,8 +87,13 @@ void _begin() {
// initialize the transport driver
if (!gatewayTransportInit()) {
debug(PSTR("transport driver init fail\n"));
while(1); // Nothing more we can do
debug(PSTR("Transport driver init fail\n"));
// Nothing more we can do
while(1) {
#if defined(MY_GATEWAY_ESP8266)
yield();
#endif
}
}
#endif
@@ -278,10 +288,14 @@ uint8_t loadState(uint8_t pos) {
return hwReadConfig(EEPROM_LOCAL_CONFIG_ADDRESS+pos);
}
void wait(unsigned long ms) {
unsigned long enter = hwMillis();
while (hwMillis() - enter < ms) {
_process();
#if defined(ARDUINO_ARCH_ESP8266)
yield();
#endif
}
}
+7 -1
View File
@@ -339,10 +339,12 @@ bool transportIsValidFirmware() {
boolean transportSendWrite(uint8_t to, MyMessage &message) {
mSetVersion(message, PROTOCOL_VERSION);
uint8_t length = mGetSigned(message) ? MAX_MESSAGE_LENGTH : mGetLength(message);
message.last = _nc.nodeId;
ledBlinkTx(1);
bool ok = transportSend(to, &message, min(MAX_MESSAGE_LENGTH, HEADER_SIZE + length));
debug(PSTR("send: %d-%d-%d-%d s=%d,c=%d,t=%d,pt=%d,l=%d,sg=%d,st=%s:%s\n"),
@@ -359,7 +361,6 @@ boolean transportSendRoute(MyMessage &message) {
uint8_t last = message.last;
bool ok;
// If we still don't have any parent id, re-request and skip this message.
if (_nc.parentNodeId == AUTO) {
transportFindParentNode();
@@ -417,6 +418,7 @@ boolean transportSendRoute(MyMessage &message) {
#endif
#if !defined(MY_REPEATER_FEATURE)
// None repeating node... We can only send to our parent
ok = transportSendWrite(_nc.parentNodeId, message);
#else
@@ -467,11 +469,14 @@ boolean transportSendRoute(MyMessage &message) {
// This message should be routed back towards sensor net gateway
ok = transportSendWrite(_nc.parentNodeId, message);
// Add this child to our "routing table" if it not already exist
hwWriteConfig(EEPROM_ROUTES_ADDRESS+sender, last);
#endif
}
#endif
if (!ok) {
// Failure when sending to parent node. The parent node might be down and we
// need to find another route to gateway.
@@ -483,6 +488,7 @@ boolean transportSendRoute(MyMessage &message) {
} else {
_failedTransmissions = 0;
}
return ok;
}
@@ -44,36 +44,33 @@ 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();
_rf24.setChannel(MY_RF24_CHANNEL);
_rf24.setPALevel(_paLevel);
_rf24.setDataRate(MY_RF24_DATARATE);
if (!_rf24.setDataRate(MY_RF24_DATARATE)) {
return false;
}
_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)
_aes.set_key(_psk, 16); //set up AES-key
#endif
//_rf24.printDetails();
return true;
}
@@ -98,6 +95,7 @@ bool transportSend(uint8_t to, const void* data, uint8_t len) {
#endif
// Make sure radio has powered up
_rf24.powerUp();
_rf24.stopListening();
_rf24.openWritingPipe(TO_ADDR(to));
@@ -113,6 +111,7 @@ bool transportSend(uint8_t to, const void* data, uint8_t len) {
bool transportAvailable(uint8_t *to) {
uint8_t pipe = 255;
boolean avail = _rf24.available(&pipe);
(void)avail; //until somebody makes use of 'avail'
if (pipe == CURRENT_NODE_PIPE)
*to = _address;
@@ -645,6 +645,9 @@ bool RF24::write( const void* buf, uint8_t len, const bool multicast )
return 0;
}
#endif
#if defined(ARDUINO_ARCH_ESP8266)
yield();
#endif
}
ce(LOW);
@@ -23,7 +23,7 @@
/*** USER DEFINES: ***/
//#define FAILURE_HANDLING
//#define SERIAL_DEBUG
//#define SERIAL_DEBUG
#define MINIMAL
//#define SPI_UART // Requires library from https://github.com/TMRh20/Sketches/tree/master/SPI_UART
//#define MY_SOFTSPI // Requires library from https://github.com/greiman/DigitalIO
@@ -38,8 +38,7 @@
* - TX (yellow) - blink fast on radio message transmitted. In inclusion mode will blink slowly
* - ERR (red) - fast blink on error during transmission error or recieve crc error
*
* See http://www.mysensors.org/build/ethernet_gateway for wiring instructions.
* The ESP8266 however requires different wiring:
* See http://www.mysensors.org/build/esp8266_gateway for wiring instructions.
* nRF24L01+ ESP8266
* VCC VCC
* CE GPIO4
@@ -67,7 +66,6 @@
#include <EEPROM.h>
#include <SPI.h>
#include <ESP8266WiFi.h>
// Enable debug prints to serial monitor
#define MY_DEBUG
@@ -79,18 +77,20 @@
#define MY_RADIO_NRF24
//#define MY_RADIO_RFM69
// Gateway mode always enabled for ESP8266
// Gateway mode always enabled for ESP8266. But we add this anyway ;)
#define MY_GATEWAY_ESP8266
#define MY_ESP8266_SSID "MySSID"
#define MY_ESP8266_PASSWORD "MyVerySecretPassword"
// Enable communicate using UDP
//#define MY_USE_UDP
// 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,98,87
// If using static ip you need to define Gateway and Subnet address as well
#define MY_IP_GATEWAY_ADDRESS 192,168,0,1
#define MY_IP_GATEWAY_ADDRESS 192,168,98,1
#define MY_IP_SUBNET_ADDRESS 255,255,255,0
// The port to keep open on node server mode
@@ -99,9 +99,8 @@
// How many clients should be able to connect to this gateway (default 1)
#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
// Controller ip address. Enables client mode. Enable this if MY_USE_UDP and you want sensor data sent somewhere.
//#define MY_CONTROLLER_IP_ADDRESS 192, 168, 178, 68
/*
// Flash leds on rx/tx/err
#define MY_LEDS_BLINKING_FEATURE
@@ -122,6 +121,12 @@
#define MY_DEFAULT_TX_LED_PIN 16 // the PCB, on board LED
*/
#if defined(MY_USE_UDP)
#include <WiFiUDP.h>
#else
#include <ESP8266WiFi.h>
#endif
#include <MySensor.h>
void setup() {
@@ -133,8 +138,7 @@ void presentation() {
void loop() {
wait(500);
Serial.println("Hello");
// Send locally attech sensors data here
}