Hans on IoT

ThingSpeak, MATLAB, and the Internet of Things

ThingSpeak Library for Arduino

The ThingSpeak team has released an updated version of theThingSpeak Communication Library for Arduino, ESP8266, and ESP32devices. 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, chooseSketch,Include Library, andManage Libraries. Search for “thingspeak” and clickInstall.

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 aWiFi Shield
  • Arduino or compatible using aWiFi Shield 101
  • Arduino or compatible using anEthernet Shield
  • Arduino or compatible using aMKR ETH Shield
  • Arduino MKR1000(使用WiFi101库版本0.13.0或以上。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 onGitHub. Discover otherMathWorks Open Source and Community Projectson The MathWorks GitHub page.

|

Comments

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