Skip to content

Commit

Permalink
Consolidate WiFi defines (#1110)
Browse files Browse the repository at this point in the history
Great work @tekka007
  • Loading branch information
tekka007 authored and mfalkvidd committed May 1, 2018
1 parent e0988e6 commit bb77661
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 71 deletions.
30 changes: 29 additions & 1 deletion MyConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* network topology allowing messages to be routed to nodes.
*
* Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
* Copyright (C) 2013-2017 Sensnology AB
* Copyright (C) 2013-2018 Sensnology AB
* Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
*
* Documentation: http://www.mysensors.org
Expand Down Expand Up @@ -1372,6 +1372,30 @@
*/
//#define MY_DEBUG_VERBOSE_GATEWAY

/**
* @def MY_WIFI_SSID
* @brief SSID of your WiFi network
*/
//#define MY_WIFI_SSID "MySSID"

/**
* @def MY_WIFI_BSSID
* @brief BSSID of your WiFi network
*/
//#define MY_WIFI_BSSID "MyBSSID"

/**
* @def MY_WIFI_PASSWORD
* @brief Password of your WiFi network
*/
//#define MY_WIFI_PASSWORD "MyVerySecretPassword"

/**
* @def MY_HOSTNAME
* @brief Hostname of your device
*/
//#define MY_HOSTNAME "MyHostname"

/**
* @def MY_PORT
* @brief The Ethernet TCP/UDP port to open on controller or gateway.
Expand Down Expand Up @@ -2172,6 +2196,10 @@
#define MY_GATEWAY_ENC28J60
#define MY_GATEWAY_ESP8266
#define MY_GATEWAY_ESP32
#define MY_WIFI_SSID
#define MY_WIFI_BSSID
#define MY_WIFI_PASSWORD
#define MY_HOSTNAME
#define MY_GATEWAY_LINUX
#define MY_GATEWAY_TINYGSM
#define MY_GATEWAY_MQTT_CLIENT
Expand Down
71 changes: 41 additions & 30 deletions core/MyGatewayTransportEthernet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,35 @@
// global variables
extern MyMessage _msgTmp;

// housekeeping, remove for 3.0.0
#ifdef MY_ESP8266_SSID
#warning MY_ESP8266_SSID is deprecated, use MY_WIFI_SSID instead!
#define MY_WIFI_SSID MY_ESP8266_SSID
#undef MY_ESP8266_SSID // cleanup
#endif

#ifdef MY_ESP8266_PASSWORD
#warning MY_ESP8266_PASSWORD is deprecated, use MY_WIFI_PASSWORD instead!
#define MY_WIFI_PASSWORD MY_ESP8266_PASSWORD
#undef MY_ESP8266_PASSWORD // cleanup
#endif

#ifdef MY_ESP8266_BSSID
#warning MY_ESP8266_BSSID is deprecated, use MY_WIFI_BSSID instead!
#define MY_WIFI_BSSID MY_ESP8266_BSSID
#undef MY_ESP8266_BSSID // cleanup
#endif

#ifdef MY_ESP8266_HOSTNAME
#warning MY_ESP8266_HOSTNAME is deprecated, use MY_HOSTNAME instead!
#define MY_HOSTNAME MY_ESP8266_HOSTNAME
#undef MY_ESP8266_HOSTNAME // cleanup
#endif

#ifndef MY_WIFI_BSSID
#define MY_WIFI_BSSID NULL
#endif

#if defined(MY_CONTROLLER_IP_ADDRESS)
IPAddress _ethernetControllerIP(MY_CONTROLLER_IP_ADDRESS);
#endif
Expand Down Expand Up @@ -116,45 +145,27 @@ void _w5100_spi_en(bool enable)
bool gatewayTransportInit(void)
{
_w5100_spi_en(true);
#if defined(MY_GATEWAY_ESP8266)
#if defined(MY_ESP8266_SSID)

#if defined(MY_GATEWAY_ESP8266) || defined(MY_GATEWAY_ESP32)
#if defined(MY_WIFI_SSID)
// Turn off access point
WiFi.mode(WIFI_STA);
#if defined(MY_ESP8266_HOSTNAME)
WiFi.hostname(MY_ESP8266_HOSTNAME);
#endif /* End of MY_ESP8266_HOSTNAME */
#if defined(MY_IP_ADDRESS)
WiFi.config(_ethernetGatewayIP, _gatewayIp, _subnetIp);
#endif /* End of MY_IP_ADDRESS */
#ifndef MY_ESP8266_BSSID
#define MY_ESP8266_BSSID NULL
#endif
(void)WiFi.begin(MY_ESP8266_SSID, MY_ESP8266_PASSWORD, 0, MY_ESP8266_BSSID);
while (WiFi.status() != WL_CONNECTED) {
wait(500);
GATEWAY_DEBUG(PSTR("GWT:TIN:CONNECTING...\n"));
}
GATEWAY_DEBUG(PSTR("GWT:TIN:IP=%s\n"), WiFi.localIP().toString().c_str());
#endif /* End of MY_ESP8266_SSID */
#if defined(MY_HOSTNAME)
#if defined(MY_GATEWAY_ESP8266)
WiFi.hostname(MY_HOSTNAME)
#elif defined(MY_GATEWAY_ESP32)
#if defined(MY_ESP32_SSID)
// Turn off access point
WiFi.mode(WIFI_STA);
#if defined(MY_ESP32_HOSTNAME)
WiFi.setHostname(MY_ESP32_HOSTNAME);
WiFi.setHostname(MY_HOSTNAME)
#endif
#endif
#ifdef MY_IP_ADDRESS
WiFi.config(_ethernetGatewayIP, _gatewayIp, _subnetIp);
#endif
#ifndef MY_ESP32_BSSID
#define MY_ESP32_BSSID NULL
#endif
(void)WiFi.begin(MY_ESP32_SSID, MY_ESP32_PASSWORD, 0, MY_ESP32_BSSID);
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
(void)WiFi.begin(MY_WIFI_SSID, MY_WIFI_PASSWORD, 0, MY_WIFI_BSSID);
while (WiFi.status() != WL_CONNECTED) {
wait(500);
GATEWAY_DEBUG(PSTR("."));
GATEWAY_DEBUG(PSTR("GWT:TIN:CONNECTING...\n"));
}
GATEWAY_DEBUG(PSTR("IP: %s\n"), WiFi.localIP().toString().c_str());
GATEWAY_DEBUG(PSTR("GWT:TIN:IP: %s\n"), WiFi.localIP().toString().c_str());
#endif
#elif defined(MY_GATEWAY_LINUX)
// Nothing to do here
Expand Down
59 changes: 44 additions & 15 deletions core/MyGatewayTransportMQTTClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,35 @@

#include "MyGatewayTransport.h"

// housekeeping, remove for 3.0.0
#ifdef MY_ESP8266_SSID
#warning MY_ESP8266_SSID is deprecated, use MY_WIFI_SSID instead!
#define MY_WIFI_SSID MY_ESP8266_SSID
#undef MY_ESP8266_SSID // cleanup
#endif

#ifdef MY_ESP8266_PASSWORD
#warning MY_ESP8266_PASSWORD is deprecated, use MY_WIFI_PASSWORD instead!
#define MY_WIFI_PASSWORD MY_ESP8266_PASSWORD
#undef MY_ESP8266_PASSWORD // cleanup
#endif

#ifdef MY_ESP8266_BSSID
#warning MY_ESP8266_BSSID is deprecated, use MY_WIFI_BSSID instead!
#define MY_WIFI_BSSID MY_ESP8266_BSSID
#undef MY_ESP8266_BSSID // cleanup
#endif

#ifdef MY_ESP8266_HOSTNAME
#warning MY_ESP8266_HOSTNAME is deprecated, use MY_HOSTNAME instead!
#define MY_HOSTNAME MY_ESP8266_HOSTNAME
#undef MY_ESP8266_HOSTNAME // cleanup
#endif

#ifndef MY_WIFI_BSSID
#define MY_WIFI_BSSID NULL
#endif

#if defined MY_CONTROLLER_IP_ADDRESS
IPAddress _brokerIp(MY_CONTROLLER_IP_ADDRESS);
#endif
Expand Down Expand Up @@ -127,13 +156,13 @@ bool gatewayTransportConnect(void)
GATEWAY_DEBUG(PSTR("GWT:TPC:CONNECTING...\n"));
}
GATEWAY_DEBUG(PSTR("GWT:TPC:IP=%s\n"),WiFi.localIP().toString().c_str());
#elif defined(MY_GATEWAY_LINUX) /* Elif part of MY_GATEWAY_ESP8266 */
#elif defined(MY_GATEWAY_LINUX)
#if defined(MY_IP_ADDRESS)
_MQTT_ethClient.bind(_MQTT_clientIp);
#endif /* End of MY_IP_ADDRESS */
#elif defined(MY_GATEWAY_TINYGSM) /* Elif part of MY_GATEWAY_ESP8266 */
#elif defined(MY_GATEWAY_TINYGSM)
GATEWAY_DEBUG(PSTR("GWT:TPC:IP=%s\n"), modem.getLocalIP().c_str());
#else /* Else part of MY_GATEWAY_ESP8266 */
#else
#if defined(MY_IP_ADDRESS)
Ethernet.begin(_MQTT_clientMAC, _MQTT_clientIp);
#else /* Else part of MY_IP_ADDRESS */
Expand All @@ -149,7 +178,7 @@ bool gatewayTransportConnect(void)
Ethernet.localIP()[1], Ethernet.localIP()[2], Ethernet.localIP()[3]);
// give the Ethernet interface a second to initialize
delay(1000);
#endif /* End of MY_GATEWAY_ESP8266 */
#endif
return true;
}

