Hi IoNeeds lovers! In this post, I will explain about bridging MQTT with URL and e-mail triggering. It’s the most excited topic of our project and we have completed it successfully. Now, we can use auto-purchase function of IoNeeds smart jars with defining rules which we want. Such as, “If fullness value of the jar is less than 20%, auto-purchase this product from site.com.” Also we can connect our smart jars to hundreds of smart devices and social services over IFTTT. Moreover, we have made the whole source code is open on github! So you can use the bridge and you can provide same functions to your product freely. Let’s have a look for details.
First of all, I should explain the necessity of this part in our project. As we explained, we’re using MQTT protocol (Eclipse paho-mqtt) between IoNeeds hub and cloud server. Cloud server receives the sensor data for logging and graphing, but if we are mentioning about a “smart” jar, it will be more “smart.” At this point, we decided to listen MQTT topics of devices and perform some tasks which specified on rule manager. Tasks can be specified like “if ioneeds/device0/fullness topic’s value is greater than 20, do something.” on rule manager. The rule manager provides bridging MQTT values with e-mail sending and URL calling. So, we called as “MQTT Bridge Rule Manager”. Well, what is the “smart” part? Using rule manager, rules like “If temperature is high, e-mail me.” can be processed. But this is’nt all. An e-mail triggering on IFTTT can connect hundreds of smart devices and services. Thus, we can perform hundreds of scenerios like “If jar’s light is less, turn on my Philips Hue smart lamp”, “If jar’s temperature is high, decrease temperature setting of Nest Thermostat” with zero-code. Also with URL triggering function, an auto-purchase API can be triggered by a rule. All of these are possible by defining rules on rule manager and we have prepared demos for all of these, let’s see!
How is it working actually?
MQTT Bridge starts working.
It listens ioneeds/# topic on our MQTT server. (All sub-topics of ioneeds)
If It receives a data from a topic…
Got a message on topic ioneeds/device0/temperature with payload: 21.9
It fetches all rules from mySQL.
|#1||device0||lux > email@example.com||#testsms|
|#5||device01||temperature = firstname.lastname@example.org||#tam20|
|#6||device0||lux > 500||url_trigger||http://shop.ioneeds.eercan.com/purchase.api.php?order_dat…|
If it matches with a rule, performs the rule.
Then, continues listening for another data from MQTT.
All of these are open source!
I published the whole source code on github.com. You can use MQTT Bridge in your own solutions and you can easily connect your solutions to IFTTT using this. Also I have shared purchase API for performing auto-purchase on WooCommerce web sites. You can find all of them in my github profile; https://github.com/eercanayar Enjoy the open source soul!
A little more detail
I have developed it with PHP & mySQL. A php file mqtt_bridge.php has a infinitive loop at it always listens the topic and performs rules on database. rule-manager folder in project is a front-end for adding and editing rules on mySQL database.
I have used phpmailer SMTP class for send e-mail to IFTTT. For auto-purchase, I have created a demo e-commerce site to test making orders. I have developed a purchase API for WooCommerce (a free eCommerce toolkit for WordPress) and when my API called on MQTT Bridge Rule Manager, it inserts and order on WooCommerce website.
Stay tuned for our final video post!