# Python UDP parsing

**URL:** <https://community.tempest.earth/t/python-udp-parsing/6887>\
**Category:** Developers\
**Tags:** python\
**Created:** [June 11, 2020, 7:57pm UTC](https://community.tempest.earth/t/python-udp-parsing/6887 "2020-06-11T19:57:24Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![jrodricks](https://sea2.discourse-cdn.com/flex020/user_avatar/community.tempest.earth/jrodricks/32/7946_2.png) [@jrodricks](https://community.tempest.earth/u/jrodricks)\
**Post date:** [June 11, 2020, 7:57pm UTC](https://community.tempest.earth/t/python-udp-parsing/6887/1 "2020-06-11T19:57:24Z")

</div>

I’m 100% new to Python. I’ve been able to get the UDP Broadcast but I don’t know how to parse the data/

The data comes in as follows:  
b’{“serial\_number”:“ST-00006839”,“type”:“rapid\_wind”,“hub\_sn”:“HB-00019566”,“ob”:[1591904071,2.59,247]}’  
b’{“serial\_number”:“ST-00006839”,“type”:“device\_status”,“hub\_sn”:“HB-00019566”,“timestamp”:1591904071,“uptime”:356173,“voltage”:2.56,“firmware\_revision”:129,“rssi”:-91,“hub\_rssi”:-94,“sensor\_status”:0,“debug”:0}’  
b’{“serial\_number”:“ST-00006839”,“type”:“obs\_st”,“hub\_sn”:“HB-00019566”,“obs”:[[1591904071,0.36,1.97,3.17,230,3,1015.44,19.16,97.13,13664,0.47,114,0.127551,1,0,0,2.561,1]],“firmware\_revision”:129}’  
b’{“serial\_number”:“ST-00006839”,“type”:“light\_debug”,“hub\_sn”:“HB-00019566”,“ob”:[1591904073,2633,207,0,0]}’  
b’{“serial\_number”:“HB-00019566”,“type”:“hub\_status”,“firmware\_revision”:“134”,“uptime”:313824,“rssi”:-61,“timestamp”:1591904077,“reset\_flags”:“PIN,SFT”,“seq”:31319,“fs”:[1,0,15675411,524288],“radio\_stats”:[22,1,0,3],“mqtt\_stats”:[16,37]}’

My question for those that know Python, how to I assign the data in “obs” to different variables? I only are about some of the data for this project and it’s all in the UDP broadcast, but I don’t know how to reference it. Thanks!

---

<div class="post-metadata">

**Author:** ![vreihen](https://sea2.discourse-cdn.com/flex020/user_avatar/community.tempest.earth/vreihen/32/13217_2.png) [@vreihen](https://community.tempest.earth/u/vreihen)\
**Post date:** [June 11, 2020, 8:35pm UTC](https://community.tempest.earth/t/python-udp-parsing/6887/2 "2020-06-11T20:35:27Z")

</div>

Does this help any?

[https://www.w3schools.com/python/python\_lists.asp](https://www.w3schools.com/python/python_lists.asp)

The contents of each ob/obs field are documented in the WF UDP API:

[https://weatherflow.github.io/SmartWeather/api/udp/v114/](https://weatherflow.github.io/SmartWeather/api/udp/v114/)

---

<div class="post-metadata">

**Author:** ![jrodricks](https://sea2.discourse-cdn.com/flex020/user_avatar/community.tempest.earth/jrodricks/32/7946_2.png) [@jrodricks](https://community.tempest.earth/u/jrodricks)\
**Post date:** [June 11, 2020, 11:02pm UTC](https://community.tempest.earth/t/python-udp-parsing/6887/3 "2020-06-11T23:02:18Z")

</div>

Thanks. I ended up with this mess:

```
    data, addr = sock.recvfrom(1024) #gets UDP data
    data2 = json.loads(data) #sets UDP received as JSON

```

if str(data2[‘type’]) == “obs\_st”: #tests for a broadcast with weather data  
dirty = str(data2[‘obs’]) #ensures data is a string for future parsing  
dirty = dirty.replace("[[", “[”) #strips one of the leading brackets  
dirty = dirty.replace("]]", “]”) #strips one of the trailing brackets  
clean = dirty.split(",") #splits the resulting string so it can be referensed as a list

```
        #assigning variables
        air_temperature = clean[7]
        wind_avg = clean[2]
        wind_gust = clean[3]
        wind_direction = clean[4]
        
        #writes to HTML when there's new data and iterates the counter
        output_to_HTML() #writes HTML file
        print('Loops: ', loop_count) #counts how many times this has run
        loop_count = loop_count + 1 # itterates count
        print()#some padding for console
        print()#some padding for console
```

---

<div class="post-metadata">

**Author:** ![vinceskahan](https://sea2.discourse-cdn.com/flex020/user_avatar/community.tempest.earth/vinceskahan/32/2785_2.png) [@vinceskahan](https://community.tempest.earth/u/vinceskahan)\
**Post date:** [June 11, 2020, 11:25pm UTC](https://community.tempest.earth/t/python-udp-parsing/6887/4 "2020-06-11T23:25:30Z")

</div>

You could always just run [my UDP listener](https://community.tempest.earth/t/weatherflow-udp-listener-and-mqtt-influxdb-publisher-utility/2053) which does this already, or refer to it as one example of how you can do it.

---

<div class="post-metadata">

**Author:** ![GaryFunk](https://sea2.discourse-cdn.com/flex020/user_avatar/community.tempest.earth/garyfunk/32/5729_2.png) [@GaryFunk](https://community.tempest.earth/u/GaryFunk)\
**Post date:** [June 11, 2020, 11:48pm UTC](https://community.tempest.earth/t/python-udp-parsing/6887/5 "2020-06-11T23:48:46Z")

</div>

Let me ask a strange question. Have you written code in JavaScript?

---

<div class="post-metadata">

**Author:** ![jrodricks](https://sea2.discourse-cdn.com/flex020/user_avatar/community.tempest.earth/jrodricks/32/7946_2.png) [@jrodricks](https://community.tempest.earth/u/jrodricks)\
**Post date:** [June 12, 2020, 12:31am UTC](https://community.tempest.earth/t/python-udp-parsing/6887/6 "2020-06-12T00:31:03Z")

</div>

I’ve never written any code of any sort. Which may have been why I was scared of [vinceskahan](https://community.tempest.earth/u/vinceskahan)‘s UDP listener.

---

<div class="post-metadata">

**Author:** ![GaryFunk](https://sea2.discourse-cdn.com/flex020/user_avatar/community.tempest.earth/garyfunk/32/5729_2.png) [@GaryFunk](https://community.tempest.earth/u/GaryFunk)\
**Post date:** [June 12, 2020, 12:50am UTC](https://community.tempest.earth/t/python-udp-parsing/6887/7 "2020-06-12T00:50:08Z")

</div>

The packets are in JSON. JavaScript Object Notation and there should be a library that can parse the data.

---

<div class="post-metadata">

**Author:** ![vreihen](https://sea2.discourse-cdn.com/flex020/user_avatar/community.tempest.earth/vreihen/32/13217_2.png) [@vreihen](https://community.tempest.earth/u/vreihen)\
**Post date:** [June 12, 2020, 1:09am UTC](https://community.tempest.earth/t/python-udp-parsing/6887/8 "2020-06-12T01:09:49Z")

</div>

I, for one, am impressed at how far you have advanced in only a few hours!!!

If you want to Python-ify your handling of [[and]], “ob” fields in the UDP packets are lists, whereas “obs” are lists of lists. Think [[1,2,3,4,5],[6,7,8,9,10]] . WF never promised that they will not send multiple “obs” lists in a single UDP packet (like in a hub down/reboot recovery catch-up scenario), although it it good to keep UDP packets short to guarantee maximum deliverability since there is no handshaking. I suspect that their intention was to keep the UDP and web packet formats as close as possible, and that seeing multiple lists in an “obs” field is more likely from the web side. For a programming exercise, you may want to treat it as a list of lists and iterate through it just for completeness. Otherwise, dirty = data2[‘obs’][0] should grab the first list from an “obs” field and replace three lines of converting to a string and deleting characters…if I’m still awake. It’s been a long day, and my brain is fried right now…

---

<div class="post-metadata">

**Author:** ![vreihen](https://sea2.discourse-cdn.com/flex020/user_avatar/community.tempest.earth/vreihen/32/13217_2.png) [@vreihen](https://community.tempest.earth/u/vreihen)\
**Post date:** [June 12, 2020, 1:13am UTC](https://community.tempest.earth/t/python-udp-parsing/6887/9 "2020-06-12T01:13:14Z")

</div>

Can you get away with clean = data2[‘obs’][0] ???

---

<div class="post-metadata">

**Author:** ![jrodricks](https://sea2.discourse-cdn.com/flex020/user_avatar/community.tempest.earth/jrodricks/32/7946_2.png) [@jrodricks](https://community.tempest.earth/u/jrodricks)\
**Post date:** [June 12, 2020, 1:29am UTC](https://community.tempest.earth/t/python-udp-parsing/6887/10 "2020-06-12T01:29:53Z")

</div>

Someone in the Raspberry Pi forum was kind enough to point me in that direction.

I’ve updated the code to reflect data2[‘obs’][0][6]

I was missing the [0] part. It’s challenging trying to learn to parse json (which it turns out is easy in hindsight) and learn some Python.

My ultimate goal is to have the Raspberry Pi turn servos to mimic analog-weather gauges. While I was waiting for my Tempest to ship I got it up and running by pulling from the demo APIs but I was really hoping to be able to get this (more or less) real-time via UDP. I’m very pleased.

And thank you all for the help. Seems like a great community.

---

<div class="post-metadata">

**Author:** ![vinceskahan](https://sea2.discourse-cdn.com/flex020/user_avatar/community.tempest.earth/vinceskahan/32/2785_2.png) [@vinceskahan](https://community.tempest.earth/u/vinceskahan)\
**Post date:** [June 12, 2020, 2:03am UTC](https://community.tempest.earth/t/python-udp-parsing/6887/11 "2020-06-12T02:03:56Z")

</div>

If you got that far never having programmed (let alone in python), you’re doing pretty well as others have said. Many of us bought the WF gear partially for the fun of rolling our own software to see what we could cook up. Go wild !

---

<div class="post-metadata">

**Author:** ![wpns](https://sea2.discourse-cdn.com/flex020/user_avatar/community.tempest.earth/wpns/32/3992_2.png) [@wpns](https://community.tempest.earth/u/wpns)\
**Post date:** [June 16, 2020, 11:50am UTC](https://community.tempest.earth/t/python-udp-parsing/6887/12 "2020-06-16T11:50:46Z")

</div>

So I split this into two pieces for ease of use. The first one listens for UDP broadcasts and puts the received data into a Redis database, which is a memory-resident data store. The second one reads the Redis database for interesting data and does stuff with it. Then the program that wants to know the temperature doesn’t have to listen for (and parse) the battery voltage and everything else. Plus, a computer ends up with one listener and more than one data consumer. I did something similar to your project with a Raspberry Pi InspiRing that shows wind direction around the circle and wind speed in the color of the LED.

My UDP listener code is here: [http://dotnetdotcom.net/WFrws.py](http://dotnetdotcom.net/WFrws.py)

The WindRose code is here: [http://dotnetdotcom.net/WindRose.py](http://dotnetdotcom.net/WindRose.py)

I borrowed heavily from Vince and others. #ShouldersOfGiants

Enjoy!

---

<div class="post-metadata">

**Author:** ![vinceskahan](https://sea2.discourse-cdn.com/flex020/user_avatar/community.tempest.earth/vinceskahan/32/2785_2.png) [@vinceskahan](https://community.tempest.earth/u/vinceskahan)\
**Post date:** [June 16, 2020, 3:31pm UTC](https://community.tempest.earth/t/python-udp-parsing/6887/13 "2020-06-16T15:31:43Z")

</div>

Need screenshots please !!!