Expand Down Expand Up @@ -207,29 +236,29 @@ bool gatewayTransportInit(void)
#if defined(MY_GATEWAY_ESP8266)
// Turn off access point
WiFi.mode(WIFI_STA);
#if defined(MY_ESP8266_HOSTNAME)
WiFi.hostname(MY_ESP8266_HOSTNAME);
#if defined(MY_HOSTNAME)
WiFi.hostname(MY_HOSTNAME);
#endif /* End of MY_ESP8266_HOSTNAME */
#if defined(MY_IP_ADDRESS)
WiFi.config(_MQTT_clientIp, _gatewayIp, _subnetIp);
#endif /* End of MY_IP_ADDRESS */
#ifndef MY_ESP8266_BSSID
#define MY_ESP8266_BSSID NULL
#ifndef MY_WIFI_BSSID
#define MY_WIFI_BSSID NULL
#endif
(void)WiFi.begin(MY_ESP8266_SSID, MY_ESP8266_PASSWORD, 0, MY_ESP8266_BSSID);
(void)WiFi.begin(MY_WIFI_SSID, MY_WIFI_PASSWORD, 0, MY_WIFI_BSSID);
#elif defined(MY_GATEWAY_ESP32)
// Turn off access point
WiFi.mode(WIFI_STA);
#if defined(MY_ESP32_HOSTNAME)
WiFi.setHostname(MY_ESP32_HOSTNAME);
#endif /* End of MY_ESP32_HOSTNAME */
#if defined(MY_HOSTNAME)
WiFi.setHostname(MY_HOSTNAME);
#endif /* End of MY_HOSTNAME */
#if defined(MY_IP_ADDRESS)
WiFi.config(_MQTT_clientIp, _gatewayIp, _subnetIp);
#endif /* End of MY_IP_ADDRESS */
#ifndef MY_ESP32_BSSID
#define MY_ESP32_BSSID NULL
#ifndef MY_WIFI_BSSID
#define MY_WIFI_BSSID NULL
#endif
(void)WiFi.begin(MY_ESP32_SSID, MY_ESP32_PASSWORD, 0, MY_ESP32_BSSID);
(void)WiFi.begin(MY_WIFI_SSID, MY_WIFI_PASSWORD, 0, MY_WIFI_BSSID);
#endif

gatewayTransportConnect();
Expand Down
8 changes: 4 additions & 4 deletions examples/GatewayESP32/GatewayESP32.ino
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,18 @@
#define MY_DEBUG

// Enables and select radio type (if attached)
#define MY_RADIO_NRF24
#define MY_RADIO_RF24
//#define MY_RADIO_RFM69
//#define MY_RADIO_RFM95

#define MY_GATEWAY_ESP32

#define MY_ESP32_SSID "MySSID"
#define MY_ESP32_PASSWORD "MyVerySecretPassword"
#define MY_WIFI_SSID "MySSID"
#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_ESP32_HOSTNAME "ESP32_GW"
#define MY_HOSTNAME "ESP32_GW"

// Enable MY_IP_ADDRESS here if you want a static ip address (no DHCP)
//#define MY_IP_ADDRESS 192,168,1,100
Expand Down
8 changes: 4 additions & 4 deletions examples/GatewayESP32MQTTClient/GatewayESP32MQTTClient.ino
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#define MY_DEBUG

// Enables and select radio type (if attached)
#define MY_RADIO_NRF24
#define MY_RADIO_RF24
//#define MY_RADIO_RFM69
//#define MY_RADIO_RFM95

Expand All @@ -51,12 +51,12 @@
//#define MY_MQTT_PASSWORD "password"

