NodeMCU Projects

From Network Security Wiki
Jump to navigation Jump to search



Small Projects

Playing with GPIO

import machine
import time
import urandom

pin = machine.Pin(2, machine.Pin.OUT)

def toggle(p):
   p.value(not p.value())

while True:
   time.sleep_ms(urandom.getrandbits(8))
   toggle(pin)


Fading an LED

import time, math
import machine

led = machine.PWM(machine.Pin(2), freq=1000)

def pulse(l, t):
    for i in range(20):
        l.duty(int(math.sin(i / 10 * math.pi) * 500 + 500))
        time.sleep_ms(t)

while True:
    pulse(led, 20)


Control a hobby servo

  1. Hobby servo motors can be controlled using PWM.
  2. They require a frequency of 50Hz and a duty between about 40 and 115, with 77 being the center value.
  • Manual Movements
servo = machine.PWM(machine.Pin(12), freq=50)
servo.duty(40)
servo.duty(115)
servo.duty(77)
  • Random movements:
servo = machine.PWM(machine.Pin(12), freq=50)
while True:
   servo.duty(urandom.getrandbits(8))
   time.sleep(1)

One Wire DS18B20 Temp Sensor

import time
import machine
import onewire, ds18x20

# the device is on GPIO12
dat = machine.Pin(12)

# create the onewire object
ds = ds18x20.DS18X20(onewire.OneWire(dat))

# scan for devices on the bus
roms = ds.scan()
print('found devices:', roms)

# loop 10 times and print all temperatures
for i in range(10):
    print('temperatures:', end=' ')
    ds.convert_temp()
    time.sleep_ms(750)
    for rom in roms:
        print(ds.read_temp(rom), end=' ')
    print()

Deep-sleep mode

  • This shut down the ESP8266 and all its peripherals & also WiFi.
  • But not including the real-time-clock, which is used to wake the chip.
  • This drastically reduces current consumption and is a good way to make devices that can run for a while on a battery.
  • You Must connect GPIO16 to the reset pin.
import machine

# configure RTC.ALARM0 to be able to wake the device
rtc = machine.RTC()
rtc.irq(trigger=rtc.ALARM0, wake=machine.DEEPSLEEP)

# check if the device woke from a deep sleep
if machine.reset_cause() == machine.DEEPSLEEP_RESET:
    print('woke from a deep sleep')

# set RTC.ALARM0 to fire after 10 seconds (waking the device)
rtc.alarm(rtc.ALARM0, 10000)

# put the device to sleep
machine.deepsleep()

Analog Temperature Meter

Requirements:

DS18B20 Temperature sensor
Servo Motor
Micropython based NodeMCU

Wiring details:

Servo motor = D4 => GPIO 2
DS18B20     = D7 => GPIO 13
import time
import machine
import onewire, ds18x20

# the device is on GPIO13
dat = machine.Pin(13)

# create the onewire object
ds = ds18x20.DS18X20(onewire.OneWire(dat))

# scan for devices on the bus
roms = ds.scan()
print('found devices:', roms)

# servo is connected to GPIO2
servo = machine.PWM(machine.Pin(2), freq=50)

# Function for mapping range
def translate(value, leftMin, leftMax, rightMin, rightMax):
    # Figure out how 'wide' each range is
    leftSpan = leftMax - leftMin
    rightSpan = rightMax - rightMin

    # Convert the left range into a 0-1 range (float)
    valueScaled = float(value - leftMin) / float(leftSpan)

    # Convert the 0-1 range into a value in the right range.
    return rightMin + (valueScaled * rightSpan)
 
# print all temperatures
while True:
    ds.convert_temp()
    time.sleep_ms(1000)
    for rom in roms:
        print(ds.read_temp(rom))
    temp = ds.read_temp(rom)
    srv = translate(temp, 20, 35, 115, 40)
    print(int(srv))
    if (temp>=20) & (temp<=35):
        servo.duty(int(srv))
    else:
        print("Out of Range")

NeoPixel

import machine
import neopixel
import time
import urandom

pin = machine.Pin(14, machine.Pin.OUT)     # WS2812 data pin connected to GPIO 14 or Pin D5
np = neopixel.NeoPixel(pin, 16)            #16 Pixels or LEDs in strip

def rand():
    return urandom.getrandbits(7)

