Last active
May 30, 2018 23:49
-
-
Save truckershitch/71a553317a8aefaf2f0604b4e4ffa77e to your computer and use it in GitHub Desktop.
Script in microPython for NodeMCU ESP8266 board - blinks in response to binary code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
import machine | |
import time | |
BLINK_ON = 0.25 | |
red_led = machine.Pin(16, machine.Pin.OUT) | |
blue_led = machine.Pin(2, machine.Pin.OUT) # 16 red, 2 blue | |
red_led.on() # turn off | |
blue_led.on() # turn off | |
UDP_IP = "192.168.1.81" | |
UDP_PORT = 2390 | |
sock = socket.socket(socket.AF_INET, # Internet | |
socket.SOCK_DGRAM) # UDP | |
sock.bind((UDP_IP, UDP_PORT)) | |
def delay(led): | |
led.on() # LED off | |
time.sleep(4 * BLINK_ON) # LED off - pausing here | |
def blink_led(led, value): | |
if value <= 15: # validate input | |
for i in range(value): | |
led.off() # LED on | |
time.sleep(BLINK_ON) | |
led.on() # LED off | |
time.sleep(BLINK_ON) | |
delay(led) | |
while True: | |
data, addr = sock.recvfrom(64) # buffer size is 64 bytes | |
print("received raw message:", data) | |
blinks = int(data) | |
if (blinks & 2) and blinks <= 15: | |
blink_led(red_led, 2) | |
blink_led(blue_led, blinks - 2) | |
else: | |
blink_led(blue_led, blinks) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment