|Software apps and online services:|
Just a few days ago IFTTT launched a channel for makers. Just in case you do not know IFTTT yet, it is a web platform that facilitates connections between other big platforms under the form of recipes giving a great degree of flexibility in creating mashups.
This was a highly awaited feature since until now it was impossible to connect prototypes to the recipes and only a few approved devices were available from large producers that were accepted in the platform. The Maker channel changes all that. It allows both making web requests to the channel to send events to other platforms from devices and from other platforms to the devices.
The only problem with the channel is that it allows only HTTP requests to be made and if you get a bit unlucky with the device and it's network the command might not reach it. Protocols like MQTT are far more reliable and have a lower footprint. A special property of the protocol is that it has a quality of service which specifies for the data to be sent until the device receives it. To take advantage of this you can either host your MQTT broker or take advantage of an IoT platform that offers this. DeviceHub.net offers this service. You can learn more about it in this tutorial specific about MQTT and what benefits it has over HTTP here.
The first step is to register on the platform and create a Device as an entry point for your data on the cloud. You can do that herehttps://dashboard.devicehub.net/register and follow the wizard there to create your device.
Every Device has a unique id (UUID) and an associated API key of the project it resides to make sending data secure and reliable.
After you have one device add an Actuator in it. This will be hooked to the prototype by using the bellow code and relay the action done with the IFTTT button.
To use IFTTT you have to set up a recipe in the Do button on your phone:
We used an ESP8266 kit because it had already a relay and all we needed for a quick IoT setup. You can use any hardware with the DeviceHub.net platform as long as you set up the API requests right. All we wanted is to switch an appliance on and off with the Do button app from IFTTT.
To upload it to the ESP8266 we used the esptool utility that runs with Python.
For the main code we used ESPlorer to upload it. You can find the init.lua and project.lua files in the software zone of this tutorial.
function startup() print('in startup') dofile('project.lua') end tmr.alarm(0,5000,0,startup)
projectID = "paste_project_id_here" ACTUATOR_NAME = "paste_actuator_name_here" apiKey = "paste_api_key_here" deviceUUID = "paste_device_id_here" serverIP = "220.127.116.11" -- mqtt.devicehub.net IP mqttport = 1883 -- MQTT port (default 1883) userID = "" -- username for authentication if required userPWD = "" -- user password if needed for security clientID = "ESP1" -- Device ID mqtt_state = 0 -- State control wifiName = "paste_wifi_ssid_here" wifiPass = "paste_wifi_password_here" wifi.setmode(wifi.STATION) wifi.sta.config(wifiName, wifiPass) wifi.sta.connect() relay_pin = 1 gpio.mode(relay_pin, gpio.OUTPUT) gpio.write(relay_pin, gpio.LOW) function setRelay(state) if state == 1 then gpio.write(relay_pin, gpio.HIGH) else gpio.write(relay_pin, gpio.LOW) end end function mqtt_do() if mqtt_state < 5 then mqtt_state = wifi.sta.status() --State: Waiting for wifi elseif mqtt_state == 5 then m = mqtt.Client(clientID, 120, userID, userPWD) mqtt_state = 10 -- State: initialised but not connected m:on("message", function(conn, topic, data) if data ~= nil then -- print("get: "..data) local pack = cjson.decode(data) if pack.state then print(pack.state) if (pack.state == 0 or pack.state == "0") then setRelay(0) elseif (pack.state == 1 or pack.state == "1") then setRelay(1) end end end end) elseif mqtt_state == 10 then m:connect( serverIP , mqttport, 0, function(conn) print("Connected to MQTT:" .. serverIP .. ":" .. mqttport .." as " .. clientID ) m:subscribe("/a/"..apiKey.."/p/"..projectID.."/d/"..deviceUUID.."/actuator/"..ACTUATOR_NAME.."/state",0, function(conn) print("subscribed!") end) end) mqtt_state = 20 end end tmr.alarm(0, 600, 1, function() mqtt_do() end)