This post will show how to connect the ESP8266 to the Thinger.io IoT server. This how-to will use the ESP8266 library for the Arduino IDE.
Install the Arduino IDE
Download the Arduino IDE (1.6.4, or 1.6.5. Currently the 1.6.6 version is not compatible)
Install the ESP8266 Board Package
Enter http://arduino.esp8266.com/stable/package_esp8266com_index.json
into Additional Board Manager URLs field in the Arduino v1.6.4+ preferences. If this url is not working, maybe you may need to check the Github project that supports the library: https://github.com/esp8266/Arduino
Next, go to the Boards manager Tools > Boards > Board manager… to install the ESP8266 package. Search for the esp8266 and install the package esp8266 by ESP8266 Community
Now you can program almost any ESP8266 directly from the Arduino IDE. From the Tools > Boards you should see now the new ESP8266 boards installed. Select your board to be able to compile code for the ESP8266.
You can find additional information for the ESP8266 package in the ESP8266 Github repository. The easiest board to program is the Node MCU, that do not require pressing Flash + Reset buttons for uploading the sketch. Anyway, at this step you should be able to compile and update some ESP8266 example provided in the ESP8266 library to continue with the integration of the thinger platform.
Install thinger.io Arduino Libraries
This part is also so easy and its covered in more detail in this additional post. But it is as simple as going to Sketch > Include Library > Manage Libraries… and search for thinger and install the library.
Running the example project
Now your IDE is ready for compiling code for the ESP8266 boards and working with the thinger platform. So we are going to load the default example for the ESP8266 from File > Examples > thinger.io > ESP8266. The example code looks like following (for the 1.1.0 library version):
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <ThingerWifi.h>
#define USERNAME "your_user_name"
#define DEVICE_ID "your_device_id"
#define DEVICE_CREDENTIAL "your_device_credential"
#define SSID "your_wifi_ssid"
#define SSID_PASSWORD "your_wifi_ssid_password"
ThingerWifi thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);
void setup() {
pinMode(BUILTIN_LED, OUTPUT);
thing.add_wifi(SSID, SSID_PASSWORD);
// resource input example (i.e. turning on/off a light, a relay, configuring a parameter, etc)
thing["led"] << [](pson& in){ digitalWrite(BUILTIN_LED, in ? HIGH : LOW); };
// resource output example (i.e. reading a sensor value)
thing["millis"] >> [](pson& out){ out = millis(); };
// resource input/output example (i.e. passing input values and do some calculations)
thing["in_out"] = [](pson& in, pson& out){
out["sum"] = (long)in["value1"] + (long)in["value2"];
out["mult"] = (long)in["value1"] * (long)in["value2"];
};
}
void loop() {
thing.handle();
}
As you can see there are some default constants called USERNAME
, DEVICE_ID
, and DEVICE_CREDENTIAL
that you must fill with the information provided in the device registration process. Also there is config for the WiFi access point. You must change SSID
with your Wifi name, and SSID_PASSWORD
with your WPA2 Wifi password.
At this point you should be able to program your device and get it connected to the platform. You can check your devices list, enter in the device dashboard clicking in the device name, and accessing the API dashboard (by clicking in the button below) where you can interact with the defined resources (led, millis, in_out). You should see in the device API something like the following pictures:
In the led resource you can turn the led on and off. It is an example of how to send information to the device. In this case it is only required sending a boolean, but you can send any value or json data. Just configure the device for reading the parameters and you could fill them from this API view.
The millis resource is an example of reading values from the device, just a output resource that provides some data. In this case it is read the current milliseconds from the device.
And finally the in_out resource is an example of how the device can take some input parameters an generate an output. In this example the device takes two input values and provides the sum and the multiplication. You can try running the resource with different input values here.
All those resources are illustrative and you can delete them to add you own resources, like reading a sensor value, using a relay, and so on.
You can see also a video that illustrates some of this steps for reading temperature and humidity values from a DHT11 sensor: