last modified: 2018-09-19

EMLyon logo corp


The code, explained steps by steps

// libraries pre-installed with the board

#include <ESP8266WiFi.h> (1)
#include <SoftwareSerial.h> (2)
#include <Wire.h> (3)
1 The library for the Huzzah Feather (the specific board we use)
2 The library enabling the board to send info to the computer (useful to debug!)
3 The library enabling Wifi
// libraries we added "manually"

#include <Adafruit_GFX.h> (1)
#include <Adafruit_FeatherOLED.h> (2)
#include <ArduinoJson.h> (3)
1 The library to help the board deal with graphics
2 The library to help the board deal with the specific screen Feather OLED
3 The library that saves us time when dealing with text formatted in JSON

The next lines of code are used to store some useful information:

char* ssid     = "name of wifi network here"; (1)
char* password = "password here";(2)
1 means: we create the variable called ssid. We give it the value 'name of wifi network here'. This variable is of type chars* (characters)
2 means: we create the variable called password. We give it the value 'password here'. This variable is of type chars* (characters)

Of coure, these values are just examples and you should put the name of your own wifi network and password.

Don’t forget the semi-colon at the end of the lines!
String location = "Saint-Etienne"; (1)
1 means: we create the variable called location. We give it the value "Saint-Etienne" (you can choose another place of course). This variable is of type String (String is the technical term for text).
Adafruit_FeatherOLED screen = Adafruit_FeatherOLED(); (1)
1 means: we create the variable called "screen". We give it the value returned by the function Adafruit_FeatherOLED(). The variable is of type Adafruit_FeatherOLED.
WiFiClient client;(1)
1 means: we create the variable named "client", of type "WifiClient". It has no value yet.
void setup() { (1)
1 This is called a function or method. The setup funcion is executed just once, when the board is powered up.

Don’t know what functions are? Check here.

// initialize the communication between the board and our computer.
// useful to display useful info.

while (!Serial)(2)
    ; (4)
1 means: we execute the method called begin on the variable Serial.
2 means: as long as the variable "Serial" is not equal to "true", execute the lines of codes in the curly braces { } in <3> and <5>. More info here.
3 The instructions in the curly braces are just this ;, which do nothing. So the board is basically waiting until Serial switches to "true".
//initialize the screen and hides the battery sign

1 means: we execute the method called init on the variable screen.
2 means: we execute the method called setBatteryVisible on the variable screen. We use the parameter false for this method.
//initialize the wifi connection

WiFi.begin(ssid, password); (1)
1 means: a variable from the board (not created by us) is called "Wifi". We execute the method begin on it, which will start the connection to the wifi. So we give it 2 parameters: the name and password for the wifi, which we defined above.
// the program freezes as long as the wifi is not connected

while (WiFi.status() != WL_CONNECTED ) { (1)
1 This another while loop, like the one above. The status method of Wifi returns a value which is true when the wifi is connected, and false otherwise.WL_CONNECTED is a variable with a value of true.
2 delay is a simple function which freezes the board. For how long? For the duration shown in the parentheses, in milliseconds. So here, delay(500) freezes the board for 0.5 seconds before it retries to connect to the wifi.
} (1)
1 Do not forget this closing bracket, corresponding to the opening brack from the line above void setup() {
void loop() { (1)
1 the lines of code inside the loop function will be executed until the last, then start over again and again. This is where the interesting stuff happens! See here for more.
1 deletes what is on screen
2 deletes the previous message
//get the quality of air at location
callAQI(location);(1) (2)
1 means: execute the method callAQI. Where does it come from? You will write the code for this function in a separate file. The role of this function is to connect via wifi to and return the air quality for a given location.
2 location is a parameter to the function. It is a variable you defined above (scroll up).
String aqiNumber = readAQIResponse();(1)
1 We create the variable aqiNumber, of type String. The value assigned to this variable is the number returned by the function readAQIResponse().

Where does the readAQIResponse come from? It is a function written in another file by you. It picks the text returned by the previous function (callAQI), and extracts from it the number representing the air quality.

//display the air quality on the screen

screen.println("air quality");(1)
screen.println("at "+ location + ":");(2)
1 will write "air quality" on the 1st line of the screen
2 will write "at Saint-Etienne" on the 2nd line of the screen (if the value of your location variable is "Saint-Etienne")
3 will write the air pollution index retrieved from above
4 will show the message on screen (if you forget this line, the message is not visible).
// wait 5 seconds
} (1)
1 Don’t forget this closing curly brace. It closes the function opened above with void loop (){

The end

Find references for this lesson, and other lessons, here.

round portrait mini 150

This course is made by Clement Levallois.

Discover my other courses in data / tech for business:

Or get in touch via Twitter: @seinecle