[Solved] How to read events from cloud?

Hi there,

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.

Thanks,

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.

For C# you could still use the discontinued SDK here
https://docs.particle.io/reference/discontinued/windows/#events-sub-system

Happy to show my working code hooking into the SSE Stream, my question was tagged above by @ScruffR

Got it working in C#.

1 Like

Thanks to both of you.
I have one more question. I can easily get diagnostics and SIM Card information using:

curl “https://api.particle.io/v1/diagnostics/DeviceID/update?access_token=1234

Now, I’m using Electron to publish Temp. How do I get the temp value using curl? This command: curl “https://api.particle.io/v1/devices/DeviceID/events/temp?access_token=1234” does not work for me.

Thanks again,

You’d need to register that variable via Particle.variable(), but that’s not an event.
The docs do help with such questions
https://docs.particle.io/reference/device-os/firmware/electron/#particle-variable-

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.

Thanks!

[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))

You will need sseclient-py and ssclient.
Thanks,

Reference: https://pypi.org/project/sseclient-py/