cog in MATLAB Answers
上次活动时间: 2023-8-2

I am attempting to bulk write some JSON data to a ThingSpeak channel using the API. An example payload is: % Payload is data = { 'write_api_key': <my_api_write_key>, 'updates': [ {'field5': 833.205, 'field4': 6, 'field3': 775.648, 'field2': 24.2523, 'delta_t': 1, 'field1': 59.1391}, {'field5': 833.199, 'field4': 6, 'field3': 771.03, 'field2': 24.2844, 'delta_t': 2, 'field1': 59.1331}, {'field5': 833.208, 'field4': 6, 'field3': 750.343, 'field2': 24.3008, 'delta_t': 3, 'field1': 59.1405}, {'field5': 833.208, 'field4': 7, 'field3': None, 'field2': 24.2903, 'delta_t': 5, 'field1': 59.1479}, {'field5': 833.216, 'field4': 7, 'field3': 748.45, 'field2': 24.2965, 'delta_t': 6, 'field1': 59.1686}, {'field5': 833.21, 'field4': 8, 'field3': None, 'field2': 24.2914, 'delta_t': 7, 'field1': 59.1756}, {'field5': 833.204, 'field4': 8, 'field3': 738.847, 'field2': 24.2867, 'delta_t': 8, 'field1': 59.1943}, {'field5': 833.208, 'field4': 8, 'field3': 732.472, 'field2': 24.2814, 'delta_t': 10, 'field1': 59.1964}, {'field5': 833.212, 'field4': 8, 'field3': None, 'field2': 24.2599, 'delta_t': 11, 'field1': 59.1971}, {'field5': 833.207, 'field4': 8, 'field3': 737.475, 'field2': 24.2658, 'delta_t': 12, 'field1': 59.2122}, {'field5': 833.212, 'field4': 8, 'field3': None, 'field2': 24.2498, 'delta_t': 14, 'field1': 59.2161}, {'field5': 833.209, 'field4': 7, 'field3': 749.432, 'field2': 24.2503, 'delta_t': 15, 'field1': 59.2298}, {'field5': 833.208, 'field4': 7, 'field3': 760.224, 'field2': 24.2396, 'delta_t': 16, 'field1': 59.2308} ] } I am sending this payload programatically, in Python, with the requests library using the following command: response = requests.post(url='https://api.thingspeak.com/channels/<my_channel_id>/bulk_update.json', ... data=json.dumps(data)) This results in the response below: % Response { "status": "401", "error": { "error_code":"error_auth_required", "message":"Authorization Required", "details":"Please provide proper authentication details." } } I am certain that the API write key and channel_id are correct. I have used both for single writes successfully. I am using a free ThingSpeak account. I have also tried adding headers to the requests.post() call to indicate explicitly that this is json data. I'm not sure why this is occuring and would sincerely appreciate any pointers.
Matt Hosini in MATLAB Answers
上次活动时间: 2022-2-18

Hi guys, One way of posting multiple data at once is bulk-write JSON Data. There is an example re this here, which shows how to update the data on ESP8266 frequently. I can't work out step 8, which expalins how to update the JSON file. Could you please provide me a reference/tutorial/manual so that I can upskill myself to work this out? Also, I need to send the temperature readings (variables). Do I need to define the temp data as object or array on JSON? Below provides the code for step 8. // Updates the josnBuffer with data void updatesJson(char* jsonBuffer){ /* JSON format for updates paramter in the API * This examples uses the relative timestamp as it uses the "delta_t". You can also provide the absolute timestamp using the "created_at" parameter * instead of "delta_t". * "[{\"delta_t\":0,\"field1\":-70},{\"delta_t\":3,\"field1\":-66}]" */ // Format the jsonBuffer as noted above strcat(jsonBuffer,"{\"delta_t\":"); unsigned long deltaT = (millis() - lastUpdateTime)/1000; size_t lengthT = String(deltaT).length(); char temp[4]; String(deltaT).toCharArray(temp,lengthT+1); strcat(jsonBuffer,temp); strcat(jsonBuffer,","); long rssi = WiFi.RSSI(); strcat(jsonBuffer, "\"field1\":"); lengthT = String(rssi).length(); String(rssi).toCharArray(temp,lengthT+1); strcat(jsonBuffer,temp); strcat(jsonBuffer,"},"); // If posting interval time has reached 2 minutes, update the ThingSpeak channel with your data if (millis() - lastConnectionTime >= postingInterval) { size_t len = strlen(jsonBuffer); jsonBuffer[len-1] = ']'; httpRequest(jsonBuffer); } lastUpdateTime = millis(); // Update the last update time }
Niall in Discussions
上次活动时间: 2021-4-14

