#----------------------------- # user/customPanels.py #------------------------------ from kivy.uix.relativelayout import RelativeLayout from panels.template import panelTemplate from kivy.properties import Clock from kivy.properties import StringProperty import requests import json #----------------------------------- # do all the grabbing the data here #----------------------------------- class ExamplePanel(panelTemplate): def __init__(self,**kwargs): super().__init__(**kwargs) self.updateData() Clock.schedule_interval(self.updateData,5) def updateData(self, *args): data = self.get_data() # set the kivy panel element text self.ids.example_label.text = data def get_purpleAirAQI(self): try: # API endpoint URL for a PurpleAir AQI sensor url = "http://192.168.2.33/json" response = requests.get(url).json() return str(response['pm2.5_aqi']) except: return "n/a" # https://weatherflow.github.io/Tempest/api/udp/v171/ def get_data(self,*args): from kivy.app import App app = App.get_running_app() TempestID = app.config['Station']['TempestID'] dataDir = "/var/tmp/" rapidWindFilename = dataDir + TempestID + ".rapid_wind" tempestObsFilename = dataDir + TempestID + ".obs_st" try: ## rapid_wind f = open(rapidWindFilename,"r") data = json.load(f) f.close() rapidWindSpd = str(data['ob'][1]) rapidWindDir = str(data['ob'][2]) # obs_st f = open(tempestObsFilename,"r") tempestData = json.load(f) f.close() lull = str(tempestData['obs'][0][1]) wind = str(tempestData['obs'][0][2]) gust = str(tempestData['obs'][0][3]) windDir = str(tempestData['obs'][0][4]) pressure = str( round(tempestData['obs'][0][6]) ) temp = str(tempestData['obs'][0][7]) humidity = str(tempestData['obs'][0][8]) lux = str(tempestData['obs'][0][9]) uv = str(tempestData['obs'][0][10]) solar = str(tempestData['obs'][0][11]) # slam our JSON values into the app's Obs data to seed the GUI with data # - some items below are hard-coded to aid in reverse engineering the kv fun # - no unit conversion has been done (yet) app.CurrentConditions.Obs['rapidSpd'] = [rapidWindSpd,'m/s'] app.CurrentConditions.Obs['outTemp'] = temp app.CurrentConditions.Obs['Humidity'] = humidity #---- solar panel --- # need PeakSun app.CurrentConditions.Obs['UVIndex'] = [ uv, "", "LOW" , "#00FF00" ] # hard-code desc and color for now lux = [ "2", ' W/m2' ] app.CurrentConditions.Obs['Radiation'] = lux #---- wind panel --- # center circle app.CurrentConditions.Obs['rapidDir'] = [rapidWindDir, u'\u00B0', '-', 'Calm'] # ms, degree symbol, ?, desc # top left app.CurrentConditions.Obs['AvgWind'] = [ wind , "m/s" ] # mid left and btm left app.CurrentConditions.Obs['WindSpd'] = [ '23' , "m/s", '', '9', 'Calm Conditions' ] # a '9' prints an Achilles Heel symbol # top right app.CurrentConditions.Obs['MaxGust'] = [ "93", "m/s" , '', '', ''] # btm right app.CurrentConditions.Obs['WindGust'] = [ gust, "m/s", '', '9', '' ] # yet a white block is printed here app.CurrentConditions.Obs['WindDir'] = [ windDir, '', '', '' ] #--- barometer panel --- app.CurrentConditions.Obs['SLP'] = [ pressure, "hPa", 1025 ] # uncomment for debugging to console after you Esc out of the gui print('---------') print(tempestData) print(app.CurrentConditions.Obs) print('---------') except Exception as e: print(e) obs = 'n/a' return rapidWindSpd # for now, seed BigTemp example panel2 with simulator rapidWind info ''' This is example data from a PurpleAir... { "SensorId": "xx:xx:xx:xx:xx:xx", "DateTime": "2023/02/12T18:58:14z", "Geo": "myhostnamehere", "Mem": 15880, "memfrag": 25, "memfb": 11920, "memcs": 960, "Id": 211261, "lat": 47.123456, "lon": -122.123456, "Adc": 0.04, "loggingrate": 15, "place": "outside", "version": "7.02", "uptime": 4033734, "rssi": -65, "period": 120, "httpsuccess": 34724, "httpsends": 34729, "hardwareversion": "2.0", "hardwarediscovered": "2.0+BME280+PMSX003-B+PMSX003-A", "current_temp_f": 48, "current_humidity": 63, "current_dewpoint_f": 36, "pressure": 1012.46, "p25aqic_b": "rgb(255,255,0)", "pm2.5_aqi_b": 51, "pm1_0_cf_1_b": 7.64, "p_0_3_um_b": 1389.57, "pm2_5_cf_1_b": 12.14, "p_0_5_um_b": 409.69, "pm10_0_cf_1_b": 13.07, "p_1_0_um_b": 57.1, "pm1_0_atm_b": 7.64, "p_2_5_um_b": 8.28, "pm2_5_atm_b": 12.14, "p_5_0_um_b": 0.79, "pm10_0_atm_b": 13.07, "p_10_0_um_b": 0.38, "p25aqic": "rgb(255,255,0)", "pm2.5_aqi": 52, "pm1_0_cf_1": 8.21, "p_0_3_um": 1517.38, "pm2_5_cf_1": 12.5, "p_0_5_um": 459.74, "pm10_0_cf_1": 13.95, "p_1_0_um": 68.69, "pm1_0_atm": 8.21, "p_2_5_um": 4.31, "pm2_5_atm": 12.5, "p_5_0_um": 1.45, "pm10_0_atm": 13.95, "p_10_0_um": 0.24, "pa_latency": 292, "wlstate": "Connected", "status_0": 2, "status_1": 2, "status_2": 2, "status_3": 2, "status_4": 0, "status_5": 0, "status_7": 0, "status_8": 0, "status_9": 0, "ssid": "myssidhere" } '''