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.
Mantas Dabrovolskas in MATLAB Answers
上次活动时间: 2023-6-21

I am making a simple temperature graph with Thingspeak. I want to use my cities publicly available meteorology API - https://api.meteo.lt/v1/stations/vilniaus-ams/observations/2023-06-21 How would I read the temperature data and put it onto graph (merge it with inside temperature)? Here is how I do it with my inside temperature sensor (I used standard Matlab example and edited it - added three hours to fix my time zone): [dustData,Timestamps]=thingSpeakRead(XXXXXXX,'Fields',1,'NumPoints',100); plot(Timestamps+hours(3),dustData,'red'); ylabel('Degrees'); title('Temperature'); grid on;
Simo Rounela in MATLAB Answers
上次活动时间: 2023-2-11

Hi! I'm using Advanticsys UCM-316 IoT gateway, with integrated analog inputs. It supports MQTT data upload, but I can't get it working with ThingSpeak MQTT server. There is username (not needed / blaablaa?) and password (Write API Key) authentication. No DNS server support, so broker URL 34.206.80.227 (mqtt.thingspeak.com), port 1883, subscriber identifier "ucm316". After IO configurations, it will make JSON message to be sent. It's fixed formation, cannot change it. Example: { "SN":"86004", "name":"ucm316", "header":{ "startTime":"2016-02-07T15:06:00.000Z", "endTime":"2016-02-07T15:06:00.000Z", "recordCount":2, "columns":{ "0":{ "id":"0", "name":"relay1", "dataType": "NUMBER", "format":"unsigned short" } }, "data":[ { "ts":"2016-02-07T15:06:00.000Z", "f":{ "0":{"v":0} } } ] } *This message is copied from manual, because user cannot access the actual JSON message.* There is no error log available. In the end, nothing happens. Most probably, because MQTT message formation is not correct. - Is there any way I could use this message structure with Thingspeak? And in case it's not visible, I'm not really a pro with MQTT yet =)
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
andy neo in Discussions
上次活动时间: 2022-8-31

Hi, I managed to get Thingspeak JSON data from my channel. However, I would like to know if theres any way to convert Thingspeak's JSON string data to decimal. I would really appreciate it if anyone could tell me how to convert it to decimal. Currently, this is my JSON: { "created_at": "2020-10-08T09:24:00.0000000Z", "entry_id": 1, "field1": "0", "field2": "12", "field3": "1", "field4": "1", "field5": "1", "field6": "1" } May I know if it is as simple as deleting the ""? For eg, { "created_at": "2020-10-08T09:24:00.0000000Z", "entry_id": 1, "field1": 0, "field2": 12, "field3": 1, "field4": 1, "field5": 1, "field6": 1 } Changing Thingspeak JSON string data to JSON decimal data Are you reading the JSON data with a device, or with MATLAB, or with some other code? MATLAB can definitely convert the JSON for you. On a device, you might consider using a JSON library, or reading a single field. When you read a single field, the number is returned as test, which might be easier for you to convert. Hi. I am sending sensor data from my hardware to Thingspeak. After getting the values in Thingspeak, I then write "https://api.thingspeak.com/channels/ChannelNumber/feeds/1.json" to get the JSON data in Thingspeak. The resulting result is: { "created_at": "2020-10-08T09:24:00.0000000Z", "entry_id": 1, "field1": "0", "field2": "12", "field3": "1", "field4": "1", "field5": "1", "field6": "1" } Hence, I was wondering if it is possible to change the strings for all of the fields to either decimal or integer. I sincerely apologize if I am asking a simple question but I just cant seem to figure out other than deleting the "", as the result of entry_id is an integer not a string. Hello I am facing the same problem. Sent data was 4.94, read data was 4.00. No idea why??? Sent data was 4.94 {"created_at":"2022-08-28T23:38:23-04:00","entry_id":1269,"field4":"4.94"}]} Read data was 4.00 Code: long PHTS = ThingSpeak.readLongField(counterChannelNumber, FieldNumber4, myCounterReadAPIKey); int statusCode = ThingSpeak.getLastReadStatus(); if (statusCode == 200) { Serial.print("ThingSpeak pH: "); Serial.println(PHTS); } How to display 4.94 (actual data) when reading the data. Thanks ThingSpeak.readFloatField json string to decimal
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 }
pema chowden in Discussions
上次活动时间: 2021-11-18

