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

Avatar

Please consider registering
Guest

sp_LogInOut Log In sp_Registration Register

Register | Lost password?
Advanced Search

— Forum Scope —






— Match —





— Forum Options —





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

sp_Feed sp_TopicIcon
Unable to clear a channel via Arduino
No permission to create posts
June 30, 2017
7:15 pm
Avatar
melanogaster

Silver
Members
Forum Posts: 5
Member Since:
June 30, 2017
sp_UserOfflineSmall Offline

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?

July 1, 2017
4:36 am
Avatar
Hans
Natick, MA

Admin
Forum Posts: 400
Member Since:
January 18, 2011
sp_UserOfflineSmall Offline

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

client.print("POST... 

Change to

client.print("DELETE... 

July 2, 2017
12:17 pm
Avatar
melanogaster

Silver
Members
Forum Posts: 5
Member Since:
June 30, 2017
sp_UserOfflineSmall Offline

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?

July 2, 2017
10:46 pm
Avatar
rw950431

Top
Members
Forum Posts: 271
Member Since:
January 30, 2014
sp_UserOfflineSmall Offline

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.

July 3, 2017
5:59 am
Avatar
melanogaster

Silver
Members
Forum Posts: 5
Member Since:
June 30, 2017
sp_UserOfflineSmall Offline

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?

July 3, 2017
9:22 am
Avatar
melanogaster

Silver
Members
Forum Posts: 5
Member Since:
June 30, 2017
sp_UserOfflineSmall Offline

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);
November 2, 2017
5:02 pm
Avatar
tingtaler
New Member
Members
Forum Posts: 1
Member Since:
October 31, 2017
sp_UserOfflineSmall Offline

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.

November 6, 2017
3:26 pm
Avatar
cstapels
Moderator
Members


Moderators
Forum Posts: 332
Member Since:
March 7, 2017
sp_UserOfflineSmall Offline

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:
29 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

rw950431: 261

Vinod: 196

piajola: 85

turgo: 70

vespapierre: 63

Adarsh_Murthy: 62

Member Stats:

Guest Posters: 1

Members: 5703

Moderators: 0

Admins: 2

Forum Stats:

Groups: 4

Forums: 17

Topics: 1313

Posts: 4565

Newest Members:

James181, kimberlypg2, JamesAdorb, waynesmith, paulettebw60, ashleytb4

Administrators: Hans: 387, lee: 457