def cycle():
    for i in range(16):
        np[i] = (128,0,0)
        np.write()
        time.sleep_ms(25)

    for i in range(16):
        np[i] = (0,128,0)
        np.write()
        time.sleep_ms(25)

    for i in range(16):
        np[i] = (0,0,128)
        np.write()
        time.sleep_ms(25)

def circle():
    for j in range(10):
        x,y,z = rand(),rand(),rand()
        for i in range(16):
            np[i] = (x,y,z)
            np.write()
            time.sleep_ms(25)

def circle_rainbow():
    for j in range(10):
        for i in range(16):
            np[i] = (rand(),rand(),rand())
            np.write()
            time.sleep_ms(25)

def fade():
    for i in range(0, 4 * 256, 8):
            for j in range(16):
                if (i // 256) % 2 == 0:
                    val = i & 0xff
                else:
                    val = 255 - (i & 0xff)
                np[j] = (val, 0, 0)
            np.write()

    for i in range(0, 4 * 256, 8):
            for j in range(16):
                if (i // 256) % 2 == 0:
                    val = i & 0xff
                else:
                    val = 255 - (i & 0xff)
                np[j] = (0, val, 0)
            np.write()

    for i in range(0, 4 * 256, 8):
            for j in range(16):
                if (i // 256) % 2 == 0:
                    val = i & 0xff
                else:
                    val = 255 - (i & 0xff)
                np[j] = (0, 0, val)
            np.write()

def clock():
    for k in range(10):
        for i in range(16):
            if i%2 == 0:
                np[i] = (64,0,10)
                np.write()
            else:
                np[i] = (10,64,0) 
                np.write()
        time.sleep_ms(200)

        for j in range(16):
            if j%2 == 0:
                np[j] = (10,64,0)
                np.write()
            else:
                np[j] = (64,10,0)
                np.write()
        time.sleep_ms(200)

def bounce():
    for i in range(3):
        x,y,z = rand(),rand(),rand()
        for i in range(4 * 16):
            for j in range(16):
                np[j] = (x,y,z)
            if (i // 16) % 2 == 0:
                np[i % 16] = (0, 0, 0)
            else:
                np[16 - 1 - (i % 16)] = (0, 0, 0)
            np.write()
            time.sleep_ms(60)

def off():
    for i in range(16):
        np[i] = (0,0,0)
        np.write()

cycle()
off()
time.sleep(1)

circle()
off()
time.sleep(1)

circle_rainbow()
off()
time.sleep(1)

fade()
off()
time.sleep(1)

clock()
off()
time.sleep(1)

bounce()
off()

Morse Code

import machine
import sys
import time

CODE = {'A': '.-',     'B': '-...',   'C': '-.-.', 
        'D': '-..',    'E': '.',      'F': '..-.',
        'G': '--.',    'H': '....',   'I': '..',
        'J': '.---',   'K': '-.-',    'L': '.-..',
        'M': '--',     'N': '-.',     'O': '---',
        'P': '.--.',   'Q': '--.-',   'R': '.-.',
     	'S': '...',    'T': '-',      'U': '..-',
        'V': '...-',   'W': '.--',    'X': '-..-',
        'Y': '-.--',   'Z': '--..',   ' ': ' ',
        
        '0': '-----',  '1': '.----',  '2': '..---',
        '3': '...--',  '4': '....-',  '5': '.....',
        '6': '-....',  '7': '--...',  '8': '---..',
        '9': '----.' 
        }

pin = machine.Pin(14, machine.Pin.OUT)        # Laser Module connected to GPIO 14 or Pin D5
pin.off()
a = 300

def morse(x):
    c = []
    for i in x:
        c.append(CODE[i.upper()])
        c.append(" ")

    for i in range(len(c)):
        for j in c[i]:
            if j == ".":
                pin.on()
                print("DOT")
                time.sleep_ms(a)
                pin.off()
                time.sleep_ms(a*2)
            elif j == "-":
                pin.on()
                print("DASH")
                time.sleep_ms(a*3)
                pin.off()
                time.sleep_ms(a*2)
            elif j == " ":
                pin.off()
                print(" ")
                time.sleep_ms(a*4)
            else:
                print("ERROR")
    pin.off()
    print("END")

morse("amandeep")


References





blog comments powered by Disqus