This forum is archived. Posts are preserved for historical reference. For current help, join us on Discord.

Quick demo of MQTT on UserSpice (with code!)

In Off-topic Discussions · Started by mudmin on 2017-11-19 10:18 pm · 10052 views · 3 replies

Bringing MQTT to UserSpice allows you to interact with the world in a completely different way. The possibilities are endless. Here's a quick sneak peak. More videos/demos to come.

I will keep a repository of my code examples for IOT stuff up to date here
https://github.com/mudmin/usiot/archive/master.zip

You can clone the repository with
git clone https://github.com/mudmin/usiot.git

https://www.youtube.com/watch?v=IYEoLhkp9eU&feature=youtu.be
I'm all in. Where should I start. I will be working with SIM800C modules and I believe I can get it to work. Right now I'm communicating with userspice over GET requests and I'm unable to poll device from server, so I'm sending GET requests every 2 minutes to send server an update and ask it if it wants anything more.

I have read some bits of MQTT on internet but clearly I don't have big picture how to start implementing this to UserSpice.

Regards,
Jakob
I've been reading about mqtt a bit more and I believe I know the protocol. I don't know yet how to implement mqtt broker onto userspice. I saw files on git but on the mqtt.php there is a function mqtt() and I don't know where can I find this function. Yet I didnt try any of this on my server but I think it won't work unless mqtt broker is somewhere in init.php or header or navigation.

Regards,
Jakob
MQTT is great and so simple. I'm going to work on some videos. Promise.

If you already have an mqtt broker you can add your credentials on the dashboard. Just go to the button at the top that says IOT and MQTT. Enter your server and note the id. It will probably be 2.

Then in your code, you can just do...
mqtt(2,"Hello","World!");
//sends Msg of "World!" with topic of "Hello" to MQTT server 2.

If you don't already have an mqtt server, you can install it on ubuntu with
sudo apt-get install mosquitto mosquitto-clients

From there, just enter your server ip and port 1883. You don't really need to worry about username and password during your early testing. Eventually you'll want one for production and there are tons of guides for that.

If you have a raspberry pi laying around you can do...
wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key
sudo apt-key add mosquitto-repo.gpg.key
sudo wget http://repo.mosquitto.org/debian/mosquitto-wheezy.list
sudo -i
apt-get update
apt-get install mosquitto