Skip to content

Commit

Permalink
Fix UDP transport and update GW examples (#1262)
Browse files Browse the repository at this point in the history
  • Loading branch information
tekka007 authored Jan 27, 2019
1 parent 71b0698 commit d3b3a1c
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 48 deletions.
25 changes: 12 additions & 13 deletions core/MyGatewayTransportEthernet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,22 @@ extern MyMessage _msgTmp;
#endif

#if defined(MY_CONTROLLER_IP_ADDRESS)
IPAddress _ethernetControllerIP(MY_CONTROLLER_IP_ADDRESS);
#define _ethernetControllerIP IPAddress(MY_CONTROLLER_IP_ADDRESS)
#endif

#if defined(MY_IP_ADDRESS)
IPAddress _ethernetGatewayIP(MY_IP_ADDRESS);
#define _ethernetGatewayIP IPAddress(MY_IP_ADDRESS)
#if defined(MY_IP_GATEWAY_ADDRESS)
IPAddress _gatewayIp(MY_IP_GATEWAY_ADDRESS);
#define _gatewayIp IPAddress(MY_IP_GATEWAY_ADDRESS)
#elif defined(MY_GATEWAY_ESP8266) || defined(MY_GATEWAY_ESP32)
// Assume the gateway will be the machine on the same network as the local IP
// but with last octet being '1'
IPAddress _gatewayIp(_ethernetGatewayIP[0], _ethernetGatewayIP[1], _ethernetGatewayIP[2], 1);
#define _gatewayIp IPAddress(_ethernetGatewayIP[0], _ethernetGatewayIP[1], _ethernetGatewayIP[2], 1)
#endif /* End of MY_IP_GATEWAY_ADDRESS */
#if defined(MY_IP_SUBNET_ADDRESS)
IPAddress _subnetIp(MY_IP_SUBNET_ADDRESS);
#define _subnetIp IPAddress(MY_IP_SUBNET_ADDRESS)
#elif defined(MY_GATEWAY_ESP8266) || defined(MY_GATEWAY_ESP32)
IPAddress _subnetIp(255, 255, 255, 0);
#define _subnetIp IPAddress(255, 255, 255, 0)
#endif /* End of MY_IP_SUBNET_ADDRESS */
#endif /* End of MY_IP_ADDRESS */

Expand Down Expand Up @@ -162,7 +162,7 @@ void gatewayTransportRenewIP(void)
return;
}
_w5100_spi_en(false);
next_time = now + MY_IP_RENEWAL_INTERVAL_MS;
_nextIPRenewal = now + MY_IP_RENEWAL_INTERVAL_MS;
}
#endif

Expand Down Expand Up @@ -260,7 +260,7 @@ bool gatewayTransportSend(MyMessage &message)
#else
_ethernetServer.beginPacket(_ethernetControllerIP, MY_PORT);
#endif /* End of MY_CONTROLLER_URL_ADDRESS */
_ethernetServer.write(_ethernetMsg, strlen(_ethernetMsg));
_ethernetServer.write((uint8_t *)_ethernetMsg, strlen(_ethernetMsg));
// returns 1 if the packet was sent successfully
nbytes = _ethernetServer.endPacket();
#else /* Else part of MY_USE_UDP */
Expand Down Expand Up @@ -290,12 +290,12 @@ bool gatewayTransportSend(MyMessage &message)
#if defined(MY_GATEWAY_ESP8266) || defined(MY_GATEWAY_ESP32)
for (uint8_t i = 0; i < ARRAY_SIZE(clients); i++) {
if (clients[i] && clients[i].connected()) {
nbytes += clients[i].write((uint8_t*)_ethernetMsg, strlen(_ethernetMsg));
nbytes += clients[i].write((uint8_t *)_ethernetMsg, strlen(_ethernetMsg));
}
}
#else /* Else part of MY_GATEWAY_ESP8266 */
#else /* Else part of MY_GATEWAY_ESPxx*/
nbytes = _ethernetServer.write(_ethernetMsg);
#endif /* End of MY_GATEWAY_ESP8266 */
#endif /* End of MY_GATEWAY_ESPxx */
#endif /* End of MY_GATEWAY_CLIENT_MODE */
_w5100_spi_en(false);
return (nbytes > 0);
Expand Down Expand Up @@ -380,7 +380,6 @@ bool gatewayTransportAvailable(void)
int packet_size = _ethernetServer.parsePacket();

