Mahesh Prasath in Discussions
上次活动时间: 2025-1-11

in the below code write is working fine, but read is failing ( 404 error) can you please help me reslove this. /* Go to thingspeak.com and create an account if you don't have one already. After logging in, click on the "New Channel" button to create a new channel for your data. This is where your data will be stored and displayed. Fill in the Name, Description, and other fields for your channel as desired, then click the "Save Channel" button. Take note of the "Write API Key" located in the "API keys" tab, this is the key you will use to send data to your channel. Replace the channelID from tab "Channel Settings" and privateKey with "Read API Keys" from "API Keys" tab. Replace the host variable with the thingspeak server hostname "api.thingspeak.com" Upload the sketch to your ESP32 board and make sure that the board is connected to the internet. The ESP32 should now send data to your Thingspeak channel at the intervals specified by the loop function. Go to the channel view page on thingspeak and check the "Field1" for the new incoming data. You can use the data visualization and analysis tools provided by Thingspeak to display and process your data in various ways. Please note, that Thingspeak accepts only integer values. You can later check the values at https://thingspeak.com/channels/2005329 Please note that this public channel can be accessed by anyone and it is possible that more people will write their values. */ #include <WiFi.h> const char *ssid = "xxxx"; // Change this to your WiFi SSID const char *password = "xxxxx"; // Change this to your WiFi password const char *host = "api.thingspeak.com"; // This should not be changed const int httpPort = 80; // This should not be changed const String channelID = "2805914"; // Change this to your channel ID const String writeApiKey = "xxxxxxxxxxxxxxxx"; // Change this to your Write API key const String readApiKey = "xxxxxxxxxxxxxxxx"; // Change this to your Read API key // The default example accepts one data filed named "field1" // For your own server you can ofcourse create more of them. int field1 = 20; //int field1 = 20; int numberOfResults = 1; // Number of results to be read int fieldNumber = 1; // Field number which will be read out void setup() { Serial.begin(115200); while (!Serial) { delay(100); } // We start by connecting to a WiFi network Serial.println(); Serial.println("******************************************************"); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); } void readResponse(NetworkClient *client) { unsigned long timeout = millis(); while (client->available() == 0) { if (millis() - timeout > 5000) { Serial.println(">>> Client Timeout !"); client->stop(); return; } } // Read all the lines of the reply from server and print them to Serial while (client->available()) { String line = client->readStringUntil('\r'); Serial.print(line); } Serial.printf("\nClosing connection\n\n"); } void loop() { NetworkClient client; String footer = String(" HTTP/1.1\r\n") + "Host: " + String(host) + "\r\n" + "Connection: close\r\n\r\n"; // WRITE -------------------------------------------------------------------------------------------- if (!client.connect(host, httpPort)) { return; } client.print("GET /update?api_key=" + writeApiKey + "&field1=" + field1 + footer); readResponse(&client); delay(200); // READ -------------------------------------------------------------------------------------------- String readRequest = "GET /channels/" + channelID + "/fields/" + fieldNumber + ".json?results=" + numberOfResults + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n"; if (!client.connect(host, httpPort)) { return; } client.print(readRequest); readResponse(&client); // ------------------------------------------------------------------------------------------------- //++field1; delay(10000); } I am using example program WiFiClient, write is working fine, but read is giving error 404 A couple of comments: 404 is not found, so I would suggest a serial.println on this line so you can see what is actually getting sent. Then try it in your browser wndow you help you trouble shoot it. String readRequest = "GET /channels/" + channelID + "/fields/" + fieldNumber + ".json?results=" + numberOfResults + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n"; it should look something like this: https://api.thingspeak.com/channels/2005329/field/1.json?results=4 I would suggest explicitly including the host address (api.thingspeak.com). You can also try to verify that you have the channel ID correct. We really reccomend you use the ThingSpeak library for arduino. It takes care of a lot of hard work for you. You can definitely send non integer values to thingspeak. The interfect trats everything as a string, but then the thingspeak plots will attempt to interprest numbes as integers or floats or what have you. API keys are kind of like passwords, so I have redacted them in your code above. Good luck on your project, please let us know if you get it working! Hi Chris, Thanks for your response, I got it sloved, readApikey was missing i added it as in the below and the code worked. String readRequest = "GET /channels/" + channelID + "/fields/" + fieldNumber + ".json?api_key=" + readApiKey + "&results=" + numberOfResults + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n"; read 404 error
Ege Hassürücü in Discussions
上次活动时间: 2024-3-26

Hello Everyone, I'm running an IoT project at my university using a free . Basically, my system uploads the sensor data to 4 different fields of a ThingSpeak channel and I'm reading from or writing to the channel through Simulink. I have a few questions regarding this topic: I'm trying to upload to the server every 3 seconds (which seemed to be providing the best results so far). Since the channel only accepts data transmit every 15 seconds, what is the optimized upload interval rate to match the interval rate of the channel? Because otherwise I have realized that my interval rate can go up to 1min sometimes for some reason. Is reading from/writing to the channel through simulink while the system is uploading data to the channel slow down th whole process? What is your overall suggestion in this case? I'm excited to hear your suggestions and experiences! Regards, Ege Does reading data from Simulink, slow down my server upload? I would prefer if you can stop trying to update every 3 seconds. This can result in a lot of wasted server time for us. Though your model may be putting other delays in there, Im not sure how to profile your model to be sure. Maybe Ill ask my Simulink firends about model profiling. I do recall having a similar issue with some Simulink models that I had made, Like this solar tracker. I recall there are settings in the model to change the timing to be closer to real time.. But I cannot report the details here off the top of my head. If you figure it out, please let us know here. For #2 are you asking if adding ThingSpeak can impede your model performance? Hi @Christopher Stapels, I solved the issue, thanks for your quick response! Also, reading the data from simulink doesn't seem to be effecting the performance of the channel now so far. The main issue was how i create the http request, in the new version I used your ESP32 library and now I'm able to update every 15.5 seconds. Here are the old and new versions: Old version: void loop() { unsigned long currentMillis = millis(); unsigned long interval = currentMillis - previousMillis; if (interval > UPLOAD_RATE) { // Create HTTP request String http_str = "GET /update?api_key=" + Apikey + "&field1=" + String(messageData1) + "&field2=" + String(messageData2) + "&field3=" + String(messageData3) + " HTTP/1.1\r\n"; http_str += "Host: " + server + "\r\n"; http_str += "Connection: close\r\n\r\n"; if(WiFi.status() != WL_CONNECTED) { Serial.print("Wifi connection failed. Attempting to reconnect..."); connectToWiFi(); delay(5000); } else { Serial.println("Connected to WiFi\n"); // Connect to the ThingSpeak server connectToThingSpeak(); // Make the HTTP request client.print(http_str); client.stop(); previousMillis = currentMillis; // Update the last time the action was performed } } delay(100); } void connectToThingSpeak() { if (client.connect(server.c_str(), 80)) { Serial.println("Connected to ThingSpeak server"); } else { Serial.println("Connection to ThingSpeak server failed"); } } New version: void loop() { unsigned long currentMillis = millis(); unsigned long interval = currentMillis - previousMillis; if (interval > UPLOAD_RATE) { ThingSpeak.setField(1,messageData1); ThingSpeak.setField(2,messageData2); ThingSpeak.setField(3,messageData3); ThingSpeak.setField(4,trialMessage); ThingSpeak.writeFields(CHANNELID,APIKEY); Serial.println("Data uploaded to the ThingSpeak server."); previousMillis = currentMillis; // Update the last time the action was performed } delay(100); } simulink interval read write iot
IOT4ALL in Discussions
上次活动时间: 2022-3-21

