Lesson 3: Test Connection to WiFi & Google Services

If you want to connect an esp8266 to a wireless network where there's only one WiFi router, then you can use the method shown in the overview which specifies an SSID (network name) and a password (if the network is password protected). However, if you are in an area where there are many WiFi routers broadcasting the same SSID, then you will need to gather and specify the BSSID and channel of the router you plan to connect to.

To gather this information, download and run WiFiSuperScan (scroll to the bottom of this page for the link). It will show all networks within range along with their BSSID, their channel, and their signal strength.

Once you find the strongest guest network's BSSID and channel, upload and run the following program to test your connection to the Internet.

ConnectionTest.ino:
 1 | #include <ESP8266WiFi.h>
 2 | #include "HTTPSRedirect.h"
 3 | #include "DebugMacros.h"
 4 | 
 5 | const char* ssid = "GUEST_MSBSD";
 6 | const char* password = "";
 7 | const byte channel = 6;
 8 | unsigned char bssid[18] = { 0xD8, 0x84, 0x66, 0x40, 0x7E, 0x49 };
 9 | 
10 | const char* host = "script.google.com";
11 | const char *GScriptId = "AKfycbxF19iZK6kJvHmdt7bpjvyfcqWS2Q-iDeCiPH0k5-vdU5B0He5y";
12 | const int port = 443;
13 | 
14 | HTTPSRedirect* client = nullptr;
15 | 
16 | void setup() {
17 |   Serial.begin(115200);
18 |   Serial.setDebugOutput(true);
19 | 
20 |   WiFi.begin(ssid, password, channel, bssid);
21 |   while (WiFi.status() != WL_CONNECTED) {
22 |     delay(500);
23 |   }
24 | }
25 | 
26 | void loop() {
27 |   client = new HTTPSRedirect(port);
28 |   client->connect(host, port);
29 | 
30 |   String url = String("/macros/s/") + GScriptId + "/exec";
31 |   Serial.println(url);
32 |   client->GET(url, host);
33 |   Serial.println(client->getResponseBody());
34 |   delete client;
35 | 
36 |   Serial.println("Waiting...");
37 |   delay(3000);
38 | }

Code Breakdown
The biggest change between this code and what you saw in the overview is the lack of a sensor and transmission of sensor data. The next biggest change is how this program connects to the WiFi.
  • Lines 7 & 8 specify the channel and BSSID of a specific WiFi access point.
  • Line 11 specifies a real GScriptId. Use this exact GScriptId to test your connection. You will change it to your own in the next lesson.
  • Line 20 uses that information to make the connection.
If successful, your serial monitor should show:
ċ
WiFiSuperScan.zip
(1k)
Michael Backus,
Apr 10, 2017, 9:57 PM
Comments