Infineon Team
Published © MIT

Breaking the Programming Barrier: CO2 + PSoC and MicroPython

Overwhelmed by PSoC6 board complexity? Micropython's user-friendly interface makes it easy to stream and visualize sensor data on cloud.

BeginnerFull instructions provided2 hours587
Breaking the Programming Barrier: CO2 + PSoC and MicroPython

Things used in this project

Hardware components

CY8CPROTO-062-4343W
Infineon CY8CPROTO-062-4343W
×1
XENSIV™ PAS CO2 Shield2Go Board
Infineon XENSIV™ PAS CO2 Shield2Go Board
×1

Software apps and online services

MicroPython
MicroPython
Adafruit
Thonny IDE

Story

Read more

Schematics

Fritzing Schematics

Fritzing schematics

Code

Application code

MicroPython
'''
Micropython demo application for PSoC6 which does the following:
- Continuously record co2(ppm) values
- Connects the on-board WiFi chip to network
- Stream the co2 data to IoT cloud platform - AdafruitIO

*Note: network and db configurations should be done by user in secrets.py
       file before trying out this code
'''
from machine import I2C
from utime import sleep,sleep_ms

import network
import secrets as s
import mip

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")

def network_connect() :
    ''' Helper function to connect to network '''
    wlan = network.WLAN(network.STA_IF)
    if wlan.isconnected():
        print('[network-module] : Already connected')
        return
    # enable and connect wlan
    wlan.connect(s.ssid,s.key)
    # wait for connection to establish
    sleep(5)
    if not wlan.active():
        print("[network-module] : Connection failed.Try again!")
        return

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

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
        
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) + '}')
    
def main():
    ''' Application Main '''
    print("*** Micropython Demo for Infineon's PAS CO2 sensor ***\n")
    # Connect to network
    network_connect()
    # Install dependencies
    install_deps()
    # Connect to cloud platform
    dbObj = database_connect()
    # Initialize sensor
    co2Obj = sensor_init()
    
    sleep(1)
    
    if co2Obj != -1:
        while True:
            #Read sensor value and stream to IoT cloud
            read_sensor_data_and_stream_to_cloud(co2Obj, dbObj)
        
                
if __name__ == "__main__":
    main()

Secrets

MicroPython
User configuration file to connect to network and cloud
# Add user configuration details

ssid 	 = '<Your WiFi SSID>'
key 	 = '<Your WiFi Password>'
username = '<Your Adafruit account Username>'
adafruitIOKey = '<Your Adafruit_IO Key>'
pubTopic = '<your_adafruitIO_username>/feeds/<feed_name>' 

Credits

Infineon Team

Infineon Team

77 projects • 117 followers

Comments