I'm sure there's a pure python variant but I tend to go low-tech bash
whenever possible.
The bash script - I called mine "reformat_mqtt.bash"
#!/bin/bash
#
# ip address of MQTT broker
BROKER="192.168.1.171"
ORIGINAL_TOPIC="RV/original"
FINAL_TOPIC="RV/final"
while true; do
# subscribe to a topic
# run the python script to reformat to stdout
# publish to a final topic
mosquitto_sub -C 1 -h ${BROKER} -t ${ORIGINAL_TOPIC} \
| python3 reformat_mqtt.py \
| mosquitto_pub --stdin-line -h ${BROKER} -t ${FINAL_TOPIC}
# sleep a bit to not eat up your cpu
sleep 1
done
And the 'reformat_mqtt.py' python script....
#
# this reformats the GPS information
# to a more normal looking output
#
# it assumes your input data is always in the same order and always complete
# ala:
[{"dateTime":"1693128700.0","gpsLat":"52.152435"},{"dateTime":"1693128700.0","gpsLong":"9.929356"}]
import json
import sys
for line in sys.stdin:
jsondata = json.loads(line)
reorganized_data = {}
reorganized_data['dateTime'] = jsondata[0]['dateTime']
reorganized_data['gpsLat'] = jsondata[0]['gpsLat']
reorganized_data['gpsLong'] = jsondata[1]['gpsLong']
print(json.dumps(reorganized_data))
Quick test.....
- in one bash window run
- * in a second bash window run 'mosquitto_pub -h 192.168.1.171 -t
RV/original -m
'[{"dateTime":"1693128700.0","gpsLat":"52.152435"},{"dateTime":"1693128700.0","gpsLong":"9.929356"}]'
On Monday, August 28, 2023 at 5:19:15 AM UTC-7 Stefan Gliessmann wrote:
> I asked in the community forum for my router and I was told that this is
> the only way they can currently forward data via MQTT.
> A future firmware release shall allow more formatting.
>
> I might now play around with this
> https://github.com/mrtncls/mqtt-translator to get the MQTT payload from
> an array in one single statement ...
> Anybody used the MQTT Translator for this purpose?
>
> On Monday, August 28, 2023 at 11:01:41 AM UTC+2 Stefan Gliessmann wrote:
>
>> Which code do you refer to?
>> [image: Screenshot 2023-08-28 at 11.00.03.png]
>> This is what I specified in the GPS/Router to push via MQTT to my broker
>> ...
>> On Sunday, August 27, 2023 at 8:39:40 PM UTC+2 vince wrote:
>>
>>> We'd have to see your code to see what you are publishing.
>>>
>>> Your MQTT log looks very odd and likely needs some tweaking
>>>
>>> MQTT:
>>> [{"dateTime":"1693128700.0","gpsLat":"52.152435"},{"dateTime":"1693128700.0","gpsLong":"9.929356"}]
>>>
>>>
>>>
>>> I'd expect something like {"dateTime":"1233456", "gpsLat":"52.1234",
>>> "gpsLong": "9.987"} for a more typical set of JSON to publish. You want
>>> one element (not in an array) with three items therein - dateTime/lat/lon.
>>>
>>>
--
You received this message because you are subscribed to the Google Groups
"weewx-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/weewx-user/be2d0234-e45a-4ba4-b7a2-81713ff753fan%40googlegroups.com.