Newbie here. I successfully run electron and I can see temp coming from the device. I also use iOS app and I get useful information from my device.
Now, I tried to use webhook and I configured it but it stops working time to time. My question is, is there a way that I can read events from the cloud? I’ve read the API section and I did not understand anything. Can I write my own code in C or C# or Python in order to pull those event information from the particle cloud? Is there any tutorial on that?
Sorry for my basic question. My background is not software.
If you want to catch events in C# or Python I guess googling SSE (server sent events) will render a load of results since it's a concept not unique to Particle devices.
However, there already are some threads about SSE here
e.g.
Thanks.
Yeah, I’ve read about it and looks very interesting. When I register my variables in the void setup, do I need to keep my electron active and connected? Or can I use System.sleep(SLEEP_MODE_DEEP,600) in the loop?
Variables request information from a device, as such, it needs to be awake to respond.
SSEs shout information through the cloud, as such, someone needs to be listening to hear it.
Thanks for the information. I’ve read about Events and variables and I could easily implement it. Here is what I’m doing:
Using Cloud APIs, I wrote a Python code to get the diagnostics info as well as the SIM info and post them to my MQTT Broker. Then I’m using Node-Red to display the information. It’s works great!
Now I’m stuck on how to read my sensor info while keeping the board in deep sleep.
If I use Variable to post, the board needs to be awake and hence I can not use System Deep Sleep.
If I use publish and then listen to events, with curl "https://api.particle.io/v1/devices/XXXX/events/Distance?access_token=XXXX" then I can’t catch the event say every 5 minutes. My python is not able to catch streams of events.
Is there another way that I can read my sensor value AND use deep sleep? I don’t use any integrations. I have my own broker.
[Update] I solved it. Here is a the python code if anyone is interested:
import json
import pprint
import sseclient
def with_urllib3(url):
"""Get a streaming response for the given event feed using urllib3."""
import urllib3
http = urllib3.PoolManager()
return http.request('GET', url, preload_content=False)
def with_requests(url):
"""Get a streaming response for the given event feed using requests."""
import requests
return requests.get(url, stream=True)
url = 'https://api.particle.io/v1/devices/XXX/events/EVENT_NAME?access_token=XXX'
response = with_urllib3(url) # or with_requests(url)
client = sseclient.SSEClient(response)
for event in client.events():
pprint.pprint(json.loads(event.data))