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”
- 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
2. Install Golang in RaspPi
sudo apt-get install golang
3. Create workspace “MyIoT”
Create empty sub-directories: bin, pkg and src
Set $GOPATH environment variable as follows
- sudo vi ~/.bashrc
- Set GOPATH in ~/.bashrc as below
3. source ~/.bashrc to update $GOPATH
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
5. Organize directory structure
Lets begin to add our code
- mkdir -p src/github.com/user/raspi-go-iot
- mkdir -p src/github.com/user/raspi-go-iot/gpio
- touch src/github.com/user/raspi-go-iot/gpio/gpio.go
- mkdir -p src/github.com/user/raspi-go-iot/pubsub
- touch src/github.com/user/raspi-go-iot/pubsub/pubsub.go
- 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
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.
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
- optsPub := MQTT.NewClientOptions(). Initialize new MQTT object
- optsPub.AddBroker(“tcp://m14.cloudmqtt.com:14205”). You can use any MQTT broker url here.
- optsPub.Username(“htmbxcyz”). MQTT broker username
- optsPub.Password(“rH2_IZj43nDy”). MQTT broker password
- optsPub.SetClientID(“rasp-pi-go”). Any name provided as Client Id.
- Green Led publish status to topic “plain/led/status/green”
- Blue Led publish status to topic “secure/led/status/blue”
- Red Led publish status to topic “secure/led/status/red”
- 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
- import “github.com/user/raspi-go-iot/pubsub” as PUBSUB
- import “github.com/user/raspi-go-iot/gpio” as GPIO
- PUBSUB.Init() configures the MQTT broker with credentials and registers the pub/sub callbacks.
- GPIO.Start() runs go routines that performs read and write GPIO pins of the LEDs.
Raspberry Pi CLI Output
CloudMQTT Broker logs
Android App — MQTT Client
raspi-go-iot - Demonstrates IoT of LED using Golang and MQTT on Raspberry Pi
android-mqtt-client - Demonstrates Android based MQTT client for project "raspi-go-iot"
Install using Go
go get github.com/mudassar-tamboli/raspi-go-iot
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.