Hello! I`ve got problems with reading last input of a channel field using AT commands, here follows the sketch I'm currently using: /* String tx_data_r = "GET https://api.thingspeak.com/channels/CHANNEL/fields/4/last.txt?api_key=API_READ_KEY&results=1"; */ String tx_data_r = "GET https://api.thingspeak.com/channels/"; tx_data_r += "CHANNEL"; tx_data_r += "/"; tx_data_r += "fields"; tx_data_r += "/4/"; tx_data_r += "last.txt?api_key="; tx_data_r += "API_READ_KEY"; tx_data_r += "&results=1"; tx_data_r += "\r\n"; Serial.print("AT+CIPSEND="); delay(1000); Serial.println(tx_data_r.length()); delay(1000); Serial.println(tx_data_r); delay(1000); This piece of code doesn`t return a valid response, however if I copy/paste https://api.thingspeak.com/channels/CHANNEL/fields/4/last.txt?api_key=API_READ_KEY&results=1" using a browser I get the right data. One more thing, if instead of using a microcontroller I use an USB serial PC interface program directly connected to my RF module, with GET https://api.thingspeak.com/channels/CHANNEL/fields/4/last.txt?api_key=API_READ_KEY&results=1 along with other commands I get the right data too! Can anybody tell me what's going on? There are no problems with any read related commands. If a working sketch is available it will be most welcome! Tks in advance for any help! Issues using AT commands to read a channel field Sorry: There are no problems with any write related commands. I've seen some advice not to include the https:\\, or to connect to the server separately from sending the command to read data to the server as in <https://arduino.stackexchange.com/questions/32567/get-data-from-website-with-esp8266-using-at-commands this post> . M y advice is generally not to use the AT command set. What is your hardware and why do you need to use AT commands? atcommands read
Danielle Honigstein in MATLAB Answers
上次活动时间: 2021-5-5

Hi all, I read the data from my channel using thingSpeakRead, but the status of the channel doesn't seem to be available. How do I read that from Matlab (not using REST or MQTT)? EDIT: Code I am using: lastData = thingSpeakRead(channelID,'OutputFormat','TimeTable','ReadKey','MY_READ_KEY') What is printed out: lastData = 1×5 timetable Timestamps AppName Action Address Version Extra ______________ ______________ _________ _________ _________ ____________ 04-May-2021 14:09:25 {'Data1'} {'Data2'} {'Data3'} 3.2 {'Data4'} I would have expected a column of "Status", as my channel has the status enabled. It is not one of the fields, it was from enabling "Show Status" in the channel settings. Thanks, Danielle
Yacine Hammouche in MATLAB Answers
上次活动时间: 2019-5-10

Hello, First of all, sorry for my english, im a french student and my english is far from being perfect. Im trying to transmit location data from an ARDUINO ESP8266 device that has wifi. I stock the lat ant longitude data in field 3 and field 4 after transmetting them. Now, I would like to use matlab to read those data, stocking them into 2 variables, then use thigspeakwrite, to write those variables into location data. I did this code below, it says that is it ok, but the latitude and longitude fields dont change. % Template MATLAB code for reading data from a private channel, analyzing % the data and storing the analyzed data in another channel. % Prior to running this MATLAB code template, assign the channel ID to read % data from to the 'readChannelID' variable. Since this is a private % channel, also assign the read API Key to the 'readAPIKey' variable. You % can find the read API Key in the right side pane of this page. % To store the analyzed data, you will need to write it to a channel other % than the one you are reading data from. Assign this channel ID to the % 'writeChannelID' variable. Also assign the write API Key to the % 'writeAPIKey' variable below. You can find the write API Key in the right % side pane of this page. % TODO - Replace the [] with channel ID to read data from: readChannelID = [123456]; % TODO - Enter the Read API Key between the '' below: readAPIKey = 'AAABBB'; % TODO - Replace the [] with channel ID to write data to: writeChannelID = [123456]; % TODO - Enter the Write API Key between the '' below: writeAPIKey = 'XXXYYY'; %% Read Data %% a= thingSpeakRead(readChannelID,'Fields',[3],'ReadKey', readAPIKey); b= thingSpeakRead(readChannelID,'Fields',[4],'ReadKey', readAPIKey); %% Analyze Data %% % Add code in this section to analyze data and store the result in the % analyzedData variable. %analyzedData = data; %% Write Data %% thingSpeakWrite(readChannelID,'Location',[a,b,3500],'WriteKey','XXXYYY')

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