Modular ESP Firmware - Baseline - Individual Functions

From The TinkerNet Wiki
Jump to navigation Jump to search

void setup_wifi();

Called from inside setup() to configure your MQTT connection.

 1 ////////////////////////////////////////////////////////////////
 2 // These exist because the variables are declared elsewhere in
 3 // the project.
 4 extern char* WiFi_ssid;
 5 extern char* WiFi_password;
 6 extern char* WiFi_ClientName;
 7 ////////////////////////////////////////////////////////////////
 8 
 9 void setup_wifi()
10 {
11 
12   // We start by connecting to a WiFi network
13   Serial.printf("|                                       |\n");
14   Serial.printf("| Connecting to: %-22s |\n", WiFi_ssid);
15   Serial.printf("| ");
16 
17   WiFi.mode(WIFI_STA); // Connecting as a STATION
18   WiFi.begin(WiFi_ssid, WiFi_password);
19 
20   int dotcount = 0;
21   while (WiFi.status() != WL_CONNECTED) // Give it a bit of time to establish the WiFi connection...
22   {
23     dotcount++;
24     delay(500);
25     Serial.printf(".");
26   }
27 
28   for (int i = 0; i < (38 - dotcount); i++)
29   {
30     Serial.printf(" ");
31   }
32   Serial.printf("|\n");
33 
34   Serial.printf("| WiFi connected on Channel %-2d          |\n", WiFi.channel());
35   Serial.printf("| IP address: %-15s           |\n", WiFi.localIP().toString().c_str());
36   Serial.printf("+---------------------------------------+\n");
37 
38 // Add "build_flags = -D DEBUG0" to your platformio.ini to get some extra WiFi stats
39 
40 #ifdef DEBUG0
41   WiFi.printDiag(Serial);
42   Serial.printf("RSSI: %d dBm\n", WiFi.RSSI());
43 #endif
44 }