Husnain in MATLAB Answers
上次活动时间: 2023-9-30

Hi, im trying to send data to Thingspeak in bulk/batches. I adopted this example <https://ww2.mathworks.cn/help/thingspeak/continuously-collect-data-and-bulk-update-a-thingspeak-channel-using-an-arduino-mkr1000-board-or-an-esp8266-board.html> . After 1st batch of data is sent to thingspeak successfully, the code doesnot run loop to continue further buffer the data in JSON. That means it send data to server only once and then do nothing. Please guide to sort this issue. #define MODEM_RST 5 #define MODEM_PWKEY 4 #define MODEM_POWER_ON 23 #define MODEM_TX 27 #define MODEM_RX 26 const char apn[] = "***********"; const char gprsUser[] = ""; const char gprsPass[] = ""; const char simPIN[] = ""; const char server[] = "api.thingspeak.com"; const int port = 80; char jsonBuffer[500] = "["; // Initialize the jsonBuffer to hold data #define SerialMon Serial #define SerialAT Serial1 #define TIMEOUT 5000 // Configure TinyGSM library #define TINY_GSM_MODEM_SIM7600 // Modem is SIM7600 #define TINY_GSM_RX_BUFFER 1024 // Set RX buffer to 1Kb #include <TinyGsmClient.h> #ifdef DUMP_AT_COMMANDS #include <StreamDebugger.h> StreamDebugger debugger(SerialAT, SerialMon); TinyGsm modem(debugger); #else TinyGsm modem(SerialAT); #endif TinyGsmClient client(modem); #define IP5306_ADDR 0x75 #define IP5306_REG_SYS_CTL0 0x00 /* Collect data once every 15 seconds and post data to ThingSpeak channel once every 2 minutes */ unsigned long lastConnectionTime = 0; // Track the last connection time unsigned long lastUpdateTime = 0; // Track the last update time const unsigned long postingInterval = 20L * 1000L; // Post data every 2 minutes const unsigned long updateInterval = 2L * 1000L; // Update once every 15 seconds // Sensor Libraries #include <SPI.h> #include <Adafruit_Sensor.h> #include <DHT.h> #define DHTPIN 32 #define DHTTYPE DHT22 DHT dht(DHTPIN, DHTTYPE); void setup() { SerialMon.begin(115200); // Set modem reset, enable, power pins pinMode(MODEM_PWKEY, OUTPUT); pinMode(MODEM_RST, OUTPUT); pinMode(MODEM_POWER_ON, OUTPUT); digitalWrite(MODEM_PWKEY, LOW); digitalWrite(MODEM_RST, HIGH); digitalWrite(MODEM_POWER_ON, HIGH); // Set GSM module baud rate and UART pins SerialAT.begin(115200, SERIAL_8N1, MODEM_RX, MODEM_TX); delay(3000); SerialMon.println("Initializing modem..."); modem.init(); // Configure the wake up source as timer wake up SerialMon.print("Connecting to APN: "); SerialMon.print(apn); if (!modem.gprsConnect(apn, gprsUser, gprsPass)) { SerialMon.println(" fail"); } else { SerialMon.println(" OK"); SerialMon.print("Connecting to "); SerialMon.print(server); if (!client.connect(server, port)) { SerialMon.println(" Fail"); } else { SerialMon.println(" OK"); } } dht.begin(); } void loop() { // If update time has reached 1 second, then update the jsonBuffer if (millis() - lastUpdateTime >= updateInterval) { updatesJson(jsonBuffer); } } // Updates the josnBuffer with data void updatesJson(char* jsonBuffer) { // Collect Hall effect sensor data //int h = hallRead(); float t = dht.readTemperature(); float h = dht.readHumidity(); Serial.print("Temperature: "); Serial.println(t); // Format a single data point as a JSON object char temp[500]; // Temporary buffer for JSON object //sprintf(temp, "{\"delta_t\":%lu,\"field1\":%d},", (millis() - lastUpdateTime) / 1000, t); sprintf(temp, "{\"delta_t\":%lu,\"field1\":%.2f,\"field2\":%.2f},", (millis() - lastUpdateTime) / 1000, t, h); // Append the JSON object to the jsonBuffer strcat(jsonBuffer, temp); // If 15 seconds have passed since the last update, send the data to ThingSpeak if (millis() - lastConnectionTime >= postingInterval) { // Close the JSON array size_t len = strlen(jsonBuffer); jsonBuffer[len - 1] = ']'; // Send data to ThingSpeak httpRequest(jsonBuffer); // Reset the jsonBuffer for the next batch of data jsonBuffer[0] = '['; jsonBuffer[1] = '\0'; } lastUpdateTime = millis(); // Update the last update time } // Updates the ThingSpeakchannel with data void httpRequest(char* jsonBuffer) { // Format the data buffer as noted above char data[500] = "{\"write_api_key\":\"MY_API_KEY\",\"updates\":"; // Replace YOUR-CHANNEL-WRITEAPIKEY with your ThingSpeak channel write API key strcat(data,jsonBuffer); strcat(data,"}"); // Close any connection before sending a new request client.stop(); String data_length = String(strlen(data)+1); //Compute the data buffer length Serial.println(data); // POST data to ThingSpeak if (client.connect(server, 80)) { client.println("POST /channels/MY_CHANNEL_NUMBER/bulk_update.json HTTP/1.1"); // Replace YOUR-CHANNEL-ID with your ThingSpeak channel ID client.println("Host: api.thingspeak.com"); client.println("User-Agent: mw.doc.bulk-update (ESP8266)"); client.println("Connection: close"); client.println("Content-Type: application/json"); client.println("Content-Length: "+data_length); client.println(); client.println(data); String answer=getResponse(); Serial.println( answer ); } else { Serial.println("Failure: Failed to connect to ThingSpeak"); } delay(250); //Wait to receive the response client.parseFloat(); String resp = String(client.parseInt()); Serial.println("Response code:"+resp); // Print the response code. 202 indicates that the server has accepted the response jsonBuffer[0] = '['; //Reinitialize the jsonBuffer for next batch of data jsonBuffer[1] = '\0'; lastConnectionTime = millis(); //Update the last conenction time } String getResponse(){ String response; long startTime = millis(); delay( 200 ); while ( client.available() < 1 && (( millis() - startTime ) < TIMEOUT ) ){ delay( 5 ); } if( client.available() > 0 ){ // Get response from server. char charIn; do { charIn = client.read(); // Read a char from the buffer. response += charIn; // Append the char to the string response. } while ( client.available() > 0 ); } client.stop(); return response; }
Andrew Clark in Discussions
上次活动时间: 2023-2-2

