Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #
- #
- # GroveWeatherPi Solar Powered Weather Station
- # Version 2.3 October 3, 2016
- #
- # SwitchDoc Labs
- # www.switchdoc.com
- #
- # extended by luksmann@gmail.com - no warranties.
- # imports
- import sys
- import time
- from datetime import datetime
- import random
- import re
- import math
- import os
- sys.path.append('/home/pi/WeatherNew')
- sys.path.append('/home/pi/WeatherNew/Adafruit_ADS1x15')
- sys.path.append('/home/pi/WeatherNew/SDL_Pi_SSD1306')
- sys.path.append('/home/pi/WeatherNew/Adafruit_Python_SSD1306')
- sys.path.append('/home/pi/WeatherNew/RTC_SDL_DS3231')
- sys.path.append('/home/pi/WeatherNew/Adafruit_Python_BMP')
- sys.path.append('/home/pi/WeatherNew/Adafruit_Python_GPIO')
- sys.path.append('/home/pi/WeatherNew/SDL_Pi_WeatherRack')
- sys.path.append('/home/pi/WeatherNew/SDL_Pi_FRAM')
- sys.path.append('/home/pi/WeatherNew/RaspberryPi-AS3935/RPi_AS3935')
- sys.path.append('/home/pi/WeatherNew/SDL_Pi_INA3221')
- sys.path.append('/home/pi/WeatherNew/SDL_Pi_TCA9545')
- sys.path.append('/home/pi/WeatherNew/graphs')
- sys.path.append('/usr/share/weewx')
- import subprocess
- import RPi.GPIO as GPIO
- import smbus
- import struct
- # Check for user imports
- try:
- import conflocal as config
- except ImportError:
- import config
- # if (config.enable_MySQL_Logging == True):
- # import MySQLdb as mdb
- import weewx.drivers
- DRIVER_NAME = 'GrovePi'
- ################
- # Device Present State Variables
- ###############
- config.TCA9545_I2CMux_Present = False
- config.SunAirPlus_Present = False
- config.AS3935_Present = False
- config.DS3231_Present = False
- config.BMP280_Present = False
- config.FRAM_Present = False
- config.HTU21DF_Present = False
- config.AM2315_Present = False
- config.ADS1015_Present = False
- config.ADS1115_Present = False
- config.OLED_Present = False
- config.WXLink_Present = False
- import SDL_Pi_INA3221
- import SDL_DS3231
- import Adafruit_BMP.BMP280 as BMP280
- import SDL_Pi_WeatherRack as SDL_Pi_WeatherRack
- import SDL_Pi_FRAM
- from RPi_AS3935 import RPi_AS3935
- import SDL_Pi_TCA9545
- import Adafruit_SSD1306
- import Scroll_SSD1306
- def loader(config_dict, engine):
- return GrovePiDriver(**config_dict[DRIVER_NAME])
- def returnStatusLine(device, state):
- returnString = device
- if (state == True):
- returnString = returnString + ": \t\tPresent"
- else:
- returnString = returnString + ": \t\tNot Present"
- return returnString
- ################
- # TCA9545 I2C Mux
- #/*=========================================================================
- # I2C ADDRESS/BITS
- # -----------------------------------------------------------------------*/
- TCA9545_ADDRESS = (0x73) # 1110011 (A0+A1=VDD)
- #/*=========================================================================*/
- #/*=========================================================================
- # CONFIG REGISTER (R/W)
- # -----------------------------------------------------------------------*/
- TCA9545_REG_CONFIG = (0x00)
- # /*---------------------------------------------------------------------*/
- TCA9545_CONFIG_BUS0 = (0x01) # 1 = enable, 0 = disable
- TCA9545_CONFIG_BUS1 = (0x02) # 1 = enable, 0 = disable
- TCA9545_CONFIG_BUS2 = (0x04) # 1 = enable, 0 = disable
- TCA9545_CONFIG_BUS3 = (0x08) # 1 = enable, 0 = disable
- #/*=========================================================================*/
- # I2C Mux TCA9545 Detection
- try:
- tca9545 = SDL_Pi_TCA9545.SDL_Pi_TCA9545(addr=TCA9545_ADDRESS, bus_enable = TCA9545_CONFIG_BUS0)
- # turn I2CBus 1 on
- tca9545.write_control_register(TCA9545_CONFIG_BUS2)
- config.TCA9545_I2CMux_Present = True
- except:
- print ">>>>>>>>>>>>>>>>>>><<<<<<<<<<<"
- print "TCA9545 I2C Mux Not Present"
- print ">>>>>>>>>>>>>>>>>>><<<<<<<<<<<"
- config.TCA9545_I2CMux_Present = False
- ################
- # turn I2CBus 0 on
- if (config.TCA9545_I2CMux_Present):
- tca9545.write_control_register(TCA9545_CONFIG_BUS0)
- # HTU21DF Detection
- try:
- HTU21DFOut = subprocess.check_output(["htu21dflib/htu21dflib","-l"])
- config.HTU21DF_Present = True
- except:
- config.HTU21DF_Present = False
- ###############
- #WeatherRack Weather Sensors
- #
- # GPIO Numbering Mode GPIO.BCM
- #
- anemometerPin = 26
- rainPin = 21
- # constants
- SDL_MODE_INTERNAL_AD = 0
- SDL_MODE_I2C_ADS1015 = 1 # internally, the library checks for ADS1115 or ADS1015 if found
- #sample mode means return immediately. THe wind speed is averaged at sampleTime or when you ask, whichever is longer
- SDL_MODE_SAMPLE = 0
- #Delay mode means to wait for sampleTime and the average after that time.
- SDL_MODE_DELAY = 1
- # turn I2CBus 0 on
- if (config.TCA9545_I2CMux_Present):
- tca9545.write_control_register(TCA9545_CONFIG_BUS0)
- weatherStation = SDL_Pi_WeatherRack.SDL_Pi_WeatherRack(anemometerPin, rainPin, 0,0, SDL_MODE_I2C_ADS1015)
- weatherStation.setWindMode(SDL_MODE_SAMPLE, 5.0)
- #weatherStation.setWindMode(SDL_MODE_DELAY, 5.0)
- ################
- # DS3231/AT24C32 Setup
- # turn I2CBus 0 on
- # if (config.TCA9545_I2CMux_Present):
- # tca9545.write_control_register(TCA9545_CONFIG_BUS0)
- # filename = time.strftime("%Y-%m-%d%H:%M:%SRTCTest") + ".txt"
- # starttime = datetime.utcnow()
- # ds3231 = SDL_DS3231.SDL_DS3231(1, 0x68)
- # try:
- # ds3231.write_now()
- # ds3231.read_datetime()
- #print "DS3231=\t\t%s" % ds3231.read_datetime()
- # config.DS3231_Present = True
- #print "----------------- "
- #print "----------------- "
- #print " AT24C32 EEPROM"
- #print "----------------- "
- #print "writing first 4 addresses with random data"
- # for x in range(0,4):
- # value = random.randint(0,255)
- #print "address = %i writing value=%i" % (x, value)
- # ds3231.write_AT24C32_byte(x, value)
- #print "----------------- "
- #print "reading first 4 addresses"
- #for x in range(0,4):
- # print "address = %i value = %i" %(x, ds3231.read_AT24C32_byte(x))
- #print "----------------- "
- # except IOError as e:
- #print "I/O error({0}): {1}".format(e.errno, e.strerror)
- # config.DS3231_Present = False
- ################
- # BMP280 Setup
- try:
- bmp280 = BMP280.BMP280()
- config.BMP280_Present = True
- except IOError as e:
- # print "I/O error({0}): {1}".format(e.errno, e.strerror)
- config.BMP280_Present = False
- ##############
- # Setup AM2315
- # turn I2CBus 0 on
- if (config.TCA9545_I2CMux_Present):
- tca9545.write_control_register(TCA9545_CONFIG_BUS0)
- ###############
- # Detect AM2315
- try:
- from tentacle_pi.AM2315 import AM2315
- try:
- am2315 = AM2315(0x5c,"/dev/i2c-1")
- outsideTemperature, outsideHumidity, crc_check = am2315.sense()
- #print "outsideTemperature: %0.1f C" % outsideTemperature
- #print "outsideHumidity: %0.1f %%" % outsideHumidity
- #print "crc: %i" % crc_check
- config.AM2315_Present = True
- if (crc_check == -1):
- config.AM2315_Present = False
- except:
- config.AM2315_Present = False
- except:
- config.AM2315_Present = False
- print "------> See Readme to install tentacle_pi"
- ###############
- # Set up FRAM
- # Set up FRAM
- # turn I2CBus 0 on
- if (config.TCA9545_I2CMux_Present):
- tca9545.write_control_register(TCA9545_CONFIG_BUS0)
- fram = SDL_Pi_FRAM.SDL_Pi_FRAM(addr = 0x50)
- # FRAM Detection
- try:
- fram.read8(0)
- config.FRAM_Present = True
- except:
- config.FRAM_Present = False
- #fram = SDL_Pi_FRAM.SDL_Pi_FRAM(addr = 0x50)
- # Main Loop - sleeps 10 seconds
- # command from RasPiConnect Execution Code
- def hardware_name(self):
- return DRIVER_NAME
- def loader(config_dict, engine):
- return GrovePiDriver(**config_dict[DRIVER_NAME])
- class GrovePiDriver(weewx.drivers.AbstractDevice):
- def __init__(self, **stn_dict):
- self.poll_interval = float(stn_dict.get('poll_interval', 30)) # seconds
- # self.air_temperature = 0
- # self.air_sensor = 1
- # grovepi.pinMode(self.air_sensor, 'INPUT')
- def genLoopPackets(self):
- while True:
- sampleAndDisplay()
- packet = {
- 'usUnits': weewx.METRIC,
- 'outTemp' : outsideTemperature,
- 'dateTime': int(time.time() + 0.5),
- # 'inTemp' : inTemp,
- 'barometer' : bmp180Pressure,
- 'pressure' : bmp180Altitude,
- 'windSpeed' : currentWindSpeed,
- 'windDir' : currentWindDirection,
- 'windGust' : currentWindGust,
- 'outHumidity': outsideHumidity,
- 'rain' : totalRain,
- }
- print packet
- yield packet
- time.sleep(15)
- def sampleAndDisplay():
- global currentWindSpeed, currentWindGust, currentWindDirection
- global bmp180Temperature, bmp180Pressure, bmp180Altitude, bmp180SeaLevel
- global outsideTemperature, outsideHumidity, crc_check
- global currentWindDirectionVoltage
- global HTUtemperature, HTUhumidity
- global totalRain
- # global inTemp
- global block1, block2
- # turn I2CBus 0 on
- if (config.TCA9545_I2CMux_Present):
- tca9545.write_control_register(TCA9545_CONFIG_BUS0)
- # print "----------------- "
- print " Local WeatherRack Weather Sensors Sampling"
- print "----------------- "
- #
- currentWindSpeed = weatherStation.current_wind_speed()
- currentWindGust = weatherStation.get_wind_gust()
- totalRain = weatherStation.get_current_rain_total()
- currentWindDirection = weatherStation.current_wind_direction()
- print("Rain Total=\t%0.2f in")%(totalRain/25.4)
- print("Wind Speed=\t%0.2f MPH")%(currentWindSpeed/1.6)
- print("MPH wind_gust=\t%0.2f MPH")%(currentWindGust/1.6)
- if (config.ADS1015_Present or config.ADS1115_Present):
- print "Wind Direction=\t\t\t %0.2f Degrees" % weatherStation.current_wind_direction()
- print "Wind Direction Voltage=\t\t %0.3f V" % weatherStation.current_wind_direction_voltage()
- else:
- print "No Wind Direction Available - No ADS1015 or ADS1115 Present"
- print "----------------- "
- print "----------------- "
- # if (config.DS3231_Present == True):
- # print " DS3231 Real Time Clock"
- # else:
- # print " DS3231 Real Time Clock Not Present"
- # print "----------------- "
- #
- # if (config.DS3231_Present == True):
- # currenttime = datetime.utcnow()
- # deltatime = currenttime - starttime
- # print "Raspberry Pi=\t" + time.strftime("%Y-%m-%d %H:%M:%S")
- # print "DS3231=\t\t%s" % ds3231.read_datetime()
- # print "DS3231 Temperature= \t%0.2f C" % ds3231.getTemp()
- # inTemp = ds3231.getTemp()
- # print "----------------- "
- # print "SecondCount=", secondCount
- # print "----------------- "
- # print "----------------- "
- print "----------------- "
- if (config.BMP280_Present == True):
- print " BMP280 Barometer"
- else:
- print " BMP280 Barometer Not Present"
- print "----------------- "
- if (config.BMP280_Present):
- print 'Temperature = \t{0:0.2f} C'.format(bmp280.read_temperature())
- print 'Pressure = \t{0:0.2f} KPa'.format(bmp280.read_pressure()/1000)
- print 'Altitude = \t{0:0.2f} m'.format(bmp280.read_altitude())
- print 'Sealevel Pressure = \t{0:0.2f} KPa'.format(bmp280.read_sealevel_pressure(config.BMP280_Altitude_Meters)/1000)
- print "----------------- "
- bmp180Pressure = bmp280.read_pressure()/100
- bmp180Altitude = bmp280.read_sealevel_pressure(config.BMP280_Altitude_Meters)/100
- print "----------------- "
- if (config.HTU21DF_Present == True):
- print " HTU21DF Temp/Hum"
- else:
- print " HTU21DF Temp/Hum Not Present"
- print "----------------- "
- # We use a C library for this device as it just doesn't play well with Python and smbus/I2C libraries
- if (config.HTU21DF_Present):
- HTU21DFOut = subprocess.check_output(["htu21dflib/htu21dflib","-l"])
- splitstring = HTU21DFOut.split()
- HTUtemperature = float(splitstring[0])
- HTUhumidity = float(splitstring[1])
- print "Temperature = \t%0.2f C" % HTUtemperature
- print "Humidity = \t%0.2f %%" % HTUhumidity
- else:
- HTUtemperature = 0.0
- HTUhumidity = 0.0
- print "----------------- "
- print "----------------- "
- # turn I2CBus 0 on
- if (config.TCA9545_I2CMux_Present):
- tca9545.write_control_register(TCA9545_CONFIG_BUS0)
- print "----------------- "
- if (config.AM2315_Present == True):
- print " AM2315 Temperature/Humidity Sensor"
- else:
- print " AM2315 Temperature/Humidity Sensor Not Present"
- print "----------------- "
- if (config.AM2315_Present):
- outsideTemperature, outsideHumidity, crc_check = am2315.sense()
- print "outsideTemperature: %0.1f C" % outsideTemperature
- print "outsideHumidity: %0.1f %%" % outsideHumidity
- print "crc: %s" % crc_check
- print "----------------- "
- print "------------------------------"
- if __name__ == "__main__":
- import weeutil.weeutil
- driver = GrovePiDriver()
- for packet in driver.genLoopPackets():
- print weeutil.weeutil.timestamp_to_string(packet['dateTime']), packet
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement