Back to Parent

# Listens to particle cloud and logs data into csv
# Harley Guo
# Carnegie Mellon University
# March 2022

from sseclient import SSEClient 
import requests, json, csv, string

TOKEN = REDUCTED
MSG = SSEClient(f'https://api.spark.io/v1/events/test?access_token={TOKEN}')
print("file name: ", end = '')
FILE = "YOURFILENAME.csv"
print(FILE)

def main():
    print("listening...")
    for msg in MSG:
        data = str(msg.data)
        try:
            j = json.loads(data)
            s = j["data"].split("|")  
            s = list(map(lambda x: int(x), s))
            with open(FILE, 'a', newline='') as f:
                writer = csv.writer(f)
                writer.writerow(s)
            print(s)
        except:
            pass

if __name__ == "__main__": 
    main()
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0