Durkesh
Published

Thingspeak get post

A random value will be updated from Python to Thingspeak Cloud and similarly, the stored data can be fetched from Thingspeak to Shell.

IntermediateFull instructions provided3 hours2,303
Thingspeak get post

Things used in this project

Hardware components

Raspberry Pi 3 Model B
Raspberry Pi 3 Model B
×1

Software apps and online services

Raspberry Pi Raspbian OS
ThingSpeak API
ThingSpeak API

Story

Read more

Schematics

Channel Dash board

Graphs, and field views can be viewed in this Panel.

API keys and Channel ID

Channel ID and API Keys are listed in this Panel.

Code

Upload data

Python
Random Data from the selected range will be uploaded to the cloud.The field views are mentioned accordingly.
import urllib.request
import requests
import threading
import json

import random

def thingspeak_post():
    threading.Timer(15,thingspeak_post).start()
    val=random.randint(1,3)
    URl='https://api.thingspeak.com/update?api_key='mention key'&field1=0'
    KEY= 'mention key'
    HEADER='&field1={}&field2={}&field3'.format(val,val)
    NEW_URL = URl+KEY+HEADER
    print(NEW_URL)
    data=urllib.request.urlopen(NEW_URL)
    print(data)
    
if __name__ == '__main__':
    thingspeak_post()

Retrieve data

Python
The Data uploaded is ' formatted ' and decoded to utf-8 format .The field values which are earlier stored, can be retrieved using' json'.
from urllib.request import urlopen

import json
import time


READ_API_KEY=' '
CHANNEL_ID='mention id '


while True:
    TS = urlopen("https://api.thingspeak.com/channels/  'id'/feeds.json?api_key='mention key'&results=2".format(CHANNEL_ID,READ_API_KEY))

    response = TS.read()

    data=json.loads(response.decode('utf-8'))

    print(data)

    print (data["feeds"][1]["field1"])

    a=data["feeds"][1]["field1"]

    if a=='1' :
        print("wood")
        break

    elif a=='2' :
        print("metal")
        break

    else:
        print("plastic")
        break

    TS.close()

Credits

Durkesh

Durkesh

1 project • 0 followers
Passionate about living Machined life

Comments