Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions blink/main.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
# A led blinking every half second for five times.
import time

from machine import Pin


# create an output pin on pin #2
# because for ESP-12 board the led light using GPIO2 which is pin2.
# the led operate in "inverted" mode, which means the pin value is '1' will set
# the led off, and pin value "0" will set the led on.
led = Pin(2, Pin.OUT)
for i in range(3):
time.sleep(.5)
led.value(1)
# set the value high to turn the led off.
led.value(1)
time.sleep(1)
for i in range(5):
time.sleep(.5)
led.value(0)

time.sleep(.5)
led.value(1)
10 changes: 7 additions & 3 deletions boot.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
import time
import machine

sta_if = network.WLAN(network.STA_IF); sta_if.active(True)
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)

try:
with open("passwords.txt") as f:
Expand All @@ -24,8 +25,9 @@

sta_if.connect(station, password)

for i in range(15):
print(".")
#for i in range(15):
for i in range(30):
print(".", end=' ')

if sta_if.isconnected():
break
Expand All @@ -36,3 +38,5 @@
break
else:
print("Connection could not be made.\n")
if sta_if.isconnected():
print("\nConnected as: {}".format(sta_if.ifconfig()[0]))
21 changes: 21 additions & 0 deletions dc_motor/DC_motor_control_over_WiFi.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
DC motor control over WiFi
1. The NodeMCU board pin 12 and pin 13 is connected to the motor driver (L293D)
pin 2 and pin 7 to control the motor rotating forward or backward or stop.
The pin values corresponding to motor direction is as follows:
Pin2 = 0 Pin7 = 0 ---> motor stop
Pin2 = 1 Pin7 = 0 ---> motor forward
Pin2 = 0 Pin7 = 1 ---> motor backward
There are four buttons on our motor control web page for controlling motor's
on/off/forward/backward.

2. The speed of the motor is also controlled by sending different PWM duty cycle
through pin14 to the L293D enable pin.
We have four button on our motor control web page for controlling the speed:
25 duty cycle, 50 duty cycle, 75 duty cycle and 100 duty cycle.

3. The speed of the motor can also be controlled by a position transducer using
the only analog input pin A0. The analogy input pin converts the position
transducer voltage output into digital value(0, 1023). This digital value is then
used as input parameter for the pwm.duty() function to control the speed of the
motor seamlessly. We have a button on our motor control web page to test this
stepless speed control.
201 changes: 201 additions & 0 deletions dc_motor/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
# This code controls a DC motor speed and direction over Wifi using
# Nodemcu esp8266 and L293D motor driver.
# http://localhost:8080/motor_control is the homepage for DC motor control.
try:
import usocket as socket
except:
import socket

response_404 = """HTTP/1.0 404 NOT FOUND

<h1>404 Not Found</h1>
"""

response_500 = """HTTP/1.0 500 INTERNAL SERVER ERROR

<h1>500 Internal Server Error</h1>
"""


response_template = """HTTP/1.0 200 OK

%s
"""
import machine
from time import sleep
from machine import Pin

# set up the pins of nodeMCU and L293D
# 1. We use GPIN 12 and 13 of nodeMCU as output control pins.
# GPIO12 and 13 is connected to the INPUT Pin 2 and 7 of L293D.
# Pin2 = 0 Pin7 = 0 ---> motor off
# Pin2 = 1 Pin7 = 0 ---> motor forward
# Pin2 = 0 Pin7 = 1 ---> motor backward
out1 = Pin(12, Pin.OUT)
out2 = Pin(13, Pin.OUT)

# 2. We use GPID14 of nodeMCU to send the PWM signal to control the speed of motor.
# GPID14 of nodeMCU is connected to the enable 1,2 (Pin 1) of L293D.
pwm = Pin(14)
pwm = machine.PWM(pwm)
pwm.duty(1023)

# 3. We use Analog input pin A0 to connect a transducer to change the speed of
# the motor.
adc = machine.ADC(0)

home_page_str = """
<!DOCTYPE HTML>
<html>
<h1 align=center>NodeMCU DC motor control over WiFi</h1><br><br>
<br><br>
<a href=\"http://192.168.1.168:8080/motor_on\"><button> Motor On </button></a>
<a href=\"http://192.168.1.168:8080/motor_off\"><button> Motor Off </button></a>
<a href=\"http://192.168.1.168:8080/motor_forward\"><button> Motor Forward </button></a>
<a href=\"http://192.168.1.168:8080/motor_backward\"><button> Motor Backward </button></a>
<br>
<br>
<a href=\"http://192.168.1.168:8080/duty_25\"><button> Duty Cycle 25% </button></a>
<a href=\"http://192.168.1.168:8080/duty_50\"><button> Duty Cycle 50% </button></a>
<a href=\"http://192.168.1.168:8080/duty_75\"><button> Duty Cycle 75% </button></a>
<a href=\"http://192.168.1.168:8080/duty_100\"><button> Duty Cycle 100% </button></a>
<br>
<br>
<a href=\"http://192.168.1.168:8080/duty_100\"><button> Stepless Speed Control</button></a>
<br>
<br>
<br>
"""
# create homepage veiw function.
def home_page():
body = home_page_str + '</html>'
return response_template % body

# create a motor on view function.
def motor_on():
out1.value(1)
out2.value(0)
pwm.duty(1023)
body = home_page_str + '<p> Motor is on</p> </html>'
return response_template % body

# create a motor off view function.
def motor_off():
out1.value(0)
out2.value(0)
body = home_page_str + 'Motor is stopped.'
return response_template % body

