def go_DHT(): dht_pin = Pin( 'G10', Pin.OPEN_DRAIN ) # connect DHT22 sensor data line to pin P9/G16 on the expansion board dht_pin(1) # drive pin high to initiate data conversion on DHT sensor while (True): temp, hum = DHT22(dht_pin) temp_str = '{}.{}'.format(temp // 10, temp % 10) hum_str = '{}.{}'.format(hum // 10, hum % 10) # Print or upload it print('temp = {}C; hum = {}%'.format(temp_str, hum_str)) # if hum!=0xffff: # sendtoLoRa(dev_ID, temp, hum) sda_pin = Pin('G16', Pin.OPEN_DRAIN) scl_pin = Pin('G17', Pin.OPEN_DRAIN) bus = I2C(0) #sda = sda_pin, scl = scl_pin) bus.init(I2C.MASTER, baudrate=100000) # on pyboard # bus = I2C(scl=Pin(4), sda=Pin(5), freq=100000) # on esp8266 bmp180 = BMP180(bus) bmp180.oversample_sett = 0 #bmp180.baseline = 101325 temp = bmp180.temperature p = bmp180.pressure altitude = bmp180.altitude sl_pressure = bmp180.sea_level_pressure print(temp, p, sl_pressure, altitude) time.sleep(2)
def setup(devs): global switch, ext_sensor, water_sensor, int_sensor switch = machine.Pin(14,machine.Pin.PULL_UP) ext_sensor = dht.DHT22(machine.Pin(12)) water_sensor = machine.ADC(0) int_sensor = BMP180(devs.i2c) int_sensor.oversample_sett = 2 int_sensor.baseline = 101325 return 'house/wether', 1000
def getPresure(): try: i2c = machine.I2C(scl=machine.Pin(22), sda=machine.Pin(21)) bmp180 = BMP180(i2c) p = bmp180.pressure * 0.00750062 time.sleep(1) except Exception as e: time.sleep(1) print("Error: ", str(e)) p = 750 return p
def __init__(self, scladd = 4, sdaadd = 5, bmpadd = 0x77, bmpfreq = 400000, oledadd = 0x3C): self.scladd = scladd self.sdaadd = sdaadd self.bmpadd = bmpadd self.bmpfreq = bmpfreq self.oledadd = oledadd self.i2c = I2C(scl=Pin(self.scladd), sda=Pin(self.sdaadd), freq=self.bmpfreq) #freq 100000 on esp8266 self.bmp180 = BMP180(self.i2c) self.oled = ssd1306.SSD1306_I2C(128, 64, self.i2c, self.oledadd) #0x3c self.bmp180.oversample_sett = 2 self.bmp180.baseline = 102225 #101325
def leer_presion(scl_pin, sda_pin): """ Lee la presion del sensor BMP180 """ bus = I2C(scl=Pin(scl_pin), sda=Pin(sda_pin), freq=400000) # create I2C peripheral at frequency of 400kHz # bus = I2C(1, baudrate=100000) bmp180 = BMP180(bus) bmp180.oversample_sett = 2 bmp180.baseline = 101325 temp = bmp180.temperature p = bmp180.pressure altitude = bmp180.altitude return p, temp
def getWeerdata(): from bmp180 import BMP180 from machine import Pin, PWM import machine bus = machine.I2C(scl=machine.Pin(5), sda=machine.Pin(4), freq=100000) bmp180 = BMP180(bus) bmp180.oversample_sett = 2 bmp180.baseline = 101325 temp = bmp180.temperature - 3.5 # deze apparaat meet altijd 4 graden te hoog. druk1 = bmp180.pressure altitude1 = bmp180.altitude return druk1, altitude1, temp
def getI2C(self): if self.i2c == None: self.i2c = machine.I2C(scl=machine.Pin(5, machine.Pin.OUT, machine.Pin.PULL_UP), sda=machine.Pin(4, machine.Pin.OUT, machine.Pin.PULL_UP)) self.dht12sensor = dht12.DHT12(self.i2c) self.bmp180 = BMP180(self.i2c) self.bmp180.oversample_sett = 2 self.bmp180.baseline = 101325 # self.envData['LIGHT_LEVEL'] = bh1750fvi.sample(self.i2c) self.envData['BMP_TEMPERATURE'] = self.bmp180.temperature self.envData['BMP_PRESSURE'] = (self.bmp180.pressure) / 100
def read_bmp180(): """ Function to read externally connected BMP180 Temperature, Barometric Pressure and Altitude sensor Returns: tuple: Temperature in Celcius, Altitude in Meters and, Pressure in Pascals """ bus = I2C(scl=Pin(22), sda=Pin(21), freq=100000) bmp180 = BMP180(bus) bmp180.oversample_sett = 2 bmp180.baseline = 101325 temperature = bmp180.temperature pressure = bmp180.pressure altitude = bmp180.altitude return temperature, pressure, altitude
async def main(client): # configure I2C bus and BMP180 sensor i2c_bus = I2C(scl=Pin(5), sda=Pin(4), freq=1000000) bmp180 = BMP180(i2c_bus) bmp180.oversample_sett = 2 bmp180.baseline = 101325 # configure local time ntptime.settime() rtc = RTC() try: await client.connect() except OSError: print('Connection failed.') return n = 0 while True: await asyncio.sleep(5) sensor_temp = bmp180.temperature sensor_press = bmp180.pressure / 100 print('publish', n, ' temperature:', sensor_temp, ', pressure:', sensor_press) payload = """ {{ "temperature": {}, "pressure": {} }} """ time = str(946684800 + utime.mktime((rtc.datetime()[0], rtc.datetime()[1], rtc.datetime()[2], rtc.datetime()[4], rtc.datetime()[5], rtc.datetime()[6], 0, 0))) tag = 'sensor1' # If WiFi is down the following will pause for the duration. await client.publish(PUB_TOPIC, payload.format(sensor_temp, sensor_press), qos=1) n += 1
def main(): net = network.WLAN(network.STA_IF) net.active(True) net.connect(WIFI_SSID, WIFI_PASS) while not net.isconnected(): pass # Connecting to MQTT broker print("Initializing MQTT client") mqtt_client = MQTTClient("micropy_client", BROKER_HOST, 1883) mqtt_client.connect() # Start reading data from sensors print("Initializing I2C for BMP180") i2c_bus = I2C(sda=Pin(SDA_PIN), scl=Pin(SCL_PIN)) bmp180 = BMP180(i2c_bus) bmp180.oversample_sett = 2 bmp180.baseline = 101325 print("Initializing phototransistor ADC") photo_transistor = ADC(Pin(ADC_PIN)) while True: time.sleep(2) data = { 'temperature': bmp180.temperature, 'pressure': bmp180.pressure, 'light': photo_transistor.read(), } serialized = json.dumps(data) print("Will publish {}".format(serialized)) # publish data to broker mqtt_client.publish(bytearray(PUBLISH_TOPIC), bytearray(serialized))
def __init__(self, mbus=None, i2c=False): self.mbus = mbus s_bmp = BMP180(i2c) s_bmp.read_delay = 300 s_bmp.sensor_detect() s_bmp.start() s_bmp.name = "bmp" if self.mbus: self.mbus.pub({ "id": "TOPIC", "msg": { "name": s_bmp.name, "data": "tem" } }) self.mbus.pub({ "id": "TOPIC", "msg": { "name": s_bmp.name, "data": "pres" } }) self.mbus.pub({ "id": "TOPIC", "msg": { "name": s_bmp.name, "data": "alt" } }) s_bmp.set_callback(self.s_bmp_cb)
from bmp180 import BMP180 bmp=BMP180(1) tem=bmp.getTemp() press=bmp.getPress() altitude=bmp.getAltitude() print('tem:',tem) print('press:',press) print('altude:',altitude)
def main(): esp.osdebug(None) import gc gc.collect() i2c = I2C(scl=machine.Pin(5), sda=machine.Pin(4)) sensor = dht.DHT11(Pin(12)) bmp180 = BMP180(i2c) bmp180.oversample_sett = 2 bmp180.baseline = 101325 mqtt_server = '192.168.10.124' net = do_connect('HoltAtHome4','anthony050192') client_id = ubinascii.hexlify(machine.unique_id()) client = MQTTClient(client_id, mqtt_server) client.connect() count = 255 base = '/home/office/' print("Free : ", gc.mem_free()) while True: lightLevel = bh1750fvi.sample(i2c) print("Light Level : ",lightLevel) print("Count : ",count) client.publish(base + 'light', str(lightLevel)) count = count+1 if count > 2: sensor.measure() humidity = sensor.humidity() temp1 = sensor.temperature() temp = bmp180.temperature p = bmp180.pressure mbar = p/100 freeMem = gc.mem_free() dewPoint = calcRH(temp,humidity) print("Temperature :", temp) print("Temperature1:", temp1) print("Pressure :", mbar) print("Humidity :", humidity) print("Dew Point :", dewPoint) print("Free : ", freeMem) client.publish(base + 'temperature', str(temp)) client.publish(base + 'pressure', str(mbar)) client.publish(base + 'humidity', str(humidity)) client.publish(base + 'dewpoint', str(dewPoint)) client.publish(base + 'envNode/freemem' , str(freeMem)) count = 0 gc.collect() time.sleep(15)
def init_bmp(self): bus = I2C(scl=Pin(self.BMPSCL), sda=Pin(self.BMPSDA), freq=100000) self.bmp = BMP180(bus)
from machine import ADC, I2C, Pin from bmp180 import BMP180 from framebuf import FrameBuffer, MONO_HLSB from ssd1306 import SSD1306_I2C from time import sleep SDA1 = Pin(26) SCL1 = Pin(27) i2c1 = I2C(1, sda=SDA1, scl=SCL1) bmp = BMP180(i2c1) WIDTH = 128 # oled display width HEIGHT = 64 # oled display height TEMP_ADC = 4 SDA0 = Pin(8) # GPI0 8 is Pin 11 SCL0 = Pin(9) # GPIO 9 is Pin 12 i2c0 = I2C(0, sda=SDA0, scl=SCL0, freq=200000) # Init I2C using pins GP8 & GP9 (default I2C0 pins) print("I2C Address : " + hex(i2c0.scan()[0]).upper()) # Display device address print("I2C Configuration: " + str(i2c0)) # Display I2C config oled = SSD1306_I2C(WIDTH, HEIGHT, i2c0) # Init oled display # Raspberry Pi logo as 32x32 bytearray #buffer = bytearray(b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00|?\x00\x01\x86@\x80\x01\x01\x80\x80\x01\x11\x88\x80\x01\x05\xa0\x80\x00\x83\xc1\x00\x00C\xe3\x00\x00~\xfc\x00\x00L'\x00\x00\x9c\x11\x00\x00\xbf\xfd\x00\x00\xe1\x87\x00\x01\xc1\x83\x80\x02A\x82@\x02A\x82@\x02\xc1\xc2@\x02\xf6>\xc0\x01\xfc=\x80\x01\x18\x18\x80\x01\x88\x10\x80\x00\x8c!\x00\x00\x87\xf1\x00\x00\x7f\xf6\x00\x008\x1c\x00\x00\x0c \x00\x00\x03\xc0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00") # Load the raspberry pi logo into the framebuffer (the image is 32x32) # fb = FrameBuffer(buffer, 32, 32, MONO_HLSB)
from machine import Pin, I2C from bmp180 import BMP180 # SDA1=Pin(21) # SCL1=Pin(20) # bus=I2C(1, sda=SDA1, scl=SCL1) bus = I2C(0) bmp = BMP180(bus) print("Temp C:", bmp.temperature) print("Pressure: ", bmp.pressure) print("Altitude: ", bmp.altitude)
def web_server(): try: pins = [machine.Pin(i, machine.Pin.OUT) for i in ( 0, 2, 4, )] #pins.extend([machine.Pin(i, machine.Pin.IN) for i in (5, 12, 13, 14, 15)]) [p.low() for p in pins] html = """<!DOCTYPE html><html> <head> <title>PyConZA IOT Demo</title> </head> <body> <h1>PyConZA IOT Demo</h1> <h2>BMP Sensor Output</h2> <h2>Temp: %s</h2><h2>Pressure: %s</h2><h2>Altitude: %s</h2> <h1>ESP8266 Pins</h1> <table border="1"> <tr><th>Pin</th><th>Value</th><th>Change Value</th></tr> %s </table> </body> </html> """ addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1] s = socket.socket() s.bind(addr) s.listen(5) print('listening on', addr) while True: i2c = I2C(scl=Pin(14), sda=Pin(12)) bmp180 = BMP180(i2c) bmp180.oversample_sett = 2 bmp180.baseline = 101325 temp = bmp180.temperature pressure = bmp180.pressure altitude = bmp180.altitude print("Temp: %s Pressure: %s Altitude: %s" % (temp, pressure, altitude)) conn, addr = s.accept() print('client connected from', addr) data = conn.recv(100) if "Pin" in data: r = str(data, 'utf-8').split("/Pin(")[1].split(")")[0] print("Match ", r) new_pin = machine.Pin(int(r)) if new_pin.value() == 1: new_pin.value(0) #new_pin.toggle() print(new_pin.value()) else: new_pin.value(1) conn_file = conn.makefile('rwb', 0) while True: line = conn_file.readline() if not line or line == b'\r\n': break print("") rows = [ '<tr><td>%s</td><td>%d</td><td><a href="http:/%s"><button>Change</button></td></a></tr>' % (str(p), p.value(), str(p)) for p in pins ] response = html % (temp, pressure, altitude, '\n'.join(rows)) conn.send(response) conn.close() except KeyboardInterrupt: s.close() print("Keyboard interrupt")
# modules from bmp180 import BMP180 from machine import I2C, Pin import time from umqtt.robust import MQTTClient # I2C bus initialiseren i2c = I2C(scl=Pin(22), sda=Pin(21), freq=20000) # bmp180 sensor initialiseren sensor3 = BMP180(i2c) sensor3.oversample_sett = 2 sensor3.baseline = 101325 # Pa # lees bmp180 sensor temp = sensor3.temperature press = sensor3.pressure altitude = sensor3.altitude # toon waarden print("Temperature: {0:.1f} C".format(temp)) print("Pressure: {0:.0f} mbar".format(press/100)) print("Altitude: {0:.0f} m".format(altitude)) # MQTT client initialiseren mqtt_server = '192.168.1.22' # mqtt_server = '172.24.0.118' mqtt_client = MQTTClient('52dc166c-2de7-43c1-88ff-f80211c7a8f6', mqtt_server) mqtt_client.connect() while True:
def _config(self): #Relay self.relay_setup() #MQTT from mqttse import MQTTClient self.mqtt = MQTTClient(client_id) # "local1": "192.168.100.240", # "local2": "192.168.254.1", # "eclipse": "iot.eclipse.org" self.mqtt.server = "192.168.100.240" self.mqtt.set_callback(self.mqtt_sub_cb) self.mqtt.set_topic("status", "status") self.mqtt.set_topic("services", "services") self.mqtt.set_topic("sw1_set", "sw1/set") self.mqtt.set_topic("sw1_state", "sw1/state") self.mqtt.set_topic("sw2_set", "sw2/set") self.mqtt.set_topic("sw2_state", "sw2/state") #i2c # esp32i2cPins = {'sda': 23, 'scl': 22} # sclPin = esp32i2cPins['scl'] # sdaPin = esp32i2cPins['sda'] i2c = I2C(scl=Pin(22), sda=Pin(23)) #si7021 from si7021 import SI7021 self.s_si = SI7021(i2c) self.s_si.read_delay = 30 self.s_si.sensor_detect() self.s_si.start() self.mqtt.set_topic("si_t", "si/temperature") self.mqtt.set_topic("si_h", "si/humidity") self.s_si.set_callback(self.s_si_cb) #bmp180 from bmp180 import BMP180 self.s_bmp = BMP180(i2c) self.s_bmp.read_delay = 30 self.s_bmp.sensor_detect() self.s_bmp.start() self.mqtt.set_topic("bmp_t", "bmp/temperature") self.mqtt.set_topic("bmp_h", "bmp/pressure") self.mqtt.set_topic("bmp_a", "bmp/altitude") self.s_bmp.set_callback(self.s_bmp_cb) #http from httpse import HTTPSE from httpse.route_system import SystemHandler from httpse.route_led import LedHandler _routeHandlers = [] _routeHandlers.append(SystemHandler().route_handler) _routeHandlers.append(LedHandler(relay=self.sw_1).route_handler) self.http = HTTPSE(route_handlers=_routeHandlers)
# main.py -- put your code here! ######################### # Imports # ######################### from bmp180 import BMP180 from pyb import UART, LED, Pin from time import sleep from micropyGPS import MicropyGPS ######################### # Prerequisites # ######################### #create BMP180 object bmp180 = BMP180('X') bmp180.oversample_sett = 3 #0=low accuracy, 3=high accuracy bmp180.baseline = 101325 #pressure at main sea level #create GPS object my_gps = MicropyGPS() #set up transceiver to send data to ground station x3_pin = Pin('X3', Pin.OUT_PP) x3_pin.high() #create transceiver object on UART4 hc12 = UART(4, 9600) #create gps object on UART3 uart = UART(3, 9600)
import time, binascii from bh1750 import BH1750 from bmp180 import BMP180 reset = Pin(23, Pin.OUT) led_onboard = Pin(2, Pin.OUT) #define pins 4 and 5 for UART because UART0 are used for USB and # UART1 are "used by Flash chip and cannot be used for other purposes."* #* https://forum.micropython.org/viewtopic.php?p=27259&sid=5ffaee5b5c0819cefc11f73e91b804aa#p27259 scl1 = Pin(5, pull=Pin.PULL_UP) sda1 = Pin(4, pull=Pin.PULL_UP) i2c = I2C(0) i2c = I2C(1, scl=scl1, sda=sda1) s = BH1750(i2c) bmp180 = BMP180(i2c) bmp180.oversample_sett = 2 def pisca(tiempo): led_onboard.on() time.sleep(tiempo) led_onboard.off() time.sleep(tiempo) led_onboard.on() time.sleep(tiempo) led_onboard.off() time.sleep(tiempo) x = 1
from bmp180 import BMP180 import time from machine import I2C, Pin # create an I2C bus object accordingly to the port you are using77 #bus = I2C(1, baudrate=100000) i2cbmp = I2C(scl=Pin(22), sda=Pin(1), freq=400000) # esp8266 bmp180 = BMP180(i2cbmp) bmp180.oversample_sett = 2 bmp180.baseline = 101325 def bmpSensor(): while True: temp = bmp180.temperature p = bmp180.pressure altitude = bmp180.altitude print(temp, p, altitude) time.sleep_ms(3500) bmpSensor()
if 'pressure' in data: pres = data['pressure'] if 'temp' in data: temp = data['temp'] if 'humidity' in data: humi = data['humidity'] if 'temp' in data and 'humidity' in data: wet = web_met.GetWetTemp(temp, humi) elif cr.json['met'].upper() == 'BMP180': from bmp180measureunit import BMP180MeasureUnit from i2ciface import I2CIface from bmp180 import BMP180 bmp_mu = BMP180MeasureUnit() i2c = I2CIface(None, 0x77) try: bmp = BMP180('BMP180', bmp_mu, i2c) except IOError: logging.fatal("BMP180 sensor not found") sys.exit(1) pres = float(bmp.GetPressure()['pressure']) temp = float(bmp.GetTemp()['temp']) wet = None # wet temperature unknown elif cr.json['met'].upper() == 'SENSEHAT': from sense_hat import SenseHat from webmet import WebMet sense = SenseHat() pres = sense.get_pressure() temp = sense.get_temperature() humi = sense.get_humidity() wet = WebMet.GetWetTemp(temp, humi) if pres is not None and temp is not None:
#-*- coding: UTF-8 -*- import commands import os import thread import time import random import sys import Adafruit_DHT from time import sleep, strftime from datetime import datetime from lcd2usb import LCD from state import * from bmp180 import BMP180 bmp = BMP180() import bh1750 exit_flag = 0 # ECHO_NUM = 100 def lcd_echo(lcd): errors = 0 for _ in range(ECHO_NUM): val = random.randint(0, 0xffff) ret = lcd.echo(val) if val != ret: errors += 1 if errors:
# IoT Esp8266 Using MicroPython http://micropython.org/ # Jason Peixoto # http://www.iotecosystem.com # IOT EcoSystem (c) is copyrighted by Jason Peixoto # GNU GENERAL PUBLIC LICENSE 3 # # Unless required by applicable law or agreed to in writing, this software is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # from bmp180 import BMP180 from machine import I2C, Pin bus = I2C(scl=Pin(4), sda=Pin(5), freq=100000) bmp180 = BMP180(bus) bmp180.oversample_sett = 2 bmp180.baseline = 101325 temp = bmp180.temperature p = bmp180.pressure altitude = bmp180.altitude print(temp, p, altitude)
from bmp180 import BMP180 import time bmp = BMP180.BMP180(2) while True: print('当前温度:%s ℃' % bmp.readtemp()) print('当前气压:%s pa' % bmp.readpress()) print('当前海拔:%s m' % bmp.readaltitude()) time.sleep_ms(500)