class Plugin(plugin.PluginProto): PLUGIN_ID = 97 PLUGIN_NAME = "Input - ESP32 Touch" PLUGIN_VALUENAME1 = "Touch" def __init__(self, taskindex): # general init plugin.PluginProto.__init__(self, taskindex) self.dtype = pglobals.DEVICE_TYPE_SINGLE self.vtype = pglobals.SENSOR_TYPE_SWITCH self.pinfilter[0] = 4 self.valuecount = 1 self.senddataoption = True self.timeroption = True self.timeroptional = True self.inverselogicoption = True self.recdataoption = False self._pin = None self.refvalue = 1500 def plugin_init(self, enableplugin=None): plugin.PluginProto.plugin_init(self, enableplugin) self.decimals[0] = 0 self.initialized = False self.timer100ms = False if int(self.taskdevicepin[0]) >= 0 and self.enabled: try: self._pin = TouchPad(Pin(int(self.taskdevicepin[0]))) self.refvalue = int( self._pin.read() / 2 ) # get value at startup, hope that it is in non-touched state, for reference self._pin.config(200) self.timer100ms = True self.initialized = True misc.addLog(pglobals.LOG_LEVEL_DEBUG, "Touch init, ref: " + str(self.refvalue)) except Exception as e: misc.addLog(pglobals.LOG_LEVEL_ERROR, "Touch config failed " + str(e)) def plugin_read(self): result = False if self.initialized: self.set_value(1, int(float(self.uservar[0])), True) # return with saved value self._lastdataservetime = utime.ticks_ms() # compat result = True return result def timer_ten_per_second(self): if self.initialized and self.enabled: prevval = int(float(self.uservar[0])) if ( self._pin.read() < self.refvalue ): # touch value read by 10 times per sec, report if change occures aval = 1 else: aval = 0 if prevval != aval: self.set_value(1, int(aval), True) self._lastdataservetime = utime.ticks_ms() # compat
def test(): import machine, display, time, math, network, utime print("Testing Display") tft = display.TFT() tft.init(tft.ST7789,bgr=True,rot=tft.LANDSCAPE, miso=17,backl_pin=4,backl_on=1, mosi=19, clk=18, cs=5, dc=16) tft.setwin(40,52,320,240) tft.set_bg(tft.WHITE) tft.clear() text="Welcome to ODF-Nanoleaf" tft.text(120-int(tft.textWidth(text)/2),10,text,tft.BLACK) from machine import TouchPad, Pin tIn2 = TouchPad(Pin(2)) tIn2.config(1000) while True: tIn2Touch = tIn2.read() if (tIn2Touch < 1007): tIn2TouchYN = True else: tIn2TouchYN = False print(tIn2TouchYN) if tIn2TouchYN: text="Touch Detected - Start LEDs" tft.text(120-int(tft.textWidth(text)/2),67-int(tft.fontSize()[1]/2+tft.fontSize()[1]+10),text,tft.BLACK) break time.sleep(1)
def read_touch(pins=(13, 12, 14, 27, 33, 32, 15, 4), sensitivity=600): r = {} for p in pins: t = TouchPad(Pin(p)) t.config(sensitivity) v = t.read() r[p] = v return r
def read(self): r = {} for p in self.pins: t = TouchPad(Pin(p)) t.config(self.sensitivity) v = t.read() r[p] = v return r
class Touch: def __init__(self, pin): self.__touch_pad = TouchPad(pin) self.__touch_pad.irq(self.__irq_handler) self.event_pressed = None self.event_released = None self.__pressed_count = 0 self.__was_pressed = False self.__value = 0 def __irq_handler(self, value): # when pressed if value == 1: if self.event_pressed is not None: self.event_pressed(value) self.__was_pressed = True self.__value = 1 if (self.__pressed_count < 100): self.__pressed_count = self.__pressed_count + 1 # when released else: self.__value = 0 if self.event_released is not None: self.event_released(value) def config(self, threshold): self.__touch_pad.config(threshold) def is_pressed(self): if self.__value: return True else: return False def was_pressed(self): r = self.__was_pressed self.__was_pressed = False return r def get_presses(self): r = self.__pressed_count self.__pressed_count = 0 return r def read(self): return self.__touch_pad.read()
if counter < max_count: counter += 1 def s2_down(p): global counter val_black = t_black.read() if (val_black < 250): led.off() print("Black touch") if counter > min_count: counter -= 1 t_red = TouchPad(Pin(14)) t_red.config( 250) # configure the threshold at which the pin is considered touched #t_red.irq(trigger=Pin.IRQ_FALLING, handler=s1_down) t_black = TouchPad(Pin(12)) t_black.config( 250) # configure the threshold at which the pin is considered touched #t_black.irq(trigger=Pin.IRQ_FALLING, handler=s2_down) led = Pin(22, Pin.OUT) timer = 0.0 print("Ready, counter ", counter) last_count = counter while True: time.sleep_ms(100)
print("Start IRQ") # onBoard LED 22 # led = Pin(22, Pin.OUT) # offBoard LED red 17 # offBoard LED Gren 16 red_pwm = PWM(Pin(17),5000) #D1 gren_pwm = PWM(Pin(16),5000) #D2 # Touch Pluss # t_plus = 13 t_plus = TouchPad(Pin(13)) t_plus.config(250) # configure the threshold at which the pin is considered touched #t_red.irq(trigger=Pin.IRQ_FALLING, handler=s1_down) # Touch Minnus # t_minus = 15 t_minus = TouchPad(Pin(15)) t_minus.config(250) counter = 0 max_count = 100 min_count = 0 def s1_down(p): global counter s1_val = s1.value()
sleep(1) print("Oh Yes! Get connected") print("Connected to LAWRENCE") print("MAC Address: {0}".format( ubinascii.hexlify(sta_if.config('mac'), ':').decode())) print("IP Address: {0}".format(sta_if.ifconfig()[0])) #2.2.2 settime() tim = Timer(1) tim.init(period=15000, mode=1, callback=lambda t: p_time(utime.localtime())) #2.2.3 t1 = TouchPad(Pin(14)) t2 = TouchPad(Pin(32)) t1.config(100) t2.config(100) g_led = Pin(33, Pin.OUT) g_tog = 0 g_led.value(0) #Toggle the green pin when t2 is touched def toggle_g(pin): global g_tog if t2.read() < 100: g_tog = 1 else: g_tog = 0
# This is script that run when device boot up or wake from sleep. import uasyncio as asyncio import machine, neopixel from machine import TouchPad, Pin import gc from global_vars import manager import urandom from utime import time np = neopixel.NeoPixel(machine.Pin(2), manager.led_amount, bpp=manager.led_bits) buzzer = Pin(27, Pin.OUT) button = TouchPad(Pin(4)) button.config(500) led_bits = manager.led_bits MAX_BRIGHTNESS = 255 async def wait_pin_change(pin): # wait for pin to change value # it needs to be stable for a continuous 500ms active = 0 while active < 100: if button.read() < 400: active += 1 else: active = 0 await asyncio.sleep_ms(1) async def wait_pin_to_untap(pin):
#get time tuple tt = utime.localtime(ntptime.time()-4*60*60) tm = (tt[0], tt[1], tt[2], tt[6], tt[3], tt[4], tt[5], 0) # initialize the real time clock rtc = machine.RTC() rtc.init(tm) # call a function that prints the date and time every 15 seconds rtc_timer = Timer(0) rtc_timer.init(mode=Timer.PERIODIC, period=15000, callback=print_datetime) # initialize capacitive touch wires touch27 = TouchPad(Pin(27)) touch27.config(325) touch33 = TouchPad(Pin(33)) # initialize leds green_led = Pin(16, Pin.OUT, value=1) red_led = Pin(17, Pin.OUT, value=1) # measure the value of one of the wires every 10 ms touch_timer = Timer(1) touch_timer.init(mode=Timer.PERIODIC, period=10, callback=measure_brown_touch) # initialize a timer to put our board to sleep every 30s sleep_timer = Timer(2) sleep_timer.init(mode=Timer.PERIODIC, period=30000, callback=go_to_sleep) # initialize_buttons
import time from machine import Pin from machine import PWM from machine import TouchPad musica_1 = None Touch = None a = None musica_1 = PWM(Pin(25)) Touch = TouchPad(Pin(33)) Touch.config(1000) while True: a = Touch.read() print('Touch: {0}'.format(a)) time.sleep(1)
print('MAC Address:', mac_address) print('IP Address:', wifi.ifconfig()[0]) connect_wifi() if machine.wake_reason() == 4: print('Timer wake-up') green_led = Pin(14,Pin.OUT) green_led.on() pushbutton1 = Pin(36,Pin.IN,Pin.PULL_DOWN) pushbutton2 = Pin(4,Pin.IN,Pin.PULL_DOWN) red_led = Pin(21,Pin.OUT) red_led.on() touch = TouchPad(Pin(15)) touch2 = TouchPad(Pin(32)) touch.config(400) touch2.config(500) esp32.wake_on_touch(True) tm = ntptime.time() tim = utime.localtime(tm-4*3600) rtc = RTC() rtc.datetime(tim[0:3]+ tim[6:7] + tim[3:6] + (0,)) def pDate(datetime): print('Date: '+ "{:02d}".format(datetime[1]) + '/' + "{:02d}".format(datetime[2]) + '/' + "{:02d}".format(datetime[0])) print('Time: ' + "{:02d}".format(datetime[4]) + ':' + "{:02d}".format(datetime[5]) + ':' + "{:02d}".format(datetime[6]) + ' ' + 'HRS') timer = Timer(1) timer.init(period=15000,mode=Timer.PERIODIC,callback=lambda t:pDate(rtc.datetime()))
#Variables #LEDs green = Pin(21, Pin.OUT) red = Pin(32, Pin.OUT) #Buttons button_red = Pin(25, Pin.IN, Pin.PULL_DOWN) button_green = Pin(26, Pin.IN, Pin.PULL_DOWN) #Touchpad tpin = TouchPad(Pin(27)) tpin_green = TouchPad(Pin(33)) # configure the threshold at which the pin is considered touched tpin.config(500) esp32.wake_on_touch(True) #External button wake-up esp32.wake_on_ext1(pins = (button_red, button_green), level = esp32.WAKEUP_ANY_HIGH) #Red led turns on red.value(1) #Hardware timer - 2 tim2 = Timer(2) tim2.init(period=10, mode=Timer.PERIODIC, callback=handletouch) # #Hardware timer - 3 tim3 = Timer(3) tim3.init(period=30000, mode=Timer.PERIODIC, callback=handlesleep)
value = 0 print("Leak capacitance: " + str(value)) self.blynk_virtual_write(LEAK_VAL_VPIN, value) initial_buzzer_duty = 0 if machine.reset_cause() == machine.PWRON_RESET: initial_buzzer_duty = 512 buzzer = PWM(Pin(BUZZER_PIN), freq=4000, duty=initial_buzzer_duty) time.sleep(0.05) buzzer.duty(0) leak_detect_pad = TouchPad(Pin(LEAK_TOUCHPAD_PIN)) leak_detect_pad.config(LEAK_CAP_THRESHOLD) print("Slowing down touch sensor measurement rate") esp32.set_touch_sensor_measurement_time( 0xffff, # 2.28885 Hz 0x1fff) # 1.02388 ms rtc = RTC() print("RTC memory: {}".format(rtc.memory())) error_reported = True if rtc.memory() else False blynk = blynklib.Blynk(secret.BLYNK_AUTH, log=print) dishwasher = Device(blynk, buzzer, connect, leak_detect_pad) if not dishwasher.is_leak(): print("There is no leak") dishwasher.leak_led = False
[802], #cloudy_ids [803, 804], #very_cloudy_ids [300, 310, 500, 520], #light_rain_ids [301, 311, 501], #rain_ids [302, 312, 313, 314, 321], #heavy_rain_ids [200, 201, 202, 210, 211, 212, 221, 230, 231, 232], #thunder_ids [600, 601, 602, 611, 612, 615, 616, 620, 621, 622] #snow_ids ] # Touch Pad configuration touchMiddle = TouchPad(Pin(13)) touchLeft = TouchPad(Pin(0)) touchRight = TouchPad(Pin(2)) touchMiddle.config(500) touchLeft.config(500) touchRight.config(500) touchPads = [touchLeft, touchMiddle, touchRight] touchStates = [0, 0, 0] tim = Timer(-1) def leftHandler(): print("Left touched") def rightHandler(): print("Right touched")
# A0 (or GPIO #26) is the gre led # A1 (or GPIO #25) is the red led # A2 and A3 (GPIO #34 and #39 respectively) are buttons but1 = Pin(34, mode=Pin.IN, pull=Pin.PULL_DOWN) but2 = Pin(39, mode=Pin.IN, pull=Pin.PULL_DOWN) esp32.wake_on_ext1((but1, but2), esp32.WAKEUP_ANY_HIGH) red_led = Pin(25, Pin.OUT) red_led.value(1) touch1 = TouchPad(Pin(14)) # yellow wire touch2 = TouchPad(Pin(32)) # blue wire # touchx.read() (x is 1 or 2) returns a number smaller than 200 when touched. touch1.config(450) esp32.wake_on_touch(True) station = network.WLAN(network.STA_IF) station.active(True) station.connect("Y2K killed millions", "foogledoogle") # this is my phone's SSID and password while (not station.isconnected()): pass print("Oh Yes! Get Connected") print("Connected to " + str(station.config('essid'))) # prints SSID Y2K killed millions print("MAC Address: " + str( ubinascii.hexlify(station.config('mac'), ':').decode())) # prints MAC address C4:85:08:49:57:37
import ntptime import utime # Initialize external LEDs as GPIO outputs led_red = Pin(12, Pin.OUT) led_green = Pin(21, Pin.OUT) led_red.value(1) # Set RED LED to ON # Initialize external push button switches as GPIO inputs switch1 = Pin(4, Pin.IN) switch2 = Pin(27, Pin.IN) # Initialize capacitive touchpads touch1 = TouchPad(Pin(14)) touch2 = TouchPad(Pin(15)) touch1.config(200) # Configure threshold for touchpad 1 # Initialize four hardware timers timer_date = Timer(0) timer_touch = Timer(1) timer_sleep = Timer(2) timer_wait = Timer(3) # Connect to WiFi network essid = "ThisLANisyourLAN" password = "******" wlan = network.WLAN(network.STA_IF) wlan.active(True) wlan.connect(essid, password) while not wlan.isconnected():
elif t == 1: fre = round(((2093 - 262) * p / 100) + 262) elif t == 2: fre = round(((2093 - 262) * p / 100) + 262) elif t == 3: fre = round(((247 - 65) * p / 100) + 65) elif t == 4: fre = round(((2093 - 65) * p / 100) + 65) return (fre) neo_pixel = NeoPixel(Pin(12, Pin.OUT), 24) musica_1 = PWM(Pin(25)) Naranja1 = TouchPad(Pin(4)) Naranja2 = TouchPad(Pin(2)) Naranja1.config(1000) Naranja2.config(1000) i2c = I2C(scl=Pin(22), sda=Pin(21)) MPU = mpu6050.accel(i2c) U_Sonico = HCSR04(trigger_pin=32, echo_pin=35) while True: if 300 > (Naranja1.read()): for i in range(3): neo_pixel[i - 1] = hex_to_rgb('#ff0000') neo_pixel.write() musica_1.freq(784) musica_1.duty(512) time.sleep_us(1968750) musica_1.freq(0) time.sleep_us(31250) if 300 > (Naranja2.read()):
from time import sleep import network from mpu6050 import MPU6050 from hcsr04 import HCSR04 import wave print('{}: telnet is {} on {}'.format(*network.telnet.status())) sensor = HCSR04(14, 12) dac = DAC(26) dac.write(0) #dac=None t = TouchPad(13) t.config(500) feedback = ADC(32) feedback.atten(ADC.ATTN_11DB) feedback.read() / 4095 vbat = ADC(35) vbat.atten(ADC.ATTN_11DB) print("battery is at {} Volts".format(vbat.read() / 4095 * 3.9 * 2)) timer = Timer(0) try: i2c = I2C(-1, Pin(22), Pin(21), freq=100000) except TypeError:
hardTimer.init(period=15000, mode=Timer.PERIODIC, callback=hardDispTime) # turn on/off green led def greenTouchSensor(timer): if greenTouch.read() < 300: led_green.on() else: led_green.off() #button wakeup esp32.wake_on_ext1(pins=(button1, button2), level=esp32.WAKEUP_ANY_HIGH) #touchpad wakeup redTouch.config(300) esp32.wake_on_touch(True) #deepsleep def deepsleep(timer): print("I am awake. Going to sleep for 1 minute") machine.deepsleep(60000) hardTimer3 = Timer(2) hardTimer3.init(period=30000, mode=Timer.PERIODIC, callback=deepsleep) hardTimer2 = Timer(1) hardTimer2.init(period=10, mode=Timer.PERIODIC, callback=greenTouchSensor)
print('Connected to ' + name) print('MAC Address: ' + ubinascii.hexlify(wlan.config('mac'), ":").decode()) print('network config:' + wlan.ifconfig()[0]) button_0 = Pin(34, Pin.IN) button_1 = Pin(39, Pin.IN) led_grn = Pin(17, Pin.OUT) led_red = Pin(16, Pin.OUT) led_red.value(1) t_0 = TouchPad(Pin(12)) t_1 = TouchPad(Pin(14)) #t_1.config(400) t_0.config(400) do_connect('Elite Longbowman', 'ageofempires') ntptime.settime() rtc = RTC() #ntptime.settime() esp32.wake_on_touch(t_0) esp32.wake_on_ext1(pins = (button_0, button_1), level = esp32.WAKEUP_ANY_HIGH) awake = {4: "Timer", 3: "Ext1", 5: "Touchpad"} #add for touchpad tim0 = Timer(0) tim0.init(period=15000, mode=Timer.PERIODIC, callback = lambda t: display())#15 tim1 = Timer(1) tim1.init(period=10, mode=Timer.PERIODIC, callback = lambda t: touched()) tim2 = Timer(2) tim2.init(period=30000, mode=Timer.PERIODIC, callback = lambda t: sleepy())#30