def lprint(text: str, delay: float): try: from nodemcu_gpio_lcd import GpioLcd lcd = GpioLcd(rs_pin=Pin(16), enable_pin=Pin(5), d4_pin=Pin(4), d5_pin=Pin(0), d6_pin=Pin(2), d7_pin=Pin(14), num_lines=2, num_columns=16) except (ImportError, OSError): from machine import I2C from nodemcu_gpio_lcd import I2cLcd # The PCF8574 has a jumper selectable address: 0x20 - 0x27 DEFAULT_I2C_ADDR = 0x27 i2c = I2C(scl=Pin(5), sda=Pin(4), freq=400000) lcd = I2cLcd(i2c, DEFAULT_I2C_ADDR, 2, 16) if len(text) <= 32: print(text) lcd.clear() sleep(0.15) lcd.putstr(text) sleep(delay) else: to_display = text[:32] print(to_display) lcd.clear() sleep(0.15) lcd.putstr(to_display) sleep(delay) lprint(text[32:], delay)
def __init__(self): _machine_id = machine.unique_id() self.count = 0 self.machine_id = '{2:0x}:{1:0x}:{0:0x}'.format(*_machine_id) self.ow = onewire.OneWire(d7) self.ds = ds18x20.DS18X20(self.ow) self.lcd = GpioLcd(d6, d5, d4, d3, d2, d1) self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.periodics = set()
def run(): lcd = GpioLcd(rs_pin=Pin(16), enable_pin=Pin(5), d4_pin=Pin(14), d5_pin=Pin(12), d6_pin=Pin(13), d7_pin=Pin(15), num_lines=2, num_columns=16) lcd.clear() new_term = None while True: # dupterm allows us to use TX/RX for the sds011 read, then restore TX/RX to the web REPL. old_term = uos.dupterm(new_term, 1) # Read the two measurements from the SDS011. readings = sds011.read(1) new_term = uos.dupterm(old_term, 1) if readings is not None: pm25, pm10 = readings lcd.move_to(0, 0) message = "PM2.5 %3d ug/m3\nPM10 %4d ug/m3" % (pm25, pm10) lcd.putstr(message) print(message) sleep(1)
def test_main(): """Test function for verifying basic functionality.""" print("Running test_main") lcd = GpioLcd(rs_pin=Pin(16), enable_pin=Pin(5), d4_pin=Pin(4), d5_pin=Pin(0), d6_pin=Pin(2), d7_pin=Pin(14), num_lines=2, num_columns=20) lcd.putstr("It Works!\nSecond Line") sleep(3) lcd.clear() count = 0 while True: lcd.move_to(0, 0) lcd.putstr("%7d" % (ticks_ms() // 1000)) sleep(1) count += 1
def initialize(): """ Initialize LCD screen, interrupts, WiFi and timers """ global count, last_time, tim, tip_cpm, tim_save, tim_uart, uart, lcd #SERIAL init_serial() #NETWORK ap_if = network.WLAN(network.AP_IF) try: f = open('SSID', 'r') ssid = f.readline() f.close() except: ssid = 'Geiger-config' if (ssid == ''): ssid = 'Geiger-empty' try: f = open('CHANNEL', 'r') channel = f.readline() f.close() except: channel = 1 if (channel == ''): channel = 2 ap_if.config(essid=ssid.strip(), password='******', channel=int(channel)) ###print(ap_if.ifconfig()) #LCD screen lcd = GpioLcd(rs_pin=Pin(4), enable_pin=Pin(0), d4_pin=Pin(12), d5_pin=Pin(13), d6_pin=Pin(14), d7_pin=Pin(15), num_lines=2, num_columns=16) lcd.putstr("Geiger FE '17\nW:%s" % ssid) sleep(3) lcd.clear() #IRQ and TIMERS p5 = Pin(5, Pin.IN) p5.irq(handler=callback, trigger=Pin.IRQ_RISING, hard=True) last_time = ticks_ms() tim.init(period=1000, mode=Timer.PERIODIC, callback=refresh_screen) tim_cpm.init(period=2000, mode=Timer.PERIODIC, callback=refresh_cpm) tim_save.init(period=30000, mode=Timer.PERIODIC, callback=save_file) tim_uart.init(period=10, mode=Timer.PERIODIC, callback=uart_handler)
def lprint(s): try: from nodemcu_gpio_lcd import GpioLcd lcd = GpioLcd(rs_pin=Pin(16), enable_pin=Pin(5), d4_pin=Pin(4), d5_pin=Pin(0), d6_pin=Pin(2), d7_pin=Pin(14), num_lines=2, num_columns=16) except (ImportError, OSError): from nodemcu_gpio_lcd import I2cLcd from machine import I2C # The PCF8574 has a jumper selectable address: 0x20 - 0x27 DEFAULT_I2C_ADDR = 0x27 i2c = I2C(scl=Pin(5), sda=Pin(4), freq=400000) lcd = I2cLcd(i2c, DEFAULT_I2C_ADDR, 2, 16) print(s) lcd.clear() sleep(0.15) lcd.putstr(s)
# 16 - K (Backlight Cathode) - Connect to Ground # # On 14-pin LCDs, there is no backlight, so pins 15 & 16 don't exist. # # The Contrast line (pin 3) typically connects to the center tap of a # 10K potentiometer, and the other 2 legs of the 10K potentiometer are # connected to pins 1 and 2 (Ground and VDD) # # The wiring diagram on the following page shows a typical "base" wiring: # http://www.instructables.com/id/How-to-drive-a-character-LCD-displays-using-DIP-sw/step2/HD44780-pinout/ # Add to that the EN, RS, and D4-D7 lines. """Test function for verifying basic functionality.""" print("Running test_main") lcd = GpioLcd(rs_pin=Pin(13), enable_pin=Pin(12), d4_pin=Pin(21), d5_pin=Pin(22), d6_pin=Pin(23), d7_pin=Pin(25), num_lines=2, num_columns=20) lcd.putstr("It Works!\nSecond Line") sleep_ms(10000) lcd.clear() count = 0 while True: lcd.move_to(0, 0) lcd.putstr("%7d" % (ticks_ms() // 1000)) sleep_ms(1000) count += 1
def test_main(): """Test function for verifying basic functionality.""" print("Running test_main") AdcPin=machine.ADC(0) '''lcd = GpioLcd(rs_pin=Pin(16), enable_pin=Pin(5), d4_pin=Pin(4), d5_pin=Pin(0), d6_pin=Pin(2), d7_pin=Pin(14), num_lines=2, num_columns=16)''' lcd = GpioLcd(rs_pin=Pin(0), enable_pin=Pin(2), d4_pin=Pin(14), d5_pin=Pin(12), d6_pin=Pin(13), d7_pin=Pin(15), num_lines=2, num_columns=16) lcd.putstr("It Works!\nSecond Line") sleep(3) lcd.clear() count = 0 lcd.putstr("Iot Impacta 2019") try: print('Tendando acessar Mosquitto') client = connect_and_subscribe() client.check_msg() print('Mosquitto acessado') except OSError as e: restart_and_reconnect() while True: lcd.move_to(0, 1) msg = str((AdcPin.read()-10)*36.5/96) lcd.putstr('Tep. = {}'.format(msg)) #lcd.putstr("%7d" % (ticks_ms() // 1000)) try: print('lendo servidor') print(client.check_msg()) print('servidor lido') print('Publicando', msg) client.publish('temp/random', msg) print('Publicado') except OSError as e: restart_and_reconnect() sleep(1)
import usocket from machine import Pin, ADC from utime import sleep, ticks_ms from nodemcu_gpio_lcd import GpioLcd CALIB_CONST_MUL = 0.08076 LIST_MAX_LENGTH = 60 IP_RECEIVER = "YOUR_SERVER_IP" PORT_RECEIVER = 8000 IOT_RECEIVER = usocket.getaddrinfo(IP_RECEIVER, PORT_RECEIVER)[0][-1] PROGRAM_NAME = "SMARTCURRENTv0.1" WARMUP_TIME = 60 current_warmup = 0 pin_ctrl = Pin(15, Pin.OUT, value=0) pin_adc = ADC(0) lcd = GpioLcd(rs_pin=Pin(16),enable_pin=Pin(5),d4_pin=Pin(4),d5_pin=Pin(0),d6_pin=Pin(2),d7_pin=Pin(14),num_lines=2, num_columns=20) lcd.putstr(PROGRAM_NAME+"\n"+" DO NOT TOUCH ") time.sleep(10) lcd.clear() while current_warmup < WARMUP_TIME: lcd.putstr("TIME REMAINING:\n "+str(WARMUP_TIME-current_warmup)) current_warmup += 1 time.sleep(1) lcd.clear() lcd.putstr(IP_RECEIVER + "\nPORT:" + str(PORT_RECEIVER)) time.sleep(15) lcd.clear() def clear_and_print(string_to_print): lcd.clear() lcd.putstr(string_to_print)
class RunTime: def __init__(self): _machine_id = machine.unique_id() self.count = 0 self.machine_id = '{2:0x}:{1:0x}:{0:0x}'.format(*_machine_id) self.ow = onewire.OneWire(d7) self.ds = ds18x20.DS18X20(self.ow) self.lcd = GpioLcd(d6, d5, d4, d3, d2, d1) self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.periodics = set() def startup(self, delay=10): self.delay = delay self.lcd.clear() self.lcd.putstr("...starting\n{}".format(self.machine_id)) self.wlan = network.WLAN(network.STA_IF) while True: if self.wlan.ifconfig()[0] != "0.0.0.0": break print("Waiting to get IP address") time.sleep(1) while True: try: ntptime.settime() break except OSError as e: print("NTP failed with {}".format(e)) time.sleep(1) self.rtc = machine.RTC() def read_temps(self): self.ds.convert_temp() time.sleep_ms(750) for rom in self.ds.scan(): value = self.ds.read_temp(rom) rom_id = '{0:x}'.format(*struct.unpack('!Q', rom)) print("{0}: {1} C".format(rom_id, value)) yield (rom_id, value) def run(self): while True: self() time.sleep_ms(self.delay * 1000 - 750) def __call__(self): self.ip_addr = self.wlan.ifconfig()[0] temps = self.get_temps() cycle_messages = [self.ip_addr, self.machine_id] temperature = temps[0][1] dt = self.rtc.datetime() h = dt[4] + HOUR_OFFSET m = dt[5] + MIN_OFFSET try: row1 = "{0:+6.1f}\xdfC {1:02d}:{2:02d}".format(temperature, h, m) except ValueError: row1 = "No 1-Wire! {0:02d}:{1:02d}".format(h, m) row2 = cycle_messages[self.count % len(cycle_messages)] self.send_temperatures(temps) self.lcd.clear() self.lcd.putstr(row1 + row2) self.count += 1 def get_temps(self): try: temps = list(self.read_temps()) except: temps = ('0' * 16, NaN) return temps def send_temperatures(self, temperatures): data = json.dumps( dict(temps=temperatures, node=self.machine_id, n=self.count)) print(data) self.send(data.encode('utf-8')) def send(self, payload): self.sock.sendto(payload, (b"172.17.2.75", 54724))
# create uart object to manage serial communication from gps module uart = UART(1, rx=16, tx=17, baudrate=9600, bits=8, parity=None, stop=1, timeout=5000, rxbuf=1024) # create lcd object to display data on the breadboard lcd = GpioLcd(rs_pin=Pin(13), enable_pin=Pin(12), d4_pin=Pin(21), d5_pin=Pin(22), d6_pin=Pin(23), d7_pin=Pin(25), num_lines=2, num_columns=20) lcd.clear() lcd.putstr("LCD Ready") # set up mqtt topic IP address and topic name for publish and subcribe # set up client id for mqtt connection mqtt_server = '151.0.230.202' topic_sub = b'weather/data' topic_pub = b'weather/data' client_id = ubinascii.hexlify(machine.unique_id()) last_message = 0 message_interval = 5