Unable to clear a channel via Arduino | ThingSpeak API | Forum

Avatar

Please consider registering
Guest

Search

— Forum Scope —






— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

Register Lost password?
sp_Feed sp_TopicIcon
Unable to clear a channel via Arduino
Avatar
melanogaster

Silver
Forum Posts: 5
sp_UserOfflineSmall Offline
1
June 30, 2017 - 7:15 pm
sp_Permalink sp_Print sp_EditHistory

Hello,

i am desperatly trying to clear a Channel with an Arduino Uno.

Can someone help me to Change the Code i am using for updateing the channel into a Code for Clearing the channel?

 

if (client.connect(server,80)) { // "184.106.153.149" or api.thingspeak.com
   String postStr = apiKeyChannel;
   postStr +="&field1=";
   postStr += str_sensor;
   client.println("POST /update HTTP/1.1");
   client.println("Host: api.thingspeak.com");
   client.println("Connection: close");
   client.println("X-THINGSPEAKAPIKEY: "+apiKeyChannel);
   client.println("Content-Type: application/x-www-form-urlencoded");
   client.print("Content-Length: ");
   client.println(postStr.length());
   client.println();
   client.print(postStr);
   }

I found this Code on the Internet and works fine for updating my channel.

 

I have read the Documentation to clear a channel (which can be found here: https://de.mathworks.com/help/thingspeak/clear-a-channel.html)  and thougt all i have to do is enter my User API Key, Change the adress, enter channel ID, etc...

But nothing worked.

Also I am new to this IOT Thing.

Please, can somebody help me?

Avatar
Hans
Natick, MA

Forum Posts: 390
sp_UserOfflineSmall Offline
2
July 1, 2017 - 4:36 am
sp_Permalink sp_Print

Sounds like you are going in the right direction. Have you tried changing the HTTP method? 

client.print("POST... 

Change to

client.print("DELETE... 

Avatar
melanogaster

Silver
Forum Posts: 5
sp_UserOfflineSmall Offline
3
July 2, 2017 - 12:17 pm
sp_Permalink sp_Print

Thanks for the Reply.

I allready tried many changes in the HTTP method.

 

For example i tried this Code:

String apiKeyUser = XXXXXXXXXXXXXXXXXXXX;

String apiKeyChannel = XXXXXXXXXXXXXXXXXXXX;

String channelID = XXXXXX;

if (client.connect(server,80)) { // "184.106.153.149" or api.thingspeak.com
   String postStr = "api_key=";

   postStr += apiKeyUser;
   client.println("DELETE /channels/"+channelID+"/feeds HTTP/1.1");
   client.println("Host: api.thingspeak.com");
   client.println("Connection: close");
   client.println("X-THINGSPEAKAPIKEY: "+apiKeyChannel);
   client.println("Content-Type: application/x-www-form-urlencoded");
   client.print("Content-Length: ");
   client.println(postStr.length());
   client.println();
   client.print(postStr);
}

 

I thought it would work, but it did not.

Also i have no idea why there is this part of the Code:

  client.println("Host: api.thingspeak.com");
   client.println("Connection: close");
   client.println("X-THINGSPEAKAPIKEY: "+apiKeyChannel);
   client.println("Content-Type: application/x-www-form-urlencoded");
   client.print("Content-Length: ");
   client.println(postStr.length());
   client.println();

I think it is there for HTTP coding or something like that.

Still, the Code does not work...

Any ideas?

Avatar
rw950431

Forum Posts: 252
sp_UserOfflineSmall Offline
4
July 2, 2017 - 10:46 pm
sp_Permalink sp_Print

The docs for delete are at https://au.mathworks.com/help/thingspeak/delete-a-channel.html

You definitely need the user API key not the channel one.

Its possible that DELETE will only work over HTTPS so wont be possible from Arduino.  You should get an add-on to your browser (eg HttpRequester for firefox) that allows you to send a DELETE requests and check if this is the case or not.

Avatar
melanogaster

Silver
Forum Posts: 5
sp_UserOfflineSmall Offline
5
July 3, 2017 - 5:59 am
sp_Permalink sp_Print sp_EditHistory

Thanks for the Answer.

As it reads in the Thread Title I am trying to CLEAR a Channel with an Arduino.

Clearing and deleting a channel use the same DELETE but different adresses.

Clearing Channel: DELETE to https://api.thingspeak.com/channels/CHANNEL_ID/feeds

Deleting channel: DELETE to https://api.thingspeak.com/channels/CHANNEL_ID

Updating channel: POST to https://api.thingspeak.com/update

 

Updating the channel works fine with the Code i wrote in the first post, even without https from my arduino.

So POST works with http, but DELETE works only with https? That seems Kind of wierd to me. Also I don't think I am the first one who tries to clear a Channel via Arduino.

 

Thanks for the browser plugin suggestion, i will try that later.

But have you looked at my Code? Can you tell me what i have to Change for it to work?

Avatar
melanogaster

Silver
Forum Posts: 5
sp_UserOfflineSmall Offline
6
July 3, 2017 - 9:22 am
sp_Permalink sp_Print

Trial and Error got me the right Code.

If anyone got the same Problem, here is my working Code:

//"Clear a Channel" https://de.mathworks.com/help/thingspeak/clear-a-channel.html

channelID = "XXXXX";
apiKeyChannel = "XXXXXXXXXXXXXXXXX";
apiKeyUser = "XXXXXXXXXXXXXXXXX";

String postStr = "api_key="+apiKeyUser;

client.println("DELETE /channels/"+channelID+"/feeds HTTP/1.1");
client.println("Host: api.thingspeak.com");
client.println("Connection: close");
client.println("Content-Type: application/x-www-form-urlencoded");
client.print("Content-Length: ");
client.println(postStr.length());
client.println();
client.print(postStr);

 

//"Update a Channel Feed" https://de.mathworks.com/help/thingspeak/update-channel-feed.html

channelID = "XXXXX";
apiKeyChannel = "XXXXXXXXXXXXXXXXX";
apiKeyUser = "XXXXXXXXXXXXXXXXX";

String str_sensor = 12345;

String postStr = "api_key="+apiKeyChannel+"&field1="+str_sensor;

client.println("POST /update HTTP/1.1");
client.println("Host: api.thingspeak.com");
client.println("Connection: close");
client.println("Content-Type: application/x-www-form-urlencoded");
client.print("Content-Length: ");
client.println(postStr.length());
client.println();
client.print(postStr);
Avatar
tingtaler
New Member
Forum Posts: 1
sp_UserOfflineSmall Offline
7
November 2, 2017 - 5:02 pm
sp_Permalink sp_Print

Can someone help me. I try too get my code too work.

I use esp 8266 nodeMCU 1.0 with arduino 1.6.9. It do nothing.

#include "ESP8266WiFi.h"          
const char* server = "api.thingspeak.com";
// WiFi parameters
const char* ssid = "HomeBox";
const char* password = "dacxxx";
String ApiKeyUser = "xxxxxxx";
String WriteApiKey = "xxxxxxxxxx";
String ReadApiKey ="xxxxxxxxxx";
String channelID = "354732";   //354732
const char* tsData="field2=";

void setup(void){
   Serial.begin(115200);      // Start Serial
    WiFi.begin(ssid, password);       // Connect to WiFi
    while (WiFi.status() != WL_CONNECTED)  {delay(500);Serial.print(".");}
    Serial.println();Serial.println(WiFi.localIP());

   }

void loop(){

  WiFiClient client;
      String postStr = "api_key="+ApiKeyUser;
   if (client.connect(server,80)) {
      client.println("DELETE /channels/"+channelID+"/feeds HTTP/1.1");
      client.println("Host: api.thingspeak.com");
      client.println("Connection: close");
      client.println("Content-Type: application/x-www-form-urlencoded");
      client.print("Content-Length: ");
      client.print(postStr.length());
      client.println();}
      delay(2000);
      Serial.print(" - ");
      }

I can change the code and upload data without any problem.

Hope someone can see what I do wrong.

Avatar
cstapels

Gold
Forum Posts: 123
sp_UserOfflineSmall Offline
8
November 6, 2017 - 3:26 pm
sp_Permalink sp_Print

tingtaler,

I think you arent posting the "postStr"  Look at the code above yours:

13 client.print("Content-Length: ");
14 client.println(postStr.length());
15 client.println();
16 client.print(postStr);

But you don't include that last line, so the user API key is never sent:

     client.print("Content-Length: ");
      client.print(postStr.length());
      client.println();}
      delay(2000);
      Serial.print(" - ");

 

If you look at the response, it should provide a 401 "error_auth_required", or something similar.

Forum Timezone: America/New_York

Most Users Ever Online: 114

Currently Online: jacktheripper125, pudderz
25 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

rw950431: 252

Vinod: 164

cstapels: 96

piajola: 75

vespapierre: 63

Adarsh_Murthy: 59

Newest Members:

bbruno22

pudderz

Utkya

Poornak26

LindasHed

reddysiva

Forum Stats:

Groups: 4

Forums: 17

Topics: 1313

Posts: 4565

 

Member Stats:

Guest Posters: 1

Members: 5703

Moderators: 0

Admins: 2

Administrators: Hans, lee