Hi - I'm currently using the code from this article: https://uk.mathworks.com/help/thingspeak/continuously-collect-data-and-bulk-update-a-thingspeak-channel-using-an-arduino-mkr1000-board-or-an-esp8266-board.html My plan was to use this so as to update my Thingspeak server every second with data recorded on my arduino of higher than 1Hz. I am currently taking vibration sensor data that for my project requires a higher sample rate than Thingspeak is able to handle naturally - thus I found bulk updates to be the solution. I have gotten this to work as specified in the article - updates every 2 minutes with data points at every 15 seconds. I have gotten this as far as data points every second with an update to the server every 10 second - however the system fails at any data point shorter than a second. I believe this to be a problem with using delta_t but I am not sure. To clarify I do not have a computer science background, this is currently a project I'm working on in Mechanical engineering and I've truly hit a wall with this problem. Any help would be appreciated! My system is an Arduino MEGA 2560 with an ethernet shield - in the sample code they use RSSI to output sample data, I simply changed this to a random number between 0-50. Using bulk-updates for larger sample rates As of April 2021, the time resolution of data stored in the ThingSpeak Channel is 1 second. If you're trying to upload raw data that is of higher resolution, you will have to pack the data into the field using some custom schema and the built in charts will not work. delta t cannot be shorter than a second at present. I recommend you use absolute times, but start them far in the past so you can keep them separated. You cannot have duplicate time stamps in the same channel so you want to make sure to avoid that. If you have a free field, you can use another field to encode the actual timestamp, or use some consistent transformation from the data point timestamp to the actual time you have for your device. For example, if you start your absolute timestamps in January 1 1900 12:00:00, then every one second could represent a tenth of a second in your system. Thank you for the reply! So in this case I would best use a Matlab visualization to take the JSON code after giving it a new time stamp? Thank you for the reply! Does this not conflict with the other comment that says channels do not allow smaller than 1 second time signatures? Regardless - I believe using your idea for a Matlab visualization may be the solution to my problem. You store the readings with a timestamp separated by one second. When you read them into the code for your visualization, you can divide the time difference by 10 to get the real time stamp. i.e. 1900-01-01 12:00:00 represents 2020-04-08 12:00:00:00 1900-01-01 12:00:01 is 2020-04-08 12:00:00:10 1900-01-01 12:00:02 is 2020-04-08 12:00:00:20 and so on. You will definitely need a custom visualization, as Vinod says. Perfect! Ill get to work looking at that now then. Thank you for your help! can I use a free field to encode the actual time stamp, or can I use some kind of sequential conversion from the data point time stamp to the actual time? Olivia,developer" <https://www.worktime.com employee monitoring software> " As long as you don't use duplicate timestamps and ensure they are separated by at least 1 second, that is a fine idea. The automatic field plots wont work, but you can make a custom MATLAB visualization to display the data. arduino bulk write bulk update json data

关于 ThingSpeak

The community for students, researchers, and engineers looking to use MATLAB, Simulink, and ThingSpeak for Internet of Things applications. You can find the latest ThingSpeak news, tutorials to jump-start your next IoT project, and a forum to engage in a discussion on your latest cloud-based project. You can see answers to problems other users have solved and share how you solved a problem.