We have made this project in order to show the easiest way to create a domotic lamp that can be controlled with a smartphone, or even using Google assistant.
Our proposal is to make it with a budget of 15€ or less and to complete it in a couple hours; even if you have never worked with Arduino and Thinger.io.
The Idea consists in equipping a cheap IKEA Lampan with a Sonoff Basic controller, that counts with a Wi-Fi controlled relay and a quite discrete enclosure. Then, we will program it to integrate with Thinger.io platform and start controlling its status with different connected tools such as Thinger.io APP, Web console, or even in response to IFTTT triggers that we can extend with Google Home or thousands of other platforms.
This project is perfect for IoT beginners, so we have included all explanations to install, configure and program the device and all the steps you have to follow to integrate it with our platform: Thinger.io
First of all, be careful! Because we are going to work with home high voltage current. Never manipulate the Sonoff without a case or the lamp wires when they are connected.
Here you can see all the materials that we will need. All of them, very easy to find at IKEA and Aliexpress with no more than 15€. So lets start!
Assembling ComponentsThe first step is to get ready the Sonoff to connect the UART interface. Just welding a 1x5 2.54mm male header, then plug the programmer with the same wiring up that is shown in the image below.
Once you have the Sonoff connected it is necessary to boot up the processor in flash mode (press and hold the button when powering up the PCB). Then open Arduino IDE and open the "SonoffBasic" example code and click in flash button without making any changes.
To flash the Sonoff, it is required to have the Computer configured with the next steps:
- 1) Arduino IDE 1.6 or newer
- 2) ESP8266 for Arduino IDE libraries (follow this link)
- 3) Thinger.io libraries
- 4) WiFiManager.h library
Then, we will need to disassemble the switch from the IKEA Lampan. Make sure that it is not connected to the current while doing this! Once we have the switch as we see it in the picture, we have to cut both cables (brown and blue) to get the switch part out of our lamp. We will be replacing this mechanism with the Sonoff Basic, and it will act as our new switch.
The cable from the lamp should look like this once we have connected it to the Sonoff. It is really easy, it is just a screw terminal where we have to insert the cable on both sides. Then, there are two top cases that locks the wire, protecting it from tuggings.
Once we have done all of this, our IKEA Lampan should look like in this pic:
Now you can connect the lamp to the current supply. By pressing the little black button of the Sonoff it is possible to control the light status directly and check if the wiring is well made.
When the Sonoff is powered up but there is any valid configuration, it will create a WiFi network that can be accessed to configure your home WiFi and Thinger.io service connection credentials. To make this, just take your Smartphone and find the "Sonoff basic" network thhen open your web browser with the direction (192.168.1.4) click on "Scan WiFi" button and complete the form with all credentials.
The next step is to sign up and integrate the project with Thinger.io Platform. It is really easy, just access to https://thinger.io and create a free account. Then you will have access to the platform workspace:
The free account allows to create 2 device connections, 4 Dashboards, 4 data buckets, and 4 Endpoints.
To create your first device just click in "Devices" menu tab and "+Add device" button. A device credentials form will appear, in which you just have to write the same data that you have introduced in the last step:
Now we will be able to control the light status from anywhere using Thinger.io web or APP console, using a switch widget or the API explorer of the device that can be accessed using the "view API" blue button of the Device Dashboard. Note that this system allows controll without delay at any conditions.
Controlling the Smart Lamp with Google Asisstant and IFTTTIFTTT is a platform that allows to configure several triggers that execute some actions following a fixed structure "IF THIS THEN THAT". That trigger includes things such as, your mobile phone entered a given location, tomorrow is going to rain, you have a new mention on twitter, you have a new email, and so on. In this how-to we are going to integrate the Google Assistant APP that can be used with our SmartPhone, to create a trigger of the action (the THIS part) and a Webhooks request to send it to Thinger.io.
The configuration of Google Assistant is the funniest part. After giving IFTTT access authorization to our account it is possible to specify three single phrases that we want to use to trigger the acction (the three that we want) and the response from Google when the phrase has been recogniced.
The second part of the recipe (the THAT) will use the "Webhooks" service from IFTTT, that allows to create personalized URL for services that don't have an official channel at IFTTT. To create this URL we will need two parts, the API REST of the device and a persistent authorization, let's see this part step by step:
1)The API REST can be consulted using the device API inspector in Thinger.io, just copy the https stream that is showed in yellow.
2) The authorization that is shown in red text at the API REST inspector is temporal, to create a permanent one we just have to go to the device dashboard > device token and click "+add" button:
This button will open a popup where we can configure some parameters. Please enter a proper token name so you can recognize it in the future. Here it is called IFTTT lamp control. One feature of the device tokens is that they allow to restric the resource access. So one token may only read a value, while another can set some value. This is specially useful if you want to share a device with someone but restricting the access to some device resources. In our case, we are going to restrict the access to the OnOff resource we defined in our sketch, as shown in the following picture.
3) The device tokens can also expire after some time, so they provide a limited time access. We want for our device to be working forever, so the token will not expire in this case. So if we press click on create, we should see a new generated token like the one shown in the following picture. We will be using this token while configuring the IFTTT service, so keep it on hand.
This token will be added to the URL using an authorization identifier, at the end, in should look something like the following string. You must change the username
, the device_id
, the resource
, and device_token
according to your parameters. Removing << and >>
.
https://api.thinger.io/v2/users/<<username>>/devices/<<device_id>>/<<resource>>?authorization=<<device_token>
4) Now that we have configured the url, we need to configure the information we are going to send to the device resource. We are going to submit a POST
request with application/json
content type that will have the same structure as the one that is showed in blue text at the API REST inspecto of our "OnOff"
resource at Thinger.io platform:
{ "in": true }
The "in" key in this JSON is used by thinger.io to indicate the resource input. It can be a boolean, a number, a string or any other value (our OnOff resource expects a boolean). This field is filled with some information available in the recipe IFTTT recipe. The recipe data can be easily put in our JSON document by placing variables with brackets {{ }}. These variables are replaced automatically by IFTTT with our new follower information. Each recipe contains different available data that you can easily check when you are composing the body.
The final configuration should look as the following picture:
In this how-to we have learned how to create a device token that allows to interact with our device from third-party services, moreover, we have to see how to configure the IFTTT Maker Channel to call a device resource based on a Twitter channel trigger. The information provided by the Twitter channel was used in our device resource to display some information about our new follower. You just need to wait for new Twitter followers and the device will automatically display them.
Comments