Have you been eyeing Infineon's PSoC6 board, but are intimidated by its steep learning curve? Don't worry, you're not alone! We know it can be a real head-scratcher. But fear not, we have a solution: MicroPython. With its easy-to-use interface that's reminiscent of python, MicroPython makes programming the PSoC6 board a breeze. Discover the effortless way to interface Infineon's PAS CO2 sensor with PSoC6 board and stream live data to cloud platform for powerful insights!
OverviewThe idea is simple: grab an Infineon PAS CO2 sensor, connect it to the CY8CPROTO-062-4343W board, and run a micropython code to read the sensor data. Then, stream it to the Adafruit cloud and voila - you'll have visualization at your fingertips!
Hardware SetupConnect the USB cable to your computer and the micro USB to the board debugger. Interface the sensor to the board by connecting the I2C lines.
Pin Connections
The diagram below shows how to connect the sensor to the board.
The first and foremost thing you require is flashing the MicroPython binary to your board. The provided utility script will take care of the setup and all you need is a couple of commands!
1. Open a terminal and run the following command
curl -s -L https://raw.githubusercontent.com/infineon/micropython/ports-psoc6-main/tools/psoc6/mpy-psoc6.py > mpy-psoc6.py
2. Make sure you have a recent version on Python3.x installed and the pip package installer. Then install the following packages:
pip install requests
3. Here, we will flash the firmware to the device. Please keep your board connected while you type in:
python mpy-psoc6.py device-setup
If everything went fine, you will see the following printed on your screen
You can find more details on how to flash and other possibilities in the official MicroPython port documentation and in hackster article.
Cloud setupHere we are using the Adafruit IO for the cloud visualization.
1. Create an Adafruit IO account if you don't have one already.
2. After setting up the account go to feeds tab and create a new feed where you will visualize your sensor data.
Refer here for more related to Adafruit IO setup.
Let's get this running now!
1. Open Thonny IDE and click on 'Tools'. Select Options and you should already find the COM port where your board is connected.
2. Download the provided application code and secrets file (found below). Open the folder where you have them downloaded in Thonny. Open the secrets file and configure your network and cloud details. Use the below explanation, to find what each field means here:
Once you have made the changes, save it and upload both the files to board.
3. Run the code and voila - you should already see the CO2 values being printed on the terminal as well as in your AdafruitIO feed.
The main application itself looks simple and make calls to relevant functions. It follows the following order:
1. Import necessary modules.
2. Connect your board to the pre-configured network.
3. Install dependent modules. MicroPython has an inbuilt tool "mip" (Yes! Similar to "pip") that allows to install any packages directly to your board. How cool is that right! All you need to do is:
def install_deps():
''' Helper function to install all required modules '''
mip.install("umqtt.simple")
mip.install("https://raw.githubusercontent.com/jaenrig-ifx/micropython-lib/mpy-lib/pasco2-sensor-module/micropython/drivers/sensor/pasco2/pasco2.py")
It is not mandatory that you have to install packages to board this way. There are many other ways to do so as listed in MicroPython docs.
3. Make a connection to the IoT cloud database (AdafruitIO here). You can ideally do this to any other cloud IoT platform you can think of!
def database_connect() :
''' Helper function to connect to IoT Cloud platform'''
from umqtt.simple import MQTTClient
client = MQTTClient(client_id=b"Psoc6",
server=b"io.adafruit.com",
port=0,
user=s.username,
password=s.adafruitIOKey,
keepalive=7200,
ssl=True,
ssl_params={'server_hostname':'io.adafruit.com'}
)
status = client.connect()
if not status:
print("[db-module] : Connection to adafruit.io successful!")
return client
4. Initialize PAS CO2 sensor. This runs the sensor in continuous mode and returns a handle to the sensor class.
def sensor_init():
''' Helper function to intialize sensor
Args: None
Returns: co2 object if success, else -1
'''
import pasco2 as sensor
bus = I2C(0)
pasco2 = sensor.PASCO2(bus)
init_status = pasco2.initialize()
if init_status != 0:
print("[co2-module] : Sensor setup failed!")
return -1
else:
print("[co2-module] : Sensor setup done successfully!")
return pasco2
5. Finally, stream the sensor data to cloud platform using MQTT.
def read_sensor_data_and_stream_to_cloud(co2Obj, dbObj):
''' Helper function to read sensor data and stream to cloud DB
Args : co2Obj - Sensor Object
dbObj - Database Object
Returns: None
'''
# wait for the value to be ready
sleep(10)
#get co2 values
co2ppm = co2Obj.get_co2_value()
if co2ppm == -1:
print("[co2-module] : Measurement not ready yet")
else:
print("[co2-module] : co2 ppm value: ", co2ppm)
# Stream data to IoT cloud
dbObj.publish(s.pubTopic,b'{"value":' + str(co2ppm) + '}')
With that, you have the code ready!
Got better ideas? Tweak the code, build your own applications and do let us know how it goes!
Got Problems with MicroPython for PSoC6?Leave us a note here and we will do all it takes to fix! 🚀
Curious for more?Find additional information related to the PSoC MicroPython enablement here.
You can find the complete project here.
Comments
Please log in or sign up to comment.