User Rating: 5 / 5

Star ActiveStar ActiveStar ActiveStar ActiveStar Active

Introductionmeteo raspberry

This article describe how to configure Raspberry PI3 with Raspbian+Domoticz to work with a USB weather station.

Raspberry PI3 is a cheap single-board computer running Raspbian, a Debian GNU/Linux distribution for Raspberry.
Domoticz is a web application designed to control a domotic system.

The middleware weeWX will be used to get data from the USB weather station and generate a XML file with all data, that will be accumulated by Domoticz through a LUA script, and shown in nice graphs.

Information are taken from the following links:

weeWX installation and configuration

From root prompt on Raspberry, type:

wget -qO - | sudo apt-key add -
wget -qO - | sudo tee /etc/apt/sources.list.d/weewx.list
apt-get update
mkdir -p /var/www/html/weewx
apt-get install weewx

The installation script will ask you to insert the location, coordinates, and define the weather station type, in my case Fine Offset USB weather station, model WH1080.

For our purpose, apache2 webserver is not needed, because we just want that weeWX generate a XML file, and don't need to read graphs and pages generated by weeWX.

Now we have to put an extension into the weewx user directory and modify the weewx.conf configuration file:

sed -i "s:self\.archive_interval = self\.engine\.\(.*\):self.archive_interval = software_interval #Creasol - do not set to self.engine.\1 because too long:" /usr/share/weewx/weewx/
cd /usr/share/weewx/user
cd /etc/weewx
nano weewx.conf

 Inside the section [StdReport] you can find the following subsections: [[StandardReport]], [[FTP]] and [[RSYNC]] : after the latter section, before the line ###############################, add the following section:

skin = Domoticz
group_altitude = meter
group_speed2 = km_per_hour2
group_pressure = mbar
group_rain = mm
group_rainrate = mm_per_hour
group_temperature = degree_C
group_degree_day = degree_C_day
group_speed = km_per_hour

and save with ctrl-x , y , enter.

Now type the following commands:

mkdir skins/Domoticz
echo "[CheetahGenerator]
    encoding = html_entities
    search_list_extensions = user.stats.MyStats

            template = domoticz.xml.tmpl

    generator_list = weewx.cheetahgenerator.CheetahGenerator
" >skins/Domoticz/skin.conf

echo "<weewx>
" >skins/Domoticz/domoticz.xml.tmpl
/etc/init.d/weewx restart

Please note that weather station must be connected to Raspberry, if you want that weewx start! Check /var/log/syslog to look for any errors, for example you can type the command tail -f /var/log/syslog |grep weewx to monitor only the log for weewx excluding all other services.

Domoticz configuration

From the domoticz web panel, select Setup -> Hardware and create a virtual hardware: Enabled: yes, Name: Meteo station, type: Dummy (Does nothing, use for virtual switches only) and click on Add button.
Then, a new line appears in the hardware list above, named Meteo station: click on the Create virtual sensors button to create the following virtual sensors:

  • Name: Meteo outdoor (roof), Sensor Type: Temp+Hum+Baro
  • Name: Meteo indoor (Raspberry room), Sensor Type: Temp+Hum
  • Name: Wind (roof), Sensor Type: Wind
  • Name: Rain, Sensor Type: Rain

Now go to Setup -> Devices and annotate the idx for the newly created sensors

Create the script script_time_weewx.lua into domoticz/scripts/lua directory:

cd /home/pi/domoticz/scripts/lua
nano script_time_weewx.lua

and put inside the following text:

-- Title: script_device_weewx.lua
-- Date: 10-01-2016
-- this script reads the weewxpws xml file and uploads current data to virtual device
-- format of the xml file =
--      8.5
--      73.2
--      19.5
--      53.2
--      1025.5
-- Config 

-- MODIFIFY THE FOLLOWING SECTION specifying the idx of each variable that you have created in Domoticz local wind_device_idx = 13 local rain_device_idx = 11 local garden_device_idx = 8 local room_device_idx = 9 local aux_device_idx = 14 -- Attention au nom du "Dispositif" , -- c'est celui que l'on a mis dans la Config (on en choisi un au hasard parmis l'un des dispositifs de notre weewx) s = otherdevices_lastupdate['Température Salon'] -- returns a date time like 2013-07-11 17:23:12 -- Remplacer 'Vent Jardin' par le nom de votre dispositif anémomètre prev_wind = otherdevices_svalues['Vent Jardin'] -- End Config commandArray = {} t1 = os.time() year = string.sub(s, 1, 4) month = string.sub(s, 6, 7) day = string.sub(s, 9, 10) hour = string.sub(s, 12, 13) minutes = string.sub(s, 15, 16) seconds = string.sub(s, 18, 19) t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds} difference = (os.difftime (t1, t2)) -- ON ne mets à jour que tous les 5 mns if ( difference < 300 ) then return commandArray end -- ca fonctionne mais c'est parfois un peu long et on dépasse les 10 secondes... on fait donc cela par crontab -- os.execute('wget -O /tmp/domoticz.xml') -- os.execute('sed s#,#.# /tmp/domoticz.xml > /tmp/domoticz.xml-pre') local filename = "/tmp/domoticz.xml-pre" local line , tempdevice = '' local temperature,humidity,pressure,inhumidity,intemperature,auxhumidity,auxtemperature,rain,rainrate,wind,winddir,windgust,windchill = 0 local windcomp = 'N' for line in io.lines(filename) do if string.find(line,"\"temp\"") then temperature=tonumber(string.match(line, "%-?%d+.?%d*")) elseif string.find(line,"\"hum\"") then humidity=tonumber(string.match(line, "%d+")) elseif string.find(line,"\"inTemp\"") then intemperature=tonumber(string.match(line, "%-?%d+.?%d*")) elseif string.find(line,"\"inHum\"") then inhumidity=tonumber(string.match(line, "%d+")) elseif string.find(line,"\"auxTemp\"") then auxtemperature=tonumber(string.match(line, "%-?%d+.?%d*")) elseif string.find(line,"\"auxHum\"") then auxhumidity=tonumber(string.match(line, "%d+")) elseif string.find(line,"\"barometer\"") then pressure=tonumber(string.match(line, "%d+")) elseif string.find(line,"\"rainrate\"") then rainrate=100 * tonumber(string.match(line, "%d+.?%d*")) elseif string.find(line,"\"Rain\"") then rain=tonumber(string.match(line, "%d+.?%d*")) elseif string.find(line,"\"wind\"") then wind=tonumber(string.match(line, "%d+")) elseif string.find(line,"\"Winddir\"") then winddir=tonumber(string.match(line, "%d+")) elseif string.find(line,"\"Windgust\"") then windgust=tonumber(string.match(line, "%d+")) elseif string.find(line,"\"Windcomp\"") then windcomp=string.match(line, "Windcomp\">([NESO/A]+)<!--") elseif string.find(line,"\"WindChill\"") then windchill=tonumber(string.match(line, "%-?%d+.?%d*")) end end -- Si la vitesse du vent est nulle winddir et windcomp garde la valeur précédente if ( wind == 0 or wind == nil ) then winddir,windcomp = prev_wind:match("([^;]+);([^;]+)") wind = 0 end -- A l'initialisation du device domoticz on peut avoir des valeurs farfelues if winddir == nil then winddir = 0 end if windcomp =='N/A' then windcomp = 'N' end if windchill == nil then windchill = 0 end if windgust== nil then windgust= 0 end -- Conversion de win et wingust (domoticz attend 10 * Winspeed [m/s] -- weewx nous donne Winspeed [km/h] wind = math.ceil(wind / 0.36) windgust = math.ceil(windgust / 0.36) -- Conditions Salon if not ( inhumidity == nil or intemperature == nil ) then tempdevice = room_device_idx..'|0|'..intemperature..';'..inhumidity..';0' commandArray [1] = {['UpdateDevice'] = tempdevice } end -- Meteo Locale idx = 8 if not ( humidity == nil or temperature == nil or pressure == nil ) then tempdevice = garden_device_idx..'|0|'..temperature..';'..humidity..';0;'..pressure..';0' commandArray [2] = {['UpdateDevice'] = tempdevice } end -- Pluie Locale idx = 11 if not ( rainrate == nil or rain == nil ) then tempdevice = rain_device_idx..'|0|'..rainrate..';'..rain commandArray [3] = {['UpdateDevice'] = tempdevice } end -- Vent Locale idx=13 dir;comp;speed;gust;temp;windchill if not ( temperature == nil ) then tempdevice = wind_device_idx..'|0|'..winddir..';'..windcomp..';'..wind..';'..windgust..';'..temperature..';'..windchill commandArray [4] = {['UpdateDevice'] = tempdevice } end -- Conditions Aux idx=14 (WC ou Cave) if not ( auxhumidity == nil or auxtemperature == nil ) then tempdevice = aux_device_idx..'|0|'..auxtemperature..';'..auxhumidity..';0' commandArray [5] = {['UpdateDevice'] = tempdevice } end return commandArray

and edit the first section to specify the right idx of the virtual sensors.

Pin It