Build

Connect Arduino to PubNub in 2 Steps

2 min readPubNub Staff on Jun 25, 2014
Try PubNub Today

Free up to 1MM monthly messages. No credit card required.

Subscribe to our newsletter

By submitting this form, you are agreeing to our Terms and Conditions and Privacy Policy.

Get your Arduino up and running with the PubNub Data Stream Network for signaling and data transfer in two easy steps.

Arduino is a microcontroller and development environment that can be used, just like Raspberry Pi, to read information from its sensors and control other devices. Programming on the Arduino is extremely simple, which has led to its widespread use in interactive environments.

As with the Raspberry Pi, it’s a great (and low cost) starting point for experimenting with the world of embedded wearables and the Internet of Things.

Arduino

This blog walks you through the steps to connect an Arduino to the PubNub Data Stream Network. This enables you to connect with a high volume of other devices at a global scale and start sending and receiving data between a wide variety of devices, including other Arduino and Raspberry Pi devices. PubNub acts as your communication layer for signaling and data transfer between embedded devices.

You can find the Arduino PubNub extensions here. For additional resources, check out the PubNub Arduino documentation page.

Basics of Connecting the Arduino to the PubNub Data Stream Network

Really, all it takes is two steps to get your Arduino up and running. Follow these steps to get connected:

STEP 1: Connect the Arduino to a monitor, keyboard, mouse and ethernet cable.

STEP 2: You’ll need to sign up for a PubNub account. Once you sign up, you can get your unique PubNub keys in the PubNub Developer Portal. Be sure to input your unique publish/subscribe keys!

Open a new window and paste the following code.

The above code lets you set the following :

  • publish key using “pubkey”
  • subscribe key using “subkey”
  • UUID using “uuid”
  • channel you wish to subscribe and publish to using “channel”

For all of you who are familiar with Arduino, you will notice that this is in fact only a few additional lines of code in loop(). The remaining code sets up the Ethernet connection to the Arduino.

The keys lines of code are just three lines:

  • arduino.init() that lets you initialize variables
  • arduino.send() that lets you send a message over the specified channel
  • arduino.connect() that lets you subscribe to a channel to receive messages

We create an instance of the iotbridge called “arduino” and we can use the SEND and CONNECT methods belonging to that class.

SEND: This invokes the publish() API to send any message over a desired channel.

CONNECT: This invokes the subscribe() API to listen for messages from other embedded devices connected to the same channel.

NOTE: The call back function receive is invoked when subscribe runs. Connect() will block on subscribe, waiting for a message to be sent to the device. You can write your own simple task scheduler and bounce between tasks according to their timing requirements.

You can customize the channels you publish/subscribe to, the UUID you assume and also the messages you send by just changing the above fields.

And huzzah! Thats it! You can now talk Arduino to any other device or application subscribing/publishing to the same channel.

More from PubNub

Can Empathy Exist in the Metaverse
News May 16, 20221 min read

Can Empathy Exist in the Metaverse

A roundtable discussion led by PubNub’s COO, Casey Clegg, exploring the topics of what it means to be human in a virtual world.

PubNub Staff

PubNub Staff

How to Advance Telehealth and Virtual Care Technologies
News May 2, 20221 min read

How to Advance Telehealth and Virtual Care Technologies

Dr. Joe Kvedar, Chair of the Board for the American Telemedicine Association, joins our COO, Casey Clegg, to discuss why...

PubNub Staff

PubNub Staff

Another Step Towards Data Security: ISO-27001 Implementation
BuildMay 2, 20221 min read

Another Step Towards Data Security: ISO-27001 Implementation

Today, we are glad to announce that we are currently in the process of implementing ISO-27001 security standards.

PubNub Staff

PubNub Staff