import requests
import json
from datetime import datetime
import time

def fetch_and_send_data():
    try:
        # Fetch the JSON data
        json_url = "http://192.168.1.9/client?command=record";
        response = requests.get(json_url)
        response.raise_for_status()  # Raise an exception for HTTP errors
        data = response.json()

        # Mapping function
        def map_data(data):
            mapped_data = {
                "tempinf": None,
                "humidityin": None,
                "baromrelin": None,
                "baromabsin": None,
                "tempf": None,
                "humidity": None,
                "winddir": None,
                "windspeedmph": None,
                "windgustmph": None,
                "maxdailygust": None,
                "solarradiation": None,
                "uv": None,
                "rainratein": None,
                "eventrainin": None,
                "hourlyrainin": None,
                "dailyrainin": None,
                "weeklyrainin": None,
                "monthlyrainin": None,
                "yearlyrainin": None,
                "totalrainin": None
            }

            for sensor in data['sensor']:
                title = sensor['title']
                for parameter in sensor['list']:
                    param_name = parameter[0]
                    value = parameter[1]

                    if title == "Indoor":
                        if param_name == "Temperature":
                            mapped_data["tempinf"] = value
                        elif param_name == "Humidity":
                            mapped_data["humidityin"] = value

                    elif title == "Outdoor":
                        if param_name == "Temperature":
                            mapped_data["tempf"] = value
                        elif param_name == "Humidity":
                            mapped_data["humidity"] = value

                    elif title == "Pressure":
                        if param_name == "Absolute":
                            mapped_data["baromabsin"] = value
                        elif param_name == "Relative":
                            mapped_data["baromrelin"] = value

                    elif title == "Wind Speed":
                        if param_name == "Direction":
                            mapped_data["winddir"] = value
                        elif param_name == "Wind":
                            mapped_data["windspeedmph"] = value
                        elif param_name == "Gust":
                            mapped_data["windgustmph"] = value
                        elif param_name == "Max Daily Gust":
                            mapped_data["maxdailygust"] = value

                    elif title == "Solar":
                        if param_name == "Light":
                            mapped_data["solarradiation"] = value
                        elif param_name == "UVI":
                            mapped_data["uv"] = value

                    elif title == "Rainfall":
                        if param_name == "Rate":
                            mapped_data["rainratein"] = value
                        elif param_name == "Hour":
                            mapped_data["hourlyrainin"] = value
                        elif param_name == "Day":
                            mapped_data["dailyrainin"] = value
                        elif param_name == "Week":
                            mapped_data["weeklyrainin"] = value
                        elif param_name == "Month":
                            mapped_data["monthlyrainin"] = value
                        elif param_name == "Year":
                            mapped_data["yearlyrainin"] = value
                        elif param_name == "Total":
                            mapped_data["totalrainin"] = value

            return mapped_data

        mapped_data = map_data(data)

        # Create the formatted response
        dateutc = datetime.utcnow().strftime('%Y-%m-%d+%H:%M:%S')
        PASSKEY = "ASDFASDF1234ASDF1234"
        stationtype = "RADDY_L7"
        MODEL = "RaddyL7"
        Frequency = "1"
        formatted_response =
f"PASSKEY={PASSKEY}&stationtype={stationtype}&dateutc={dateutc}&model={MODEL}&Frequency={Frequency}"

        for key, value in mapped_data.items():
            if value is not None:
                formatted_response += f"&{key}={value}"

        # Define the URL to send the POST request to
        post_url = "http://localhost:8998/station/ecowitt";

        # Send the POST request
        headers = {'Content-Type': 'application/x-www-form-urlencoded'}
        post_response = requests.post(post_url, data=formatted_response,
headers=headers)
        print(formatted_response)
        # Print the response from the POST request
        print(post_response.status_code)
        print(post_response.text)

    except Exception as e:
        print(f"An error occurred: {e}")

# Main loop to repeat the function every x seconds
x = 10  # Set the interval (in seconds)
while True:
    fetch_and_send_data()
    time.sleep(x)

On Sun, Jun 23, 2024 at 3:05 PM Eddie Bryant <[email protected]> wrote:

> I did a search for Python script but there seems to be a lot of posts.
> What is your user name there?
>
> Eddie
>
> On Sat, Jun 22, 2024, 10:40 PM Eddie Bryant <[email protected]> wrote:
>
>> Thanks, I'll check it out tomorrow. Good night.
>>
>> Eddie
>>
>> On Sat, Jun 22, 2024, 10:38 PM Michael <[email protected]> wrote:
>>
>>> I would head over the CumulusMX forum like.  I posted a Python script
>>> you can modify to reformat the data for CMX.
>>>
>>> On Sat, Jun 22, 2024 at 7:37 PM Eddie Bryant <[email protected]> wrote:
>>>
>>>> Michael,
>>>>
>>>> I get a page that shows the data.
>>>>
>>>> Eddie
>>>>
>>>> On Sat, Jun 22, 2024, 10:17 PM Michael <[email protected]> wrote:
>>>>
>>>>> go to http://192.168.1.5/client?command=record  Do you get a result?
>>>>>
>>>>> On Saturday, June 22, 2024 at 6:35:17 PM UTC-7 Eddie Bryant wrote:
>>>>>
>>>>>> Michael,
>>>>>>
>>>>>> Thank you for your response. I am not familiar with JSON. The WiFi
>>>>>> setup for the WF-100SE says to connect to the L7-xxxx network and using a
>>>>>> browser type in 192.168.1.5. That brings up a page where I type in my
>>>>>> network SSID and the pass key. Below that is sections for adding IDs and
>>>>>> keys for linking to weather unground and weather cloud.
>>>>>>
>>>>>> I reason if I can link to those I should be able to link to APRS.fi
>>>>>> and CWOP but I am not sure how to do that.
>>>>>>
>>>>>> Eddie
>>>>>>
>>>>>>
>>>>>>
>>>>>> On Sat, Jun 22, 2024, 9:12 PM Michael <[email protected]> wrote:
>>>>>>
>>>>>>> I would verify if you can get the observations out of the unit like
>>>>>>> I was able to.
>>>>>>>
>>>>>>> I went with CumulusMX instead of WeeWX and their board suggested
>>>>>>> translating the JSON to a ECOWITT HTTP Post.  Script is in the thread
>>>>>>> below.  Overall, I am happier with CumulusMX both in terms of product 
>>>>>>> and
>>>>>>> support community over WeeWX.  I was able to enable the WXNOW file for
>>>>>>> Direwolf to post APRS reports easily.
>>>>>>>
>>>>>>> https://cumulus.hosiene.co.uk/viewtopic.php?t=22258&start=15
>>>>>>>
>>>>>>> On Saturday, June 22, 2024 at 5:47:05 PM UTC-7 Ebryant wrote:
>>>>>>>
>>>>>>>> I have a Raddy WF-100SE station that I would like to get data to
>>>>>>>> aprs.fi and CWOP. If anyone has any suggestions it would help.
>>>>>>>>
>>>>>>>>
>>>>>>>> On Wednesday, March 13, 2024 at 11:42:52 AM UTC-4 Michael wrote:
>>>>>>>>
>>>>>>>>> Hello,
>>>>>>>>>
>>>>>>>>> I currently have a Raddy L7 LoRA station.  It appears that that
>>>>>>>>> the weather station provides a JSON output of observations.  I am
>>>>>>>>> soliciting a paid opportunity to develop a driver for this weather 
>>>>>>>>> station
>>>>>>>>> and create a Github page for driver to spread the word.  I am just a
>>>>>>>>> personal user, but figure this may be a better approach than having 
>>>>>>>>> to buy
>>>>>>>>> a supported station.  I am not a developer and simply don't have the 
>>>>>>>>> time
>>>>>>>>> to undertake the effort, but want users of this weather station to be 
>>>>>>>>> able
>>>>>>>>> to use WeeWx (Yes, I tried Interceptor, but I failed in getting to 
>>>>>>>>> work and
>>>>>>>>> this appears to be a more graceful approach).  If you are interested 
>>>>>>>>> in the
>>>>>>>>> work, please let me know.
>>>>>>>>>
>>>>>>>>>  The JSON URL is http://ip/client?command=record
>>>>>>>>>
>>>>>>>>> The update period appears to vary between 5-30 seconds.
>>>>>>>>>
>>>>>>>>> Example output
>>>>>>>>> Sensor         Parameter           Value
>>>>>>>>> -------------------------------------------------------
>>>>>>>>> Indoor         Temperature         69.4
>>>>>>>>> Indoor         Humidity            43
>>>>>>>>> Outdoor        Temperature         44.7
>>>>>>>>> Outdoor        Humidity            70
>>>>>>>>> Pressure       Absolute            26.74
>>>>>>>>> Pressure       Relative            29.86
>>>>>>>>> Wind Speed     Max Daily Gust      3.8
>>>>>>>>> Wind Speed     Wind                0.0
>>>>>>>>> Wind Speed     Gust                0.0
>>>>>>>>> Wind Speed     Direction           213
>>>>>>>>> Wind Speed     Wind Average 2 Minute0.0
>>>>>>>>> Wind Speed     Direction Average 2 Minute204
>>>>>>>>> Wind Speed     Wind Average 10 Minute0.2
>>>>>>>>> Wind Speed     Direction Average 10 Minute300
>>>>>>>>> Rainfall       Rate                0.0
>>>>>>>>> Rainfall       Hour                0.0
>>>>>>>>> Rainfall       Day                 0.0
>>>>>>>>> Rainfall       Week                0.73
>>>>>>>>> Rainfall       Month               0.73
>>>>>>>>> Rainfall       Year                6.45
>>>>>>>>> Rainfall       Total               11.39
>>>>>>>>> Solar          Light               0.3
>>>>>>>>> Solar          UVI                 0.0
>>>>>>>>> JSON Message:
>>>>>>>>> {"sensor":[
>>>>>>>>> {"title":"Indoor","list":[["Temperature","71.6","°F"],["Humidity","41","%"]]},{"title":"Outdoor","list":[["Temperature","59.2","°F"],["Humidity","44","%"]]},{"title":"Pressure","list":[["Absolute","26.77","inhg"],["Relative","29.89","inhg"]]},{"title":"Wind
>>>>>>>>> Speed","list":[["Max Daily
>>>>>>>>> Gust","8.9","mph"],["Wind","3.6","mph"],["Gust","3.8","mph"],["Direction","264","°"],["Wind
>>>>>>>>> Average 2 Minute","4.3","mph"],["Direction Average 2
>>>>>>>>> Minute","246","°"],["Wind Average 10 Minute","3.1","mph"],["Direction
>>>>>>>>> Average 10
>>>>>>>>> Minute","241","°"]]},{"title":"Rainfall","list":[["Rate","0.0","inch/hr"],["Hour","0.0","inch","43"],["Day","0.0","inch","44"],["Week","0.73","inch","45"],["Month","0.73","inch","46"],["Year","6.45","inch","47"],["Total","11.39","inch","48"]],"range":"Range:
>>>>>>>>> 0inch to
>>>>>>>>> 393.7inch."},{"title":"Solar","list":[["Light","153.59","w/m²"],["UVI","0.8",""]]}],"battery":{"title":"Battery","list":["All
>>>>>>>>> battery are ok"]}}
>>>>>>>>>
>>>>>>>>> --
>>>>>>> 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/28bf858b-693d-4dc0-9ac1-182cc61fa7den%40googlegroups.com
>>>>>>> <https://groups.google.com/d/msgid/weewx-user/28bf858b-693d-4dc0-9ac1-182cc61fa7den%40googlegroups.com?utm_medium=email&utm_source=footer>
>>>>>>> .
>>>>>>>
>>>>>> --
>>>>> 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/357ea7c6-b2a7-47a5-ac75-d5e90b96b72fn%40googlegroups.com
>>>>> <https://groups.google.com/d/msgid/weewx-user/357ea7c6-b2a7-47a5-ac75-d5e90b96b72fn%40googlegroups.com?utm_medium=email&utm_source=footer>
>>>>> .
>>>>>
>>>> --
>>>> You received this message because you are subscribed to a topic in the
>>>> Google Groups "weewx-user" group.
>>>> To unsubscribe from this topic, visit
>>>> https://groups.google.com/d/topic/weewx-user/Lz7X2sp3B8g/unsubscribe.
>>>> To unsubscribe from this group and all its topics, send an email to
>>>> [email protected].
>>>> To view this discussion on the web visit
>>>> https://groups.google.com/d/msgid/weewx-user/CAJ8RQDqKCX1eis1vf1pc63y1%3D305W1oh2xNLse1Bi7iiD1UWJw%40mail.gmail.com
>>>> <https://groups.google.com/d/msgid/weewx-user/CAJ8RQDqKCX1eis1vf1pc63y1%3D305W1oh2xNLse1Bi7iiD1UWJw%40mail.gmail.com?utm_medium=email&utm_source=footer>
>>>> .
>>>>
>>>
>>>
>>> --
>>>
>>> (813) 957-5402
>>> [email protected]
>>>
>>> --
>>> 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/CAKNRWhoYmeq3NCQsjG-%2BFyAQXn7KXsv12nMvcVSR71Qkm%3DajXA%40mail.gmail.com
>>> <https://groups.google.com/d/msgid/weewx-user/CAKNRWhoYmeq3NCQsjG-%2BFyAQXn7KXsv12nMvcVSR71Qkm%3DajXA%40mail.gmail.com?utm_medium=email&utm_source=footer>
>>> .
>>>
>> --
> You received this message because you are subscribed to a topic in the
> Google Groups "weewx-user" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/weewx-user/Lz7X2sp3B8g/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/weewx-user/CAJ8RQDpNYWVT%3D9wBbakqF873FJpYhBde63FT5L9xpD1ZZuc5zQ%40mail.gmail.com
> <https://groups.google.com/d/msgid/weewx-user/CAJ8RQDpNYWVT%3D9wBbakqF873FJpYhBde63FT5L9xpD1ZZuc5zQ%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>


-- 

(813) 957-5402
[email protected]

-- 
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/CAKNRWhrm_dHXRRJtUja6w%2ByNSH-M%2BtssdrYCa87GxkntMLUyeA%40mail.gmail.com.

Reply via email to