iot Connecting with Other Devices¶
The iot module provides a collection of classes to connect to WiFi and exchange data with other devices, such as Raspberry Pis.
WiFi¶
This class can be used to connect the Wio Link board to WiFi.
-
class
iot.WiFi(ssid, password)¶ Stores the WiFi
ssidandpassword, so when theconnect()method is called, the Wio Link board will connect to the specified WiFissidwith thepassword.Note
After WiFi is successfully connected, the Wio Link board will attempt to access the same WiFi at each subsequent boot. You can click the “Soft Reset” button to get the terminal to work again.
-
WiFi.connect()¶ When the
connect()method is called, the Wio Link board will connect to the specified WiFissidwith thepasswordspecified when object is created.
-
Example:¶
# Connects to a WiFi access point
# with SSID starbucks
# and Password 12345678
from iot import WiFi
wifi = WiFi(ssid="starbucks", password="12345678")
wifi.connect()
Node-RED¶
This class can be used to send data to any device that runs Node-RED, such as Raspberry Pis, or even a Node-RED instance running on the cloud, provided the IP address of the hosting machine is known.
-
class
iot.NodeRed(ip[, port=1880])¶ Stores the
ipaddress (or domain) of the machine running Node-RED. If Node-RED runs on a differentportthan1880, theportparameter can be used to specify the port.-
NodeRed.send_http(url, data, debug[=True])¶ Send the specified
datato the http-in node in the Node-RED flow. theurlparameter needs to be exactly the same as theURLfield of the http-in node (shown below).datacan either be a number, a string, or a dictionary. By default, if the data is sent successfully, the program will prompt"Data sent!. If that is undesirable, you can turn it off by settingdebugtoFalse.
-
Example:¶
# Connects to a WiFi access point
# with SSID starbucks
# and Password 12345678
from iot import WiFi, NodeRed
from sensors import TemperatureSensor
import time
wifi = WiFi(ssid="starbucks", password="12345678")
wifi.connect()
# Temperature Sensor is connected to Port 3
temp_sensor = TemperatureSensor(3)
# Node-RED is hosted on a machine with IP 192.168.1.123
# It has a http in node configured with URL /test
node = NodeRed(ip="192.168.1.123")
# Sends temperature and humidity data to Node-RED every 10 seconds
while True:
t = temp_sensor.get_temperature()
h = temp_sensor.get_humidity()
data_dict = {
"temperature": t,
"humidity": h
}
node.send_http(url="/test", data=data_dict)
time.sleep(10)
Connecting to BC Server¶
-
class
iot.BcServer¶ There is a dedicated server set up at Boston College with a Node-RED instance running for data collection purposes. To use the server, you can directly use this class. No parameters is needed to set it up.
-
BcServer.send_http(data, debug[=True])¶ Specify the
data, either aJSONor aString, and this method will send the data directly to the BC server. No need to specify IP or URL. By default, if the data is sent successfully, the program will prompt"Data sent!. If that is undesirable, you can turn it off by settingdebugtoFalse.
-