Hello, I need help with a simple example. I am trying to do a simple bulk update using JSON & Javascript, using this parm, header and body: $.post("https://api.thingspeak.com/channels/xxxxxxxxx/bulk_update.json" ,{ "Content-Type": "application/json", "write_api_key" : "xxxxxxxxxxxxxxxx", "updates": [ { "created_at": "2022-12-30 10:26:2 -0800", "field1": 100, "status": "good" }, { "created_at": "2022-01-12 10:27:2 -0800", "field2": 100, "field3": 200, "field5": 600, "latitude": 123, "longitude": 23, "elevation": 34 }], function(data){ console.log(data) // log anything returned to the console } }); When I run the above (Mac OS, Chrome) I receive this error message: Access to XMLHttpRequest at 'https://api.thingspeak.com/channels/1283582/bulk_update.json' from origin 'https://backpaqlabs.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. api.thingspeak.com/channels/1283582/bulk_update.json:1 Failed to load resource: net::ERR_FAILED Is there something wrong with the syntax or JSON payload? I have literally taken it from the example in the doc, so I assume it should comply with the API endpoint. BTW, I am receiving CORS errors with getJSON as well, also from Javascript/Chrome. Appreciate any help. Thanks! Still getting CORS error with Bulk Update I think the browser route is specifically denied in the CORS policy on purpose. Ill see what else I can find out. Hi Christopher, thanks for the response. Javascript, which runs in browsers such as Chrome, is allowed, and I use it extensively to fetch data (ie, READ) from Thingspeak. The issue seems to be with WRITE/UPDATE, as all of my READ API functions are currently working. My question is concerning the WRITE function and what seems to be blocking my API calls on your server/endpoint. Can you please verify that UPDATE API requests (and specifically, BULK UPDATE) from outside THINGSPEAK.COM domain are allowed by your CORS policy to access the Thingspeak servers? Thanks much! The dev team agrees with you that bulk update via browser is not specifically prohibited in our policy. You should also have the header 'Access-Control-Allow-Origin' with the value set to *. Do you have this in your request? Christopher, thanks for the tip. Unfortunately, same result using 'Access-Control-Allow-Origin' : '*' in the request Header. I was able to get the "non-bulk" request to work, that is, "update.json". So it seems it's something specific to Bulk. I am also wondering if it could be something in the JSON body that's triggering the error...is the JSON syntax somehow position sensitive, ala Python (in other words, are invisible blanks or tabs a problem in the JSON?) Thanks! Hi Christopher, need to bump this Q as i am still unable to perform the Bulk Updates using JSON due to CORS errors. Can you please confirm with development that I should be able to do the bulk method as described in the above example? Thanks much! I tried out your code and I think this is not a ThingSpeak issue. If I change the URL to anything, I still get the error. Ill keep plugging though. Hi Christopher, thanks for taking a look! It may not be a Thingspeak issue per se, but may be something to do with how the JSON request is formatted or specified. I have tested the JSON and it's semantically correct and correctly formatted. To me, it's still a Thingspeak issue if simple Update API requests will not work or are so hard to use that users will give up. As has been mentioned several times prior, it would be great if we could do this request from the Library API or some other easier-to-use method. Thanks! bulk update json javascript cors
Jakub R in MATLAB Answers
上次活动时间: 2022-9-18

Hello, I have a problem to write a bulk-update using csv format. I have some experience with thingspeak using JSON update method, but I need to change it because *csv format would be more RAM-saving method in case of bulk update (less characters = less memory needed). I was trying to change my code written for JSON method to csv according to instructions: https://www.mathworks.com/help/thingspeak/bulkwritecsvdata.html but my example-string csv_feed is not accepted by Thingspeak server. I don't have ideas where is the problem, I tried to comment "//" some non-necessery lines, because in API's example, those lines doesn't appears in code. Thingspeak response code is always the same: 401, meaning "authorization required". But I wrote in code "write_api_key" properly I think (I'm sure that API key and channel number is the same as it is in channel settings on Thingspeak page). The key is authentic, I don't have important data on this channel and I'll change the key after getting rid problems with that code. Without success, I've been changing csv_feed content with diffrent time format (I'd like to use EPOCH time), diffrent number of commas representing field values and I'm still stuck with that code. Could anybody show my mistake? Thank you in advance, Jakub char csv_feed[] = "write_api_key%3DxxxxxxxxxxxxxxxxC%26time_format%3Dabsolute%26updates%3D1662883022%2C20%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C"; String csv_lenght = String(strlen(csv_feed)+1); if(client.connect(server, 80)){ client.println("POST /channels/647137/bulk_update.csv HTTP/1.1"); client.println("Host: api.thingspeak.com"); //client.println("User-Agent: mw.doc.bulk-update (Arduino ESP8266)"); //client.println("Connection: close"); client.println("Content-Type: application/x-www-form-urlencoded"); client.println("time_format: absolute"); //client.println("write_api_key: xxxxxxxxxxxxxxxx"); client.println("Content-Length: "+csv_lenght); client.println(); client.println(csv_feed);
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
avner gideobi in MATLAB Answers
上次活动时间: 2020-4-30

Using bulk upload in a CSV format to store IoT logs onto Thinkspeak. My IoT (ESP32) will bulk upload few (~10) lines by the end of a cycle, once in about an hour. However, ont he time line (both on automatic charts and on export) it seems that the lines are spread on over an hour, so actually I have no idea when this is really happened. the two following pictures demonstrates it well: both "points" where reported at the same time using a simple bulk update call, but reported like it was posted 40 minutes apart. this is consistent with the data exported in the export function. However there, it is 10-15 seconds shifted from the time on the chart... (?!?!!?) Why is the timestamp incorrect and spead over time ? How can I make sure I get the correct timestamp to a log submitted ? thanks!
Divyansh Deshmukh in MATLAB Answers
上次活动时间: 2020-2-19

How to write a matlab code to automate the bulk write procedure on Thingspeak
Alberto Maccioni in MATLAB Answers
上次活动时间: 2019-5-8

I'm not able to perform the bulk update using either an embedded system (ESP8266) or sending a POST request via web based service (https://reqbin.com). I always get a 400 response with "The request cannot be fulfilled due to bad syntax." No problem using the standard write data interface (but of course it's limited to one message). The POST request content is (I have my key instead of XX): write_api_key=XXXXXXXXXXXXXXXX&timeFormat=relative&updates=4,1.1,2,0.3,6,7.7,0.8,0,0,41.2,9,1,ok I did a lot of trials changing the number of parameters and the time format, but always with the same result. Is anyone able to perform any kind of bulk update?

关于 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.