RaspPi, Golang, MQTT and IoT of LED

Mudassar Tamboli
Jun 18, 2018 · 6 min read


A couple of years ago i had bought Raspberry Pi 2.0 to experiment on its GPIO pins. I was curious to check how Linux installs on such a small, credit card size micro computer. Having worked in Arduino Uno/Pro Mini microcontrollers, RaspPi was kind of an overkill to do anything that Arduino Uno/Pro Mini could do. $ 5 vs $ 50 . However due to excellent CPU and more RAM available, RaspPi, turned out to be an inevitable option for Camera related projects which requires quick frame processing and lots of RAM. Moving a robot,capturing pictures, messaging to cloud, notifying about GPS location and controlling the RaspPi movements in return, looked feasible with todays available technologies like Cloud, Pub/Sub MQTT, WiFi etc.

So, i decided to first get my RaspPi programmed with the basic infrastructure, cloud connected, message and notification capable and able to communicate with the peripheral… GPIO pins to begin with.

Why Golang ?

Having worked in C/C++, obviously, i wanted to start with C++ to control the peripherals… atleast the GPIO pins. I would not stop just there, i would also publish the status of the GPIO pins to Cloud. There will some client apps installed in some smartphone, or PC, or Cloud itself waiting for the pin statuses. I chose MQTT for this. I needed a MQTT library that would interact with my C++ program. I would need C++ based MQTT library to interact with the firmware program. Now this adds complexity unlike Python way of programming RaspPi. What if i install NoSQL on my RaspPi ? Again a new NoSQL C++ library and handling pointers. My idea was to handle pointers only while driving GPIO and RaspPi peripherals. Not beyond that !!!!!

I chose Golang as it is statically type compiled language with memory safety and garbage collection, easy installation and packaging, easy intergration with third party libraries, and resembles quite close to C. It has better performance than Python. Also Go, from Google factory, is widely supported by its fast growing community. It has become a language of choice in developing cloud microservices and is adopted by Amazon, Facebook, Google, Netflix and many more.


Raspberry Pi is an IoT thing controlling different LEDs. It is a Pub/Sub client connected to CloudMQTT broker. Mobile Phone, PC are MQTT clients that control the “things”. MQTT messaging protocol is used to communicate among “things” and “clients”


RaspPi Setup

  1. Boot RaspPi. Type “arp -a” in your Windows host command prompt and find out the ip address of RaspPi. Login using ssh or VNC viewer
RaspPi IP, Linux version and gpio pins

2. Install Golang in RaspPi

sudo apt-get install golang

3. Create workspace “MyIoT”

Create empty sub-directories: bin, pkg and src

Empty workspace with bin, pkg and src directories

Set $GOPATH environment variable as follows

  1. sudo vi ~/.bashrc
  2. Set GOPATH in ~/.bashrc as below
Setting GOPATH in ~./bashrc

3. source ~/.bashrc to update $GOPATH

source ~/.bashrc

4. Install github.com/stianeikeland/go-rpio

We will be using “go-rpio” to talk to the RaspPi hardware. This Go library abstracts the dirty work of mapping the system space to virtual address and perform bit manipulation on values at the different GPIO pin addresses.

Run the following command

get sources of go-rpio
directory tree after getting go-rpio

5. Organize directory structure

Lets begin to add our code

  1. mkdir -p src/github.com/user/raspi-go-iot
  2. mkdir -p src/github.com/user/raspi-go-iot/gpio
  3. touch src/github.com/user/raspi-go-iot/gpio/gpio.go
  4. mkdir -p src/github.com/user/raspi-go-iot/pubsub
  5. touch src/github.com/user/raspi-go-iot/pubsub/pubsub.go
  6. touch src/github.com/user/raspi-go-iot/main.go

user directory is in accordance to your git username. We do this so that the code is git-ready for code commits.

raspi-go-iot is the IoT example repository

gpio package implements hardware interfacing API

pubsub package implements MQTT messaging

gpio.go, pubsub.go and main.go are empty files for now.

Lets start adding code.

Hardware code walk : sample-gpio.go

Image Courtesy : www.raspberrypi.org

In the above code go-rpio is imported first

rpio.Open maps the system address space to user address space.

rledPin1 is a GPIO pin set to Output mode so that the LED can be turned OFF and ON

rledPin1.Write(rpio.High) turns LED ON

Dancing LEDs code walk : gpio.go

The intention of this article is to publish the status of LEDs and subscribe for the command (to turn LED On and OFF) from the other publisher through MQTT broker.

PCB Board

I developed a PCB board with 3 Green, 3 Blue and 3 Red LEDs.

The “Go” firmware program does the following

Two Go routines will randomly turn the Green and Blue LEDs ON/OFF and Publish their status to the interested Subscribers.

One Go routine will ONLY read the status of the Red LED and Publish their status to the interested Subscribers.

goDancingGreenLed is a go routine that runs forever. It randomly turns Green LED ON/OFF and publishes the status through PubGreenLedStatus.

goDancingBlueLed is a go routine that runs forever. It randomly turns Blue LED ON/OFF and publishes the status through PubBlueLedStatus.

goStillRedLed is a go routine that runs forever. It ONLY reads Red LED state and publishes the status through PubRedLedStatus.

PubGreenLedStatus is a callback routine initialized by the pubsub.go for MQTT publishing.

PubBlueLedStatus is a callback routine initialized by the pubsub.go for MQTT publishing.

PubRedLedStatus is a callback routine initialized by the pubsub.go for MQTT publishing.

SubRedLedAction is a callback routine initialized by the pubsub.go for MQTT subscription. In this case we take commands to turn LED On/Off

Pub/Sub code walk : pubsub.go

Eclipse Paho MQTT Go client library

MQTT client Go library is provided by Eclipse. Install it using the following command

go get github.com/eclipse/paho.mqtt.golang

Installing MQTT Go library
  1. optsPub := MQTT.NewClientOptions(). Initialize new MQTT object
  2. optsPub.AddBroker(“tcp://m14.cloudmqtt.com:14205”). You can use any MQTT broker url here.
  3. optsPub.Username(“htmbxcyz”). MQTT broker username
  4. optsPub.Password(“rH2_IZj43nDy”). MQTT broker password
  5. optsPub.SetClientID(“rasp-pi-go”). Any name provided as Client Id.
  6. Green Led publish status to topic “plain/led/status/green”
  7. Blue Led publish status to topic “secure/led/status/blue”
  8. Red Led publish status to topic “secure/led/status/red”
  9. Red Led subscribe to topic “secure/led/action/red” to get command from user apps to turn Red LED On/Off

main code walk : main.go

  1. import “github.com/user/raspi-go-iot/pubsub” as PUBSUB
  2. import “github.com/user/raspi-go-iot/gpio” as GPIO
  3. PUBSUB.Init() configures the MQTT broker with credentials and registers the pub/sub callbacks.
  4. GPIO.Start() runs go routines that performs read and write GPIO pins of the LEDs.


Raspberry Pi CLI Output

Raspberry Pi CLI Ouput

CloudMQTT Broker logs

CloudMQTT broker pub/sub messages

Android App — MQTT Client

Android App — MQTT Client

Git Sources

Install using Go

go get github.com/mudassar-tamboli/raspi-go-iot

Install project sources


This articled explained the IOT of LED using Raspberry Pi with Golang. Android app sources are provided to view the status of randomly dancing LED and also control an LED on the PCB board connected to RaspPI.

Mudassar Tamboli

Written by

AWS Certified Solutions Architect, IoT enthusiast, Entrepreneurial Thinker, Polyglot programmer

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade