| ... | ... |
@@ -14,6 +14,13 @@ ESP8266 project: |
| 14 | 14 |
- my_wifi.h : Add your WiFi credentials |
| 15 | 15 |
- my_mqtt.h : Add you broker details (ip, username, password) |
| 16 | 16 |
|
| 17 |
+Copy the templates and adapt them to your needs |
|
| 18 |
+ |
|
| 19 |
+```bash |
|
| 20 |
+cp my_wifi.h.sample my_wifi.h |
|
| 21 |
+cp my_mqtt.h.sample my_mqtt.h |
|
| 22 |
+``` |
|
| 23 |
+ |
|
| 17 | 24 |
# Dependencies |
| 18 | 25 |
- **PubSubClient** by Nick O'Leary (v2.7.0+) |
| 19 | 26 |
- **DHT sensor library** by Adafruit (v1.3.8) |
| ... | ... |
@@ -0,0 +1,56 @@ |
| 1 |
+#ifndef _MQTT_H |
|
| 2 |
+#define _MQTT_H |
|
| 3 |
+ |
|
| 4 |
+#include <PubSubClient.h> // Allows us to connect to, and publish to the MQTT broker |
|
| 5 |
+ |
|
| 6 |
+ |
|
| 7 |
+// MQTT |
|
| 8 |
+const int MAX_MQTT_CNX_RETRIES = 3; |
|
| 9 |
+static char mqtt_server[32] = ""; |
|
| 10 |
+const char* mqtt_server_int = "192.168.0.1"; |
|
| 11 |
+const char* mqtt_server_ext = "a.domain.tld"; |
|
| 12 |
+const char* mqtt_topic = "topicDHT"; |
|
| 13 |
+const int mqtt_port = 1883; |
|
| 14 |
+// The client id identifies the ESP8266 device. Think of it a bit like a hostname (Or just a name, like Greg). |
|
| 15 |
+char clientID[22] = ""; |
|
| 16 |
+const char* clientIDFmt="esp-%s"; |
|
| 17 |
+ |
|
| 18 |
+const char* mqtt_user="esp8266"; |
|
| 19 |
+const char* mqtt_password="SomeRandomPassword"; |
|
| 20 |
+ |
|
| 21 |
+// Initialise the WiFi and MQTT Client objects |
|
| 22 |
+WiFiClient wifiClient; |
|
| 23 |
+PubSubClient client(mqtt_server, 1883, wifiClient); // 1883 is the listener port for the Broker |
|
| 24 |
+ |
|
| 25 |
+void mqtt_connect() {
|
|
| 26 |
+ if( 0 == strcmp(aplist[currentAPIndex].ssid,"wifi1") ) {
|
|
| 27 |
+ //House Wifi |
|
| 28 |
+ client.setServer(mqtt_server_int, mqtt_port); |
|
| 29 |
+ //if (client.connect(clientID)) {
|
|
| 30 |
+ if (client.connect(clientID,mqtt_user,mqtt_password)) {
|
|
| 31 |
+ Serial.println("Connected to MQTT Broker : "+String(mqtt_server_int));
|
|
| 32 |
+ } else {
|
|
| 33 |
+ Serial.println("Connection to MQTT Broker "+String(mqtt_server_int)+"failed...");
|
|
| 34 |
+ delay(2000); |
|
| 35 |
+ } |
|
| 36 |
+ } else {
|
|
| 37 |
+ // External Wifi |
|
| 38 |
+ client.setServer(mqtt_server_ext, mqtt_port); |
|
| 39 |
+ if (client.connect(clientID,mqtt_user,mqtt_password)) {
|
|
| 40 |
+ Serial.println("Connected to MQTT Broker : "+String(mqtt_server_ext));
|
|
| 41 |
+ } else {
|
|
| 42 |
+ Serial.println("Connection to MQTT Broker "+String(mqtt_server_ext)+"failed...");
|
|
| 43 |
+ delay(2000); |
|
| 44 |
+ } |
|
| 45 |
+ } |
|
| 46 |
+} |
|
| 47 |
+ |
|
| 48 |
+void mqtt_reconnect() {
|
|
| 49 |
+ byte count = 0; |
|
| 50 |
+ while (!client.connected() && count < MAX_MQTT_CNX_RETRIES ) {
|
|
| 51 |
+ Serial.print("Attempting MQTT reconnection...");
|
|
| 52 |
+ mqtt_connect(); |
|
| 53 |
+ count++; |
|
| 54 |
+ } |
|
| 55 |
+} |
|
| 56 |
+#endif //_MQTT_H |
| ... | ... |
@@ -0,0 +1,88 @@ |
| 1 |
+#ifndef _MY_WIFI_H |
|
| 2 |
+#define _MY_WIFI_H |
|
| 3 |
+ |
|
| 4 |
+#include <ESP8266WiFi.h> |
|
| 5 |
+ |
|
| 6 |
+//----------------------------------------------------------------------------------- |
|
| 7 |
+//Wifi Constants |
|
| 8 |
+//----------------------------------------------------------------------------------- |
|
| 9 |
+String WiFiIP; |
|
| 10 |
+typedef struct _ssid_list_t {
|
|
| 11 |
+ const char* ssid; |
|
| 12 |
+ const char* password; |
|
| 13 |
+} ssid_list_t; |
|
| 14 |
+ |
|
| 15 |
+//#define _TEST |
|
| 16 |
+#ifndef _TEST |
|
| 17 |
+static int currentAPIndex = -1; |
|
| 18 |
+const int MAX_CNX_RETRIES = 20; |
|
| 19 |
+ssid_list_t aplist[]={
|
|
| 20 |
+ {"wifi1", "pass1"},
|
|
| 21 |
+ {"wifi2", "pass2"},
|
|
| 22 |
+ {"wifi3", "pass3"},
|
|
| 23 |
+ {NULL,NULL}
|
|
| 24 |
+}; |
|
| 25 |
+#else |
|
| 26 |
+const int MAX_CNX_RETRIES = 1; |
|
| 27 |
+ssid_list_t aplist[]={
|
|
| 28 |
+ {"Phony", "phonyaccount"},
|
|
| 29 |
+ {NULL,NULL}
|
|
| 30 |
+}; |
|
| 31 |
+#endif //_TEST |
|
| 32 |
+ |
|
| 33 |
+byte WiFiConnect() {
|
|
| 34 |
+ WiFiIP = "No IP"; |
|
| 35 |
+ //WiFi.disconnect(true); |
|
| 36 |
+ //delay(2000); |
|
| 37 |
+ //WiFi.mode(WIFI_STA); |
|
| 38 |
+ //WiFi.setAutoConnect(true); |
|
| 39 |
+ |
|
| 40 |
+ byte i = 0; |
|
| 41 |
+ while(aplist[i].ssid!= NULL) {
|
|
| 42 |
+ WiFi.persistent(false); |
|
| 43 |
+ WiFi.mode(WIFI_STA); |
|
| 44 |
+ WiFi.disconnect(true); |
|
| 45 |
+ delay(1000); |
|
| 46 |
+ WiFi.softAPdisconnect(true); |
|
| 47 |
+ delay(1000); |
|
| 48 |
+ Serial.print("WIFI status = ");
|
|
| 49 |
+ Serial.println(WiFi.getMode()); |
|
| 50 |
+ |
|
| 51 |
+ WiFi.begin(aplist[i].ssid, aplist[i].password); |
|
| 52 |
+ byte count = 0; |
|
| 53 |
+ Serial.print("WiFiConnect: Connecting to ");
|
|
| 54 |
+ Serial.print(aplist[i].ssid); |
|
| 55 |
+ draw(aplist[i].ssid, -1, -1, -1); |
|
| 56 |
+ while(WiFi.status() != WL_CONNECTED && count < MAX_CNX_RETRIES) |
|
| 57 |
+ {
|
|
| 58 |
+ count ++; |
|
| 59 |
+ #ifdef BLINK_FLASH |
|
| 60 |
+ digitalWrite(LED_BUILTIN, 3); |
|
| 61 |
+ delay(500); |
|
| 62 |
+ digitalWrite(LED_BUILTIN, 0); |
|
| 63 |
+ delay(500); |
|
| 64 |
+ #else |
|
| 65 |
+ delay(500); |
|
| 66 |
+ #endif //BLINK_FLASH |
|
| 67 |
+ Serial.print(".");
|
|
| 68 |
+ draw(aplist[i].ssid, NOLOGO, -1, 100*count/MAX_CNX_RETRIES); |
|
| 69 |
+ } |
|
| 70 |
+ if( WiFi.status() == WL_CONNECTED ) {
|
|
| 71 |
+ Serial.println("");
|
|
| 72 |
+ IPAddress ip = WiFi.localIP(); |
|
| 73 |
+ WiFiIP = String(ip[0]) + '.' + String(ip[1]) + '.' + String(ip[2]) + '.' + String(ip[3]); |
|
| 74 |
+ Serial.println("WiFiConnect: "+WiFiIP);
|
|
| 75 |
+ currentAPIndex = i; |
|
| 76 |
+ break; |
|
| 77 |
+ } |
|
| 78 |
+ if(WiFi.status() != WL_CONNECTED) |
|
| 79 |
+ {
|
|
| 80 |
+ Serial.println("WiFiConnect: Failed");
|
|
| 81 |
+ } |
|
| 82 |
+ i++; |
|
| 83 |
+ } |
|
| 84 |
+ digitalWrite(LED_BUILTIN, HIGH); |
|
| 85 |
+ return (WiFi.status() == WL_CONNECTED) ? 1 : 0; |
|
| 86 |
+} |
|
| 87 |
+ |
|
| 88 |
+#endif //_MY_WIFI_H |