4. Connecting EMIT to the Internet - 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!
4.1 Connecting to the Internet via Wi-Fi
The first thing we need to do is import the 'network' module by adding the following line to boot.py.
import network
This module 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 boot.py in exactly the same way as we did the Red LED:
GreenLED = machine.Pin(17, machine.Pin.OUT)
GreenLED.value(0)
Next, we need to add your Wi-Fi's SSID & Password as variables to boot.py.
# define Wi-Fi settings
wifiSSID = 'your-WiFi-SSID-goes-here'
wifiPasswd = 'your-WiFi-password-goes-here'
And finally, we need to define a function that will connect to our Wi-Fi using the settings we've just defined. We'll call this function 'wifi_connect()'.
We define the function like this:
def wifi_connect():
Remembering that all code within this function must be indented correctly - again the Thonny IDE will help you with this.
The first thing we need to do within our 'wifi_connect()' function is to define our network object, which we will call 'wifi' and turn on the Wi-Fi hardware within the ESP32. This is done like this:
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
We then need to create a loop that will check to see if the wifi is connected using the network modules '.isconnected()' function like this:
while wifi.isconnected() == False:
If it is not connected, then (i.e. isconnected() == False) we need to attempt to connect to the network using the '.connect(wifiSSID, wifiPasswd)' funtion like this:
wifi.connect(wifiSSID, wifiPasswd)
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 Shell window 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:
print(wifi.ifconfig())
The full function, heavily commented to clarify how it all works, should look something like this:
# define function for setting up Wi-Fi network
def wifi_connect():
wifi = network.WLAN(network.STA_IF) # create our 'wifi' network object
wifi.active(True) # turn on the Wi-Fi hardware
# if not connected ...
while wifi.isconnected() == False:
GreenLED.value(1) # turn Green LED ON
print('trying WiFi connection ', wifiSSID) # print 'trying..' message to Shell
wifi.connect(wifiSSID, wifiPasswd) # try connecting to wifi
time.sleep(1) # wait 1 second
GreenLED.value(0) # turn Green LED OFF
time.sleep(2) # wait 2 second
# if connected ...
GreenLED.value(1) # turn Green LED ON
print('WiFi connection successful') # print 'WiFi connection successful' message to Shell
print(wifi.ifconfig()) # print WiFi network settings (inc. IP Address) to Shell
With this new function integrated, the completed boot.py should now look like this:
# general board setup
import machine, time, dht
import network
# configure GPIO pins
RedLED = machine.Pin(16, machine.Pin.OUT)
RedLED.value(0)
GreenLED = machine.Pin(17, machine.Pin.OUT)
GreenLED.value(0)
AM2302 = dht.DHT22(machine.Pin(14))
# define Wi-Fi settings
wifiSSID = 'your-WiFi-SSID-goes-here'
wifiPasswd = 'your-WiFi-password-goes-here'
# define function for setting up Wi-Fi network
def wifi_connect():
wifi = network.WLAN(network.STA_IF) # create our 'wifi' network object
wifi.active(True) # turn on the Wi-Fi hardware
# if not connected ...
while wifi.isconnected() == False:
GreenLED.value(1) # turn Green LED ON
print('trying WiFi connection ', wifiSSID) # print 'trying..' message to Shell
wifi.connect(wifiSSID, wifiPasswd) # try connecting to wifi
time.sleep(1) # wait 1 second
GreenLED.value(0) # turn Green LED OFF
time.sleep(2) # wait 2 second
# if connected ...
GreenLED.value(1) # turn Green LED ON
print('WiFi connection successful') # print 'WiFi connection successful' message to Shell
print(wifi.ifconfig()) # print WiFi network settings (inc. IP Address) to Shell
As we've done all the hard work in our boot.py, all we need to do in our main.py is call the wifi_connect() function that we just created with the line:
wifi_connect()
With this added, our main.py file should look like this:
# connect to wifi
wifi_connect()
# this is the main program loop
while True:
time.sleep(5) # wait 5 seconds
RedLED.value(1) # turn RedLED ON
print("Reading AM2302 ...")
AM2302.measure() # start AM2302 measurement
RedLED.value(0) # turn RedLED OFF
tempC = AM2302.temperature() # get temperature (Celsius) from AM2302
humidity = AM2302.humidity() # get humidity from AM2302
tempF = (tempC * 9/5) + 32.0 # convert Celcius result into Fahrenheit
print("Temperature (C): " + str(tempC))
print("Temperature (F): " + str(tempF))
print("Humidity (%RH): " + str(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