In this last tutorial, you will start collecting actual sensor data and utilize the internet connectivity to turn your Starter Kit into an IoT Edge Device.
Hook up with F1 Sensor Shield
from machine import Pin, I2C from lib import sht4x i2c = I2C(1, scl=Pin(16), sda=Pin(15), freq=100000) i2c.scan()
sht = sht4x.SHT4X(i2c) sht.heater_power = sht4x.HEATER20mW while True: temperature, relative_humidity = sht.measurements print(f"Temperature: {temperature:.2f}°C") print(f"Relative Humidity: {relative_humidity/100:.2%}") print("") time.sleep(0.5)
This will be the foundation of this data uploading tutorial.
The default main.py of the Starter Kit has handled the establishment of internet connection via LTE. Base on the default main.py, we will integrate the sensor data uploading functionality to main.py.
Code Change on main.py
Below the following code:
import LTE LTE=LTE.LTE()
Add import I2C and SHT30 sensor library:
from machine import Pin, I2C from lib import sht4x i2c = I2C(1, scl=Pin(16), sda=Pin(15), freq=100000)
After the following LTE connection handling code, add the measurement and sensor data reading loop:
To support data uploading to Ctrl platform, we need to import SGW Ctrl library:
from SGW import ctrl
Then in the measurement and sensor data reading loop, we use data uploading function (ctrl.signal) to replace terminal printing:
ctrl.signal("Temperature”, {temperature:.2f}) ctrl.signal("Relative Humidity”, {relative_humidity/100:.2})
To avoid overwhelming of data, we can change the sleep time to 10 seconds:
time.sleep(10)
Verification of sensor data on Ctrl platform
This is the end of the beginner tutorial, please visit doc.F1.sgwireless.com to look for more advanced tutorial which will cover Wi-Fi configuration, LoRa, BLE (Device and Gateway) and more.