If you have one of these PSoC 6 MCUs with WiFi capabilities (CY8CPROTO-062-4343W & CY8CKIT-062S2-AI) and let's say, you have build some embedded projects with Micro Python. Now you want to monitor your system remotely from phone. You want to know when something happens ( i.e. motion detected, temperature changed, liquid level changed or any sensor events ), then this project is for you !
Please note that, if you are looking for remote controlIoT solution(controlling your hardware remotely), then this is not the right project for you, this is only for
monitoring events with real time notifications
This project is a walkthrough only for PSoC6 WiFi with Micro Python. The installations are done on Windows 10/11 PC and Android 10+ Phone/Tablet
*****************************************************************************************
(Note: If you are using ModusToolBox for programming in C, search for WiFi example with HTTP Post requesting JSON format from client to server. Then modify it with example codes from Pushover or use Avnet IoT Connect)
If you already have PSoC6 ready for MicroPython, skip step 0, 1, 2 and start from step 3
Step 0: Get PythonDownload the latest version of Python and install if you don't have Python installed on your host PC. I am using Python 3.13 pre release here.
Important:
- Install Python to the default directory
- Make sure to add PATH to python.exe
- Install at least version 3.x (latest recommended)
Download this python code editor Arduino Lab for Micro Python. We will need this later for testing, editing, uploading Python codes on PSoC6
First, Connect PSoC™ 6 Wi-Fi BT Prototyping Kit (CY8CPROTO-062-4343W) board to PC with USB like this:
Some serial communication drivers will get installed automatically
( if not follow MiniProg3 user guide here or here and install driver manually)
Next, Open a Command Prompt terminal
Now check if python installed correctly
python --version
If python is working on host PC, lets install Micro Python on PSoC 6
Installing MicroPython
To install the MicroPython PSoC6™ port, use the provided `mpy-psoc6.py` Python script, compatible with Windows, Linux, and macOS.
You can easily download the script via the terminal using the following command:
curl -s -L https://raw.githubusercontent.com/infineon/micropython/ports-psoc6-main/tools/psoc6/mpy-psoc6.py -o mpy-psoc6.py
Make sure you have a recent version of Python 3.x and the `pip` package installer installed. Then, install the required package:
pip install requests
Device Setup
To set up MicroPython on a PSoC6™ board, use the `device-setup` command from the `mpy-psoc6.py` utility. Follow the prompts to select your target PSoC6™ board and install the latest MicroPython firmware:
python mpy-psoc6.py device-setup
You can run this command any time you want to upgrade to the latest MicroPython firmware. The command will handle the following steps:
- 1. Download and install OpenOCD, the software required for deploying firmware on PSoC6™ controllers.
- 2. Download the latest `.hex` file for your chosen board.
- 3. Deploy the latest version of MicroPython firmware on your board.
- We will select 0 for CY8CPROTO-062-4343W board and press enter.
- If you are trying this on CY8CKIT-0620S2-AI board, then press 2
- The option 1 board only have BLE and no WiFi
Check this link for understanding more
Step 3: Signup for Push Notification on PushoverGo to https://pushover.net/signup and create a new account with your email address and a new password. Remember this email and password for logging in from Android/iOS app later
You will receive an email with verification link. Complete your account and login to Pushover
Step 4: Download Pushover App on Android/iOSDownload the Pushover app on your Android or iOS device, then login with the email and password of your newly created pushover account
After logging in, give your device a name (phone's name), this name will reflect on your Pushover account. Device name can be helpful when you have multiple devices and multiple projects but you want specific notifications to be send to certain devices only.
Log into your newly made Pushover account from any web browser.
Find and copy the Unique User Key string for your account, you will need this in the coding. This AlphaNumeric string is unique only for your account
Scroll down and look into the Your Devices section, each device you add to your account will show up here. Earlier, we added Xiaomi_5 as device from the Android app, which is visible here
Scroll down to Your Application section, here is where you can create an API Token for your embedded application. Each application can send up to 10000 messages per month.
Now, click on "Create an Application/API Token" to give your new application a name, agree to the T&C and proceed.
Now you will receive an unique API Token Key for your application, which is an AlphaNumeric string. Copy and save it, we will need this string in Python programming.
Important summery: From your Pushover account you get 2 things:
you will receive some strings, something like these (not these ofc)
- User Key string : sdhf89weurfw9ur923jri34r03
- API Token string: 394rwfh34r38r34t8wer84tjksfj
These two pieces of information will let your hardware communicate with Pushover server and send notification to your Pushover app
Before programming PSoC6 in Micro Python, you will need 3 more things:
- Your WiFi Network name (WPA2, 2.4 GHz)
- Your WiFi's Password
- Notification Message you want to send from PSoC6
Launce Arduino Lab for MicroPython, which we have downloaded from step 1
Connect PSoC6 board to your PC with USB cable
Click on the connect icon (as shown in the image below) and select the Com port to connect PSo6 to the Arduino Lab
Now, copy paste the python code attached below and save as main.py on your board
Make sure to modify my code with your 5 information before saving the code into PSoC6
Your Pushover Account's User Key string
Your Pushover App's API Token string
Your Home/Collage/Office WiFi Network name
Your WiFi's Password
Your Notification Message String
Now, reboot the board and let it connect to your wifi network
Code Explained
Adding necessary modules to the code:
import network
import time
from machine import Pin
from utime import sleep,sleep_ms
Connect to WiFi network and wait a bit for connection process to complete:
wlan = network.WLAN(network.STA_IF)
wlan.connect('YourWifiNetwork','YourWifiPassword')
time.sleep_ms(8000)
Setup IO for LED and Button to show Indication and receive user Input:
BLUE_BTN = Pin('P13_2', mode=Pin.IN, pull=Pin.PULL_UP)
BLUE_LED = Pin('P13_1', mode=Pin.OUT, value=0)
Let user know if WiFi connection is successful with blinking LED:
while wlan.isconnected()==False:
BLUE_LED.low()
#wait
if wlan.isconnected():
BLUE_LED.high()
time.sleep_ms(200)
BLUE_LED.low()
time.sleep_ms(200)
Add Interrupt and function on push button:
BLUE_BTN.irq(handler=lambda t:push_message(),trigger=Pin.IRQ_FALLING)
Install urequest for HTTPs services on micro python
import mip
mip.install('micropython-urequests')
import urequests as requests
Define a function that will take the Pushover User Key, App Token and Notification Message and send whenever button is pressed by user:
def push_message():
BLUE_LED.high()
payload = {
"token": "Your_Pushover_App_Token",
"user": "Your_Pushover_User_Key",
"message": " Your_Message (PSoC6 Fire on Blue Button District!!) ",
}
r = requests.post("https://api.pushover.net/1/messages.json", json=payload)
time.sleep_ms(1000)
BLUE_LED.low()
(Note : Button needs debouncing)
Here is a demo project showing the reception of notification from PSoC6 hardware to my android device:
This idea is an IoT summon button for sending notification to Fire Fighters from PSoC6 mcu in the event of a fire occurrence somewhere
Check out the video demonstration showing how it works:
Reference Links
Comments