Integrate your ESP8266-based NodeMCU (or any ESP8266-based board) with AllThingsTalk interactively without any additional hardware in less than 15 minutes.
After following this guide, you’ll have your ESP8266 (NodeMCU) sending values from its analog pin to your AllThingsTalk Maker page.
If you’d like to go a step further, you’ll be able to do so at the end of this article!
What you'll need
- NodeMCU Board (or any other ESP8266-based board)
Setup
Installing Arduino IDE
The Arduino Integrated Development Environment (IDE) is an open source, cross-platform application that’s used for programming Arduino boards! It runs on Windows, Mac OS X, and Linux.
Download it here.
Installing ESP8266 Arduino Core
Some boards (such as the ESP8266) require board “cores” to be installed inside the Arduino IDE. This enables Arduino IDE to program the board. It’s a simple step, so let’s get on with it!
In the Arduino IDE, Go to File > Preferences for Windows (Arduino > Preferences on MacOS) and add the following URL to Additional Board Manager URLs:
http://arduino.esp8266.com/stable/package_esp8266com_index.json
- Confirm by clicking OK
- Open the Board Manager via Tools > Board > Board Manager
- Search for, select and install the “esp8266” by the ESP8266 Community.
- Once done, click Close
Installing Libraries
We’ll need a few libraries that the example sketch relies on, so let’s install them:
- In Arduino IDE, go to Tools > Manage Libraries
- Search for and install “AllThingsTalk WiFi SDK” (by AllThingsTalk)
- Search for and install “ArduinoJson” (by Benoit Blanchon)
- Click Close. Done!
Create a Device in AllThingsTalk Maker
To create a new device in AllThingsTalk maker, do the following:- Sign in/up to AllThingsTalk Maker
- Select your Ground (every new account has an automatically created “Playground” ground)
- Create a new device by clicking “+ New Device”
- Select the Arduino tile and name your device
- Click to open your newly created device
Configuring the Example Sketch
Now we’re going to program our ESP8266 (NodeMCU) board using the sketch (program) below.
To do this, click download below and open it in Arduino IDE, or just copy/paste the code into your Arduino IDE:
#include <AllThingsTalk_WiFi.h> // Load (include) the AllThingsTalk WiFi SDK - Read more about it on https://gitub.com/allthingstalk/arduino-wifi-sdkAs you may have noticed, the code has a lot of comments (shown after each “//”), so if you’re curious about how it works, you’ll get it in no time!
auto wifiCreds = WifiCredentials("Your_WiFi", "Your_WiFi_Password"); // Your WiFi Network Name and Password
auto deviceCreds = DeviceConfig("Your_Device_ID", "maker:Your_Device_Token"); // Go to AllThingsTalk Maker > Devices > Your Device > Settings > Authentication to get your Device ID and Token
auto device = Device(wifiCreds, deviceCreds); // Create "device" object
char* sensorAsset = "analog-example"; // Name of asset on AllThingsTalk to which you'll receive the value (automatically created below)
const long period = 1000; // Change this to change how many milliseconds you want between analog port readings
int analogValue = 0; // Variable that will store the value of the analog port
unsigned long startMillis; // Used to keep track of send intervals
unsigned long currentMillis; // Used to keep track of send intervals
void setup() { // This function is only called once, when the device boots
Serial.begin(115200); // Starts the Serial port for debugging (at baud rate 115200)
device.debugPort(Serial); // Enable debug output from AllThingsTalk SDK.
device.wifiSignalReporting(true); // Enable AllThingsTalk WiFi SDK's feature that sends NodeMCU's WiFi Signal Strength to your AllThingsTalk Maker
device.createAsset(sensorAsset, "Analog Value", "sensor", "integer"); // Create asset on AllThingsTalk to send analog value to
device.init(); // Initialize WiFi and AllThingsTalk
startMillis = millis(); // Saves the initial millis value at boot to startMillis variable
}
void analogCheck() { // This is the function that checks the value of analog port
currentMillis = millis(); // Saves the value of "millis()" at the time of execution of this line
if (currentMillis - startMillis >= period) { // If current time minus the last saved 'startMillis' time is bigger than the period defined above, it'll run the code below
analogValue = analogRead(A0); // Reads the analog port A0 of ESP8266 (NodeMCU) and saves it to "analogButton" variable
Serial.print("Current Analog Value: "); // Prints to Serial port
Serial.println(analogValue); // Prints to Serial port
device.send(sensorAsset, analogValue); // Sends the data to AllThingsTalk. Data is sent to "sensorAsset"
startMillis = currentMillis; // Resets the startMillis by assigning it the value of currentMillis
}
}
void loop() { // Main code that'll be run in loop all the time
device.loop(); // Keep AllThingsTalk and WiFi connection alive
analogCheck(); // Runs our "analogCheck" function, which checks the value of analog port and publishes it
}
Alright, let’s see the thing in action by configuring the code:
- Change Your_WiFi to your WiFi Network’s name
- Change Your_WiFi_Password to your WiFi Network’s Password
- Change the Your_Device_ID to your Device ID.
- To get this value, go to your AllThingsTalk devices, select the device you created, click “Settings”, then choose “Authentication” and copy the Device ID value.
- Change Your_Device_Token to your device’s token on AllThingsTalk.
- To get this value, go to your device on AllThingsTalk, click “Settings”, then click “Authentication” and copy the device token from “Device Tokens”
- Done! Feel free to save the changes in your Arduino sketch
Uploading the Example Sketch
Now you’re going to upload the sketch you just configured onto your ESP8266:- Plug your ESP8266 (NodeMCU) to your computer via USB
- Go to Tools > Board > NodeMCU 1.0 (ESP-12E Module) (choose accordingly if you’re using a different ESP8266 development board)
- In Arduino IDE, go to Tools > Ports and choose the port that the device is on (should be the only one you can see)
- Click Sketch > Upload or just click CTRL+U
In the bottom part of the Arduino IDE, you should see a message “Compiling sketch” and it should start uploading it to your ESP8266 right after that.
Once you see “Done Uploading.”, the code has been uploaded to your ESP8266 and you’re all set!
You can now go to Tools > Serial Monitor to actually see what’s happening on your ESP8266. Make sure to set the baud rate of your Serial Monitor to 115200.
Give your ESP8266 a reset (by pressing the RST button on your NodeMCU) after you open Serial Monitor so you could see what’s going on from the beginning:
If your ESP8266 (NodeMCU) has connected to the internet and if you see the “Connected to AllThingsTalk!” in your Serial Monitor, you’re good to go!
You should see the blue LED on your ESP8266 start fading in/out (it’s a new feature of our SDK), which means it’s connecting to WiFi and AllThingsTalk.
Once it blinks quickly and goes off, it means you’re connected!
Testing it
Go to your AllThingsTalk Maker device page and you should see 2 new assets in your device: “Analog Value” and “WiFi Signal Strength”. These were automatically created by your ESP8266 (NodeMCU).
You’ll see the analog pin value being shown on the “Analog Value” asset and WiFi Signal Strengh of your ESP8266 on the other.
And now, for the real trick, try touching the Analog pin (marked as “A0” on your NodeMCU) and see the value of your Asset on AllThingsTalk update in real-time!
Congratulations!
You’ve just successfully integrated your ESP8266 (NodeMCU) with the platform. And yes, it’s that easy!
Now, this is barely scratching the surface of what’s possible with our platform and the device in front of you, so if you’re curious, we suggest you check our Arduino WiFi SDK and the examples that come with it!
For the more curious
Increment counter on every touch
Could you make a counter that would increment every time you touch the analog pin of your ESP8266 (NodeMCU) and upload the current counter value to your AllThingsTalk device page?
Don’t be discouraged, it may sound hard, but it’s actually very easy and doesn’t require a whole lot of stuff to make it happen. Oh, and you also have the comments on the code to help you understand what’s going on!
Give it a try and if you get stuck, we’ve already wrote the code for you to help you out, and you can use it if you need it (don’t cheat!). Happy making!
#include <AllThingsTalk_WiFi.h> // Load (include) the AllThingsTalk WiFi SDK - Read more about it on https://gitub.com/allthingstalk/arduino-wifi-sdk
auto wifiCreds = WifiCredentials("Your_WiFi", "Your_WiFi_Password"); // Your WiFi Network Name and Password
auto deviceCreds = DeviceConfig("Your_Device_ID", "Your_Device_Token"); // Go to AllThingsTalk Maker > Devices > Your Device > Settings > Authentication to get your Device ID and Token
auto device = Device(wifiCreds, deviceCreds); // Create "device" object
char* sensorAsset = "analog-example"; // Name of asset on AllThingsTalk to which you'll receive the analog value (automatically created below)
char* counterAsset = "counter-example"; // Name of asset on AllThingsTalk to which you'll receive the counter value (automatically created below)
const long period = 1000; // Change this to change how many milliseconds you want between analog port readings
int analogValue = 0; // Variable that will store the value of the analog port
int analogCounter = 0; // Initial value of the counter
int analogTrigger = 40; // If exceeded, it's considered a touch and the counter increments. Change this to "calibrate" it
int counterMax = 10; // Maximum value the counter can reach before resetting itself
unsigned long startMillis; // Used to keep track of send intervals
unsigned long currentMillis; // Used to keep track of send intervals
void setup() { // This function is only called once, when the device boots
Serial.begin(115200); // Starts the Serial port for debugging (at baud rate 115200)
device.debugPort(Serial); // Enable debug output from AllThingsTalk SDK.
device.wifiSignalReporting(true); // Enable AllThingsTalk WiFi SDK's feature that sends NodeMCU's WiFi Signal Strength to your AllThingsTalk Maker
device.createAsset(sensorAsset, "Analog Value", "sensor", "integer"); // Create asset on AllThingsTalk to send analog value to
device.createAsset(counterAsset, "Counter", "sensor", "integer"); // Create asset on AllThingsTalk to send counter value to
device.init(); // Initialize WiFi and AllThingsTalk
startMillis = millis(); // Saves the initial millis value at boot to startMillis variable
}
void analogCheck() { // This is the function that checks the value of analog port
currentMillis = millis(); // Saves the value of "millis()" at the time of execution of this line
if (currentMillis - startMillis >= period) { // If current time minus the last saved startMillis time is bigger than the period defined above, it'll run the code below
analogValue = analogRead(A0); // Reads the analog port A0 of ESP8266 (NodeMCU) and saves it to "analogButton" variable
Serial.print("Current Analog Value: "); // Prints to Serial port
Serial.println(analogValue); // Prints to Serial port
if (analogValue >= analogTrigger) { // Checks if the current analog port value is bigger than the threshold we've set for trigger
Serial.println("Counter triggered! Incrementing..."); // Prints to Serial port
analogCounter++; // If it is, increment the analogCounter value
if (analogCounter > counterMax) { // Checks if the current analogCounter value is bigger than the maximim we've set
analogCounter = 1; // If it is, reset the counter to 1
Serial.println("Counter has been reset!"); // Prints to Serial port
}
device.send(counterAsset, analogCounter); // Publish the counter value to AllThingsTalk
Serial.print("Published counter value: "); // Prints to Serial port
Serial.println(analogCounter); // Prints the current counter number to Serial port
delay(1000); // Creates a 1 second dalay between this and next message
}
device.send(sensorAsset, analogValue); // Publish the analog value to AllThingsTalk
startMillis = currentMillis; // Resets the startMillis by assigning it the value of currentMillis
}
}
void loop() { // Main code that'll be run in loop all the time
device.loop(); // Keep AllThingsTalk and WiFi connection alive
analogCheck(); // Runs our "analogCheck" function, which checks the value of analog port and publishes it
}