# this module is to setup your board # iotBoard for project parallel garden # import machine from machine import Pin, PWM, ADC import time, os, ubinascii from util.pinout import set_pinout pinout = set_pinout() pwM = Pin(pinout.PWM1_PIN, Pin.OUT) # moisture pin_an = Pin(pinout.I35_PIN, Pin.IN) adcM = adc = machine.ADC(pin_an) pin_an = Pin(pinout.ANALOG_PIN, Pin.IN) adc = machine.ADC(pin_an) # ---------------- procedures def getGardenLibVer(): print("garden lib.ver: 26.2.2019") def getADvolt(Debug): # AD > volts? an = adc.read() if Debug: print("> analog RAW: " + str(an)) # TODO improve mapping formula, doc: https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/peripherals/adc.html print("volts: {0:.2f} V".format(an/4096*10.74), 20, 50) return an def get_moisture():
d = dht.DHT22(machine.Pin(18)) base_url = 'https://wolfesneck.farmos.net/farm/sensor/listener/' public_key = '054d3116a74fae5dd36550013d50c848' private_key = '014d3116a74fae5dd36550013d50c848' url = base_url + public_key + '?private_key=' + private_key headers = {'Content-type': 'application/json', 'Accept': 'application/json'} d.measure() t = d.temperature() h = d.humidity() adc = machine.ADC(machine.Pin(35)) adc_val = adc.read() payload = {"temp": t, "humidity": h, "adc_val": adc_val} print(payload) time.sleep(2) def post_data(): try: r = requests.post(url, data=json.dumps(payload), headers=headers) except Exception as e: print(e)
client_id = ubinascii.hexlify(machine.unique_id()) client = connect_and_subscribe(client_id, credentials["mqtt"]["host"], credentials["mqtt"]["port"]) #################################### # # # Initialize Sensors # # # #################################### i2c = machine.I2C(0, scl=machine.Pin(22), sda=machine.Pin(21)) bme = bme280.BME280(i2c=i2c) bh = BH1750.BH1750(i2c) adc_sol = machine.ADC(machine.Pin(34)) adc_vol = machine.ADC(machine.Pin(33)) adc_vol_mean = 2048 adc_wind = machine.ADC(machine.Pin(39)) def get_wind_dir(adc): wind_dir_dict = { 3143: 0, 1624: 22.5, 1845: 45, 335: 67.5, 372: 90, 264: 112.5, 739: 135, 506: 157.5, 1149: 180,
''' LDR photocell sensor on OLED display 2017-0805 PePo initial version #''' import machine, time import ssd1306 import machine, time _ADC_PIN = const(0) _WARNING_LED_PIN = const(14) # create ADC-object adc = machine.ADC(_ADC_PIN) #TEST: print('ADC reading:', adc.read()) led = machine.Pin(_WARNING_LED_PIN, machine.Pin.OUT) # create i2c for display i2c = machine.I2C(scl=machine.Pin(5), sda=machine.Pin(4), freq=100000) print('i2c.scan: ', i2c.scan()) #[60] # OLED screen dimensions __WIDTH = const(128) __HEIGHT = const(32) oled = ssd1306.SSD1306_I2C(__WIDTH, __HEIGHT, i2c) # alert ON and OFF def alertOn(): led.on() def alertOff():
import machine from utime import sleep from ssd1306 import SSD1306_I2C POT_PIN_1 = 26 POT_PIN_2 = 27 POT_PIN_3 = 28 adc_1 = machine.ADC(POT_PIN_1) adc_2 = machine.ADC(POT_PIN_2) adc_3 = machine.ADC(POT_PIN_3) sda=machine.Pin(0) scl=machine.Pin(1) i2c=machine.I2C(0, sda=sda, scl=scl, freq=400000) oled = SSD1306_I2C(128, 64, i2c) def read_pot(adc): return int(adc.read_u16()) >> 8 while True: oled.fill(0) oled.text("CoderDojo Robot", 0, 0) oled.text("P1:", 0, 20) oled.text(str(read_pot(adc_1)), 40, 20) oled.text("P2:", 0, 35) oled.text(str(read_pot(adc_2)), 40, 35) oled.text("P3:", 0, 50) oled.text(str(read_pot(adc_3)), 40, 50) oled.show() sleep(.1)
title = M5Title(title=" Smart Device ESP32 MQTT demo V3.4", x=3 , fgcolor=0xff9900, bgcolor=0x1F1F1F) label_info = M5TextBox(15, 30, "<-->"+MQTT_Server+":1883 as "+Device_ID, lcd.FONT_Default,0xDDDDDD, rotate=0) #label_info = M5TextBox(15, 30, "<-->test.mosquitto.org:1883 as M5_SMD01", lcd.FONT_Default,0xDDDDDD, rotate=0) label1 = M5TextBox(15, 50, "Status Info", lcd.FONT_DejaVu18,0xFFFFFF, rotate=0) #status info label2 = M5TextBox(15, 80, "MQTT_send:", lcd.FONT_Ubuntu,0xAFFFAF, rotate=0) #MQTT sent msg display label3 = M5TextBox(15, 100, "Sensor: ", lcd.FONT_DejaVu18,0x9FFF9F, rotate=0) #MQTT sent msg display label_MQTT_R = M5TextBox(15, 120, "RX_Msg:", lcd.FONT_DejaVu24,0xFFF000, rotate=0) #MQTT received msg display label_MQTT_Rs = M5TextBox(15, 145, "Rx_echo:", lcd.FONT_DejaVu18,0x1F1FF2, rotate=0) #MQTT received msg display label_cnt = M5TextBox(220, 225, "CNT: ", lcd.FONT_Default,0xFFEEBB, rotate=0) # run count label_LCD = M5TextBox(220, 205, "LCD_BK: ", lcd.FONT_Default,0xAAEEAA, rotate=0) label_cnt_msg = M5TextBox(20, 205, "MQTT CNT: ", lcd.FONT_Default,0xAAEEAA, rotate=0) label_sys = M5TextBox(20, 185, "Heap: ", lcd.FONT_Default,0xF0CEAA, rotate=0) image_B_icon = M5Img(140,220, "res/bnt_B.jpg", True) #Vb values label_akku = M5TextBox(20, 225, "Akku", lcd.FONT_Default,0xFFFFAA, rotate=0) adc = machine.ADC(35) ratio =2 #------------ADC V akku G35 adc.atten(adc.ATTN_11DB) #--------MQTT---------------- m5mqtt.subscribe(str('test_ESP32_smartdevice_server'), fun_smartdevice_mqtt) wait(0.1) #m5mqtt.subscribe(str(Topic_ID), fun_echo_esp32_mqtt) #will echo received msg #m5mqtt.subscribe(str('stsmd/'+Device_ID+'/alert'), fun_echo_esp32_mqtt) #will echo received msg #wait(0.1) #m5mqtt.subscribe(str('test_ESP32_smartdevice_server'), func_echo_esp32_mqtt) #will echo received msg #wifiCfg.doConnect('TC', 'sthz@2020') m5mqtt.start() #wait(0.1) lcd.font(lcd.FONT_Comic) label1.setText('Hi, SmartDevice MQTT start')
import machine import utime pot = machine.ADC(26) conversion_factor = 3.3 / (65535) MAX_VOLTAGE = 3.3 while True: voltage = pot.read_u16() * conversion_factor print('Voltage: %.1f' % voltage) percentage = (voltage / MAX_VOLTAGE) * 100 print('Percentage: %.0f' % percentage) utime.sleep(2)
room_temp = 'Temperature' room_humidity = 'RelativeHumidity' room_lum = 'Luminosity' channels = [ Channel(active_channel, credentials.API_KEY, [room_temp, room_humidity, room_lum]) ] thing_speak = ThingSpeakAPI(channels, protocol_class=ProtoHTTPS, log=True) import dht import machine d_pin = dht.DHT11(machine.Pin(5)) #Inialize pin D1 of nodemcu lum_pin = machine.ADC(0) def get_data(): d_pin.measure() lum_voltage = lum_pin.read() * (3.3 / 1024.0) returner = [d_pin.temperature(), d_pin.humidity(), lum_voltage] return returner def send_data(): try: data = get_data() thing_speak.send(active_channel, { room_temp: data[0], room_humidity: data[1],
# ambient temperature in a simple way. # The temperature sensor measures the Vbe voltage of a biased bipolar diode. # Typically, Vbe = 0.706V at 27 degrees C, with a slope of -1.721mV (0.001721) per degree. # # (c) 2021-02-05 Claus Kuehnel ([email protected]) import machine, time import picodisplay as display from machine import Timer from machine import Pin led = Pin(25, Pin.OUT) # external LED on Pi Pico t = Timer() sensor_temp = machine.ADC(4) # internal temperature sensor conversion_factor = 3.3 / (65536) # Thermostat Range (set your limits here) # OverTemp > 30 -> LED red # Normal 28..30 -> LED green # Under < 28 -> LED blue UL = 30 # Upper Limit LL = 28 # Lower Limit def blink(Timer): led(1) time.sleep_ms(20) # LED on for 20 milliseconds led(0) def initDisplay():
import machine from machine import Pin from machine import SPI import ssd1306 from machine import I2C #from upy_rfm9x import RFM9x import dht TIMEOUT = .2 REQUESTS_TIMEOUT=10000 import time DISPLAY=False adc=machine.ADC(Pin(35)) d=dht.DHT22(machine.Pin(18)) i2c = I2C(-1, Pin(14), Pin(2)) #radio #sck=Pin(25) #mosi=Pin(33) #miso=Pin(32) #cs = Pin(26, Pin.OUT) #resetNum=27 #spi=SPI(1,baudrate=5000000,sck=sck,mosi=mosi,miso=miso) #rfm9x = RFM9x(spi, cs, resetNum, 915.0) # set up the display
dom = (9, 40, 68, 99, 129, 160, 190, 221, 252, 282, 313, 343) hours = 0 tstump = '' vbat = 2000 az_pin = machine.Pin(AZ_PIN, machine.Pin.OUT) paz = machine.PWM(az_pin, freq=50) paz.init() #duty=round((AZ_MAX+AZ_MIN)/2)) alt_pin = machine.Pin(ALT_PIN, machine.Pin.OUT) palt = machine.PWM(alt_pin, freq=50) palt.init() #duty=round((ALT_MAX+ALT_MIN)/2)) lvlpin = machine.ADC(machine.Pin(LVL_PIN)) lvlpin.width(lvlpin.WIDTH_12BIT) lvlpin.atten(lvlpin.ATTN_11DB) if LVL_SUNPIN: lvlspin = machine.ADC(machine.Pin(LVL_SUNPIN)) lvlspin.width(lvlspin.WIDTH_12BIT) lvlspin.atten(lvlspin.ATTN_11DB) def run(): global tstump (t, h, p, v, vs, msg) = measure() vbat = v if FAKE_SLEEP: print("No watchdog")
def __init__(self): adc = machine.ADC() self.analogTempPin = adc.channel(pin='P20') pycom.heartbeat(False)
import machine PIN_VBAT = 39 PIN_VUSB = 36 PIN_VIDENT = 32 _vbat = machine.ADC(PIN_VBAT) _vbat.atten(machine.ADC.ATTN_11DB) _vusb = machine.ADC(PIN_VUSB) _vusb.atten(machine.ADC.ATTN_11DB) _vident = machine.ADC(PIN_VIDENT) _vident.atten(machine.ADC.ATTN_11DB) _vident.width(machine.ADC.WIDTH_12BIT) def usb(): ''' Read USB input voltage :return: integer, voltage in mV ''' return int(_vusb.read() * 1.97) def battery(): ''' Read battery voltage :return: integer, voltage in mV ''' return int(_vbat.read() * 1.94)
def battery_level(): return machine.ADC(0).read()
import utime import os redLed = machine.Pin(15, machine.Pin.OUT) blueLed = machine.Pin(27, machine.Pin.OUT) greenLed = machine.Pin(33, machine.Pin.OUT) button1 = machine.Pin(36, machine.Pin.IN) button2 = machine.Pin(39, machine.Pin.IN) file = open('datafile.txt', 'w') #Turn on only blue LED redLed.value(1) greenLed.value(1) blueLed.value(0) data = machine.ADC(machine.Pin(32)) data.atten(ADC.ATTN_11DB) #data.width(ADC.WIDTH_11BIT) mainLed = machine.PWM(machine.Pin(13)) mainLed.freq(78000) mainLed.duty(600) k = 0 r = 0 while True: if button1.value() != 0: blueLed.value(1) redLed.value(0) while True:
import machine from time import sleep from machine import Pin from neopixel import NeoPixel BUILT_IN_LED = 2 PIN_WS = 13 PIN_AN = 35 NUM_LED = 1 pin = Pin(PIN_WS, Pin.OUT) np = NeoPixel(pin, NUM_LED) pin_led = Pin(BUILT_IN_LED, Pin.OUT) pin_an = Pin(PIN_AN, Pin.IN) adc = machine.ADC(pin_an) # <--this is A/D conversion def simple_blink(): pin_led.value(0) sleep(1 / 10) pin_led.value(1) sleep(1 / 5) while True: an = adc.read() if (an < 2000): np[0] = (128, 0, 0) #red np.write()
from bmp180 import BMP180 from umqtt.simple import MQTTClient DHT11_PIN=5 RAIN_PIN=13 DELAY=1 last_mesurement_time = 0 #year=99; month=0; day=0; hour=0; minute=0; second=0; ms=0; dayinyear=0; t=0; h=0; l=0 rain='' rtc=machine.RTC() rtc.datetime((2020, 2, 16, 7, 19, 12, 0, 0)) #t=0; h=0; l=0 j=0 RainSensor=machine.Pin(RAIN_PIN, machine.Pin.IN) LightSensor=machine.ADC(0) bus = machine.I2C(scl=machine.Pin(14), sda=machine.Pin(12), freq=100000) # on esp8266 bmp180 = BMP180(bus) bmp180.oversample_sett = 2 bmp180.baseline = 101325 WiFi_SSID = "5T" WiFi_PASS = "******" SERVER = "mqtt.thingspeak.com" client = MQTTClient("umqtt_client", SERVER) CHANNEL_ID = "992282" WRITE_API_KEY = "T0C09N6ZGNLAZUSD"
SENSOR_ENn = machine.Pin(25, machine.Pin.OUT) def power_enable(): SENSOR_ENn.value(0) def power_disable(): SENSOR_ENn.value(1) i2c = machine.I2C(scl=machine.Pin(16), sda=machine.Pin(4), freq=100000) i2c.scan() moist_pin = machine.Pin(35, machine.Pin.IN) moist_adc = machine.ADC(moist_pin) # ADC1 CH 7 moist_adc.atten(machine.ADC.ATTN_11DB) def moisture(): """Read the moisture sensor voltage""" return 3.3 * moist_adc.read() / 1023 battery_pin = machine.Pin(32, machine.Pin.IN) battery_adc = machine.ADC(battery_pin) # ADC1 CH 4 battery_adc.atten(machine.ADC.ATTN_11DB) def battery_voltage(): """Read the total battery voltage"""
# temperature.py # April 29, 2021 import machine import utime sensor_temp = machine.ADC(4) conversion_factor = 3.3 / (65535) while True: reading = sensor_temp.read_u16() * conversion_factor temperature = 27 - (reading - 0.706) / 0.001721 print(round(temperature, 1)) utime.sleep(2)
import machine import time potentiometer = machine.ADC(26) # set GP26 as analog input pin while True: print(potentiometer.read_u16()) # print analog value to serial time.sleep_ms(50) # sleep for 50ms, then repeat.
pin12 = pins.Pins(2) pin13 = pins.Pins(18) pin14 = pins.Pins(19) pin15 = pins.Pins(23) pin16 = pins.Pins(5) pin19 = pins.Pins(22) pin20 = pins.Pins(21) import display Image = display.Image display = display.Display() import button button_a = button.Button(35) button_b = button.Button(27) import temperature __adc = machine.ADC(machine.Pin(34, machine.Pin.IN)) __adc.atten(machine.ADC.ATTN_11DB) temperature = temperature.Temperature(__adc) try: import mpu9250 __sensor = mpu9250.MPU9250( machine.I2C(scl=machine.Pin(22), sda=machine.Pin(21), freq=200000)) import compass compass = compass.Compass(__sensor) import accelerometer accelerometer = accelerometer.Accelerometer(__sensor) except Exception as e: print("MPU9250 ERROR")
def __init__(self): self.sensor = machine.ADC(0)
import socket import machine from machine import UART import time import re import pycom pycom.heartbeat(False) uart = UART(1, baudrate=9600) #setting bus for serial communication lora = LoRa(mode=LoRa.LORA, region=LoRa.EU868) #setting up LoRa transmission s = socket.socket(socket.AF_LORA, socket.SOCK_RAW) s.setblocking(False) adc = machine.ADC() # reading analogue signal from LDR apin = adc.channel(pin="P16") while True: numOfChar = uart.any() if (numOfChar is not None and numOfChar > 0): val = uart.read(numOfChar) + str(apin()) print(val) if "null" in val: s.send("null") for i in range(0, 3): pycom.rgbled(0xff0000) time.sleep(0.2) pycom.rgbled(0x000000) time.sleep(0.2) else:
# Measuring temperature by TMP36 import machine adc = machine.ADC() # create an ADC object apin = adc.channel(pin='P16') # create an analog pin on P16 & connect TMP36 print("") print("Reading TMP36 Sensor...") value = apin() print("ADC count = %d" % (value)) # LoPy has 1.1 V input range for ADC temp = ((value * 1100) / 1024 - 500) / 10 print("Temperature = %5.1f grdC" % (temp))
# Source: https://learn.sparkfun.com/tutorials/micropython-programming-tutorial-getting-started-with-the-esp32-thing/experiment-3-analog-input import machine import sys import utime # Pin Definitions repl_button = machine.Pin(0, machine.Pin.IN, machine.Pin.PULL_UP) repl_led = machine.Pin(5, machine.Pin.OUT) adc_pin = machine.Pin(36) # Create an ADC object out of our pin object adc = machine.ADC(adc_pin) # 11dB attenuation means full 0V to 3.3V range adc.atten(adc.ATTN_11DB) # Blink forever while True: # If button 0 is pressed, drop to REPL if repl_button.value() == 0: print("Dropping to REPL") repl_led.value(1) sys.exit() # Read ADC and convert to voltage val = adc.read() # Will be a value between 0 and 4095 val = val * (3.3 / 4095) # Convert to value between 0 and 3.3 val = round(val, 2) print("{:.2f}".format(val) + "V")
def get_LM35_temperature(val): my_lm35 = machine.ADC(machine.Pin(val)) my_lm35.atten(machine.ADC.ATTN_11DB) return (my_lm35.read() / 4096) * 5000 / 10.24
cooler = Cool() # temperature PID init temppid = [20, 6, 30] temperr = [0] optTemp = 19 # OD PID init odpid = [0, 0, 0] oderr = [0] optOD = 590 # Water level control wlOut = machine.Pin(21, machine.Pin.OUT) wlOut.value(1) wl = machine.ADC(machine.Pin(36)) wl.atten(3) wl.width(machine.ADC.WIDTH_10BIT) # initial algae parameters cia = 50000.0 cim = 20000.0 cd = 20000.0 pr = 1.25 vm = 2000.0 # Connect to WiFi tryConnect(display) # Set to your Adafruit IO key & username below.
def hall_effect_adc(): global _adc if not _adc: _adc = machine.ADC(machine.ADC.ADC_HALLEFFECT) return _adc
def callbackup(p): time.sleep_ms(100) flag = p.value() if flag == 1: state = 1 else: state = 0 return state def callbackdown(p): time.sleep_ms(100) flag = p.value() if flag == 0: state = 0 else: state = 1 return state adc = machine.ADC(0) pwm = machine.PWM(machine.Pin(15)) pwm.freq(60) button = Pin(16,PIN.in) state = button.irq(trigger=Pin.IRQ_FALLING, handler=callbackup) state = button.irq(trigger=Pin.IRQ_FALLING, handler=callbackdown) while True: state = vol_val = adc.read() if state == 1: pwm.duty(vol_val)
self.update_line(0, line1) self.update_line(1, line2) def update_line(self, j, line): line = "{:16s}".format(line) if line != self.lines[j]: for i, char in enumerate(line): self.d.setCursor(i, j) self.d.write(ord(char)) self.lines[j] = line def display_adc(timer): global adc, lcd val = 0 for k in range(10): val += 10 * adc.read_u16() / 65535 lcd.update(f"ADC = {val:5.1f}%", "") adc = machine.ADC(27) i2c = machine.I2C(0) lcd = LCD16x2(i2c) lcd.update("Go Irish!", "") time.sleep(1) timer = machine.Timer(freq=10, mode=machine.Timer.PERIODIC, callback=display_adc)