# create a motor forward veiw function.
def motor_forward():
out1.value(0)
out2.value(0)
sleep(3)
out1.value(1)
out2.value(0)
#pwm.duty(1023)
body = home_page_str + 'Motor rotating in forward direction.'
return response_template % body

# create a motor backward veiw function.
def motor_backward():
out1.value(0)
out2.value(0)
sleep(3)
out1.value(0)
out2.value(1)
#pwm.duty(1023)
body = home_page_str + 'Motor rotating in backward direction.'
return response_template % body

# create PWM duty cycle = 25 veiw function.
def duty_25():
pwm.duty(255)
body = home_page_str + 'PWM duty cycle 25%.'
return response_template % body

# create PWM duty cycle = 50 veiw function.
def duty_50():
pwm.duty(512)
body = home_page_str + 'PWM duty cycle 50%.'
return response_template % body

# create PWM duty cycle = 75 veiw function.
def duty_75():
pwm.duty(767)
body = home_page_str + 'PWM duty cycle 75%.'
return response_template % body

# create PWM duty cycle = 100 veiw function.
def duty_100():
pwm.duty(1023)
body = home_page_str + 'PWM duty cycle 100%.'
return response_template % body

# create analog input duty cycle veiw function.
def analog_duty():
motor_duty_cycle = adc.read()
pwm.duty(motor_duty_cycle)
body = home_page_str + 'PWM duty cycle {}'.format(motor_duty_cycle)
return response_template % body

# routing dictionary for different view functions.
handlers = {
'motor_control': home_page,
'motor_on': motor_on,
'motor_off': motor_off,
'motor_forward': motor_forward,
'motor_backward': motor_backward,
'duty_100': duty_100,
'duty_75': duty_75,
'duty_50': duty_50,
'duty_25': duty_25,
'analog_duty': analog_duty,
}

def main():
s = socket.socket()
ai = socket.getaddrinfo("0.0.0.0", 8080)
addr = ai[0][-1]

s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

s.bind(addr)
s.listen(5)
print("Listening, connect your browser to http://<this_host>:8080/")

while True:
sleep(.5)
print("Before accept:")
res = s.accept()
print("After accept:")
client_s = res[0]
client_addr = res[1]
req = client_s.recv(4096)
print("Request:")
print(req)

# This first line of a request looks like "GET /arbitrary/path/ HTTP/1.1 "
# It has three parts and seperated by a space char (' ').
# So the first part is the GET method, second is the path, the third is the
# HTTP version used. We only need the second part.
# The first line of a request is: req.decode().split('\r\n')[0]
# The second part of first line is:
# req.decode().split('\r\n')[0].split(" ")[1]
try:
path = req.decode().split('\r\n')[0].split(' ')[1]
# http://localhost:8080/motor_control is the homepage.
handler = handlers[path.strip('/').split('/')[0]]
response = handler()
except KeyError:
response = response_404
except Exception as e:
response = response_500
print(str(e))

client_s.send(b"\r\n".join([line.encode() for line in response.split("\n")]))

client_s.close()
print()

main()
3 changes: 1 addition & 2 deletions passwords.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
EarlGreyTea HotHotHot
MyHomeAccessPoint aueiUeh73NB
Jia2 123456
49 changes: 46 additions & 3 deletions simple_web_server/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,22 @@
except:
import socket

response_404 = """HTTP/1.0 404 NOT FOUND

<h1>404 Not Found</h1>
"""

response_500 = """HTTP/1.0 500 INTERNAL SERVER ERROR

<h1>500 Internal Server Error</h1>
"""
response_template = """HTTP/1.0 200 OK

%s
"""
import machine
import ntptime, utime
import ntptime
import utime
from machine import RTC
from time import sleep

Expand All @@ -19,6 +29,7 @@
seconds = 0
rtc.datetime(utime.localtime(seconds))

# time view
def time():
body = """<html>
<body>
Expand All @@ -30,6 +41,17 @@ def time():

return response_template % body

# dummy view
def dummy():
body = "This is a dummy endpoint"
return response_template % body

# routing dictionary for different view functions.
handlers = {
'time': time,
'dummy': dummy,
}

def main():
s = socket.socket()
ai = socket.getaddrinfo("0.0.0.0", 8080)
Expand All @@ -39,18 +61,39 @@ def main():

s.bind(addr)
s.listen(5)
print("Listening, connect your browser to http://<this_host>:8080/")
print("Listening, connect your browser to http://", end='')
print(addr)

while True:
sleep(.5)
print("Before accept")
res = s.accept()
client_s = res[0]
client_addr = res[1]
req = client_s.recv(4096)
print("Request:")
print(req)

response = time()
# This first line of a request looks like "GET /arbitrary/path/ HTTP/1.1 "
# It has three parts and seperated by a space char (' ').
# So the first part is the GET method, second is the path, the third is the
# HTTP version used. We only need the second part.
# The first line of a request is: req.decode().split('\r\n')[0]
# The second part of first line is:
# req.decode().split('\r\n')[0].split(" ")[1]
try:
path = req.decode().split('\r\n')[0].split(' ')[1]
# the path like /dummy/dog/, we want to only get 'dummy'
# so we need to strip slashes from the left or right side of the path.
# and then get the first string from the remaining path.
handler = handlers[path.strip('/').split('/')[0]]
# if the handler is 'dummy', then we will call dummy().
response = handler()
except KeyError:
response = response_404
except Exception as e:
response = response_500
print(str(e))

client_s.send(b"\r\n".join([line.encode() for line in response.split("\n")]))

Expand Down
Loading