This is an old revision of the document!


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, so you will present it on the LCD screen

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 4×20 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 “Connecting Wifi” and the second line should present the attempt number. When connected, the first line of the LCD should present information “WiFi connected”, following one your given IP address including “IP: XXX.XXX.XXX.XXX”. As MAC address is pretty long (17 chars including separators, here we will use the fourth line to present it, while “MAC: ” header should be present in the third line of the LCD. Note, there is a ticker, showing that the device is alive, implemented the same way as in scenario B1, for beginners.

We suggest putting connection and information code into the separate function that you will call from the setup() one, just for cleaner code.

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's MAC.

Start

Define some identifiers to separate and update AP's SSID and passphrase easily. To format lines for the LCD, we suggest using a char buffer of 20 characters (one full line) and some 2-3 integers for iterators. Remember to declare the LCD control class in your code. You do not need to instantiate WiFi communication class - as you have only one interface here, it is singleton class you can refer directly using WiFi. Note - in this scenario, you connect only once. If your connection breaks, you will have no information about it here. To handle such situation, there are multiple solutions: you can check in the loop() if the connection is OK and in case it is down, you can call your re-connecting function, or you can use one of the asynchronous handlers, triggered automatically via the WiFi manager. To use the latter approach, refer to the ESP8266 WiFi implementation for Arduino documentation.

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:

#include <Arduino.h>
#include <ittiot.h>
#include <ESP8266WiFi.h>
#include <LiquidCrystal_I2C.h>
...
Step 2

Give it some definitions as identifiers for cleaner code:

#define wifi_ssid "internal.IOT"
#define wifi_password "IoTlab32768"
Always refer to the node documentation to ensure you know current SSID and passphrase. They may differ to those in the code above!
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:

  delay(10);
  WiFi.mode(WIFI_STA);
  WiFi.begin(wifi_ssid, wifi_password);
  n=0;
delay(10) is necessary to give it a breath before you ask it to connect to the web - wifi interface itself boots slower than rest of the ESP8266 because of radio physics.
n=0 is an iterator here - you will use it to show number of attempts - you should usually succeed in one or couple. If attempts start ticking up, look for the code mistakes and check your SSID and passphrase.
Step 4

Check if connected, if not, give it next attempt:

while (WiFi.status() != WL_CONNECTED) {
    lcd.setCursor(0,1);
    n++;
    sprintf(buffer,"Attempt %d",n);
    lcd.print(buffer);
    delay(1000);
Step 5

When connected, show details to the camera:

  lcd.clear();
  lcd.home();
  lcd.print("WiFi connected!");
  //Print IP
  String s = WiFi.localIP().toString();
  sprintf(buffer,"IP: %s",s.c_str());
  lcd.setCursor(0,1);
  lcd.print(buffer);
  //Print MAC
  lcd.setCursor(0,2);
  lcd.print("MAC:");
  s = WiFi.macAddress();
  lcd.setCursor(0,3);
  lcd.print(s.c_str());
IP address returned by the WiFi.localIP() function of the WiFi manager returns IPAddress structure so you need to convert it to String. Supprisingly, MAC address is an already pre-formatted String object.
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

Provide some result validation methods, for self assesment.

FAQ

This section is to be extended as new questions appear.
When using the printed version of this manual, please refer to the latest online version of this document to obtain the valid and up-to-date list of the FAQ. Provide some FAQs in the following form:
Question?: Answer.

en/iot-open/remotelab/sut/generalpurpose2/u1.1564855440.txt.gz · Last modified: 2020/07/20 09:00 (external edit)
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0