Advertisement
Guest User

weewx for weatherPi

a guest
Aug 31st, 2017
843
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 13.46 KB | None | 0 0
  1. #
  2. #
  3. # GroveWeatherPi Solar Powered Weather Station
  4. # Version 2.3 October 3, 2016
  5. #
  6. # SwitchDoc Labs
  7. # www.switchdoc.com
  8. #
  9. # extended by luksmann@gmail.com - no warranties.
  10.  
  11. # imports
  12.  
  13. import sys
  14. import time
  15. from datetime import datetime
  16. import random
  17. import re
  18. import math
  19. import os
  20.  
  21. sys.path.append('/home/pi/WeatherNew')
  22. sys.path.append('/home/pi/WeatherNew/Adafruit_ADS1x15')
  23. sys.path.append('/home/pi/WeatherNew/SDL_Pi_SSD1306')
  24. sys.path.append('/home/pi/WeatherNew/Adafruit_Python_SSD1306')
  25. sys.path.append('/home/pi/WeatherNew/RTC_SDL_DS3231')
  26. sys.path.append('/home/pi/WeatherNew/Adafruit_Python_BMP')
  27. sys.path.append('/home/pi/WeatherNew/Adafruit_Python_GPIO')
  28. sys.path.append('/home/pi/WeatherNew/SDL_Pi_WeatherRack')
  29. sys.path.append('/home/pi/WeatherNew/SDL_Pi_FRAM')
  30. sys.path.append('/home/pi/WeatherNew/RaspberryPi-AS3935/RPi_AS3935')
  31. sys.path.append('/home/pi/WeatherNew/SDL_Pi_INA3221')
  32. sys.path.append('/home/pi/WeatherNew/SDL_Pi_TCA9545')
  33. sys.path.append('/home/pi/WeatherNew/graphs')
  34. sys.path.append('/usr/share/weewx')
  35.  
  36. import subprocess
  37. import RPi.GPIO as GPIO
  38.  
  39. import smbus
  40.  
  41. import struct
  42.  
  43. # Check for user imports
  44. try:
  45.     import conflocal as config
  46. except ImportError:
  47.     import config
  48.  
  49. # if (config.enable_MySQL_Logging == True):
  50. #   import MySQLdb as mdb
  51.  
  52. import weewx.drivers
  53. DRIVER_NAME = 'GrovePi'
  54.  
  55. ################
  56. # Device Present State Variables
  57. ###############
  58.  
  59. config.TCA9545_I2CMux_Present = False
  60. config.SunAirPlus_Present = False
  61. config.AS3935_Present = False
  62. config.DS3231_Present = False
  63. config.BMP280_Present = False
  64. config.FRAM_Present = False
  65. config.HTU21DF_Present = False
  66. config.AM2315_Present = False
  67. config.ADS1015_Present = False
  68. config.ADS1115_Present = False
  69. config.OLED_Present = False
  70. config.WXLink_Present = False
  71.  
  72.  
  73. import SDL_Pi_INA3221
  74. import SDL_DS3231
  75. import Adafruit_BMP.BMP280 as BMP280
  76. import SDL_Pi_WeatherRack as SDL_Pi_WeatherRack
  77.  
  78. import SDL_Pi_FRAM
  79. from RPi_AS3935 import RPi_AS3935
  80.  
  81. import SDL_Pi_TCA9545
  82.  
  83. import Adafruit_SSD1306
  84.  
  85. import Scroll_SSD1306
  86.  
  87.  
  88. def loader(config_dict, engine):
  89.     return GrovePiDriver(**config_dict[DRIVER_NAME])
  90.  
  91. def returnStatusLine(device, state):
  92.  
  93.         returnString = device
  94.         if (state == True):
  95.                 returnString = returnString + ":   \t\tPresent"
  96.         else:
  97.                 returnString = returnString + ":   \t\tNot Present"
  98.         return returnString
  99.  
  100. ################
  101. # TCA9545 I2C Mux
  102.  
  103. #/*=========================================================================
  104. #    I2C ADDRESS/BITS
  105. #    -----------------------------------------------------------------------*/
  106. TCA9545_ADDRESS =                         (0x73)    # 1110011 (A0+A1=VDD)
  107. #/*=========================================================================*/
  108.  
  109. #/*=========================================================================
  110. #    CONFIG REGISTER (R/W)
  111. #    -----------------------------------------------------------------------*/
  112. TCA9545_REG_CONFIG            =          (0x00)
  113. #    /*---------------------------------------------------------------------*/
  114.  
  115. TCA9545_CONFIG_BUS0  =                (0x01)  # 1 = enable, 0 = disable
  116. TCA9545_CONFIG_BUS1  =                (0x02)  # 1 = enable, 0 = disable
  117. TCA9545_CONFIG_BUS2  =                (0x04)  # 1 = enable, 0 = disable
  118. TCA9545_CONFIG_BUS3  =                (0x08)  # 1 = enable, 0 = disable
  119.  
  120. #/*=========================================================================*/
  121.  
  122. # I2C Mux TCA9545 Detection
  123. try:
  124.     tca9545 = SDL_Pi_TCA9545.SDL_Pi_TCA9545(addr=TCA9545_ADDRESS, bus_enable = TCA9545_CONFIG_BUS0)
  125.  
  126.  
  127.     # turn I2CBus 1 on
  128.     tca9545.write_control_register(TCA9545_CONFIG_BUS2)
  129.     config.TCA9545_I2CMux_Present = True
  130. except:
  131.     print ">>>>>>>>>>>>>>>>>>><<<<<<<<<<<"
  132.     print "TCA9545 I2C Mux Not Present"
  133.     print ">>>>>>>>>>>>>>>>>>><<<<<<<<<<<"
  134.     config.TCA9545_I2CMux_Present = False
  135.  
  136.  
  137. ################
  138. # turn I2CBus 0 on
  139. if (config.TCA9545_I2CMux_Present):
  140.      tca9545.write_control_register(TCA9545_CONFIG_BUS0)
  141.  
  142. # HTU21DF Detection
  143. try:
  144.         HTU21DFOut = subprocess.check_output(["htu21dflib/htu21dflib","-l"])
  145.         config.HTU21DF_Present = True
  146. except:
  147.         config.HTU21DF_Present = False
  148.  
  149.  
  150. ###############
  151.  
  152. #WeatherRack Weather Sensors
  153. #
  154. # GPIO Numbering Mode GPIO.BCM
  155. #
  156.  
  157. anemometerPin = 26
  158. rainPin = 21
  159.  
  160. # constants
  161.  
  162. SDL_MODE_INTERNAL_AD = 0
  163. SDL_MODE_I2C_ADS1015 = 1    # internally, the library checks for ADS1115 or ADS1015 if found
  164.  
  165. #sample mode means return immediately.  THe wind speed is averaged at sampleTime or when you ask, whichever is longer
  166. SDL_MODE_SAMPLE = 0
  167. #Delay mode means to wait for sampleTime and the average after that time.
  168. SDL_MODE_DELAY = 1
  169.  
  170. # turn I2CBus 0 on
  171. if (config.TCA9545_I2CMux_Present):
  172.     tca9545.write_control_register(TCA9545_CONFIG_BUS0)
  173. weatherStation = SDL_Pi_WeatherRack.SDL_Pi_WeatherRack(anemometerPin, rainPin, 0,0, SDL_MODE_I2C_ADS1015)
  174.  
  175. weatherStation.setWindMode(SDL_MODE_SAMPLE, 5.0)
  176. #weatherStation.setWindMode(SDL_MODE_DELAY, 5.0)
  177.  
  178. ################
  179. # DS3231/AT24C32 Setup
  180. # turn I2CBus 0 on
  181. # if (config.TCA9545_I2CMux_Present):
  182. #         tca9545.write_control_register(TCA9545_CONFIG_BUS0)
  183.  
  184. # filename = time.strftime("%Y-%m-%d%H:%M:%SRTCTest") + ".txt"
  185. # starttime = datetime.utcnow()
  186.  
  187. # ds3231 = SDL_DS3231.SDL_DS3231(1, 0x68)
  188.  
  189.  
  190. # try:
  191.  
  192.        
  193. #        ds3231.write_now()
  194. #        ds3231.read_datetime()
  195.         #print "DS3231=\t\t%s" % ds3231.read_datetime()
  196. #        config.DS3231_Present = True
  197.         #print "----------------- "
  198.         #print "----------------- "
  199.         #print " AT24C32 EEPROM"
  200.         #print "----------------- "
  201.         #print "writing first 4 addresses with random data"
  202. #        for x in range(0,4):
  203. #                value = random.randint(0,255)
  204.             #print "address = %i writing value=%i" % (x, value)
  205. #                ds3231.write_AT24C32_byte(x, value)
  206.         #print "----------------- "
  207.  
  208.         #print "reading first 4 addresses"
  209.         #for x in range(0,4):
  210.         #        print "address = %i value = %i" %(x, ds3231.read_AT24C32_byte(x))
  211.         #print "----------------- "
  212.  
  213. # except IOError as e:
  214.         #print "I/O error({0}): {1}".format(e.errno, e.strerror)
  215. #        config.DS3231_Present = False
  216.  
  217.  
  218.  
  219. ################
  220.  
  221. # BMP280 Setup
  222.  
  223. try:
  224.         bmp280 = BMP280.BMP280()
  225.         config.BMP280_Present = True
  226.  
  227. except IOError as e:
  228.  
  229.         #    print "I/O error({0}): {1}".format(e.errno, e.strerror)
  230.         config.BMP280_Present = False
  231.  
  232.  
  233. ##############
  234. # Setup AM2315
  235. # turn I2CBus 0 on
  236. if (config.TCA9545_I2CMux_Present):
  237.          tca9545.write_control_register(TCA9545_CONFIG_BUS0)
  238.  
  239.  
  240. ###############
  241.  
  242. # Detect AM2315
  243. try:
  244.         from tentacle_pi.AM2315 import AM2315
  245.         try:
  246.         am2315 = AM2315(0x5c,"/dev/i2c-1")
  247.             outsideTemperature, outsideHumidity, crc_check = am2315.sense()
  248.         #print "outsideTemperature: %0.1f C" % outsideTemperature
  249.             #print "outsideHumidity: %0.1f %%" % outsideHumidity
  250.             #print "crc: %i" % crc_check
  251.                 config.AM2315_Present = True
  252.         if (crc_check == -1):
  253.                     config.AM2315_Present = False
  254.  
  255.         except:
  256.                 config.AM2315_Present = False
  257. except:
  258.         config.AM2315_Present = False
  259.         print "------> See Readme to install tentacle_pi"
  260.  
  261.  
  262. ###############
  263.  
  264. # Set up FRAM
  265. # Set up FRAM
  266.  
  267. # turn I2CBus 0 on
  268. if (config.TCA9545_I2CMux_Present):
  269.          tca9545.write_control_register(TCA9545_CONFIG_BUS0)
  270. fram = SDL_Pi_FRAM.SDL_Pi_FRAM(addr = 0x50)
  271. # FRAM Detection
  272. try:
  273.         fram.read8(0)
  274.         config.FRAM_Present = True
  275. except:
  276.         config.FRAM_Present = False
  277.  
  278.  
  279. #fram = SDL_Pi_FRAM.SDL_Pi_FRAM(addr = 0x50)
  280.  
  281.  
  282. # Main Loop - sleeps 10 seconds
  283. # command from RasPiConnect Execution Code
  284.  
  285. def hardware_name(self):
  286.         return DRIVER_NAME
  287.  
  288. def loader(config_dict, engine):
  289.     return GrovePiDriver(**config_dict[DRIVER_NAME])
  290.  
  291. class GrovePiDriver(weewx.drivers.AbstractDevice):
  292.     def __init__(self, **stn_dict):
  293.         self.poll_interval = float(stn_dict.get('poll_interval', 30)) # seconds
  294. #        self.air_temperature = 0
  295. #        self.air_sensor = 1
  296. #        grovepi.pinMode(self.air_sensor, 'INPUT')     
  297.  
  298.     def genLoopPackets(self):
  299.         while True:
  300.             sampleAndDisplay()
  301.             packet = {
  302.                 'usUnits': weewx.METRIC,
  303.                 'outTemp'    : outsideTemperature,
  304.                 'dateTime': int(time.time() + 0.5),
  305. #                       'inTemp'     : inTemp,
  306.                         'barometer'  : bmp180Pressure,
  307.                         'pressure'   : bmp180Altitude,
  308.                         'windSpeed'  : currentWindSpeed,
  309.                         'windDir'    : currentWindDirection,
  310.                         'windGust'   : currentWindGust,
  311.                         'outHumidity': outsideHumidity,
  312.                         'rain'       : totalRain,
  313.                 }
  314.             print packet
  315.             yield packet
  316.             time.sleep(15)
  317.            
  318. def sampleAndDisplay():
  319.        
  320.     global currentWindSpeed, currentWindGust, currentWindDirection
  321.     global bmp180Temperature, bmp180Pressure, bmp180Altitude,  bmp180SeaLevel
  322.     global outsideTemperature, outsideHumidity, crc_check
  323.     global currentWindDirectionVoltage
  324.  
  325.     global HTUtemperature, HTUhumidity
  326.     global totalRain
  327. #   global inTemp
  328.  
  329.     global block1, block2
  330.    
  331.     # turn I2CBus 0 on
  332.     if (config.TCA9545_I2CMux_Present):
  333.              tca9545.write_control_register(TCA9545_CONFIG_BUS0)
  334. #       print "----------------- "
  335.     print " Local WeatherRack Weather Sensors Sampling"
  336.     print "----------------- "
  337.     #
  338.  
  339.     currentWindSpeed = weatherStation.current_wind_speed()
  340.     currentWindGust = weatherStation.get_wind_gust()
  341.     totalRain = weatherStation.get_current_rain_total()
  342.     currentWindDirection = weatherStation.current_wind_direction()
  343.  
  344.     print("Rain Total=\t%0.2f in")%(totalRain/25.4)
  345.     print("Wind Speed=\t%0.2f MPH")%(currentWindSpeed/1.6)
  346.     print("MPH wind_gust=\t%0.2f MPH")%(currentWindGust/1.6)
  347.  
  348.     if (config.ADS1015_Present or config.ADS1115_Present):
  349.         print "Wind Direction=\t\t\t %0.2f Degrees" % weatherStation.current_wind_direction()
  350.         print "Wind Direction Voltage=\t\t %0.3f V" % weatherStation.current_wind_direction_voltage()
  351.     else:
  352.         print "No Wind Direction Available - No ADS1015 or ADS1115 Present"
  353.    
  354.     print "----------------- "
  355.         print "----------------- "
  356. #        if (config.DS3231_Present == True):
  357. #                print " DS3231 Real Time Clock"
  358. #        else:
  359. #                print " DS3231 Real Time Clock Not Present"
  360.  
  361. #        print "----------------- "
  362.         #
  363.  
  364. #        if (config.DS3231_Present == True):
  365. #                currenttime = datetime.utcnow()
  366.  
  367. #                deltatime = currenttime - starttime
  368.  
  369. #                print "Raspberry Pi=\t" + time.strftime("%Y-%m-%d %H:%M:%S")
  370.  
  371. #                print "DS3231=\t\t%s" % ds3231.read_datetime()
  372.  
  373. #                print "DS3231 Temperature= \t%0.2f C" % ds3231.getTemp()
  374. #                inTemp = ds3231.getTemp()
  375.  
  376.  
  377. #   print "----------------- "
  378. #   print "SecondCount=", secondCount
  379. #        print "----------------- "
  380. #   print "----------------- "
  381.         print "----------------- "
  382.         if (config.BMP280_Present == True):
  383.                 print " BMP280 Barometer"
  384.         else:
  385.                 print " BMP280 Barometer Not Present"
  386.         print "----------------- "
  387.  
  388.     if (config.BMP280_Present):
  389.         print 'Temperature = \t{0:0.2f} C'.format(bmp280.read_temperature())
  390.         print 'Pressure = \t{0:0.2f} KPa'.format(bmp280.read_pressure()/1000)
  391.         print 'Altitude = \t{0:0.2f} m'.format(bmp280.read_altitude())
  392.         print 'Sealevel Pressure = \t{0:0.2f} KPa'.format(bmp280.read_sealevel_pressure(config.BMP280_Altitude_Meters)/1000)
  393.         print "----------------- "
  394.        
  395.         bmp180Pressure = bmp280.read_pressure()/100
  396.         bmp180Altitude = bmp280.read_sealevel_pressure(config.BMP280_Altitude_Meters)/100
  397.  
  398.         print "----------------- "
  399.         if (config.HTU21DF_Present == True):
  400.                 print " HTU21DF Temp/Hum"
  401.         else:
  402.                 print " HTU21DF Temp/Hum Not Present"
  403.         print "----------------- "
  404.  
  405.         # We use a C library for this device as it just doesn't play well with Python and smbus/I2C libraries
  406.         if (config.HTU21DF_Present):
  407.                 HTU21DFOut = subprocess.check_output(["htu21dflib/htu21dflib","-l"])
  408.                 splitstring = HTU21DFOut.split()
  409.  
  410.                 HTUtemperature = float(splitstring[0])
  411.                 HTUhumidity = float(splitstring[1])
  412.                 print "Temperature = \t%0.2f C" % HTUtemperature
  413.                 print "Humidity = \t%0.2f %%" % HTUhumidity
  414.     else:
  415.         HTUtemperature = 0.0
  416.         HTUhumidity = 0.0
  417.  
  418.         print "----------------- "
  419.  
  420.     print "----------------- "
  421.  
  422.     # turn I2CBus 0 on
  423.     if (config.TCA9545_I2CMux_Present):
  424.             tca9545.write_control_register(TCA9545_CONFIG_BUS0)
  425.  
  426.  
  427.         print "----------------- "
  428.         if (config.AM2315_Present == True):
  429.                 print " AM2315 Temperature/Humidity Sensor"
  430.         else:
  431.                 print " AM2315 Temperature/Humidity Sensor Not Present"
  432.         print "----------------- "
  433.  
  434.         if (config.AM2315_Present):
  435.             outsideTemperature, outsideHumidity, crc_check = am2315.sense()
  436.             print "outsideTemperature: %0.1f C" % outsideTemperature
  437.             print "outsideHumidity: %0.1f %%" % outsideHumidity
  438.             print "crc: %s" % crc_check
  439.         print "----------------- "
  440.  
  441.     print "------------------------------"
  442.  
  443. if __name__ == "__main__":
  444.     import weeutil.weeutil
  445.     driver = GrovePiDriver()
  446.         for packet in driver.genLoopPackets():
  447.             print weeutil.weeutil.timestamp_to_string(packet['dateTime']), packet
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement