After its success at Embedded World 2019, it’s time to explain the whole “Python on PSoC® 6 microcontrollers” demo in detail.
Simply follow all the steps in this tutorial, and you’ll recreate the whole demo. And for those of you who are hearing about this for the first time, here’s a bit of an explanation:
Two Cypress PSoC 6 boards sending data to both AWS and Ethereum blockchain. Of course, there are two Cloud dashboards, and lots of Python too. You can read our post about when we presented the demo at Embedded World and find out more.
As we said, you can use up to two Cypress boards for this demo.
To get started and play with your microcontrollers just follow these simple steps:
- Download and Install Zerynth Studio
- Connect, Register and Virtualize your board. More info here.
- Click on Project > New, and fill the “Clone from GitHub” field with this url: https://github.com/zerynth/demo-ew19-firmware.
Once done, you will see three Python files, containing our code, and JSON file when AWS credentials are defined.
For preparing and sending messages to AWS IoT, the following steps must be followed:
- A Thing has to be registered in the AWS IoT registry
- credentials, in the form of a private key and related certificate, have to be generated, activated and attached;
- a policy has to be defined and attached to the generated certificate.
Once done, you will be able to fill the fields of
thing.conf.json file with your Thing information (double-check all fields!), and storing the certificate in the project directory with names
Setting up an Ethereum account
As already done in this tutorial regarding a blockchain application using simple Python directly on microcontrollers, we are going to use Infura, a free and easy to setup Ethereum RPC server that will act as an HTTP gateway between the device and the Ethereum blockchain. If you don’t have one already, create an account on their website: https://infura.io/register.
Once you’re logged in, create a new project and note its Endpoint for the Ropsten network, fill this URL in the RPC_URL variable in the
We will also need three Ethereum address, one for the dashboard – which will receive transactions and show them, and one for each board to send data from and identify them.
For creating them, we’ll use Metamask:
- Install the MetaMask browser extension from https://metamask.io/
- Choose the Ropsten test network from the top-right corner (instead of the main network).
- Click on the round top-right icon of your profile from the Metamask main view. You can create as many accounts as you want there.
Make three accounts, and select one.
- Request your first Ether from https://faucet.metamask.io/, since we are using the test network they have no real monetary value. We need one address with some Ether for each of the boards, so you have to switch to a second account from Metamask and request some Ether from there too if you plan to run two different boards.
- Export and note your Ethereum private key from MetaMask pressing the three lines menu button, Details, and finally Export private key (you will be prompted for the password you created when you installed MetaMask). Again, do this for the two accounts if you are using two boards.
- Store the address and key in the
eth.py file. After flashing the first board, you’ll need to change them before flashing the second board – we’ll get to this soon.
As a final touch, edit the
config.py with some basic settings: your Wi-Fi SSID, your Wi-Fi password, and the “stand id” which was used to identify which board was being used.
Now, all files are set up, and we are ready to uplink the project through Zerynth Studio.
After that, your Cypress boards should be up and running, sending environmental data every 5 seconds – and Blockchain transactions on CapSense™ touch.
Running the dashboard
If you also want to run our web interface, there are some additional steps:
- Install Node.js on your computer
- Clone this GitHub repo.
- Copy the same certificate files used in the AWS Connection section to
- Open a terminal/command prompt in the repository folder and run:
$ cd backend
$ npm install
$ node index.js
This should be enough to have a backend which will subscribe to AWS MQTT and forward data via websocket to the front-end application.
Do not close this terminal window.
- Open another terminal in the repository folder and run:
$ cd frontend
$ npm install
$ npm run dev
This may keep a while. Once done, you can open http://localhost:3000 in your browser and see the demo up and running!