From bb7766197c982eb36410da2e2aab82c58b0a66aa Mon Sep 17 00:00:00 2001 From: tekka Date: Tue, 1 May 2018 16:54:43 +0200 Subject: [PATCH] Consolidate WiFi defines (#1110) Great work @tekka007 --- MyConfig.h | 30 +++++++- core/MyGatewayTransportEthernet.cpp | 73 +++++++++++-------- core/MyGatewayTransportMQTTClient.cpp | 59 +++++++++++---- examples/GatewayESP32/GatewayESP32.ino | 8 +- .../GatewayESP32MQTTClient.ino | 8 +- examples/GatewayESP8266/GatewayESP8266.ino | 6 +- .../GatewayESP8266MQTTClient.ino | 6 +- .../GatewayESP8266OTA/GatewayESP8266OTA.ino | 6 +- keywords.txt | 16 ++-- 9 files changed, 140 insertions(+), 72 deletions(-) diff --git a/MyConfig.h b/MyConfig.h index 5b1d84a..c08b1f2 100644 --- a/MyConfig.h +++ b/MyConfig.h @@ -6,7 +6,7 @@ * network topology allowing messages to be routed to nodes. * * Created by Henrik Ekblad - * 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 @@ -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. @@ -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 diff --git a/core/MyGatewayTransportEthernet.cpp b/core/MyGatewayTransportEthernet.cpp index 7676613..ac17394 100644 --- a/core/MyGatewayTransportEthernet.cpp +++ b/core/MyGatewayTransportEthernet.cpp @@ -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 @@ -116,45 +145,27 @@ void _w5100_spi_en(bool enable) bool gatewayTransportInit(void) { _w5100_spi_en(true); + +#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_HOSTNAME) #if defined(MY_GATEWAY_ESP8266) -#if defined(MY_ESP8266_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 */ + 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 diff --git a/core/MyGatewayTransportMQTTClient.cpp b/core/MyGatewayTransportMQTTClient.cpp index e3bf94f..1c96dab 100644 --- a/core/MyGatewayTransportMQTTClient.cpp +++ b/core/MyGatewayTransportMQTTClient.cpp @@ -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 @@ -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 */ @@ -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; } @@ -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(); diff --git a/examples/GatewayESP32/GatewayESP32.ino b/examples/GatewayESP32/GatewayESP32.ino index 6bd440f..c2d1f99 100644 --- a/examples/GatewayESP32/GatewayESP32.ino +++ b/examples/GatewayESP32/GatewayESP32.ino @@ -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 diff --git a/examples/GatewayESP32MQTTClient/GatewayESP32MQTTClient.ino b/examples/GatewayESP32MQTTClient/GatewayESP32MQTTClient.ino index 9d58793..9783ddd 100644 --- a/examples/GatewayESP32MQTTClient/GatewayESP32MQTTClient.ino +++ b/examples/GatewayESP32MQTTClient/GatewayESP32MQTTClient.ino @@ -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 @@ -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 diff --git a/examples/GatewayESP8266/GatewayESP8266.ino b/examples/GatewayESP8266/GatewayESP8266.ino index 6532b53..32bbf33 100644 --- a/examples/GatewayESP8266/GatewayESP8266.ino +++ b/examples/GatewayESP8266/GatewayESP8266.ino @@ -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 diff --git a/examples/GatewayESP8266MQTTClient/GatewayESP8266MQTTClient.ino b/examples/GatewayESP8266MQTTClient/GatewayESP8266MQTTClient.ino index b1d10ab..f517cb5 100644 --- a/examples/GatewayESP8266MQTTClient/GatewayESP8266MQTTClient.ino +++ b/examples/GatewayESP8266MQTTClient/GatewayESP8266MQTTClient.ino @@ -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 diff --git a/examples/GatewayESP8266OTA/GatewayESP8266OTA.ino b/examples/GatewayESP8266OTA/GatewayESP8266OTA.ino index 6a0bb79..f83fe52 100644 --- a/examples/GatewayESP8266OTA/GatewayESP8266OTA.ino +++ b/examples/GatewayESP8266OTA/GatewayESP8266OTA.ino @@ -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 diff --git a/keywords.txt b/keywords.txt index 494e979..b22e5fd 100644 --- a/keywords.txt +++ b/keywords.txt @@ -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 @@ -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 @@ -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 @@ -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