6 Oct 2024

Python IoT: Sensors, Controllers & Devices with Pycom and Adafruit

In recent years, the Internet of Things (IoT) has become an increasingly popular field of study and development. IoT involves the connection of devices to the internet, enabling them to exchange data and interact with each other. Python, as a versatile and easy-to-learn programming language, has become a popular choice for developing IoT applications. In this blog, we will explore how to use Python with two popular IoT hardware platforms, Pycom and Adafruit, to build IoT applications that involve sensors, controllers, and devices.

Pycom

Pycom is a company that produces a range of IoT hardware devices that can be programmed using MicroPython, a version of Python that has been optimized for use on microcontrollers. Pycom devices are designed to be low power, compact, and easy to use, making them ideal for use in IoT applications.

Sensors

Sensors are devices that can detect and measure physical properties such as temperature, humidity, light, and motion. Pycom devices can be used with a range of sensors to collect data for use in IoT applications. To use a sensor with a Pycom device, you will need to connect it to one of the device's input/output (I/O) pins. The specific pins you use will depend on the type of sensor you are using.

For example, to connect a temperature sensor to a Pycom device, you might use a pin such as Pin.G17. Once the sensor is connected, you can use MicroPython to read data from the sensor. For example, to read the temperature from a connected sensor, you might use code like the following:

from machine import Pin
from machine import ADC

temp_pin = Pin('G17')
adc = ADC(0)
reading = adc.read()

voltage = reading * 3.3 / 4095
temperature = (voltage - 0.5) * 100
print("Temperature:", temperature, "°C")

In this example, we are using the machine module to create a Pin object for the pin connected to the temperature sensor. We are then using the ADC (analog-to-digital converter) module to read the analog signal from the sensor and convert it to a digital value. We then convert the digital value to a voltage value and use that to calculate the temperature.

Controllers

Controllers are devices that can be used to control other devices, such as motors, lights, and switches. Pycom devices can be used as controllers by connecting them to other devices using I/O pins. To control a device, you will need to send signals to the device using the Pycom device's I/O pins.

For example, to control a motor using a Pycom device, you might connect the motor to a pin such as Pin.G22. You can then use code like the following to control the motor:

In this example, we are using the machine module to create a Pin object for the pin connected to the motor. We are then setting the pin mode to OUTPUT and using the value method to send a signal to the motor to turn it on.

Devices

Devices are devices that can be connected to the internet to exchange data with other devices. Pycom devices can be used as devices by connecting them to the internet using Wi-Fi or other wireless protocols. Once connected to the internet, Pycom devices can use protocols such as MQTT or HTTP to exchange data with other devices.

For example, to connect a Pycom device to a Wi-Fi network, you might use code like the following:

import network

ssid = 'my_wifi_network'
password = 'my_wifi_password'

sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.connect(ssid, password)

while not sta_if.isconnected():
    pass

print("Connected to", ssid)

In this example, we are using the network module to connect to a Wi-Fi network. We are creating a WLAN (wireless local area network) object and setting it to active mode. We are then using the connect method to connect to the specified Wi-Fi network. We are also using a while loop to wait until the device is connected to the network before continuing.

Adafruit

Adafruit is a company that produces a range of IoT hardware devices that can be programmed using CircuitPython, a version of Python that has been optimized for use on microcontrollers. Adafruit devices are designed to be easy to use and beginner-friendly, making them ideal for learning about IoT.

Sensors

Adafruit devices can be used with a range of sensors to collect data for use in IoT applications. To use a sensor with an Adafruit device, you will need to connect it to one of the device's pins. The specific pins you use will depend on the type of sensor you are using.

For example, to connect a temperature sensor to an Adafruit device, you might use a pin such as board.A1. Once the sensor is connected, you can use CircuitPython to read data from the sensor. For example, to read the temperature from a connected sensor, you might use code like the following:

import board
import adafruit_analog_temperature_sensor

temp_pin = board.A1
sensor = adafruit_analog_temperature_sensor.AnalogTemperatureSensor(temp_pin)

temperature = sensor.temperature
print("Temperature:", temperature, "°C")

In this example, we are using the board module to create a Pin object for the pin connected to the temperature sensor. We are then using the adafruit_analog_temperature_sensor module to create an AnalogTemperatureSensor object for the sensor. We are then using the temperature attribute to read the temperature from the sensor.

Controllers

Adafruit devices can be used as controllers by connecting them to other devices using pins. To control a device, you will need to send signals to the device using the Adafruit device's pins.

For example, to control a motor using an Adafruit device, you might connect the motor to a pin such as board.D5. You can then use code like the following to control the motor:

import board
import digitalio
import time

motor_pin = digitalio.DigitalInOut(board.D5)
motor_pin.direction = digitalio.Direction.OUTPUT

motor_pin.value = True
time.sleep(2)
motor_pin.value = False

In this example, we are using the board module to create a Pin object for the pin connected to the motor. We are then setting the pin direction to OUTPUT and using the value attribute to send a signal to the motor to turn it on. We are also using the time module to pause the program for 2 seconds before turning the motor off.

Devices

Adafruit devices can be connected to the internet using Wi-Fi or other wireless protocols. Once connected to the internet, Adafruit devices can use protocols such as MQTT or HTTP to exchange data with other devices.

For example, to connect an Adafruit device to a Wi-Fi network, you might use code like the following:

import board
import busio
from adafruit_esp32spi import adafruit_esp32spi
import adafruit_esp32spi.adafruit_esp32spi_socket as socket

spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, board.D10, board.D11, board.D12, board.D13, board.D9)
esp.reset()

print("Connecting to Wi-Fi...")
while not esp.is_connected:
    try:
        esp.connect_AP("my_wifi_network", "my_wifi_password")
    except RuntimeError:
        print("Failed to connect, retrying...")
        continue

print("Connected to Wi-Fi!")

In this example, we are using the board and busio modules to create a SPI object for communicating with the Adafruit ESP32-SPI module, which is used for connecting the Adafruit device to a Wi-Fi network. We are then creating an ESP_SPIcontrol object for the module and resetting it. We are using a while loop to repeatedly attempt to connect to the specified Wi-Fi network using the connect_AP method until the device is connected. Once the device is connected, we print a message indicating that the device is connected to Wi-Fi.

Conclusion

Python is a versatile and easy-to-learn language that is well-suited for use in IoT applications. By using Python with Pycom and Adafruit devices, you can create powerful and flexible IoT solutions that can collect data, control devices, and communicate with other devices over the internet. Whether you are a beginner or an experienced developer, Python IoT with Pycom and Adafruit can help you to bring your IoT ideas to life.

You may also like

Python for Robotics: Controlling Robots with Python

Python has emerged as a popular language for controlling robots due ...

Continue reading

Mastering Python Networking - Python Networking Sockets http and More

Mastering Python networking - we provide an introduction to Python n...

Continue reading

Python Robotics Control Perception Navigation With Ros and Pyrobot

Python Robotics- Use of Python in Robotics, we are specifically usin...

Continue reading