PubNub recently revamped its C-Core SDK’s you can find the details here on our GitHub page. It provides updated support for newer chips including Espressif, notably ESP32S3 chipsets!
PubNub has long been a champion of the IoT space. We have a number of customers who use us to deliver sensor data to & from factories, farms and fields. Creating digital twins of buildings, spaces and events for a while now.
With access to IoT chips readily available and incredibly cheap. You can build some amazing IoT projects powered by PubNub.
One recent project that I did using an ESP32 chip & PubNub was to manage the turning on & off of a ground source heat water pump.
Here’s some brief details on the project
The pump needs to function automatically, turning on at 11pm and off at 8am daily.
An ‘On’ Switch needs to be included so that a human can manually turn on the pump.
A temperature sensor needs to be included, when the temperature drops below 1C then turn on the pump.
Using PubNub, I want to be able to log when the last on/off event occurred
Using PubNub, I want to be able to turn on & off the pump
Here’s a small circuit diagram
(I'm aware this is an Arduino UNO Chip, for some reason the schematic software I use couldn't make an ESP chip look this good!)
Breaking down the circuit
Black - Ground
Red - 5 Volt
Orange - Temperature input sensor
Green - Switch / Button input
Yellow - Relay activate output
Red/Green/Blue - Tricolor LED.
Brown & Purple - AC Lines that power the pump.
Playing with high voltage electricity is very dangerous and you should take all necessary precautions, consult a qualified electrician as required.
Here's the physical hardware:
With the hardware now complete we can turn to the software component. With ESP32 chips you can use Arduino IDE or equivalent to write and upload the code to the chip.
The Libraries used in this script are:
WiFi.h
WiFiUdp.h
NTPClient.h
TimeLib.h
OneWire.h
DallasTemperature.h
PubNub.h
WiFi Libraries are used to connect to a Wifi network and manage that connection. NTP and TimeLib are used to get and maintain the current time from an NTP Server. OneWire and DallasTemperature are used to get the temperature from the thermal probe. PubNub is used to publish data to PubNub and separately subscribe to a channel ready to receive data and/or commands.
Here's the initial code:
If you're copying this, make sure you update the code to represent your pin numbers, pump on/off times, wifi credentials and UTC time.
Once we have declared all of our variables we can compile our setup loop.
Looking at what goes on in the setup loop;
A serial connection is created
The chip connects to the wifi
Once a connection is established, we connect to an NTP Server and obtain the local time
The setup for the temperature sensor takes place
The setup for the input and output pins takes place
This is the main loop code:
At the top of the function we get the current time, read the temperature value and check to see if the manual switch is on.
Once we have all the required inputs we can determine what state the relay should be in.
Adding PubNub to the mix
As I mentioned in the beginning, I want to use PubNub to both log the on/off events and also trigger a remote activation of the pump.
You can find the the new SDK here: https://github.com/pubnub/c-core
With the library installed we can setup our connection to PubNub.
With sending data to PubNub you need a few things:
Pub and Sub keys, these can be found in your PubNub Console at: https://admin.pubnub.com
Channel to send messages to, you can find a doc on channel design here: https://www.pubnub.com/docs/general/channels/overview
JSON Message Object, as part of our docs you can read all about how to send structured data over PubNub; https://www.pubnub.com/docs/general/messages/type
These changes allow the script to connect to PubNub and receive a json message {"type": "action", "command": "on"} which will activate the pump.
Finally lets add in the logging so that when the pump is turned on or off, it publishes a message to PubNub letting us log and track when the pump is turned on:
If you need to track what state the pump is, you can jump into your admin console at: https://admin.pubnub.com and navigate to the debug page:
Closing Out
PubNub is a great use case for the control and utility monitoring of IoT Devices. With realtime messaging at the core of sending information to/from a device you can quickly build a stable and fast network that lets you monitor and interact with systems, devices, pumps and digital twins.
If you would like to know more about how PubNub can help you with your IoT projects please reach out, our teams would love to help.
Sign up for a free account today, or discuss your needs with one of our solution experts.