Hans on IoT

ThingSpeak, MATLAB, and the Internet of Things

ThingSpeak Library for Arduino

The ThingSpeak team has released an updated version of the ThingSpeak Communication Library for Arduino, ESP8266, and ESP32 devices. The ThingSpeak library is the easiest way to get Arduino devices connected to ThingSpeak IoT services. The library now features a way to separate secret credentials from your main source code. This helps when you want to share your code but don’t want to share your WiFi credentials and ThingSpeak keys.

Installation

The Arduino IDE needs to have the ThingSpeak library installed in order for your devices to know how to send data to ThingSpeak. In the Arduino IDE, choose Sketch, Include Library, and Manage Libraries. Search for “thingspeak” and click Install.

Device Support

The ThingSpeak Communication Library supports many devices. Using the library makes the experience the same for each board type. When you learn one way to work with ThingSpeak, you will be to work with other devices in the same way.

  • Arduino or compatible using a WiFi Shield
  • Arduino or compatible using a WiFi Shield 101
  • Arduino or compatible using an Ethernet Shield
  • Arduino or compatible using a MKR ETH Shield
  • Arduino MKR1000 (use the WiFi101 library version 0.13.0 or older. WiFi101 library versions 0.14.0 and newer have a bug that stops this ThingSpeak library from working properly)
  • Arduino MKR1010
  • Arduino VIDOR 4000
  • Arduino GSM 14000
  • Arduino Yún (Rev1 and Rev2)
  • ESP8266 (tested with SparkFun ESP8266 Thing – Dev Board and NodeMCU 1.0 module)
  • ESP32 (tested with SparkFun ESP32 Thing)

Each supported device includes three ThingSpeak examples.

  • ReadField: Reading from a public channel and a private channel on ThingSpeak
  • WriteSingleField: Writing a sensor value to a single field on ThingSpeak
  • WriteMultipleFields: Writing sensor values to multiple fields and status in one transaction with ThingSpeak

Request Status Codes

To get the best compatibility with ThingSpeak IoT services, we recommend using the ThingSpeak library. The library has extra features that help you troubleshoot and get visibility into any issues with requests to ThingSpeak. I find it useful to store the last status code from ThingSpeak. I can use this code to understand if the request succeeded or failed. Here’s an example of how to use the “getLastReadStatus” method.

 // Read in field 1 of the private channel which is a counter  
 long fieldValue = ThingSpeak.readLongField(myChannelNumber, myFieldNumber, myThingSpeakReadAPIKey);  

  // Check the status of the read operation to see if it was successful
 statusCode = ThingSpeak.getLastReadStatus();
 if(statusCode == 200) {
   Serial.println("Field Value: " + String(fieldValue));
 }
 else {
   Serial.println("Problem reading channel. HTTP error code " + String(statusCode)); 
 }

The code behind the ThingSpeak library is available on GitHub. Discover other MathWorks Open Source and Community Projects on The MathWorks GitHub page.

|
  • print

Comments

To leave a comment, please click here to sign in to your MathWorks Account or create a new one.