# std imports import machine import ssd1306 # custom imports import coreboot.constants as constants Ssd1306 = None isSsd1306Mounted = False i2c = machine.I2C(-1, machine.Pin(constants.PIN_SCL), machine.Pin(constants.PIN_SDA)) def testSsd1306Mounted(): global isSsd1306Mounted isSsd1306Mounted = 60 in i2c.scan() return isSsd1306Mounted def init(): global Ssd1306 testSsd1306Mounted() if (isSsd1306Mounted): Ssd1306 = ssd1306.SSD1306_I2C(constants.OLED_WIDTH, constants.OLED_HEIGHT, i2c) Ssd1306.poweroff()
import machine import ssd1306 import utime value = 0 sda=machine.Pin(0) scl=machine.Pin(1) i2c=machine.I2C(0,sda=sda, scl=scl, freq=400000) print(i2c.scan()) display = ssd1306.SSD1306_I2C(128, 64, i2c) def disp(): global display display.fill(0) display.text("Wert ist: %d" % value, 20, 20, 1) display.fill_rect(0, 40, value, 5, 1) display.show() def decrement(pin): global value value -= 1 if (value < 0): value = 0 disp() def increment(pin): global value value += 1 if value > 123: value = 123
import machine import si1145 import time i2c = machine.I2C(sda=machine.Pin(4), scl=machine.Pin(5)) sensor = si1145.SI1145(i2c=i2c) for i in range(10): uv = sensor.read_uv ir = sensor.read_ir view = sensor.read_visible print(" UV: %f\n IR: %f\n Visible: %f" % (uv, ir, view)) time.sleep(1)
# EECSE4764 Lab4 Check3 # Group 6: NetSpeed Fast # Group members: Ruochen You (ry2349), Penghe Zhang (pz2244), Linnan Li(ll3235) # Date: 10/9/2018 import machine import ssd1306 import urequests import network import time from machine import Pin i2c = machine.I2C(-1, Pin(5), Pin(4)) oled = ssd1306.SSD1306_I2C(128, 32, i2c) msg_list = ["Hello", "world", "Lab4"] msg_idx = 0 msg_sent = False def do_connect(): sta_if = network.WLAN(network.STA_IF) if not sta_if.isconnected(): print('connecting to network...') sta_if.active(True) sta_if.connect('Columbia University', '') while not sta_if.isconnected(): pass return sta_if.ifconfig() def change(p):
# HSR04 GPIO pins trigger = 12 #gpio[ "D6" ] echo = 14 #gpio[ "D5" ] # WeMOS-LoLin32-OLED i2c pins _SCL = const(4) _SDA = const(5) _DISPLAY_WIDTH = const(128) # Width of display in pixels. _DISPLAY_HEIGHT = const(64) # LoLin-ESP32-OLED height of display. #create sensor hc on pins trigger and echo hc = ultrasonic.Ultrasonic(trigger, echo) # create i2c and OLED-i2c objects i2c = machine.I2C(scl=machine.Pin(_SCL), sda=machine.Pin(_SDA)) #i2c = I2C(scl=Pin(SCL), sda=Pin(SDA), freq=100000) i2c.scan() #[60] oled = ssd1306.SSD1306_I2C(_DISPLAY_WIDTH, _DISPLAY_HEIGHT, i2c) # OLED helper: blank oled screen def eraseOled(): oled.fill(0) oled.show() # OLED helper: specify display_pixel function including oled.show() def display_pixel(x, y, color): oled.pixel(x, y, color) oled.show()
from esp8266_i2c_lcd import I2cLcd import lcd_api import machine, onewire, ds18x20, time print("Let's start") print("setup LCD") i2c_lcd = machine.I2C(1, sda=machine.Pin(14), scl=machine.Pin(15), freq=400000) lcd = I2cLcd(i2c_lcd, 0x27, 2, 16) lcd.clear() lcd.putstr("Start LCD!") time.sleep(1.0) print("LCD should be ok") print("preparing thermometer") lcd.clear() lcd.putstr("Ustawiam\ntermomentr") ds_pin = machine.Pin(16) ds_sensor = ds18x20.DS18X20(onewire.OneWire(ds_pin)) roms = ds_sensor.scan() print('Found a ds18x20 device') lcd.clear() lcd.putstr("Termometr\ngotowy") time.sleep(1.0) lcd.clear() lcd.putstr("Zaczynam\nliczyć temp.") time.sleep(1.0)
try: _adc = machine.ADC(0) except Exception: error.add_error("ADC is not defined") try: _dht = dht.DHT22(machine.Pin(12)) except Exception: error.add_error("Dht22 is not running.") try: _scl = machine.Pin(14) _sda = machine.Pin(2) _i2c = machine.I2C(scl=_scl, sda=_sda) except Exception: error.add_error("Lightsensor is not running.") _config_file = "sensor_config.json" history_sensor_data_file = "sensor_data_log.json" power_off() def _trim_sensor_data(total_lines, keep_lines): os.rename(history_sensor_data_file, "temp.json") file_ptr = open("temp.json", "r") for _ in range(0, total_lines - keep_lines): file_ptr.readline() gc.collect() new_file_ptr = open(history_sensor_data_file, "a")
import machine import ssd1306 import framebuf # get data pins from LED screen # ESP32 i2c = machine.I2C(scl=machine.Pin(22), sda=machine.Pin(21)) # ESP8266 # i2c = machine.I2C(scl=machine.Pin(4), sda=machine.Pin(5)) # scan LED to see if it is connected i2c.scan() # declare display variable, from the SSD1306 screen display = ssd1306.SSD1306_I2C(128, 64, i2c) # clear screen: set all pixels to 0 (black) display.fill(0) # print text on screen: # X: value of 0 means the left border, and a value of 127 means the right border # Y: vertical position and it goes from 0 for the top of the screen to 63 for the bottom display.text('Hello', 0, 0) display.text('from', 0, 16) display.text('MicroPython!', 0, 32) # show the text written above display.show() # draw a rectangle: left, top, width, height, color display.fill(0) display.rect(0, 0, 128, 64, 1) display.show()
def test_main(): """Test function for verifying basic functionality.""" sda = machine.Pin(18) scl = machine.Pin(19) i2c = machine.I2C(scl=scl, sda=sda, freq=100000) lcd = I2cLcd(i2c, DEFAULT_I2C_ADDR, 4, 20) lcd.blink_cursor_on() lcd.putstr("It Works!\nSecond Line") time.sleep(3) lcd.clear() # custom characters: battery icons - 5 wide, 8 tall lcd.custom_char(0, bytearray([0x0E, 0x1B, 0x11, 0x11, 0x11, 0x11, 0x11, 0x1F])) # 0% Empty lcd.custom_char(1, bytearray([0x0E, 0x1B, 0x11, 0x11, 0x11, 0x11, 0x1F, 0x1F])) # 16% lcd.custom_char(2, bytearray([0x0E, 0x1B, 0x11, 0x11, 0x11, 0x1F, 0x1F, 0x1F])) # 33% lcd.custom_char(3, bytearray([0x0E, 0x1B, 0x11, 0x11, 0x1F, 0x1F, 0x1F, 0x1F])) # 50% lcd.custom_char(4, bytearray([0x0E, 0x1B, 0x11, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F])) # 66% lcd.custom_char(5, bytearray([0x0E, 0x1B, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F])) # 83% lcd.custom_char(6, bytearray([0x0E, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F])) # 100% Full lcd.custom_char(7, bytearray([0x0E, 0x1F, 0x1B, 0x1B, 0x1B, 0x1F, 0x1B, 0x1F])) # ! Error for i in range(8): lcd.putchar(chr(i)) time.sleep(3) lcd.clear() lcd.blink_cursor_off() count = 0 while True: lcd.move_to(0, 0) lcd.putstr("Time: " + str(time.time())) time.sleep(1) count += 1 if count % 10 == 3: print("Turning backlight off") lcd.backlight_off() if count % 10 == 4: print("Turning backlight on") lcd.backlight_on() if count % 10 == 5: print("Turning display off") lcd.display_off() if count % 10 == 6: print("Turning display on") lcd.display_on() if count % 10 == 7: print("Turning display & backlight off") lcd.backlight_off() lcd.display_off() if count % 10 == 8: print("Turning display & backlight on") lcd.backlight_on() lcd.display_on()
def __init__(self, scl_pin, sda_pin, address): self.address = address self.bus = machine.I2C(-1, machine.Pin(scl_pin), machine.Pin(sda_pin))
def main(): # setup sensors bus = machine.I2C(scl=machine.Pin(16), sda=machine.Pin(13)) bme = BME280(i2c=bus) oled = SSD1306_I2C(128, 32, bus) # setup storage card = machine.SDCard() os.mount(card, '/card') # setup networking config = load_config('/card', 'config.yml') eth = eth_start(config, mdc=machine.Pin(23), mdio=machine.Pin(18), phy_type=network.PHY_LAN8720, phy_addr=0, clock_mode=network.ETH_CLOCK_GPIO17_OUT, power_pin=machine.Pin(12, machine.Pin.OUT)) # setup display sl = Sparkline(32, 128) oled.init_display() oled.fill(0x0) oled.text('loading', 0, 0) oled.show() # setup Prometheus metrics registry = CollectorRegistry(namespace='prometheus_express') metric_beat = Counter('system_heartbeat', 'system heartbeat counter', labels=['location'], registry=registry) metric_temp = Gauge('sensor_temperature', 'temperature data from the sensors', labels=['location', 'sensor'], registry=registry) router = Router() router.register('GET', '/metrics', registry.handler) server = False # wait for incoming connection while True: while not server: time.sleep(1) server = bind(eth, config) bme_reading = bme.read_compensated_data() temp_line = ((bme_reading[0] - 12) * 2) % 32 print('temp line: {}'.format(temp_line)) oled.fill(0x0) sl.push(temp_line) sl.draw(oled, 0, 12) oled.text(str(bme_reading[0]), 0, 0) oled.show() location = config['metric_location'] metric_beat.labels(location).inc(1) metric_temp.labels(location, 'esp32').set(temp_ftoc(esp32.raw_temperature())) metric_temp.labels(location, 'bme280').set(bme_reading[0]) try: server.accept(router) except OSError as err: print('Error accepting request: {}'.format(err)) except ValueError as err: print('Error parsing request: {}'.format(err))
# This file is executed on every boot (including wake-boot from deepsleep) #import esp #esp.osdebug(None) import uos, machine #uos.dupterm(machine.UART(0, 115200), 1) import gc import webrepl webrepl.start() gc.collect() try: from scripts.welcome import Welcome import ssd1306 import machine i2c = machine.I2C(-1, machine.Pin(0), machine.Pin(16)) oled = ssd1306.SSD1306_I2C(128, 64, i2c) Welcome(oled) except Exception as e1: print("Something goes wrong with welcome file: %s" % (str(e1))) import sys sys.print_exception(e1) pass
import machine import struct import utime I2C_2 = machine.I2C(2) LSM6DSL_ADDR = 106 LSM6DSL_REG_CTRL1_XL = 0x10 LSM6DSL_REG_CTRL2_G = 0x11 LSM6DSL_REG_OUT_TEMP_H = 0x21 LSM6DSL_REG_OUT_TEMP_L = 0x20 LSM6DSL_REG_OUTX_L_G = 0x22 LSM6DSL_REG_OUTX_H_G = 0x23 LSM6DSL_REG_OUTY_L_G = 0x24 LSM6DSL_REG_OUTY_H_G = 0x25 LSM6DSL_REG_OUTZ_L_G = 0x26 LSM6DSL_REG_OUTZ_H_G = 0x27 LSM6DSL_REG_OUTX_L_XL = 0x28 LSM6DSL_REG_OUTX_H_XL = 0x29 LSM6DSL_REG_OUTY_L_XL = 0x2A LSM6DSL_REG_OUTY_H_XL = 0x2B LSM6DSL_REG_OUTZ_L_XL = 0x2C LSM6DSL_REG_OUTZ_H_XL = 0x2D HIGH_PERF_416HZ = b'\x60' HIGH_PERF_6KHZ = b'\xA0'
def reconnect(): wlan_client.start() success = wlan_client.check() and mqtt_client.check() if success: mqtt_client.broker.subscribe(TOPIC_SUB) return success gc.collect() led = StatusLED(D6) led.on() # Replace this lines with your sensor i2c = machine.I2C(scl=machine.Pin(D1), sda=machine.Pin(D2)) # Pin 5 = D1 | Pin 4 = D2 from sensor_manager import Sensor_BME280 sensor = Sensor_BME280(i2c=i2c, address=0x77) #End of sensor settings from wlan_manager import WLAN_Manager wlan_client = WLAN_Manager() from mqtt_manager import MQTT_Manager mqtt_client = MQTT_Manager() TOPIC_SUB = mqtt_client.get_topic('control') # You talking to the sensor TOPIC_PUB = mqtt_client.get_topic('status') # The sensor talking to you chatty_client = bool(mqtt_client.CONFIG.get('chatty', True)) mqtt_client.broker.set_callback(mqtt_callback) print('client_id:', mqtt_client.CONFIG['client_id'])
MCP23008_IPOL = 0x01 MCP23008_GPINTEN = 0x02 MCP23008_DEFVAL = 0x03 MCP23008_INTCON = 0x04 MCP23008_IOCON = 0x05 MCP23008_GPPU = 0x06 MCP23008_INTF = 0x07 MCP23008_INTCAP = 0x08 MCP23008_GPIO = 0x09 MCP23008_OLAT = 0x0A INPUT = 0x0 OUTPUT = 0x1 INPUT_PULLUP = 0x2 i2c = machine.I2C(1) # Set up pins outdata = bytearray(b'\x00') # IODIRA value lowest bit output i2c.writeto_mem(MCP23008_BASEADDR, MCP23008_IOCON, outdata) # Set the IODIRA to output for lowest bit def digitalWrite(bit, value): # Writes to a single bit # Need to do a read-modify-write to not mess up other bits rwValue = readRegister(MCP23008_OLAT) if (value == 0): rwValue &= ~(1 << (bit & 0x7)) else: rwValue |= (1 << (bit & 0x7)) writeRegister(MCP23008_OLAT, rwValue) return
# Author: George Kaimakis import machine import ssd1306 import time # define the display size: width = 128 # pixels height = 64 # pixels # define i2c pins: data = machine.Pin(4) clk = machine.Pin(5) # create i2c object: i2c = machine.I2C(scl=clk, sda=data) #create oled object: oled = ssd1306.SSD1306_I2C(width, height, i2c) # blank the display: oled.fill(0) oled.show() time.sleep_ms(500) # the framebuf class contains methods for drawing rectangles # fill_rect, rect - also use these with height/width of 1 pixel to create lines: while True: oled.framebuf.rect(0, 32, 1, 20, 1)
# Necessary Libraries to be Imported import machine import ssd1306 import utime #Specifiy Desired Pins For CLK and DATA respectively i2c = machine.I2C(-1, machine.Pin(5), machine.Pin(4)) #Creation of Display Object using SSD1306 Library and binding it to I2C bus # 128 is X direction pixels and 32 is y direction pixels display = ssd1306.SSD1306_I2C(128, 64, i2c) # Fill the Display with 0's display.fill(0) # Set a pixel in the origin 0,0 position. display.text("Hello", 0, 0) #Display the above on to display display.show() utime.sleep_ms(1000)
i.writeto_mem(SENSORADDR, raddr, val) sdapin = 21 sclpin = 22 cspin = 15 standby = False hspi = machine.SPI(1, baudrate=80000000, polarity=0, phase=0, sck=machine.Pin(14), mosi=machine.Pin(13), miso=machine.Pin(12)) i2c = machine.I2C(scl=machine.Pin(22), sda=machine.Pin(21), freq=1000000) # first init spi assuming the hardware spi is connected hspi.init(baudrate=2000000) # chip select -- active low cspin = machine.Pin(15, machine.Pin.OUT) cspin.value(1) addrs = i2c.scan() print('ov2640_init: devices detected on on i2c:') for a in addrs: print('0x%x' % a) time.sleep(1) i2c.writeto_mem(SENSORADDR, 0xff, b'\x01')
elif code == 401: print('Error Code:', code) print(data['message']) return results city_name = 'Nonthaburi,TH' results = get_weather(APPID, city_name) #--------------------------------------------------------- # Step 3) Display weather data on LCD16x2 I2C # Use GPIO22=SCL, GPIO21=SDA i2c_bus = 0 # use bus 0 or 1 i2c = machine.I2C(i2c_bus, freq=400000, scl=machine.Pin(22), sda=machine.Pin(21)) # use I2C address = 0x3f lcd = LCD(i2c, 0x3f) weather_data = { 't': ('Temperature', 'deg.C'), 'h': ('Humidity', '%'), 'p': ('Pressure', 'hPa'), } try: while True: for key in weather_data: lcd.clear() # clear display lcd.goto_line(0) # goto the first line
import machine try: import esp esp.osdebug(None) i2c = machine.I2C(sda=machine.Pin(4), scl=machine.Pin(5)) except: pass try: import pyb i2c = machine.I2C(sda=machine.Pin('Y10'), scl=machine.Pin('Y9'), freq=400000) except: pass import lcd160cr lcd = lcd160cr.LCD160CR('X') dt = 20 b1 = bytearray(1) b2 = bytearray(2) def hdc1080_read(a=0): b1[0] = a i2c.writeto(64, b1) pyb.delay(dt) i2c.readfrom_into(64, b2) #return '%02x%02x' % (b[0], b[1]) return (b2[0] << 8) | b2[1]
import machine, College4.ssd1306, time screenWidth = 128 screenHeight = 32 delay = 1 i2c = machine.I2C(scl=machine.Pin(5), sda=machine.Pin(4)) oled = College4.ssd1306.SSD1306_I2C(screenWidth, screenHeight, i2c) adc = machine.ADC(0) while True: oled.fill(0) volts = adc.read() temp = (volts - 500) / 10 temp -= 5 oled.text('Temp:', 0, 5) oled.text(str(temp), int(screenWidth / 2), 5) oled.text('Volt:', 0, int(screenHeight / 2)) oled.text(str(volts), int(screenWidth / 2), int(screenHeight / 2)) oled.show() time.sleep(delay)
def __init__(self, sda, scl): self.sda_pin = sda self.scl_pin = scl self.i2c = machine.I2C(id = 1, mode=machine.I2C.SLAVE, speed=FREQUENCY_SCL, sda=self.sda_pin, scl=self.scl_pin, slave_addr=DS3231_I2C_ADDR)
Set your SCL and SDA pins in constants Installation: ampy -p /dev/ttyUSB0 put ./octopus_robot_board.py ampy -p /dev/ttyUSB0 put ./ssd1306.py ampy -p /dev/ttyUSB0 put ./05-oled.py main.py # reset device """ import machine import ssd1306 import time import octopus_robot_board as o #octopusLab main library - "o" < octopus i2c = machine.I2C(-1, machine.Pin(o.I2C_SCL_PIN), machine.Pin(o.I2C_SDA_PIN)) oled = ssd1306.SSD1306_I2C(128, 64, i2c) # test_whole display oled.fill(1) oled.show() time.sleep_ms(300) # reset display oled.fill(0) oled.show() # write text on x, y oled.text('OLED test', 20, 20) oled.show()
import lvgl as lv import lvgl_helper as lv_h import lvesp32 import display import time import machine import touchscreen as ts import axp202 import random i2c0 = machine.I2C(scl=22, sda=21) pmu = axp202.PMU(i2c0) pmu.enablePower(axp202.AXP202_LDO2) pmu.setLDO2Voltage(3300) tft = display.TFT() sda_pin = machine.Pin(23) scl_pin = machine.Pin(32) i2c = machine.I2C(id=1, scl=scl_pin, sda=sda_pin, speed=400000) ts.init(i2c) tft.init(tft.ST7789,width=240, invrot=3,rot=1,bgr=False, height=240, miso=2, mosi=19, clk=18, cs=5, dc=27,speed=40000000,color_bits=tft.COLOR_BITS16,backl_pin=12,backl_on=1) tft.clear(tft.RED) time.sleep(1) tft.clear(tft.GREEN) time.sleep(1) tft.clear(tft.BLUE) time.sleep(1)
def __init__(self): self.iic = machine.I2C(scl=machine.Pin(5), sda=machine.Pin(4)) self.iic.start() self.iic.writeto(0x68, bytearray([107, 0])) self.iic.stop()
import machine import adafruit_sht31d # Create library object using our Bus I2C port i2c = machine.I2C(0, scl=machine.Pin(22), sda=machine.Pin(21)) # esp32 sensor = adafruit_sht31d.SHT31D(i2c, address=69) print("\033[1mSensor\033[0m = SHT31-D") print("\033[1mSerial Number\033[0m = ", sensor.serial_number, "\n") for i in range(3): if i == 0: sensor.repeatability = adafruit_sht31d.REP_LOW print("\033[1m\033[36mLow Repeatability:\033[0m\n") if i == 1: sensor.repeatability = adafruit_sht31d.REP_MED print("\n\033[1m\033[36mMedium Repeatability:\033[0m\n") if i == 2: sensor.repeatability = adafruit_sht31d.REP_HIGH sensor.clock_stretching = False print("\n\033[1m\033[36mHigh Repeatability:\033[0m") # print("\033[1m\033[95mClock Stretching:\033[0m \033[92mEnabled\033[0m\n") for itr in range(3): print("\033[1mTemperature:\033[0m %0.3f ºC" % sensor.temperature) print("\033[1mHumidity:\033[0m %0.2f %%" % sensor.relative_humidity, "\n")
#code for i2c(2 sensors) import machine, utime #utime.sleep_ms(100) i2c = machine.I2C(scl=machine.Pin(33), sda=machine.Pin(32), freq=200000) a = i2c.scan() #utime.sleep_ms(100) i2c.writeto(a[0], 'r') utime.sleep_ms(1000) addr1 = i2c.readfrom(a[0], 5) utime.sleep_ms(1000) i2c.writeto(a[1], 'r') utime.sleep_ms(1000) addr2 = i2c.readfrom(a[1], 5) #utime.sleep_ms(100) print(a, addr1, addr2)
def __init__(self): self.scl_pin = machine.Pin(26) self.sda_pin = machine.Pin(27) self.i2c = machine.I2C(sda=self.sda_pin, scl=self.scl_pin) _thread.start_new_thread(self.thread, ())
import time import zcoap import ujson import machine from sys import exit i2c = machine.I2C(1) # Default I2C slave address ADXL345_I2CADDR = 0x53 ADXL345_DATA_FORMAT = 0x31 ADXL345_BW_RATE = 0x2C POWER_CTL = 0x2D ADXL345_BW_RATE1600HZ = 0x0F ADXL345_BW_RATE800HZ = 0x0E ADXL345_BW_RATE400HZ = 0x0D ADXL345_BW_RATE200HZ = 0x0C ADXL345_BW_RATE100HZ = 0x0B ADXL345_BW_RATE50HZ = 0x0A ADXL345_BW_RATE25HZ = 0x09 ADXL345_FS_2g = 0x00 ADXL345_FS_4g = 0x01 ADXL345_FS_8g = 0x02 ADXL345_FS_16g = 0x03 MEASURE = 0x08 AXES_DATA = 0x32
BIGSLEEP = 800 # 800 seconds for 15 minutes intervals. ROUND = 1 TICKER = 1 while True: f = open("data/data.csv", "a") print("862c. Round ", ROUND) time.sleep(2) insert_one_1 = aq(5, 1) insert_two_1 = aq(5, 2) time.sleep(2) temphum1 = am2302.th1.read() temphum2 = am2302.th2.read() i2c_light = machine.I2C(0, pins=("P21", "P22")) # (P21 is SDA, P22 is clock) lightsensor = BH1750(i2c_light) luminance = lightsensor.luminance(BH1750.ONCE_HIRES_1) time.sleep(2) timesec = time.time() f.write( str(ROUND) + '; ' + str("862c") + '; ' + str(insert_one_1[0]) + '; ' + str(insert_one_1[1]) + '; ' + str(insert_one_1[2]) + '; ' + str(insert_two_1[0]) + '; ' + str(insert_two_1[1]) + '; ' + str(insert_two_1[2]) + '; ' + str(temphum1.temperature) + '; ' + str(temphum2.temperature) + '; ' + str(temphum1.humidity) + '; ' + str(temphum2.humidity) + '; ' + str(luminance) + '; ' + str(timesec) + '\n') f.close() pms5003.fan_off() ROUND += 1