4. Connecting EMIT to the Internet (Arduino) - ControlBits/EMIT GitHub Wiki
We now have a fully functional Environmental Sensor but EMIT is an IoT (Internet of Things) sensor - we can't send any data anywhere without an Internet connection ... so let's get EMIT connected to the Internet right now!
The first thing we need to do is include the 'WiFi' library by adding the following line to the include section of the Sketch.
#include <WiFi.h>
This library contains all of the functions we need to connect and manage EMIT's connection to a Wi-Fi network.
We'll also be using EMIT's Green LED (connected to GPIO17) to indicate the status of our network connection. We'll configure this in setup() in exactly the same way as we did the Red LED but first we will add its Macro defination before the setup() function to improve readability:
#define EMIT_GREEN_LED 17
In setup we will initialize the GREEN LED to output and will initially turn it OFF as follows:
// configure Green LED GPIO to be OUTPUT
// so that we would control the LED (ON , OFF)
pinMode(EMIT_GREEN_LED , OUTPUT);
// on the beginning we will turn OFF the GREEN LED
digitalWrite(EMIT_GREEN_LED , LOW);
We need to define two macro definitions for the WiFi SSID name and password strings.
// define Wi-Fi settings
#define WIFISSID 'your-WiFi-SSID-goes-here'
#define WIFIPASSWORD 'your-WiFi-password-goes-here'
Now we will create a function that handles the Wi-Fi connection process, we will name it 'wifi_connect()', we will put the function before the setup function as it will be called on the setup function later so we need to define it first.
void wifi_connect(){
// wifi connection code.
}
The wifi_connect() function will use the different functions from the WiFi object to establish the WiFi connection to the router.
First we will change the mode of the WIFI interface to station, we have two options (Station, Access Point) but for this demonstration we will focus on the station mode.
WiFI.mode(WIFI_STA);
we need to attempt to connect to the network using the '.begin(WIFISSID, WIFIPASSWORD)' function like this:
WiFi.begin(WIFISSID, WIFIPASSWORD);
We then need to create a loop that will check to see if the wifi is connected using the network modules '.status() != WL_CONNECTED' function like this:
while (WiFi.status() != WL_CONNECTED)
If the connection fails, we should wait a couple of seconds and try again.
To complete, the function, we should turn the Green LED ON and OFF and send some some debug messages to the Serial monitor to indicate the status of the connection and/or reconnection attempts.
Finally once successfully connected, we should print the acquired network settings including the IP address that has been assigned to our EMIT by the network using:
Serial.println("WiFi connection successful");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
The full function, heavily commented to clarify how it all works, should look something like this:
void wifi_connect(void){
// change the wifi interface mode to station
WiFi.mode(WIFI_STA);
// attempt a WiFi connection to the router with credentials of WIFISSID and WIFIPASSWORD
WiFi.begin(WIFISSID, WIFIPASSWORD);
// wait for the connection to be established.
while (WiFi.status() != WL_CONNECTED){
// flash the Green LED indication a wifi connection attempt.
// turn on the Green LED
digitalWrite(EMIT_GREEN_LED , HIGH);
// print 'trying..' message to Serial Monitor
Serial.print("trying WiFi connection ");
Serial.println(WIFISSID);
// wait 1 second (1000 milliseconds)
delay(1000);
// turn off the Green LED
digitalWrite(EMIT_GREEN_LED , LOW);
// wait 2 second (2000 milliseconds)
delay(2000);
}
// if connected ...
// turn on the Green LED
digitalWrite(EMIT_GREEN_LED , HIGH);
// print 'WiFi connection successful' message to Serial Monitor
Serial.println("WiFi connection successful") ;
// print WiFi network settings (inc. IP Address) to Serial Monitor.
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
As we've done all the hard work in our wifi_connect function , all we need to do in our setup() function is call the wifi_connect() function that we just created with the line
// attempt a wifi connection
wifi_connect();
With this added, our Sketch file should look like this:
// WiFi library is essential for connecting to WIFI
#include <WiFi.h>
// include the Adafruit DHT libraries to use its functions reading the temperature and humidity from the DHT22 sensor.
#include "DHT.h"
#define EMIT_RED_LED 16
#define EMIT_GREEN_LED 17
#define EMIT_DHT_PIN 14 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
// Initialize DHT sensor.
DHT dht(EMIT_DHT_PIN, DHTTYPE);
// define Wi-Fi settings
#define WIFISSID "your-WiFi-SSID-goes-here"
#define WIFIPASSWORD "your-WiFi-password-goes-here"
void wifi_connect(void){
// change the wifi interface mode to station
WiFi.mode(WIFI_STA);
// attempt a WiFi connection to the router with credentials of WIFISSID and WIFIPASSWORD
WiFi.begin(WIFISSID, WIFIPASSWORD);
// wait for the connection to be established.
while (WiFi.status() != WL_CONNECTED){
// flash the Green LED indication a wifi connection attempt.
// turn on the Green LED
digitalWrite(EMIT_GREEN_LED , HIGH);
// print 'trying..' message to Serial Monitor
Serial.print("trying WiFi connection ");
Serial.println(WIFISSID);
// wait 1 second (1000 milliseconds)
delay(1000);
// turn off the Green LED
digitalWrite(EMIT_GREEN_LED , LOW);
// wait 2 second (2000 milliseconds)
delay(2000);
}
// if connected ...
// turn on the Green LED
digitalWrite(EMIT_GREEN_LED , HIGH);
// print 'WiFi connection successful' message to Serial Monitor
Serial.println("WiFi connection successful") ;
// print WiFi network settings (inc. IP Address) to Serial Monitor.
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void setup() {
// Enable the serial terminal
// Useful for debugging messages
Serial.begin(115200);
// configure RED LED GPIO to be OUTPUT
// so that we would control the LED (ON , OFF)
pinMode(EMIT_RED_LED , OUTPUT);
// on the begining we will turn OFF the RED LED
digitalWrite(EMIT_RED_LED , LOW);
// configure Green LED GPIO to be OUTPUT
// so that we would control the LED (ON , OFF)
pinMode(EMIT_GREEN_LED , OUTPUT);
// on the begining we will turn OFF the GREEN LED
digitalWrite(EMIT_GREEN_LED , LOW);
// initialize the DHT communication
dht.begin();
// attempt a wifi connection
wifi_connect();
}
void loop() {
//Wait for 5 seconds ( 5000 milliseconds )
delay(5000);
// turn the red LED ON
digitalWrite(EMIT_RED_LED , HIGH);
// Print a debugging message on the Serial Terminal indicating the start of the read attempt.
Serial.println("Reading AM2302 ...");
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds
float humidity = dht.readHumidity();
// Read temperature as Celsius (the default)
float tempC = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float tempF = dht.readTemperature(true);
// turn the red LED off indicating the end of the read cycle
digitalWrite(EMIT_RED_LED , LOW);
// Print the read values on the Serial Monitor.
Serial.print("Temperature (C): ");
Serial.println(tempC);
Serial.print("Temperature (F): ");
Serial.println(tempF);
Serial.print("Humidity (%RH): ");
Serial.println(humidity);
}
So before moving on, lets upload and test our code...
If everything is working, you should see one or more attempts to connect to you local Wi-Fi network and the successfully acquired network settings before EMIT resumes with it's temperature and humidity measurements.
Next up, we need to add an accurate Timestamp to EMITs data