How to clean data of a channel by sending a request on python ?

Arthur SOGHOYAN on 7 Jun 2023
Latest activity Reply by avaq on 13 Jun 2023

Hi,
I am using ThingSpeak with Raspberry Pi Pico W, by using python langage.
I am sending some pressure data from the raspberry with this method :
request = urequests.post('http://api.thingspeak.com/update?api_key=' + THINGSPEAK_WRITE_API_KEY, json=dht_readings, headers=HTTP_HEADERS)
And it works pretty good, i have fields with the new data every 15sec.
But now i am trying send a CLEAR request by using the following method :
url = 'http://api.thingspeak.com/channels/{}/fields/{}.json?api_key={}&days={}'.format(CHANNEL_ID, FIELD_NUMBER , THINGSPEAK_WRITE_API_KEY, NUM_DAYS )
clear_request = urequests.delete(url)
Nothing to be done.
I also tried to clean or delete directly all info in the channel and not only the fields' data by this method :
delete_url = 'https://api.thingspeak.com/channels/{}/feeds?api_key={}'.format(CHANNEL_ID,THINGSPEAK_WRITE_API_KEY)
clear_request = urequests.delete(delete_url)
Still not working.
I tired several ways, clearing the channel, deleting the channel, sending some empty update... none of them works.
Clear Channel is working only from the website when clicking on the button, i would like to do it with a request.
So to make a little summary of what i am looking for : I want to automatically send a clear request from my microcontroller to thingspeak channel 1 each X times.
Is there any way to make it possible ?
Thank you for your answer.
Sincerly Arthur
avaq
avaq on 13 Jun 2023
It seems that you are trying to send a clear request to ThingSpeak to remove data from a channel using a microcontroller. To achieve the functionality you are looking for, you can consider an alternative approach. Instead of trying to clear the channel directly from the microcontroller, you can use a workaround by updating the channel with empty or null values for the fields. Here's an example:
import urequests
# Clear all fields in the channel by sending empty/null values
clear_data = {"field1": "", "field2": "", "field3": ""} # Update with the relevant field names
url = 'http://api.thingspeak.com/update?api_key=' + THINGSPEAK_WRITE_API_KEY
response = urequests.post(url, json=clear_data)
if response.status_code == 200:
print("Channel cleared successfully!")
else:
print("Failed to clear the channel.")
In this approach, you update each field in the channel with empty or null values using the urequests.post method. This effectively clears the field data in the channel.
Christopher Stapels
Christopher Stapels on 7 Jun 2023 (Edited on 7 Jun 2023)
Have a look at the clear chanel page, I think you have the syntax incorrect. Be sure to remember that modifying channel operations require the user API key and not the channel API key.
EDIT updataed to clear channel (@Arthur SOGHOYAN thanks for posting the right format):
Arthur SOGHOYAN
Arthur SOGHOYAN on 7 Jun 2023 (Edited on 7 Jun 2023)
Thank you,
I missed the information about the Key, that's where the problem came from, i was effectively using Channel API Key.
And the right syntaxt of cleaning the channel is this one :
delete_url = f'https://api.thingspeak.com/channels/{CHANNEL_ID}/feeds?api_key={USER_API_KEY}'
request = urequests.delete(delete_url)
With f strings or .format