This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:iot-open:remotelab:sut:generalpurpose2:u1 [2019/07/25 13:36] – pczekalski | en:iot-open:remotelab:sut:generalpurpose2:u1 [2020/07/20 12:00] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ==== U1: Connecting to the network in STA mode ==== | ||
| + | Most IoT (if not all of them) require your device to communicate via a network. Here we connect to the existing WiFi network, 2.4GHz. All laboratory nodes can access common access point and require credentials to connect to it (see laboratory description section for the credentials and latest updates). ESP 8266 has a built-in WiFi interface, so you're using it to connect to the network. Every ESP has an individual MAC address. An IP address is assigned to the ESP via DHCP server, automatically, | ||
| + | === Target group === | ||
| + | Undergraduate / Bachelor / Engineering Students | ||
| + | |||
| + | === Prerequisites === | ||
| + | You will need to know the credentials of the network - see node description for details. Mind, those may be different than in the code chunks presented below. You need to know how to handle 4x20 characters LCD screen. In case of doubt re-work on scenarios B1 and B2. | ||
| + | |||
| + | === Scenario === | ||
| + | In this scenario, you will create a WiFi client to connect to the AP then present connection status (eventually the failure, with attempts) on the LCD screen. Then you will show on the LCD given IP address and MAC address of your ESP8266.\\ | ||
| + | While attempting to connect, the first line of the LCD should present information " | ||
| + | When connected, the first line of the LCD should present information "WiFi connected", | ||
| + | |||
| + | We suggest putting connection and information code into the separate function that you will call from the '' | ||
| + | === Result === | ||
| + | The LCD screen should present the current situation and all the necessary information. Once connected, observe the IP address assigned by the DHCP server and device' | ||
| + | |||
| + | === Start === | ||
| + | Define some identifiers to separate and update AP's SSID and passphrase easily. To format lines for the LCD, we suggest using a '' | ||
| + | |||
| + | === Steps === | ||
| + | Following steps do not present full code - you need to supply missing parts on your own! | ||
| + | |||
| + | == Step 1 == | ||
| + | Include all necessary libraries. The minimum set here is: | ||
| + | <code c> | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | ... | ||
| + | </ | ||
| + | == Step 2 == | ||
| + | Give it some definitions as identifiers for cleaner code: | ||
| + | <code c> | ||
| + | #define wifi_ssid " | ||
| + | #define wifi_password " | ||
| + | </ | ||
| + | <note important> | ||
| + | |||
| + | == Step 3 == | ||
| + | Print some information about starting connecting to WiFi, configure network interface as a WiFi client and give it a try to connect to the AP: | ||
| + | <code c> | ||
| + | delay(10); | ||
| + | WiFi.mode(WIFI_STA); | ||
| + | WiFi.begin(wifi_ssid, | ||
| + | n=0; | ||
| + | </ | ||
| + | <note important>'' | ||
| + | <note tip>'' | ||
| + | <note warning> | ||
| + | |||
| + | == Step 4 == | ||
| + | Check if connected, if not, give it next attempt: | ||
| + | <code c> | ||
| + | while (WiFi.status() != WL_CONNECTED) { | ||
| + | lcd.setCursor(0, | ||
| + | n++; | ||
| + | sprintf(buffer," | ||
| + | lcd.print(buffer); | ||
| + | delay(1000); | ||
| + | </ | ||
| + | |||
| + | == Step 5 == | ||
| + | When connected, show details to the camera: | ||
| + | <code c> | ||
| + | lcd.clear(); | ||
| + | lcd.home(); | ||
| + | lcd.print(" | ||
| + | //Print IP | ||
| + | String s = WiFi.localIP().toString(); | ||
| + | sprintf(buffer," | ||
| + | lcd.setCursor(0, | ||
| + | lcd.print(buffer); | ||
| + | //Print MAC | ||
| + | lcd.setCursor(0, | ||
| + | lcd.print(" | ||
| + | s = WiFi.macAddress(); | ||
| + | lcd.setCursor(0, | ||
| + | lcd.print(s.c_str()); | ||
| + | </ | ||
| + | <note tip>IP address returned by the WiFi.localIP() function of the WiFi manager returns '' | ||
| + | == Step 6 == | ||
| + | In the loop() function present ticker in the 3rd line, right side of the LCD. The 4th line is all occupied by the MAC address, if you followed 1:1 the guide. In case your UI looks different, handle coordinates appropriatelly. | ||
| + | |||
| + | === Result validation === | ||
| + | Run your code and observe LCD presenting information on connection progress, IP and MAC addresses. | ||
| + | |||
| + | === FAQ === | ||
| + | **Does IP address change over time?**: Yes. First of all, IP is given automatically by the DHCP server. There is no strict rule saying, your IP is always going to be the same. Second, IP address reservation is done for some period of time and then DHCP server may assign you other IP so it may change over runtime, not necessarily between restarts only.\\ | ||
| + | **Does MAC address change**: No. It is a factory given one and should be unique worldwide. Anyway, you can programmatically change it - this technique is commonly used by hackers to mimic other devices.\\ | ||
| + | **Do I need to use '' | ||