Hi there, I have tried to get the data on thingspeak via integrating it on the things Network. but the GPS data I am receiving on the thingspeak is delayed by 2 mins. is there any way I can code to send JSON file straight on thingspeak from the sensor or the gateway? Following is the ardiuno sketch #include <lmic.h> #include <hal/hal.h> #include <SPI.h> #include <SoftwareSerial.h> #include <TinyGPS.h> TinyGPS gps; SoftwareSerial ss(3, 4); // Arduino RX, TX to conenct to GPS module. static void smartdelay(unsigned long ms); unsigned int count = 1; //For times count String datastring1=""; String datastring2=""; String datastring3=""; uint8_t datasend[20]; //Used to store GPS data for uploading char gps_lon[20]={"\0"}; //Storage GPS info char gps_lat[20]={"\0"}; //Storage latitude char gps_alt[20]={"\0"}; //Storage altitude float flat, flon,falt; static uint8_t mydata[] = "Hello, world!"; //For test using. /* LoRaWAN NwkSKey, network session key This is the default Semtech key, which is used by the prototype TTN network initially. ttn*/ static const PROGMEM u1_t NWKSKEY[16] = { 0xE4, 0x2A, 0x93, 0x96, 0xF7, 0xC9, 0x65, 0x9E, 0xF8, 0x90, 0xC6, 0xA0, 0x1A, 0x88, 0xF7, 0x47 }; /* LoRaWAN AppSKey, application session key This is the default Semtech key, which is used by the prototype TTN network initially. ttn*/ static const u1_t PROGMEM APPSKEY[16] = { 0x75, 0x43, 0x26, 0xA1, 0x82, 0x79, 0x7F, 0xCF, 0x3C, 0x1D, 0xBF, 0xF9, 0xBF, 0xCB, 0xC6, 0xD9 }; /* LoRaWAN end-device address (DevAddr) See http://thethingsnetwork.org/wiki/AddressSpace ttn*/ static const u4_t DEVADDR = 0x260111D1; /* These callbacks are only used in over-the-air activation, so they are left empty here (we cannot leave them out completely unless DISABLE_JOIN is set in config.h, otherwise the linker will complain).*/ void os_getArtEui (u1_t* buf) { } void os_getDevEui (u1_t* buf) { } void os_getDevKey (u1_t* buf) { } static osjob_t initjob,sendjob,blinkjob; /* Schedule TX every this many seconds (might become longer due to duty cycle limitations).*/ const unsigned TX_INTERVAL = 20; // Pin mapping const lmic_pinmap lmic_pins = { .nss = 10, .rxtx = LMIC_UNUSED_PIN, .rst = 9, .dio = {2, 6, 7}, }; void do_send(osjob_t* j){ // Check if there is not a current TX/RX job running if (LMIC.opmode & OP_TXRXPEND) { Serial.println("OP_TXRXPEND, not sending"); } else { GPSRead(); GPSWrite(); // Prepare upstream data transmission at the next possible time. LMIC_setTxData2(1,datasend,sizeof(datasend)-1,0); //LMIC_setTxData2(1, mydata, sizeof(mydata)-1, 0); Serial.println("Packet queued"); Serial.print("LMIC.freq:"); Serial.println(LMIC.freq); Serial.println(""); Serial.println(""); Serial.println("Receive data:"); } // Next TX is scheduled after TX_COMPLETE event. } void onEvent (ev_t ev) { Serial.print(os_getTime()); Serial.print(": "); Serial.println(ev); switch(ev) { case EV_SCAN_TIMEOUT: Serial.println("EV_SCAN_TIMEOUT"); break; case EV_BEACON_FOUND: Serial.println("EV_BEACON_FOUND"); break; case EV_BEACON_MISSED: Serial.println("EV_BEACON_MISSED"); break; case EV_BEACON_TRACKED: Serial.println("EV_BEACON_TRACKED"); break; case EV_JOINING: Serial.println("EV_JOINING"); break; case EV_JOINED: Serial.println("EV_JOINED"); break; case EV_RFU1: Serial.println("EV_RFU1"); break; case EV_JOIN_FAILED: Serial.println("EV_JOIN_FAILED"); break; case EV_REJOIN_FAILED: Serial.println("EV_REJOIN_FAILED"); break; case EV_TXCOMPLETE: Serial.println("EV_TXCOMPLETE (includes waiting for RX windows)"); if(LMIC.dataLen) { // data received in rx slot after tx Serial.print("Data Received: "); Serial.write(LMIC.frame+LMIC.dataBeg, LMIC.dataLen); Serial.println(); } // Schedule next transmission os_setTimedCallback(&sendjob, os_getTime()+sec2osticks(TX_INTERVAL), do_send); break; case EV_LOST_TSYNC: Serial.println("EV_LOST_TSYNC"); break; case EV_RESET: Serial.println("EV_RESET"); break; case EV_RXCOMPLETE: // data received in ping slot Serial.println("EV_RXCOMPLETE"); break; case EV_LINK_DEAD: Serial.println("EV_LINK_DEAD"); break; case EV_LINK_ALIVE: Serial.println("EV_LINK_ALIVE"); break; default: Serial.println("Unknown event"); break; } } void setup() { // initialize digital pin as an output. Serial.begin(9600); ss.begin(9600); while(!Serial); Serial.println("LoRa GPS Example---- "); Serial.println("Connect to TTN"); #ifdef VCC_ENABLE // For Pinoccio Scout boards pinMode(VCC_ENABLE, OUTPUT); digitalWrite(VCC_ENABLE, HIGH); delay(1000); #endif // LMIC init os_init(); // Reset the MAC state. Session and pending data transfers will be discarded. LMIC_reset(); /*LMIC_setClockError(MAX_CLOCK_ERROR * 1/100); Set static session parameters. Instead of dynamically establishing a session by joining the network, precomputed session parameters are be provided.*/ #ifdef PROGMEM /* On AVR, these values are stored in flash and only copied to RAM once. Copy them to a temporary buffer here, LMIC_setSession will copy them into a buffer of its own again.*/ uint8_t appskey[sizeof(APPSKEY)]; uint8_t nwkskey[sizeof(NWKSKEY)]; memcpy_P(appskey, APPSKEY, sizeof(APPSKEY)); memcpy_P(nwkskey, NWKSKEY, sizeof(NWKSKEY)); LMIC_setSession (0x1, DEVADDR, nwkskey, appskey); #else // If not running an AVR with PROGMEM, just use the arrays directly LMIC_setSession (0x1, DEVADDR, NWKSKEY, APPSKEY); #endif // Disable link check validation LMIC_setLinkCheckMode(0); // TTN uses SF9 for its RX2 window. LMIC.dn2Dr = DR_SF9; // Set data rate and transmit power (note: txpow seems to be ignored by the library) LMIC_setDrTxpow(DR_SF7,14); // Start job do_send(&sendjob); } void GPSRead() { unsigned long age; gps.f_get_position(&flat, &flon, &age); falt=gps.f_altitude(); //get altitude flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flat, 6; flon == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flon, 6;//save six decimal places falt == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : falt, 2;//save two decimal places } void GPSWrite() { /*Convert GPS data to format*/ datastring1 +=dtostrf(flat, 0, 4, gps_lat); datastring2 +=dtostrf(flon, 0, 4, gps_lon); //datastring3 +=dtostrf(falt, 0, 2, gps_alt); if(flon!=1000.000000) { strcat(gps_lon,","); strcat(gps_lon,gps_lat); //strcat(gps_lon,","); //strcat(gps_lon,gps_alt); int i = 0; for(i = 0; i < 2; i++) { //datasend.toFloat(); atof(gps_lon); //Serial.println((char*)datasend); Serial.println("Testing converted data:"); Serial.println(gps_lon); // atof(gps_alt); // Serial.print(gps_alt); } strcpy(datasend,gps_lon); //the format of datasend is longtitude,latitude,altitude Serial.print("########### "); Serial.print("NO."); Serial.print(count); Serial.println(" ###########"); Serial.println("The longtitude and latitude are:"); Serial.print("["); Serial.print((char*)datasend); Serial.print("]"); Serial.print(""); /* for(int k = 0; k < 20;k++) { Serial.print("["); Serial.print(datasend[k], HEX); Serial.print("]"); } Serial.println(""); Serial.println("");*/ count++; } int32_t lng = flat * 10000; int32_t lat = flon * 10000; datasend[0] = lat; datasend[1] = lat >> 8; datasend[2] = lat >> 16; datasend[3] = lng; datasend[4] = lng >> 8; datasend[5] = lng >> 16; smartdelay(1000); } static void smartdelay(unsigned long ms) { unsigned long start = millis(); do { while (ss.available()) { gps.encode(ss.read()); } } while (millis() - start < ms); } void loop() { os_runloop_once(); } Lora/shield GPS to thingspeak issue What is your target time? You might consider using a mobile connection if one is available in you area. There is a smaller delay if you just posted directly to ThingSpeak. thingspeak thingsnetwork mqtt json
Pavel ASCA in Discussions
上次活动时间: 2021-6-16

