Python script to upload weatherdata to WeatherCloud

Python and python framework

Moderator: leecollings

Post Reply
peerkersezuuker
Posts: 70
Joined: Monday 14 December 2015 22:16
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Mierlo
Contact:

Python script to upload weatherdata to WeatherCloud

Post by peerkersezuuker »

Hi all,
After running a WS-5500 PWS for a while, I wanted to collect my data in domoticz.
Thanks to the plugin written by Xorfor https://www.domoticz.com/forum/viewtopi ... 65&t=29165 it was a peace of cake :D
Then i wanted to upload my data to a weather service on the internet, but I am no great fan of wunderground.
After some searching I came across WeatherCloud

So I took the liberty to write a python script for uploading the domoticz gathered data to my virtual device at WeatherCloud.
First you have to register at WeatherCloud to get an ID and a Key, which is needed in the script.
The script runs from cron every 10 minutes (WeatherCloud free account only accepts data every 10 minutes)

Hope someone else can make use of this :-)

Code: Select all

#!/usr/bin/python3
#script to upload weather data from WS-5500 domoticz device to weather cloud
#

import json
import requests

# Settings for the domoticz server
domoticzserver = "192.168.0.31:8880"

def domoticzrequest (url):
    response = requests.get(url)
    return response.json()

def read_data(sensor):
    for i, v in enumerate(json_object["result"]):
        if json_object["result"][i]["Name"].lower() == sensor.lower():
            data_read = json_object["result"][i]["Data"]
    return data_read

def log_to_domoticz(message):
    domoticzrequest("http://" + domoticzserver + "/json.htm?type=command&param=addlogmessage&message=" + str(message))

#function to calculate heat index
def heat_index(temp,hum):
    
    fahrenheit = ((temp * 9/5) + 32)
    # Creating multiples of 'fahrenheit' & 'hum' values for the coefficients
    T2 = pow(fahrenheit, 2)
    T3 = pow(fahrenheit, 3)
    H2 = pow(hum, 2)
    H3 = pow(hum, 3)
    
    C3 = [ 16.923, 0.185212, 5.37941, -0.100254, 0.00941695, 0.00728898, 0.000345372, -0.000814971, 0.0000102102, -0.000038646, 0.0000291583, 0.00000142721, 0.000000197483, -0.0000000218429, 0.000000000843296, -0.0000000000481975]
    heatindex3 = C3[0] + (C3[1] * fahrenheit) + (C3[2] * hum) + (C3[3] * fahrenheit * hum) + (C3[4] * T2) + (C3[5] * H2) + (C3[6] * T2 * hum) + (C3[7] * fahrenheit * H2) + (C3[8] * T2 * H2) + (C3[9] * T3) + (C3[10] * H3) + (C3[11] * T3 * hum) + (C3[12] * fahrenheit * H3) + (C3[13] * T3 * H2) + (C3[14] * T2 * H3) + (C3[15] * T3 * H3)
    heatindex = (round(((heatindex3 - 32) * 5/9), 0))
    return heatindex

#fetch domoticz data
json_object = domoticzrequest("http://" + domoticzserver + "/json.htm?type=devices&filter=all&used=true&order=Name")

#calculate weather data en convert to float for easy calculation.

# temperature / humidity / barometer
arr_thb_data = read_data("WS-5500 - THB").split(",")
arr_temp = arr_thb_data[0].split(" ")
arr_hum = arr_thb_data[1].split(" ")
arr_bar = arr_thb_data[2].split(" ")
humidity = float(arr_hum[1])
ambient_temp = float(arr_temp[0])
pressure = float(arr_bar[1])

# dew point
arr_dewpoint = read_data("WS-5500 - Dew point").split(" ")
dewpoint = float(arr_dewpoint[0])

# rainfall past day
arr_rain_daily = read_data("WS-5500 - Rain").split(";")
rainin = float(arr_rain_daily[0])
# rainfall past hour
arr_rain_in = read_data("WS-5500 - Rain rate").split(" ")
rainrate = float(arr_rain_in[0])

# wind
arr_winddirection = read_data("WS-5500 - Wind direction").split(" ")
wind_direction = int(float(arr_winddirection[0]))
arr_windchill = read_data("WS-5500 - Chill").split(" ")
wind_chill = float(arr_windchill[0])
arr_wind_speed = read_data("WS-5500 - Wind speed").split(" ")
wind_speed = float(arr_wind_speed[0])
arr_wind_gust = read_data("WS-5500 - Gust").split(" ")
wind_gust = float(arr_wind_gust[0])

# uv index
arr_uv = read_data("WS-5500 - UVI").split(" ")
uv = float(arr_uv[0])

# solar radiation
arr_solar = read_data("WS-5500 - Solar radiation").split(" ")
solar = float(arr_solar[0])

# compose url for uplad

#http://api.weathercloud.net/set/wid/xxxxxxx/key/xxxxxxxxxxxxx/temp/210/tempin/233/chill/214/heat/247/hum/33/humin/29/wspd/30/wspdhi/30/wspdavg/21/wdir/271/wdiravg/256/bar/10175/rain/0/solarrad/1630/uvi/ 5/ver/1.2/type/201
#http://api.weathercloud.net/get/wid/xxxxxxxx/key/xxxxxxxxxxx/temp/48/tempin/233/dewin/110/chill/0/heat/48/dew/15/hum/79/humin/46/wspd/5/wspdhi/10/wspdavg/7/wdir/152/wdiravg/149/bar/10128/rain/0.0/solarrad/0/uvi/0/rainrate/0/thw/48/et/0/ver/1.7/type/201

WCurl = "http://api.weathercloud.net/set/wid"
WC_Id = "/xxxxxxxxxxxxxxxxx" #replace all x with you personal ID
WC_key = "/yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy" #replace all y with you personal key
last = "/ver/1.2/type/201"

p= requests.get(
    WCurl +
    WC_Id +
    "/key" +
    WC_key +
    "/temp/" + str(ambient_temp * 10) + # Weathercloud seems to divide the delivered data by 10, so we first mutliply them before delivering.
    "/bar/" + str(int(pressure) * 10) + # Weathercloud seems to divide the delivered data by 10, so we first mutliply them before delivering.
    "/dew/" + str(dewpoint * 10) + # Weathercloud seems to divide the delivered data by 10, so we first mutliply them before delivering.
    "/hum/" + str(humidity) +
    "/wspd/" + str(wind_speed * 10) + # Weathercloud seems to divide the delivered data by 10, so we first mutliply them before delivering.
    "/wdir/" + str(wind_direction) +
    "/chill/" + str(wind_chill *  10) + # Weathercloud seems to divide the delivered data by 10, so we first mutliply them before delivering.
    "/heat/" + str(heat_index(ambient_temp,humidity)) +
    "/wspdhi/" + str(wind_gust + 10) +  # Weathercloud seems to divide the delivered data by 10, so we first mutliply them before delivering.
    "/rain/" + str(rainin) +
    "/rainrate/" + str(rainrate) +
    "/solarrad/" + str(solar * 10) +  # Weathercloud seems to divide the delivered data by 10, so we first mutliply them before delivering.
    "/uvi/" + str(uv) +
    last)

#print some data to check if values are ok.
print(WCurl + WC_Id + "/key" + WC_key + "/temp/" + str(ambient_temp * 10) + "/bar/" + str(int(pressure) * 10) + "/dew/" + str(dewpoint * 10) + "/hum/" + str(humidity) + "/wspd/" + str(wind_speed * 10) + "/wdir/" + str(wind_direction) + "/chill/" + str(wind_chill *  10) + "/heat/" + str(heat_index(ambient_temp,humidity)) + "/wspdhi/" + str(wind_gust * 10) + "/rain/" + str(rainin) + "/rainrate/" + str(rainrate) + "/solarrad/" + str(solar * 10) + "/uvi/" + str(uv) + last)
print("Received " + str(p.status_code) + " " + str(p.text))
Script is still work in progress because not all data available on WeatherCloud is send, so in time I will post a new script.
mcgiak
Posts: 18
Joined: Sunday 24 December 2017 10:15
Target OS: -
Domoticz version: 2023.2
Location: Veneto, Italy
Contact:

Re: Python script to upload weatherdata to WeatherCloud

Post by mcgiak »

Hello.
I'm trying to do the opposite.
transfer the data that is on a weathercloud page (a weather station very close to my home) into domoticz.
Is it possible in your opinion?
peerkersezuuker
Posts: 70
Joined: Monday 14 December 2015 22:16
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Mierlo
Contact:

Re: Python script to upload weatherdata to WeatherCloud

Post by peerkersezuuker »

Hi mcgiak,
What i have read and seen it is not possible (yet)
https://blog.weathercloud.net/announcem ... r-display/
(almost at the bottom of the thread)

You could always try to scrape the page you want to use and use this data in your script.
https://www.edureka.co/blog/web-scraping-with-python/
https://towardsdatascience.com/how-to-w ... 49186a8460
And so on.... when you google "extract data from website with python" you get a lot of hits

Regards
Peer
Toulon7559
Posts: 843
Joined: Sunday 23 February 2014 17:56
Target OS: Raspberry Pi / ODroid
Domoticz version: mixed
Location: Hengelo(Ov)/NL
Contact:

Re: Python script to upload weatherdata to WeatherCloud (and Download)

Post by Toulon7559 »

Somebody seen any developments for a DIY 'download'-function for WeatherCloud?
.
Have studied the public webpage with WeatherCloud-presentation for my own station and other stations), but the value-fields for temp/hum/baro/wind/rain have hidden contents.
IMHO, it means that
either a much more clever method of 'functional' scraping is required,
or better a setup to be developed using login-codes etc. for the subject station, enabling the reading of background data accessible for the account-owner, such as https://app.weathercloud.net/reports
Even there (although the values are visible at display) the value-contents remain well hidden in the source-info of the webpage:
therefore, an approach via web-scraping not promising much.
.
;-( We will have to wait until WeatherCloud offers an 'real' API for download.
Set1 = RPI-Zero+RFXCom433+S0PCM+Shield for BMP180/DS18B20/RS485+DDS238-1ZNs
Set2 = RPI-3A++RFLinkGTW+ESP8266s+PWS_WS7000
Common = KAKUs+3*PVLogger+PWS_TFA_Nexus
plus series of 'satellites' for dedicated interfacing, monitoring & control.
Toulon7559
Posts: 843
Joined: Sunday 23 February 2014 17:56
Target OS: Raspberry Pi / ODroid
Domoticz version: mixed
Location: Hengelo(Ov)/NL
Contact:

Re: Python script to download weatherdata from WeatherCloud

Post by Toulon7559 »

As far as I can see, WeatherCloud only has an export-function, which after manual selection of period and parameters produces a csv-file.
Nice for the specific user of WeatherCloud for local storage of his/her picked historic data,
but not very useful for an actual readout to feed a dynamic application.

Would it be possible to externally 'manipulate' this export-function for a selected WeatherCloud-account,
aiming to periodically send a csv-file to a specified remote destination?
Set1 = RPI-Zero+RFXCom433+S0PCM+Shield for BMP180/DS18B20/RS485+DDS238-1ZNs
Set2 = RPI-3A++RFLinkGTW+ESP8266s+PWS_WS7000
Common = KAKUs+3*PVLogger+PWS_TFA_Nexus
plus series of 'satellites' for dedicated interfacing, monitoring & control.
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests