I wanted to log data from sensors to ThingSpeak™ using a microcontroller board and the ESP8266 WiFi module. While writing the firmware and testing it on the FRDM-K64F board from NXP, I realized it was kind of cumbersome to test because there were many things that were going wrong with the TCP connection being unpredictable. Also the AT commands would provide a slightly different response message depending on the state of the ESP8266 module. To make testing easier, I fired up MATLAB and started looking at how to log data on thingspeak.com in a more consistent way.
A quick search led to me to MATLAB thingspeak support toolbox. It makes it very easy to push data or read data from thingspeak.com. I downloaded the file from the Matlab Central toolbox page, opened the file in MATLAB and installed the toolbox. After this I read the documentation and I was able to write a single value to thingspeak.com using the thingSpeakWrite function. To simulate periodic data upload, I created a MATLAB timer with a period of 15 seconds to upload a sequence of five values, [10, 30, 60, 90, 99].
On running the test, some values were successfully submitted to thingspeak.com and some were not and I got an error in MATLAB:
So I adjusted the period of the timer to 20 seconds and it worked better but the problem persisted. I adjusted the period further to 30 seconds all the values were submitted. Then I put the code in an infinite loop and for several cycles. To make sure that there was a delay in between sequences of the 5 numbers, I set the timer to have a start delay equal to the period. The success of writing to the server was now consistent.
Below is the MATLAB code I used to simulate the data logger.
From this test it is clear that we need a long enough delay[at least 30 seconds] between write attempts in order to consistently have success in submitting the data to a thingspeak.com channel.