{"id":2582,"date":"2019-02-12T10:31:48","date_gmt":"2019-02-12T15:31:48","guid":{"rendered":"https:\/\/blogs.mathworks.com\/iot\/?p=2582"},"modified":"2020-12-16T21:19:30","modified_gmt":"2020-12-17T02:19:30","slug":"thingspeak-library-for-arduino","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/iot\/2019\/02\/12\/thingspeak-library-for-arduino\/","title":{"rendered":"ThingSpeak Library for Arduino"},"content":{"rendered":"
The ThingSpeak team has released an updated version of the ThingSpeak Communication Library for Arduino, ESP8266, and ESP32<\/a> 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.<\/p>\n 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<\/em>, Include Library<\/em>, and Manage Libraries<\/em>. Search for \u201cthingspeak\u201d and click Install<\/em>.<\/p>\n The ThingSpeak Communication Library supports many devices. Using the library makes the experience the same for each board type. When you learn one\u00a0way to\u00a0work with ThingSpeak, you will be to work with other devices in the same way.<\/p>\n Each supported device includes three ThingSpeak examples.<\/p>\n 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\u00a0into 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.<\/p>\n The code behind the ThingSpeak library is available on GitHub<\/a>. Discover other\u00a0MathWorks Open Source and Community Projects<\/a> on The MathWorks GitHub page.<\/p>\n","protected":false},"excerpt":{"rendered":" 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… read more >><\/a><\/p>\n","protected":false},"author":148,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[4,293],"tags":[14,49,60,50,239],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/iot\/wp-json\/wp\/v2\/posts\/2582"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/iot\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/iot\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/iot\/wp-json\/wp\/v2\/users\/148"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/iot\/wp-json\/wp\/v2\/comments?post=2582"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/iot\/wp-json\/wp\/v2\/posts\/2582\/revisions"}],"predecessor-version":[{"id":2586,"href":"https:\/\/blogs.mathworks.com\/iot\/wp-json\/wp\/v2\/posts\/2582\/revisions\/2586"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/iot\/wp-json\/wp\/v2\/media?parent=2582"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/iot\/wp-json\/wp\/v2\/categories?post=2582"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/iot\/wp-json\/wp\/v2\/tags?post=2582"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}Installation<\/h3>\n
<\/p>\nDevice Support<\/h3>\n
\n
\n
Request Status Codes<\/h3>\n
\/\/ Read in field 1 of the private channel which is a counter \r\n long fieldValue = ThingSpeak.readLongField(myChannelNumber, myFieldNumber, myThingSpeakReadAPIKey); \r\n\r\n \/\/ Check the status of the read operation to see if it was successful\r\n statusCode = ThingSpeak.getLastReadStatus();\r\n if(statusCode == 200) {\r\n Serial.println(\"Field Value: \" + String(fieldValue));\r\n }\r\n else {\r\n Serial.println(\"Problem reading channel. HTTP error code \" + String(statusCode)); \r\n }<\/code><\/pre>\n
<\/div>\n