if (packet_size) {
//GATEWAY_DEBUG(PSTR("UDP packet available. Size:%" PRIu8 "\n"), packet_size);
_ethernetServer.read(inputString.string, MY_GATEWAY_MAX_RECEIVE_LENGTH);
inputString.string[packet_size] = 0;
GATEWAY_DEBUG(PSTR("GWT:TSA:UDP MSG=%s\n"), inputString.string);
Expand Down Expand Up @@ -418,7 +417,7 @@ bool gatewayTransportAvailable(void)
#endif /* End of MY_USE_UDP */
#else /* Else part of MY_GATEWAY_CLIENT_MODE */
#if defined(MY_GATEWAY_ESP8266) || defined(MY_GATEWAY_ESP32) || defined(MY_GATEWAY_LINUX)
// ESP8266: Go over list of clients and stop any that are no longer connected.
// ESP8266/ESP32: 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++) {
Expand Down
12 changes: 6 additions & 6 deletions core/MyGatewayTransportMQTTClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,23 +62,23 @@
#endif

#if defined MY_CONTROLLER_IP_ADDRESS
IPAddress _brokerIp(MY_CONTROLLER_IP_ADDRESS);
#define _brokerIp IPAddress(MY_CONTROLLER_IP_ADDRESS)
#endif

#if defined(MY_IP_ADDRESS)
IPAddress _MQTT_clientIp(MY_IP_ADDRESS);
#define _MQTT_clientIp IPAddress(MY_IP_ADDRESS)
#if defined(MY_IP_GATEWAY_ADDRESS)
IPAddress _gatewayIp(MY_IP_GATEWAY_ADDRESS);
#define _gatewayIp IPAddress(MY_IP_GATEWAY_ADDRESS)
#elif defined(MY_GATEWAY_ESP8266) || defined(MY_GATEWAY_ESP32)
// Assume the gateway will be the machine on the same network as the local IP
// but with last octet being '1'
IPAddress _gatewayIp(_MQTT_clientIp[0], _MQTT_clientIp[1], _MQTT_clientIp[2], 1);
#define _gatewayIp IPAddress(_MQTT_clientIp[0], _MQTT_clientIp[1], _MQTT_clientIp[2], 1)
#endif /* End of MY_IP_GATEWAY_ADDRESS */

#if defined(MY_IP_SUBNET_ADDRESS)
IPAddress _subnetIp(MY_IP_SUBNET_ADDRESS);
#define _subnetIp IPAddress(MY_IP_SUBNET_ADDRESS)
#elif defined(MY_GATEWAY_ESP8266) || defined(MY_GATEWAY_ESP32)
IPAddress _subnetIp(255, 255, 255, 0);
#define _subnetIp IPAddress(255, 255, 255, 0)
#endif /* End of MY_IP_SUBNET_ADDRESS */
#endif /* End of MY_IP_ADDRESS */

Expand Down
10 changes: 9 additions & 1 deletion examples/GatewayESP32/GatewayESP32.ino
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@
#define MY_WIFI_SSID "MySSID"
#define MY_WIFI_PASSWORD "MyVerySecretPassword"

// Enable UDP communication
//#define MY_USE_UDP // If using UDP you need to set MY_CONTROLLER_IP_ADDRESS or MY_CONTROLLER_URL_ADDRESS below

// Set the hostname for the WiFi Client. This is the hostname
// it will pass to the DHCP server if not static.
// passed to the DHCP server if not static.
#define MY_HOSTNAME "ESP32_GW"

// Enable MY_IP_ADDRESS here if you want a static ip address (no DHCP)
Expand All @@ -58,6 +61,11 @@
// 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 (default is "server" mode).
// Also enable this if MY_USE_UDP is used and you want sensor data sent somewhere.
//#define MY_CONTROLLER_IP_ADDRESS 192, 168, 178, 68
//#define MY_CONTROLLER_URL_ADDRESS "my.controller.org"

#include <MySensors.h>

void setup()
Expand Down
2 changes: 1 addition & 1 deletion examples/GatewayESP32MQTTClient/GatewayESP32MQTTClient.ino
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
#define MY_WIFI_PASSWORD "MyVerySecretPassword"

// Set the hostname for the WiFi Client. This is the hostname
// it will pass to the DHCP server if not static.
// passed to the DHCP server if not static.
#define MY_HOSTNAME "ESP32_MQTT_GW"

// Enable MY_IP_ADDRESS here if you want a static ip address (no DHCP)
Expand Down
4 changes: 1 addition & 3 deletions examples/GatewayESP32OTA/GatewayESP32OTA.ino
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
#define MY_WIFI_PASSWORD "MyVerySecretPassword"

// Set the hostname for the WiFi Client. This is the hostname
// it will pass to the DHCP server if not static.
// passed to the DHCP server if not static.
#define MY_HOSTNAME "ESP32_GW"

// Enable MY_IP_ADDRESS here if you want a static ip address (no DHCP)
Expand Down Expand Up @@ -71,8 +71,6 @@

//#define MY_WITH_LEDS_BLINKING_INVERSE // At the time of Error, Receive, Transmit the pin is at a high level



#include <ArduinoOTA.h>
#include <MySensors.h>

Expand Down
12 changes: 4 additions & 8 deletions examples/GatewayESP8266/GatewayESP8266.ino
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*******************************
*
* REVISION HISTORY
* Version 1.0 - Henrik EKblad
* Version 1.0 - Henrik Ekblad
* Contribution by a-lurker and Anticimex,
* Contribution by Norbert Truchsess <norbert.truchsess@t-online.de>
* Contribution by Ivo Pullens (ESP8266 support)
Expand Down Expand Up @@ -68,11 +68,11 @@
#define MY_WIFI_PASSWORD "MyVerySecretPassword"

// Enable UDP communication
//#define MY_USE_UDP // If using UDP you need to set MY_CONTROLLER_IP_ADDRESS below
//#define MY_USE_UDP // If using UDP you need to set MY_CONTROLLER_IP_ADDRESS or MY_CONTROLLER_URL_ADDRESS below

// Set the hostname for the WiFi Client. This is the hostname
// it will pass to the DHCP server if not static.
//#define MY_HOSTNAME "sensor-gateway"
#define MY_HOSTNAME "ESP8266_GW"

// Enable MY_IP_ADDRESS here if you want a static ip address (no DHCP)
//#define MY_IP_ADDRESS 192,168,178,87
Expand All @@ -90,6 +90,7 @@
// Controller ip address. Enables client mode (default is "server" mode).
// Also enable this if MY_USE_UDP is used and you want sensor data sent somewhere.
//#define MY_CONTROLLER_IP_ADDRESS 192, 168, 178, 68
//#define MY_CONTROLLER_URL_ADDRESS "my.controller.org"

// Enable inclusion mode
//#define MY_INCLUSION_MODE_FEATURE
Expand All @@ -110,11 +111,6 @@
//#define MY_DEFAULT_RX_LED_PIN 16 // Receive led pin
//#define MY_DEFAULT_TX_LED_PIN 16 // the PCB, on board LED

#if defined(MY_USE_UDP)
#include <WiFiUdp.h>
#endif

#include <ESP8266WiFi.h>
#include <MySensors.h>

void setup()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@
#define MY_WIFI_PASSWORD "MyVerySecretPassword"

// Set the hostname for the WiFi Client. This is the hostname
// it will pass to the DHCP server if not static.
// #define MY_HOSTNAME "mqtt-sensor-gateway"
// passed to the DHCP server if not static.
#define MY_HOSTNAME "ESP8266_MQTT_GW"

// Enable MY_IP_ADDRESS here if you want a static ip address (no DHCP)
//#define MY_IP_ADDRESS 192,168,178,87
Expand Down Expand Up @@ -110,7 +110,6 @@
//#define MY_DEFAULT_RX_LED_PIN 16 // Receive led pin
//#define MY_DEFAULT_TX_LED_PIN 16 // the PCB, on board LED

#include <ESP8266WiFi.h>
#include <MySensors.h>

void setup()
Expand Down
14 changes: 4 additions & 10 deletions examples/GatewayESP8266OTA/GatewayESP8266OTA.ino
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*******************************
*
* REVISION HISTORY
* Version 1.0 - Henrik EKblad
* Version 1.0 - Henrik Ekblad
* Contribution by tekka,
* Contribution by a-lurker and Anticimex,
* Contribution by Norbert Truchsess <norbert.truchsess@t-online.de>
Expand Down Expand Up @@ -69,8 +69,8 @@
#define MY_WIFI_PASSWORD "MyVerySecretPassword"

// Set the hostname for the WiFi Client. This is the hostname
// it will pass to the DHCP server if not static.
// #define MY_HOSTNAME "sensor-ota-gateway"
// passed to the DHCP server if not static.
#define MY_HOSTNAME "ESP8266_GW"

// Enable UDP communication
//#define MY_USE_UDP // If using UDP you need to set MY_CONTROLLER_IP_ADDRESS below
Expand All @@ -96,7 +96,7 @@
#define MY_INCLUSION_MODE_FEATURE

// Enable Inclusion mode button on gateway
// #define MY_INCLUSION_BUTTON_FEATURE
//#define MY_INCLUSION_BUTTON_FEATURE
// Set inclusion mode duration (in seconds)
#define MY_INCLUSION_MODE_DURATION 60
// Digital pin used for inclusion mode button
Expand All @@ -111,12 +111,6 @@
#define MY_DEFAULT_RX_LED_PIN 16 // Receive led pin
#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 <ArduinoOTA.h>
#include <MySensors.h>

Expand Down
8 changes: 5 additions & 3 deletions examples/GatewayW5100/GatewayW5100.ino
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
*******************************
*
* REVISION HISTORY
* Version 1.0 - Henrik EKblad
* Contribution by a-lurker and Anticimex,
* Version 1.0 - Henrik Ekblad
* Contribution by a-lurker and Anticimex
* Contribution by Norbert Truchsess <norbert.truchsess@t-online.de>
* Contribution by Tomas Hozza <thozza@gmail.com>
*
Expand Down Expand Up @@ -75,7 +75,7 @@
#endif

// Enable UDP communication
//#define MY_USE_UDP // If using UDP you need to set MY_CONTROLLER_IP_ADDRESS below
//#define MY_USE_UDP // If using UDP you need to set MY_CONTROLLER_IP_ADDRESS or MY_CONTROLLER_URL_ADDRESS below

// Enable MY_IP_ADDRESS here if you want a static ip address (no DHCP)
#define MY_IP_ADDRESS 192,168,178,66
Expand All @@ -93,12 +93,14 @@
// Controller ip address. Enables client mode (default is "server" mode).
// Also enable this if MY_USE_UDP is used and you want sensor data sent somewhere.
//#define MY_CONTROLLER_IP_ADDRESS 192, 168, 178, 254
//#define MY_CONTROLLER_URL_ADDRESS "my.controller.org"

// The MAC address can be anything you want but should be unique on your network.
// Newer boards have a MAC address printed on the underside of the PCB, which you can (optionally) use.
// Note that most of the Arduino examples use "DEAD BEEF FEED" for the MAC address.
#define MY_MAC_ADDRESS 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED


// Enable inclusion mode
#define MY_INCLUSION_MODE_FEATURE
// Enable Inclusion mode button on gateway
Expand Down
2 changes: 2 additions & 0 deletions hal/architecture/ESP8266/MyHwESP8266.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#define MyHwESP8266_h

#include <SPI.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>

#ifdef __cplusplus
#include <Arduino.h>
Expand Down

0 comments on commit d3b3a1c

Please sign in to comment.