// Set WIFI SSID and password
#define MY_ESP32_SSID "MySSID"
#define MY_ESP32_PASSWORD "MyVerySecretPassword"
#define MY_WIFI_SSID "MySSID"
#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_ESP32_HOSTNAME "ESP32_MQTT_GW"
#define MY_HOSTNAME "ESP32_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
6 changes: 3 additions & 3 deletions examples/GatewayESP8266/GatewayESP8266.ino
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@

#define MY_GATEWAY_ESP8266

#define MY_ESP8266_SSID "MySSID"
#define MY_ESP8266_PASSWORD "MyVerySecretPassword"
#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 below

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

// Enable MY_IP_ADDRESS here if you want a static ip address (no DHCP)
//#define MY_IP_ADDRESS 192,168,178,87
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@
//#define MY_MQTT_PASSWORD "password"

// Set WIFI SSID and password
#define MY_ESP8266_SSID "MySSID"
#define MY_ESP8266_PASSWORD "MyVerySecretPassword"
#define MY_WIFI_SSID "MySSID"
#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_ESP8266_HOSTNAME "mqtt-sensor-gateway"
// #define MY_HOSTNAME "mqtt-sensor-gateway"

// Enable MY_IP_ADDRESS here if you want a static ip address (no DHCP)
//#define MY_IP_ADDRESS 192,168,178,87
Expand Down
6 changes: 3 additions & 3 deletions examples/GatewayESP8266OTA/GatewayESP8266OTA.ino
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@

#define MY_GATEWAY_ESP8266

#define MY_ESP8266_SSID "MySSID"
#define MY_ESP8266_PASSWORD "MyVerySecretPassword"
#define MY_WIFI_SSID "MySSID"
#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_ESP8266_HOSTNAME "sensor-ota-gateway"
// #define MY_HOSTNAME "sensor-ota-gateway"

// Enable UDP communication
//#define MY_USE_UDP // If using UDP you need to set MY_CONTROLLER_IP_ADDRESS below
Expand Down
16 changes: 8 additions & 8 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ MY_GATEWAY_ESP8266 LITERAL1
MY_GATEWAY_MQTT_CLIENT LITERAL1
MY_GATEWAY_SERIAL LITERAL1
MY_GATEWAY_W5100 LITERAL1
MY_HOSTNAME LITERAL1
MY_INCLUSION_BUTTON_EXTERNAL_PULLUP LITERAL1
MY_MQTT_CLIENT_ID LITERAL1
MY_MQTT_CLIENT_PUBLISH_RETAIN LITERAL1
Expand All @@ -248,6 +249,9 @@ MY_MQTT_PUBLISH_TOPIC_PREFIX LITERAL1
MY_MQTT_SUBSCRIBE_TOPIC_PREFIX LITERAL1
MY_MQTT_USER LITERAL1
MY_W5100_SPI_EN LITERAL1
MY_WIFI_SSID LITERAL1
MY_WIFI_BSSID LITERAL1
MY_WIFI_PASSWORD LITERAL1

# Ethernet
MY_CONTROLLER_IP_ADDRESS LITERAL1
Expand All @@ -265,17 +269,9 @@ MY_PORT LITERAL1
MY_USE_UDP LITERAL1

# ESP32
MY_ESP32_SSID LITERAL1
MY_ESP32_PASSWORD LITERAL1
MY_ESP32_BSSID LITERAL1
MY_ESP32_HOSTNAME LITERAL1

# ESP8266
MY_ESP8266_BSSID LITERAL1
MY_ESP8266_HOSTNAME LITERAL1
MY_ESP8266_PASSWORD LITERAL1
MY_ESP8266_SERIAL_MODE LITERAL1
MY_ESP8266_SSID LITERAL1

# Blacklist - autodefines that are used internally and should not be highlighted, hence commented.
# MY_CAP_ARCH
Expand Down Expand Up @@ -347,6 +343,10 @@ MY_ESP8266_SSID LITERAL1
# MY_RF69_RESET
# MY_RF69_SPI_CS
# MY_TRANSPORT_DONT_CARE_MODE
# MY_ESP8266_BSSID LITERAL1
# MY_ESP8266_HOSTNAME LITERAL1
# MY_ESP8266_PASSWORD LITERAL1
# MY_ESP8266_SSID LITERAL1

# Blacklist - descriptional only
# MY_XYZ_POWER_PIN
Expand Down

0 comments on commit bb77661

Please sign in to comment.