We love the embedded world here at PubNub, and the Microchip PIC32 line of microcontrollers is no exception. With bidirectionally data streaming via the PubNub Data Stream Network, the Internet of Things use cases are endless.
What is Microchip and where does PubNub fit in?
Microchip manufactures microcontroller, memory and analog semiconductors. They offer a line of powerful 32-bit microcontrollers (their PIC32 line), for embedded projects, and a number of software tools for easy development and deployment. PubNub on the other hand streams the between connected devices, from 1-to-1 (unicast), or 1-to-many (broadcast).
Combining PubNub with Microchip, you can build real-time, cloud connected embedded systems, for example, remote control of devices, monitoring and data collection from a network of sensors, and home automation.
Now, onto the tutorial!
This tutorial will get you started with real-time data streaming for a Microchip microcontroller, facilitating real-time communication between our MCU and any other device (another MCU(s), mobile app, or web app). Overall, we’ll create a bidirectional flow of data between different endpoints.
Specifically in this demo, we’ll control LEDs using the Ethernet Starter Kit from a PC or mobile device, and send messages from the chip to our other devices.
Here’s the design:
MPLAB harmony is included to the IDE to simplify the development process for the PIC32 microcontrollers.
On your computer, install the following:
Including MPLAB Harmony Configurator within MPLAB IDE
MPLAB Harmony provides a MPLAB Harmony Configurator (MHC) MPLAB-X IDE plug-in that can be installed in MPLAB X IDE to help you create your own MPLAB Harmony applications.
To create a new MPLAB Harmony application with MHC, follow these steps:
To add PubNub PIC32 Harmony library to your project, you have several options:
In this tutorial, we’ll go with the first option. You’ll have to link against the Harmony library because that is what the PIC32 Harmony library uses. We use the TCPIP, TCPIP_DNS, SYS_TMR, and, optionally, TCPIP_SSL modules from Harmony.
`~/microchip/harmony/v1_03_01/apps`
`c:\microchip\harmony\v1_03_01\apps`
This will create following basic directory structure under apps:
pubnub_client
|
+--pubnub_pic32_client
|
+--lib
+--firmware
The lib directory contains the PubNub client Harmony (static) library. The firmware directory contains a sample project you can load to your MPLABX IDE and try it “out of the box”.
pic32mx_eth_sk : choose this if you're using PIC32 Ethernet Starter Kit
pic32mx_eth_sk2 : choose this if you're using PIC32 Ethernet Starter Kit II
pic32mz_ec_sk : choose this if you're using PIC32MZ EC Starter Kit
pubnub_pic32_client
, choose properties. Make sure the options chosen are as shown in the image below.The PubNub Harmony library has two APIs – static and dynamic.
By default, this demo runs the static version. But changing it from static to dynamic is as simple as linking:
In order to see the output of this demo, open the PubNub Developer Console, put in the same channel and the pub/sub keys as that in the code running in the IDE.
Note: You will find the channel and pub/sub keys in pubnubStaticDemo.c
or pubnubDemo.c
depending on whether you use the static or dynamic version.
If all went well, then after a few seconds, you will see constant messages from the MCU.
The MCU publishes messages (purposely set to do every few seconds), which is received by the console. Publish is broadcasting a message onto a specific channel. Options contains channel name, message, and callback values.
This is PubNub publish from the board to any other device.
You may send the following message:
{"led":{"2":1}}
to turn the LED number 2 ON.
So, in general, the first number (quoted) is the number of the LED (on most boards there are LEDS 0 – 2), the second is 0 to turn OFF, or 1 to turn ON.
So this should blink the LED 1:
{"led":{"1", 0}}
{"led":{"1", 1}}
{"led":{"1", 0}}
{"led":{"1", 1}}
This is the board subscribing to any other device.
As you can see, I have set the channel key, publish key and subscribe key. The ‘message’ box shows me the messages I have sent, and ‘history’ displays the messages sent and received on the channel
that I have set.
Using the ‘message’ box, I am able to send messages to the MCU to turn on and off the LEDs on the board itself.
And that’s it! We’re remotely controlling our Microchip MCU from a web UI (in this case, our Developer’s Console). Now, go forth and create powerful IoT applications!
PubNub runs on 70+ SDKs, letting you choose from a wide variety of platforms to communicate with the Microchip hardware. And if you need some more inspiration, check our all our real-time embedded tutorials here.
There are common underlying technologies for a dating app, and in this post, we’ll talk about the major technologies and designs...
Michael Carroll
How to use geohashing, JavaScript, Google Maps API, and BART API to build a real-time public transit schedule app.
Michael Carroll
How to track and stream real-time vehicle location on a live-updating map using EON, JavaScript, and the Mapbox API.
Michael Carroll