How to read a JSON output of another device and put data into thingspeak channel: Maybe useful for others: just figured out, how to parse a JSON, search for a special entry and enter searched value into thingspeak. For this I used the "Get data from webpage" template. An internet search resolved the access to the UBIBOT-json output at "https://api.ubibot.com/channels?account_key=XXX". This is used as "spcify URL". Having several devices a unique search keyword is needed and added in the template for the "specify target string". So even if the template is not 100% for JSON abjects, this is a quick and dirty method to read it out and use it for further analysis. Full code is this: % TODO - Specify URL of the page to read data from url = 'https://api.ubibot.com/channels?account_key=yyy'; % TODO - Specify the target string to search in webpage targetString = 'last_values":"{\"field3\":{\"value\":'; % TODO - Replace the [] with channel ID to write data to: writeChannelID = 666; % TODO - Enter the Write API Key between the '' below: writeAPIKey = 'yxc'; %% Scrape the webpage %% data = urlfilter(url, targetString); %% Analyze Data %% analyzedData = data; %% Write Data %% thingSpeakWrite(writeChannelID, analyzedData, 'WriteKey', writeAPIKey); parse a JSON into a thingspeak channel - here UBIBOT device Do you need an account at ubibot to use this? You could easily do this transformation in MATLAB (within ThingSpeak), then you wouldn't need to include another API. json parse ubibot external devices
Sergey Trofimov in MATLAB Answers
上次活动时间: 2018-6-22

I am trying to use "Bulk-Write JSON Data" API to send some data to my ThingSpeak channel: <https://www.mathworks.com/help/thingspeak/bulkwritejsondata.html> My channel has four fields: "Temperature", "Pressure", "Humidity", "CO2" My POST query looks like this: POST /channels/XXXXXX/bulk_update.json HTTP/1.1 Host: api.thingspeak.com Content-Type: application/json { "write_api_key": "XXXXXXXXXXXXXXXX", "updates": [{ "created_at": "2018-06-21 22:21:40 +0300", "Temperature": "27.51", "Pressure": "746.02576", "Humidity": "42.407", "CO2": "978" } ] } But server returns response: { "status": "400", "error": { "error_code": "error_bad_request", "message": "Bad Request", "details": "The request cannot be fulfilled due to bad syntax." } } What is wrong?

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