Meeo and Raspberry Pi = ❤️
Are you in need of an awesome MQTT broker for your Raspberry Pi project? Want to get started with Internet of Things using the Pi? Look no further, because Meeo is here to the rescue!
Control your appliances anytime, anywhere using your Pi and Meeo!
Prerequisites
First and foremost, you need to know some basics by checking this link out. Make sure that you have a Meeo account. This guide will utilize the Toggle Widget. If you are not yet familiar with it, check this link to know the intricacies of the Toggle Widget.
Since we’ll be making the project with the Raspberry Pi, you need to have a Raspberry Pi and an SD card first! You may use the latest Raspberry Pi 3 Model B (in my case I used the first version of the Pi Zero) The board should have the latest Raspbian OS! You may check the latest OS here. Use Etcher to make OS flashing a piece of pi! (I mean, cake!)
Setup!
When you have your Pi up and running (making sure that it is connected to the Internet via WiFi or Ethernet), make sure that you get the latest and greatest by doing:
sudo apt-get update
sudo apt-get upgrade
Let’s proceed by installing npm via sudo apt-get install npm
At this point you have realized that nodejs will be used for this guide! Hooray for Javascript!
When npm is installed, you may now setup any folder you would like your nodejs script to be in. Just cd
on the folder you like and type npm init
. Issuing the init command will ask you a few questions. To use the default, just hit the enter key until it stops asking questions.
Time to install the packages we need! We’ll use wiring-pi and mqtt.js to be able to control the GPIO of the Pi and connect to Meeo! To install the packages, just type the following:
npm install node-wiring-pi --save
npm install mqtt --save
After installing the packages, it is now the time to do some magic 😎
Code code code
Since we’ll be using the Pi’s GPIO, it is best that you issue the command gpio readall
to know what pins could be used. Here’s an example output of the command:
Since wiring-pi is used, you may use the pin names under wPi. If you connect your relay’s input signal to the Pi on Physical pin 7, then you should use wPi pin 7
on your code. If the input signal is on Physical pin 37, then you should use wPi pin 25
on your code.
var mqtt = require("mqtt");
var wpi = require('node-wiring-pi');var namespace = "your-namespace";
var accessKey = "your-accessKey";
var channel = "your-channel";var client = mqtt.connect('mqtts://mq.meeo.io', {
clientId: "something random",
username: namespace,
password: accessKey,
retain: true
})var relay = 7;wpi.setup('wpi');
wpi.pinMode(relay, wpi.OUTPUT);client.on("connect", function () {
console.log("Connected to server");
client.subscribe(relayChannel);
});client.on("message", function(topic, message) {
if (topic === channel) {
if (parseInt(message.toString()) === 1) {
wpi.digitalWrite(relay, wpi.HIGH);
} else {
wpi.digitalWrite(relay, wpi.LOW);
}
}
})