... | ... |
@@ -0,0 +1,2 @@ |
1 |
+my_mqtt.h |
|
2 |
+my_wifi.h |
... | ... |
@@ -1,59 +0,0 @@ |
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 |
-#ifdef USE_MQTT |
|
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.1.33"; |
|
11 |
-const char* mqtt_server_ext = "sg.kawi.fr"; |
|
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="NjWxXrEMnOeaNtv8b40u"; |
|
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 |
- // Connect to MQTT Broker |
|
27 |
- // client.connect returns a boolean value to let us know if the connection was successful. |
|
28 |
- if( 0 == strcmp(aplist[currentAPIndex].ssid,"kawifi") ) { |
|
29 |
- //House Wifi |
|
30 |
- client.setServer(mqtt_server_int, mqtt_port); |
|
31 |
- //if (client.connect(clientID)) { |
|
32 |
- if (client.connect(clientID,mqtt_user,mqtt_password)) { |
|
33 |
- Serial.println("Connected to MQTT Broker : "+String(mqtt_server_int)); |
|
34 |
- } else { |
|
35 |
- Serial.println("Connection to MQTT Broker "+String(mqtt_server_int)+"failed..."); |
|
36 |
- delay(2000); |
|
37 |
- } |
|
38 |
- } else { |
|
39 |
- // External Wifi |
|
40 |
- client.setServer(mqtt_server_ext, mqtt_port); |
|
41 |
- if (client.connect(clientID,mqtt_user,mqtt_password)) { |
|
42 |
- Serial.println("Connected to MQTT Broker : "+String(mqtt_server_ext)); |
|
43 |
- } else { |
|
44 |
- Serial.println("Connection to MQTT Broker "+String(mqtt_server_ext)+"failed..."); |
|
45 |
- delay(2000); |
|
46 |
- } |
|
47 |
- } |
|
48 |
-} |
|
49 |
- |
|
50 |
-void mqtt_reconnect() { |
|
51 |
- byte count = 0; |
|
52 |
- while (!client.connected() && count < MAX_MQTT_CNX_RETRIES ) { |
|
53 |
- Serial.print("Attempting MQTT reconnection..."); |
|
54 |
- mqtt_connect(); |
|
55 |
- count++; |
|
56 |
- } |
|
57 |
-} |
|
58 |
-#endif //USE_MQTT |
|
59 |
-#endif //_MQTT_H |
... | ... |
@@ -1,88 +0,0 @@ |
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 |