Esp8266 Nodemcu Kit
Esp8266 Nodemcu Kit
Esp8266 Nodemcu Kit
1
Par t1 Modules Introdu ction ......................................................................................................4
1. Crowta il - ES P826 6 Node MCU .................................................................................... 4
2. Crowta il - LED ................................................................................................................... 4
3.Crowtail- Button....................................................................................................................... 4
4.Crowtail- Buzzer........................................................................................................................4
5. Crowtail- Relay.........................................................................................................................5
6.Crowtail- Moisture Sensor....................................................................................................... 5
7. Crowtail- Temperature& Humidity Sensor..............................................................................6
8. Crowtail- Light Sensor............................................................................................................. 6
9. Crowtail- Vibration Motor....................................................................................................... 6
10. Crowtail- Hall Sensor............................................................................................................. 7
11.Crowtail- Tilt Switch............................................................................................................... 7
12.Crowtail- OLED........................................................................................................................7
13. Crowtail- RGB-LED................................................................................................................. 7
14. Crowtail- Water Sensor......................................................................................................... 8
15.Crowtail- Laser Pointer...........................................................................................................8
16. One Wrie Waterproof Temperature Sensor..........................................................................8
17. Crowtail- MOSFET..................................................................................................................9
28.Crowtail – Flame Sensor.........................................................................................................9
19.Crowtail- Rotary Angle Sensor............................................................................................... 9
20.Crowtail- 3-Axis Digital Accelerometer.................................................................................. 9
21.Crowtail- PIR Sensor............................................................................................................. 10
22.Crowtail- 9G Servo................................................................................................................10
23.Crowtail- Magnetic Switch................................................................................................... 10
24.Crowtail- Protoboard............................................................................................................11
25.Crowtail- Switch....................................................................................................................11
Par t2 Crow tai l Appli cation ...................................................................................................... 12
Less on1 : LE D Control ......................................................................................................... 12
Less on2: Bu tton Control LED .......................................................................................... 14
Lesson3: Moisture Sensor......................................................................................................... 15
Lesson4: Vibration Motor......................................................................................................... 16
Lesson5: Emergency Light.........................................................................................................17
Lesson6: Raining detect............................................................................................................ 18
Lesson7: Flame detect...............................................................................................................19
Lesson8: Motion Detect............................................................................................................ 19
Lesson9: Hall Switch.................................................................................................................. 20
Lesson10 Breathing Lamp......................................................................................................... 21
Lesson11 Laser Pointer..............................................................................................................22
Lesson12: One Wire Waterproof Temperature Sensor............................................................ 23
Lesson13: Rainbow Light...........................................................................................................27
Lesson14 MOSFET Control Fan................................................................................................. 29
Lesson15: Read Data From ADXL345 Digital Accelerometer Sensor........................................30
Lesson16: Servo Control............................................................................................................32
Lesson17: TWO NodeMCU Communication.............................................................................33
Lesson18: DHT11 Temperature Display System........................................................................35
Lesson19: Wireless Toliet Led Indicator....................................................................................40
Preface –Getting Started
Welcome to the world of ESP8266 NodeMCU. It’s an ESP8266 WiFi module with Crowtail
interface. We will use it to make some simple application of fun with various Crowtail modules .
When the information”Connected to COMX at 9600baud” display in the debug window, it means
connecting is successful, and you can input command statement into the bellow edit box. As send
command “ print(“Hello world!”) ”, then the result will display in the debug window.
1
Also, you can use some of the function which display on the right side of Lualoader interface.
Baud rate setting: set the baud rate that you need ,normally set at 9600.
GPIO Function: Choose different GPIO port, set its mode(Input, Output or Interrupt ),set GPIO
Pull up resistor (Floating, Pull up or Pull down), and “Read” or “Write” operation to the GPIO
2
port.
Chip information check: check the information about the chip or restart the NodeMCU.
Wireless network connect: Enter the wireless network account and password, you can connect it
and get the IP.
Compile and download program: First, click “Upload File…”upload the .lua file that you want to
download. Secondly click “compile” to compile the .lua file, through the compile can click
“Download” to download the program.
This site: http://w w w. benlo.c om/es p826 6/ has its detail introduction.
3
Part1 Modules Introduction
1. Crowtail- ESP8266 Node MCU
2. Crowtail- LED
3.Crowtail- Button
Description
The Crowtail-Button is a momentary push button which rebounds on its own after it is released.
The button outputs a HIGH signal when pressed, and LOW when released.
4.Crowtail- Buzzer
4
Description
The Crowtail- Buzzer module is a piezo buzzer which can be connected to digital outputs, and will
emit a tone when the output is HIGH.
5. Crowtail- Relay
Description
The Crowtail- Relay module is a digital normally-open switch. Through it, you can control circuit
of high voltage with low voltage, say 5V on the controller.
Description
This Moisture Sensor can be used to detect the moisture of soil or your pet plant's water level, let
the plants in your garden reach out for human help. It is connected to Analog I/O port.
5
7. Crowtail- Temperature& Humidity Sensor
Description
The Crowtail- Temperature& Humidity Sensor is based on DH11 which is a complex sensor with a
calibrated digital signal out.It used to detect the temperature & humidity.
Description
The Crowtail- Light Sensor module incorporates a light dependent resistor (LDR), is a commonly
used sensor in a wide variety of applications from DIY projects to industrial automation. Typically,
the resistance of the LDR or Photo resistor will decrease when the ambient light intensity
increases. This means that the output signal from this module will be HIGH in bright light, and
LOW in the dark.
Description
This is a mini vibration motor suitable as a non-audible indicator. When the input is HIGH, the
motor will vibrate just like your cell phone on silent mode.
6
10. Crowtail- Hall Sensor
Description
The Crowtail- Hall Sensor uses the Allegro Hall-effects switches are next generation for the
population Allegro 312x and 314x lines of unipolar switches. It switches HIGH(turn off) when the
magnetic field disappears.
Description
The Crowtail- Tilt Switch is the equivalent of a button, and is used as a digital input. Inside the tilt
switch is a pair of balls that make contact with the pins when the case is upright. Tilt the case
over and the balls don't touch, thus not making a connection.
12.Crowtail- OLED
Description
The Crowtail- OLED is constructed from 128 x 64 dot matrix OLED module. The display offers high
brightness, self-emission, high contrast ratio, slim/thin outline, wide viewing angle, wide
temperature range and low power consumption.
7
Description
The Crowtail- RFB-LED module with 4 pcs of WS2812B which is a Chainable & Addressable LED.
Users can control all the LED with only one microcontroller pin! Besides, the LED bar can be also
chainable, that is, you can connect more than one LED bar together to make your project more
dreamful. In this module you can control every LED with different color at the same time.
Description
The Crowtail- water sensor detects water by having a series of exposed traces, The resistor will
pull the sensor trace value high until a drop of water shorts the sensor trace to the grounded
trace.
Description
This laser pointer or laser pen is a small handheld device with a laser diode emitting a very
narrow coherent low-powered laser beam of visible light, it is frequently used in lecture halls and
demonstrations to point at topics of interest on a presentation board. In a school setting, they
have become ubiquitous, and they are very useful teaching aids. Please note that DO NOT Point
this module to eyes directly, It may cause some blindness, glare and afterimages.
Description
This is a waterproofed version of the DS18B20 Temperature sensor. Handy for when you need to
measure something far away, or in wet conditions. While the sensor is good up to 125 degree,
the cable is jacketed in PVC so we suggest keeping it under 100 degree.
8
17. Crowtail- MOSFET
Description
Crowtail- MOSFET enables you to control higher voltage project, say 50VDC, with low voltage, say
5V, on microcontroller. MOSFET is also a kind of switch. There are two screw terminals on the
board. One for input power source and the other for device you want to control. Crowtail-
MOSFET will pass the power from one end to another when closed.
Description
The Crowtail- Flame Sensor can be used to detect fire source or other light sources of the
wavelength in the range of 760nm - 1100 nm. It is based on the YG1006 sensor which is a high
speed and high sensitive NPN silicon phototransistor.
Description
Rotary angle sensor for crowduino.The angular range is 300 degrees with a linear change in value.
The resistance value is 10k ohms, perfect for crowduino use. This may also be known as a
“potentiometer ”.
9
Description
This is a high resolution digital accelerometer providing you at max 3.9mg/LSB resolution and
large ±16g measurement range. It's base on an advanced 3-axis IC ADXL345. Have no worry to
implement it into your free-fall detection project, cause it's robust enough to survive up to
10,000g shock. Meanwhile, it's agile enough to detect single and double taps. It's ideal for
motion detection, Gesture detection as well as robotics.
Description
The Crowtail-PIR Motion sensor is simply connect to Crowtail base shield and program it. When
anyone moves in its detecting range, the sensor outputs HIGH on its SIG PIN.
22.Crowtail- 9G Servo
Description
Tower Pro SG90 is a high quality, low-cost servo for all your mechatronic needs. It comes with a
3-pin power and control cable.
10
Description
The Crowtail- Magnetic Sensor is a double-ended type and may be actuated with an
electromagnet, a permanent magnet or a combination of both. The magnetic switch is a
wonderful tool for designers who would like to turn a circuit on and off based on proximity
24.Crowtail- Protoboard
Description
This Crowtail allows you to add your own circuitry or components to your Crowtail system
prototypes.
25.Crowtail- Switch
Description
The Crowtail- Switch is a Latching switch. When the first press the switch, the switch and keep the
current adjustment and the button outputs a HIGH signal,namely the self-locking. When the
second press the switch, the switch off and switch button to pop up at the same time, outputs a
LOW signal.
11
Part2 Crowtail Application
Open the LuaL oader. Clic k the butto n in the GPIO func tion area, choo se GPIO5
port, set it as Output mo de, cli ck ” 1” the LE D will light up, cli ck “0 ” the LED will
light of f.
Also , we can downl oad the code to the NodeM CU. Copy the bellow code and
paste it to LuaEdi tor, then save the file an d named it “init.lua ”
12
Open the LuaL oader and fol low the below steps before downl oad pro gram.
First, cli ck “Upload File …” to choo se ES P8 266 NodeMC U kit demo code -> P01_LED
_Control -> init.lua
Secondly, cli ck “compile ” to compi le the .lua file and then clic k “ Dow nloa d ” to
downl oad the .lua file
13
The Crowtail- LED will be blink every 0.2 second.
Why th e led blin k every 0.2 secon d? How to control it keep li ghti ng mo re longer?
Now let me tell you. In thi s demo code, first def ine the gpi o1 into output port ,
then let the gpio 1 co nstantly switc h between high and low level , set the ti me
inter val as 0.2 second and make it cyc le. You can chan ge the tmr.delay( )
param eter in the program to chan ge the inter val.
Demo code:
fu n c t io n in i t _L E D ( )
gp i o . m o de ( 1 , gp i o .O U T P U T ) -- S e t th e po r t mo de
en d
wh i l e ( 1 ) do
gp i o .w r it e( 1 , gp i o . H I G H ) -- gp i o 5 is th e po r t 1
tm r.d e l ay ( 2 0 0 0 0 0 ) -- d el a y 0.2 s
gp i o .w r it e( 1 , gp i o . LOW )
tm r.d e l ay ( 2 0 0 0 0 0 )
en d
In this lesson we learn the following function.
gpi o.mod e ()
gpi o.wri te()
tmr.delay( )
14
to realize it. When pressed the button, the led was lighten and turn off when released.
Material:
Crowtail- LED x 1
Crowtail- Button x 1
Crowtail- ESP8266 NodeMCU x 1
Hardware Connection
Open the LuaLoader, click the “Upload File…” and choo se ESP826 6 NodeM CU kit dem o
code -> P02_Button_Control_Led -> init.lua. After the upload completed then click “Download ”
to download the init.lua file.
Demo code:
gpio.mode(1,gpio.OUTPUT) --Setup the port mode
gpio.mode(2,gpio.INPUT)
while(1) do
gpio.read(2) -- read port 2(gpio4)
tmr.delay(200000) --Delay 0.2 s
if (gpio.read(2)==1) then
gpio.write(1,gpio.HIGH) -- set the port 1 high level
end
if (gpio.read(2)==0) then
gpio.write(1,gpio.LOW) --set the port 1 low level
end
end
When you press the button, the led lighting , the led out when loosen the button.
15
Open the LuaLoader, click the “Upload File…”choo se ESP8 266 NodeM CU kit demo
code -> P03_ Moisture_sensor -> init.lua. After the upload completed then click “Download ”
to download the init.lua file.
Demo code:
while(1) do
b=adc.read(0) -- analog read A0
print (b)
tmr.delay(2000000) --delay 2s
end
you can see the sensor value in the LuaLoader debug window.
Open the LuaLoader, click the “Upload File…” to cho ose ES P8 266 NodeM CU kit demo
16
code -> P04_Vibration_Motor -> init.lua. After the upload completed then click “Download ”
to download the init.lua file.
Demo code:
gpio.mode(1,gpio.OUTPUT) -- set port1 output mode
gpio.mode(2,gpio.INPUT) -- set port2 input mode
while(1) do
gpio.read(2)
tmr.delay(200000) --delay 2s
if (gpio.read(2)==1) then
gpio.write(1,gpio.HIGH)
end
if (gpio.read(2)==0) then
gpio.write(1,gpio.LOW)
end
end
When the tilt switch slope, the vibration motor vibrating. Otherwise, the vibration motor was
not vibrating.
Open the LuaLoader, click the “Upload File…” to cho ose ES P8 266 NodeM CU kit demo
code -> P05_Emergency_Light -> init.lua. After the upload completed then click “Download ” to
download the init.lua file.
17
Demo code:
while (1) do
b=adc.read(0) --Analog read A0
if (b<100) then
gpio.write(1,gpio.HIGH)
else
gpio.write(1,gpio.LOW)
end
end
Under the environment of light, blocking light sensor , led will turn on. No block ,led turn off.
In our daily life, we play computer game or watch TV all-possessed indoor,sometime raining,
our clothes usually be wetted in the outdoor, but if there is a sensor that it can remind us that
when it begin to rain outside, tragedy will not happen to us. In this lesson, we will tell you how to
build it. When raining outside, the LED inside will be lighted.
Material:
Crowtail- ESP8266 NodeMCU x 1
Crowtail- LED x 1
Crowtail- Water sensor x 1
Hardware Connection
Open the LuaLoader, click the “Upload File…”choo se ESP8 266 NodeM CU kit demo
code -> P06_Rainning_detect -> init.lua. After the upload completed then click “Download ” to
download the init.lua file.
Demo code:
a=gpio.read(2) --Digital read port 2
if (a==1) then
gpio.write(1,gpio.HIGH)
if (a==0) then
gpio.write(1,gpio.LOW)
end
end
When detect raining outside, the LED lights up.
18
Lesson7: Flame detect
Have you seen the fire-fighting robots? Why it can find the source of fire?In this lesson, we will
tell you the principle about it and you can make a robot like that.
Material:
Crowtail- ESP8266 NodeMCU x 1
Crowtail- Flame Sensor x 1
Crowtail- buzzer x 1
Hardware Connection
Open the LuaLoader, click the “Upload File…”choo se ESP8 266 NodeM CU kit demo
code -> P07_Flame_detect -> init.lua. After the upload completed then click “Download ” to
download the init.lua file.
Demo code:
a=gpio.read(2)
if (a==1) then
gpio.write(1,gpio.LOW)
if (a==0) then
gpio.write(1,gpio.HIGH)
end
end
When detect the flame, buzzer alarming. The adjustable resistance may be adjusted to vary
sensitivity of detection.
19
Open the LuaLoader, click the “Upload File…”choo se ESP8 266 NodeM CU kit demo
code -> P08_Motion_detect -> init.lua. After the upload completed then click “Download ” to
download the init.lua file.
Demo code:
while(1) do
b=gpio.read(2)
print (b)
tmr.delay(2000000)
if(b==1) then
gpio.write(1,gpio.HIGH)
else
gpio.write(1,gpio.LOW)
end
end After successfully download the code, you will observed that: when there is a motive
object in the detection range of the sensor , the buzzer will alarm.
20
Open the LuaLoader, click the “Upload File…”choo se ESP8 266 NodeM CU kit demo
code -> P9_Hall_Switch -> init.lua. After the upload completed then click “Download ” to
download the init.lua file.
Demo code:
while(1) do
a=gpio.read(1)
print (b)
tmr.delay(2000000) --delay 2s
if(a==1) then
gpio.write(2,gpio.HIGH)
else
gpio.write(2,gpio.LOW)
end
end
If there is a magnetic material approaching the hall sensor, the relay will start to work, on the
contrary, the relay will be disconnected. Just like a hall switch.
21
Open the LuaLoader, click the “Upload File…”choo se ESP8 266 NodeM CU kit demo
code -> P10_Breathing_Lamp -> init.lua. After the upload completed then click “Download ” to
download the init.lua file.
Demo Code:
print("Breathing Lamp Start")
pwm.setup(1,1000,1023); -- set pot 1 PWM mode frequency 1000hz and the duty cycle 1023pwm.start(1);
tmr.alarm(2,100,1,function() --Timer 2 using the timing 100ms Repeatability
local r=adc.read(0)
if(r<1024) then -- the most analog read is 1024 but the most PWM’ suty cycle is 1023
pwm.setduty(1,r); -- set the port 1 PWM’s duty cycle is “r”
else
pwm.setduty(1,1023);
end
end)
The LED will lighten or darken with the Rotary Angle Sensor forward or reverse rotate.
Open the LuaLoader, click the “Upload File…” to choo se ES P8 266 NodeM CU kit demo
code -> P11_Laser_Pointer -> init.lua. After the upload completed then click “Download ” to
download the init.lua file.
Demo code:
a=gpio.read(2)
while (1) do
if (a==1) then
gpio.write(1,gpio.HIGH)
else
22
gpio.write(1,gpio.LOW)
end
end
Open the LuaLoader, click the “Upload File…” to choo se ES P8 266 NodeM CU kit demo
code -> P12_DS18B20_One_Wire_Waterproof_Temperature_Sensor -> ds18b20.lua. After the
upload completed then click “Download ” to download the ds18b20.lua file. Then click the
“Upload File…”choos e the init.lua in the same fol der, downlo ad it.
Demo Code:
Ds18b 20 .lua:
-- Set mo du l e na m e as pa r a m e te r of re q u i re
lo c a l mo dn a m e = .. .
lo c a l M = {}
_G [ m o d n a m e ] = M
-- -- - -- - -- - -- - -- - -- -- - -- - -- - -- - -- - -- -- - -- - -- - -- - -- - -- -- - -- - -- - -- - -- - -- -- - -- - -- - -
-- Lo c a l us e d va r i a b l e s
-- -- - -- - -- - -- - -- - -- -- - -- - -- - -- - -- - -- -- - -- - -- - -- - -- - -- -- - -- - -- - -- - -- - -- -- - -- - -- - -
-- DS 1 8 B 2 0 dq pi n
lo c a l pi n = ni l
-- DS 1 8 B 2 0 de fa u l t pi n
lo c a l de fa u lt P i n = 1
-- -- - -- - -- - -- - -- - -- -- - -- - -- - -- - -- - -- -- - -- - -- - -- - -- - -- -- - -- - -- - -- - -- - -- -- - -- - -- - -
-- Lo c a l us e d mo du l e s
-- -- - -- - -- - -- - -- - -- -- - -- - -- - -- - -- - -- -- - -- - -- - -- - -- - -- -- - -- - -- - -- - -- - -- -- - -- - -- - -
-- Tab l e mo du l e
lo c a l ta b le = ta b le
-- Str in g mo du l e
lo c a l st r i ng = st r i ng
-- On e wi r e mo du l e
23
lo c a l ow = ow
-- Tim er mo d u l e
lo c a l tm r = tm r
-- Li m i te d to lo c a l en v ir o n m e nt
se t fe nv ( 1 , M )
-- -- - -- - -- - -- - -- - -- -- - -- - -- - -- - -- - -- -- - -- - -- - -- - -- - -- -- - -- - -- - -- - -- - -- -- - -- - -- - -
-- Im p l em en ta t io n
-- -- - -- - -- - -- - -- - -- -- - -- - -- - -- - -- - -- -- - -- - -- - -- - -- - -- -- - -- - -- - -- - -- - -- -- - -- - -- - -
C = 0
F = 1
K = 2
fu n c t io n se t u p( d q )
pi n = dq
if ( p i n == ni l ) th e n
pi n = de fa u lt P in
en d
ow. se t u p( p i n)
en d
fu n c t io n add r s ( )
se t u p( p i n)
tb l = {}
ow. re s e t_ s e a r c h( p i n )
re p e a t
add r = ow. s e a r c h( p i n)
if ( a dd r ~= ni l ) th e n
ta b l e. i n se r t( t bl , add r )
en d
tm r.w d c l r ( )
un t il (a d d r == ni l )
ow. re s e t_ s e a r c h( p i n )
re t ur n tb l
en d
fu n c t io n re a d N u m b e r ( a d d r, un i t)
re s u l t = ni l
se t u p( p i n)
fl a g = fa l s e
if ( a dd r == ni l ) th e n
ow. re s e t_ s e a r c h( p i n )
co u n t = 0
re p e a t
co u n t = co u n t + 1
add r = ow.s e a r c h ( p i n )
tm r.w d c l r ( )
24
un t il ( ( a d d r ~= ni l ) or (co u n t > 10 0 ) )
ow. re s e t_ s e a r c h( p i n )
en d
if ( a dd r == ni l ) th e n
re t ur n re s u lt
en d
crc = ow.c r c 8 ( st r i n g .s u b ( a dd r, 1 , 7 ) )
if (c r c == add r : by te ( 8 ) ) th e n
if (( a d dr : b y te ( 1 ) == 0x 1 0 ) or (a d d r : b y t e( 1 ) == 0x 2 8 ) ) th e n
-- pr in t ( " D ev i c e is a DS 1 8 S 2 0 fa m i l y de v i c e ." )
ow. re s e t( p in )
ow. se l e c t ( p i n , ad d r )
ow.w r i t e( p i n, 0x 4 4 , 1)
-- tm r.d e l ay ( 1 0 0 0 0 0 0 )
pr e s e nt = ow.r e s et ( pi n )
ow. se l e c t ( p i n , ad d r )
ow.w r i t e( p i n, 0 x B E , 1 )
-- pr in t ( " P = " . .p r e se n t )
da ta = ni l
da ta = st r i n g . c ha r ( o w.r e a d ( p i n ) )
fo r i = 1, 8 do
da ta = da ta .. st r i n g. c h a r ( o w. r ea d ( p i n) )
en d
-- pr in t ( d a ta : b y t e ( 1 , 9 ) )
crc = ow. c rc 8 ( st r i n g .s u b ( da ta , 1 , 8 ) )
-- pr in t ( " C R C = " . .c r c )
if (c r c == da ta : b y t e ( 9 ) ) th e n
if ( u n i t == ni l or un i t == C) th e n
t = (d at a : b y te ( 1 ) + da ta : by te ( 2 ) * 25 6 ) * 62 5
el s ei f ( u n i t == F) th e n
t = (d at a : b y te ( 1 ) + da ta : by te ( 2 ) * 25 6 ) * 11 2 5 + 32 0 0 0 0
el s ei f ( u n i t == K) th e n
t = (d at a : b y te ( 1 ) + da ta : by te ( 2 ) * 25 6 ) * 62 5 + 27 3 1 5 0 0
el s e
re tu r n ni l
en d
t1 = t / 10 0 0 0
t2 = t % 10 0 0 0
-- pr in t ( " Te m p e r at u r e =" ..t 1 . ." . " . .t2 .. " Ce n ti g r a d e" )
-- re s u l t = t1 . . " ." ..t 2
re tu r n t1 , t2
en d
tm r.w d c l r ( )
el s e
25
-- pr in t ( " D ev i c e fa m il y is no t re c o g ni ze d . " )
en d
el s e
-- pr in t ( " C R C is no t va li d ! " )
en d
re t ur n re s ul t
en d
fu n c t io n re a d ( a d dr, un i t )
t1 , t2 = re a d N u m b e r ( a d d r, un i t)
if ( ( t 1 == ni l ) or (t2 == n i l ) ) th e n
re t ur n ni l
el s e
re t ur n t1 . . " ." .. st r i n g .fo r m at ( " % 0 4 u " , t2 )
en d
en d
-- Re tu r n mo d u l e ta b le
re t ur n M
In i t . l u a :
fu n c t io n ge tD S d a ta ( )
pi n = 1 -- co n n e c t th e si g n a l wi r e to pi n GP IO 5
t= r e q ui r e ( " d s 1 8 b2 0 " )
t. s et u p ( p i n ) -- gp i o 0
add r s = t. a d d rs ( )
no d e _ i d = no d e . c h ip i d ( )
pr in t ( " To t a l Se n so r s .: "..ta b l e . ge t n ( a d d r s) . ." ")
pr in t ( " S e ns o r Ty p e . .. : ".. n o d e_ i d . ." ")
he x _ fo r m a t =" % 0 2 X %0 2 X % 0 2 X % 0 2 X % 0 2 X % 0 2 X % 0 2 X % 0 2 X "
se n s o r _ c o un t = ta b l e. g e tn ( a d d rs )
if (s e n so r _c o u nt > 0 ) th e n
fo r i= 1 , s en s o r _ c o u nt do
si d =s t r i n g.fo r m a t ( h e x _ fo r m a t, st r i n g .b y t e ( a d d r s[ i ] , 1 , 9 ) )
pr i n t( " t " . .i . ." Un iq u e ID : ".. s i d. . " ")
tm r.w dc l r ( )
en d
en d
t1 = t. r ea d( a d d rs [ 1 ] , t.C )
t5 = t. r ea d( a d d rs [ 2 ] , t.C )
t6 = t. r ea d( a d d rs [ 3 ] , t.C )
t2 = t. r ea d( a d d rs [ 4 ] , t.C )
t3 = t. r ea d( a d d rs [ 5 ] , t.C )
t4 = t. r ea d( a d d rs [ 6 ] , t.C )
pr in t ( " Te m p t1 Lo w e r.: "..t 1 . ." de g C" )
pr in t ( " Te m p t2 Up pe r.: "..t 2 . ." de g C" )
26
pr in t ( " Te m p t3 Mi dd l e : ". .t 3 .. " de g C" )
pr in t ( " Te m p t4 Top. . .: "..t 4 . ." de g C" )
pr in t ( " Te m p t5 Fl ow. .: "..t 5 . ." de g C" )
pr in t ( " Te m p t6 Re t u r n : "..t 6 . ." de g C" )
-- Cl e a n u p
t = ni l
ds 1 8 b2 0 = ni l
pa c ka g e .l o a d e d [" d s 1 8 b2 0 " ] =n i l
en d
tm r.a la r m ( 2 , 10 0 0 , 1, fu n c t io n ( ) get D S d at a ( ) en d )
Hardware Connection
Open the LuaLoader, click the “Upload File…”choos e ESP826 6 NodeM CU ki t demo co de
-> P13_Rainbow_Light -> init.lua. After the upload completed then click “Download ” to
download the init.lua file.
BRIGHT =1
ON = BRIGHT * 255
LED_PIN =1 -- GPIO5
PIXELS =8
TIME_ALARM = 25 -- 0.025 second, 40 Hz
TIME_SLOW = 500000 -- 0.500 second, 2 Hz
27
WHITE = string.char(ON, ON, ON)
BLACK = string.char( 0, 0, 0)
function colourWheel(index)
if index < 85 then
return string.char(index * 3 * BRIGHT, (255 - index * 3) * BRIGHT, 0)
elseif index < 170 then
index = index - 85
return string.char((255 - index * 3) * BRIGHT, 0, index * 3 * BRIGHT)
else
index = index - 170
return string.char(0, index * 3 * BRIGHT, (255 - index * 3) * BRIGHT)
end
end
rainbow_speed = 8
function rainbow(index)
buffer = ""
for pixel = 0, 7 do
buffer = buffer .. colourWheel((index + pixel * rainbow_speed) % 256)
end
return buffer
end
if true then
ws2812.write(LED_PIN, RED:rep(PIXELS))
tmr.delay(TIME_SLOW)
ws2812.write(LED_PIN, GREEN:rep(PIXELS))
tmr.delay(TIME_SLOW)
ws2812.write(LED_PIN, BLUE:rep(PIXELS))
tmr.delay(TIME_SLOW)
ws2812.write(LED_PIN, WHITE:rep(PIXELS))
tmr.delay(TIME_SLOW)
ws2812.write(LED_PIN, BLACK:rep(PIXELS))
end
rainbow_index = 0
function rainbowHandler()
while(1) do
ws2812.write(LED_PIN, rainbow(rainbow_index))
rainbow_index = (rainbow_index + 1) % 256
end
end
rainbowHandle
28
14.Lesson14 MOSFET Control Fan
The MOSFET is a kind of switch. It enables you to control higher voltage project with low
voltage. There are two screw terminals on the board. One for input power source and the other
for device you want to control. In this lesson we will teach you how to use the MOSFET to control
a fan.
Material:
Crowtail- ESP8266 NodeMCU x 1
Crowtail- MOSFET x1
Hardware Connection
Open the LuaLoader, click the “Upload File…”cho ose ESP826 6 No deMC U kit demo code
-> P14_MOSFET_Control_Fan -> init.lua. After the upload completed then click “Download ” to
download the init.lua file.
print("Crowtail- MOSFET Test")
while (1) do
i=0
if (i==0) then
for i=0,1020,4 do
pwm.setup(1,500,512)
pwm.start(1)
pwm.setduty(1,i)
tmr.delay(20000)
if(i==1020) then
for i=0,1020,4 do
b=1020-i
pwm.setup(1,500,512)
pwm.start(1)
pwm.setduty(1,b)
tmr.delay(20000) -- delay 20ms
end
end
end
end
end
After successful download the file you will see the fan rotating faster and faster, when it
reach the fastest speed, it will become rotating slower and slower, and circulating.
29
Lesson15: Read Data From ADXL345 Digital Accelerometer Sensor
In this lesson ESP8266 is used to read data from ADXL345 Digital Accelerometer Sensor, It
easy to use and obviously observe the result from the debug window. There is also some
application of ADXL345 accelerometer in our daily life, likes free-hall detection and so on.
Material:
Crowtail- ESP8266 NodeMCU x 1
Crowtail- 3-Axis Digital Accelerometer
Hardware Connection
Open the LuaLoader, click the “Upload File…”cho ose ESP826 6 No deMC U kit demo code
-> P15_Read_Data_From_ADXL345 -> init.lua. After the upload completed then click “Download ”
to download the init.lua file.
sda=5 --SDA connected to pin 5
scl=6 --SCL connected to pin 6
dev_addr=0x53 --Address of accelerometer
i2c.start(id)
i2c.address(id, dev_addr,i2c.TRANSMITTER)
i2c.write(id,0x2D) --Power control register
i2c.write(id,0x00) --Activate standby mode to configure device
i2c.stop(id)
i2c.start(id)
i2c.address(id, dev_addr,i2c.TRANSMITTER)
i2c.write(id,0x31) --Data format register
i2c.write(id,0x0B) --Set g range to 16, Full res
i2c.stop(id)
i2c.start(id)
i2c.address(id, dev_addr,i2c.TRANSMITTER)
30
i2c.write(id,0x2C) --BW rate register
i2c.write(id,0x0A) --Data rate 100Hz
i2c.stop(id)
i2c.start(id)
i2c.address(id, dev_addr,i2c.TRANSMITTER)
i2c.write(id,0x2D) --Power control register
i2c.write(id,0x08) --Activate measure mode
i2c.stop(id)
function adxl()
X0 = read_reg(0x32)
X1 = read_reg(0x33)
Y0 = read_reg(0x34)
Y1 = read_reg(0x35)
Z0 = read_reg(0x36)
Z1 = read_reg(0x37)
31
Xn=bit.band(0x1000, Xaxis)
Yn=bit.band(0x1000, Yaxis)
Zn=bit.band(0x1000, Zaxis)
32
Hardware Connection
Open the LuaLoader, click the “Upload File…”cho ose ESP826 6 No deMC U kit demo code
-> P16_Servo_Remote_Control -> init.lua. After the upload completed then click “Download ” to
download the init.lua file.
Forward rotation:
for i=1,100,1 do
tmr.alarm(0,20,1,function () -- 50Hz
gpio.write(1, gpio.HIGH)
tmr.delay(2000) --Forward
gpio.write(1, gpio.LOW)
end)
end
Reversion
for i=1,100,1 do
tmr.alarm(0,20,0,function () -- 50Hz
gpio.write(1, gpio.HIGH)
tmr.delay(500) --Reversion
gpio.write(1, gpio.LOW)
end)
end
After the su cc essfu l up lo ad ing , you will see the 9G ser vo auto rotate in two
different dir ection .
In this lesson we will use two NodeMCU to conmmunicate with each other. One NodeMCU to be
set as “AP” mode and another to be set as “STA” mode, then establish TCP service between them.
Material:
Crowtail- ESP8266 NodeMCU x 2
Hardware Connection
33
Open the LuaLoader, set one serial port which connect the NodeMCU (“AP mode”) and
connecting , then click the “Upload File…” after connecting successful, choo se ESP826 6
NodeM CU ki t demo code -> P17_NodeMCU_Communication ->TCP Server-> init.lua. After
the upload completed then click “Download ” to download the init.lua file.
The following, set another serial port which connect the NodeMCU (“STA mode”) and
connecting , then click the “Upload File…” after connecting successful, choo se ESP826 6
NodeM CU ki t demo code -> P17_NodeMCU_Communication ->TCP Client-> init.lua. After
the upload completed then click “Download ” to download the init.lua file.
Demo code
AP:
print("ESP8266 Server")
wifi.setmode(wifi.STATIONAP);
wifi.ap.config({ssid="test",pwd="12345678"});
print("Server IP Address:",wifi.ap.getip())
sv = net.createServer(net.TCP)
sv:listen(80, function(conn)
conn:on("receive", function(conn, receivedData)
print("Received Data: " .. receivedData)
end)
conn:on("sent", function(conn)
collectgarbage()
end)
end)
STA:
print("ESP8266 Client")
wifi.sta.disconnect()
wifi.setmode(wifi.STATION)
wifi.sta.config("test","12345678") -- connecting to server
wifi.sta.connect()
print("Looking for a connection")
i=0
tmr.alarm(1, 2000, 1, function()
if(wifi.sta.getip()~=nil) then
34
tmr.stop(1)
print("Connected!")
print("Client IP Address:",wifi.sta.getip())
cl=net.createConnection(net.TCP, 0)
cl:connect(80,"192.168.4.1")
tmr.alarm(2, 5000, 1, function()
if(wifi.sta.getip()==nil) then
wifi.sta.disconnect()
wifi.sta.config("test","12345678") -- connecting to server
wifi.sta.connect()
print("Looking for a connection")
tmr.start(1)
end
cl:send("Hello World!")
cl:send(i)
i=i+1
end)
else
print("Connecting...")
end
end)
We can according the print information display in the Lualoader debug window to judge the
two NodeMCU Whether have established a connection. If the “AP mode ” NodeMCU receive a
“Hello World!” from the “STA mode” NodeMCU that means the communication establish
successful.
35
Open the LuaLoader, click the “Upload File…”cho ose ESP826 6 No deMC U kit demo code
-> P18_DHT11_Temperature_OLED_Display-> TCP Server->init.lua. After the upload completed
then click “Download ” to download the init.lua file. Then Click “Upload File”, Choose
the”main.lua” in the same folder, download it.
The following, set another serial port which connect the NodeMCU (“AP mode”) and connecting ,
then click the “Upload File…” after connecting successful, cho ose ESP826 6 NodeM CU
kit demo co de -> P18_DHT11_Temperature_OLED_Display ->DHT11-> init.lua. After the
upload completed then click “Download ” to download the init.lua file. Then Click “Upload File”,
Choose the” dht11.lua” in the same folder, download it.
TCP Serve:
-- init.lua --
--tcp server--
print("ESP8266 Server")
wifi.setmode(wifi.STATIONAP);
wifi.ap.config({ssid="test2",pwd="12345678"});
print("Server IP Address:",wifi.ap.getip())
-- Run the main file
dofile("main.lua")
-- main.lua --
function init_OLED(sda,scl) --Set up the u8glib lib
sla = 0x3c
i2c.setup(0, sda, scl, i2c.SLOW)
disp = u8g.ssd1306_128x64_i2c(sla)
disp:setFont(u8g.font_6x10)
disp:setFontRefHeightExtendedText()
disp:setDefaultForegroundColor()
disp:setFontPosTop()
end
-- Connect--
print('\nAll About Circuits main.lua\n')
init_OLED(5,6) --Run setting up
i=0
-- Start a simple http server
if srv~=nil then
36
srv:close()
end
srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
conn:on("receive",function(conn,payload)
print(payload)
i=i+1
disp:firstPage()
repeat
disp:drawStr(0,0,payload) --Print the data out
disp:drawStr(15,15,i) --Print the data out
until disp:nextPage() == false
conn:send("<h1> Hello, NodeMCU!!! </h1>")
end)
conn:on("sent",function(conn) conn:close() end)
end)
DHT11:
--init.lua
-- tcp client
print("ESP8266 Client1")
wifi.sta.disconnect()
tmr.delay(100)
wifi.setmode(wifi.STATION)
wifi.sta.config("test2","12345678") -- connecting to server
wifi.sta.connect()
i=0
tmr.alarm(0,2000, 1, function()
if wifi.sta.getip()== nil then
print("IP unavaiable, Waiting...")
i=i+1
if(i>10) then
print("restart nodeMCU")
node.restart()
end
wifi.sta.disconnect()
wifi.sta.connect()
else
tmr.stop(0)
print("Config done, IP is "..wifi.sta.getip())
dofile("dht11.lua")
end
end)
37
--dht11.lua--
-- Measure temperature, humidity and post data to thingspeak.com
-- 2014 OK1CDJ
-- DHT11 code is from esp8266.com
---Sensor DHT11 is conntected to GPIO0
pin = 1
Humidity = 0
HumidityDec=0
Temperature = 0
TemperatureDec=0
Checksum = 0
ChecksumTest=0
function getTemp()
Humidity = 0
HumidityDec=0
Temperature = 0
TemperatureDec=0
Checksum = 0
ChecksumTest=0
38
--acquisition loop
for j = 1, 40, 1 do
while (gpio_read(pin)==1 and bitlength<10 ) do
bitlength=bitlength+1
end
bitStream[j]=bitlength
bitlength=0
--bus will always let up eventually, don't bother with timeout
while (gpio_read(pin)==0) do end
end
for i = 1, 8, 1 do
if (bitStream[i+0] > 2) then
Humidity = Humidity+2^(8-i)
end
end
for i = 1, 8, 1 do
if (bitStream[i+8] > 2) then
HumidityDec = HumidityDec+2^(8-i)
end
end
for i = 1, 8, 1 do
if (bitStream[i+16] > 2) then
Temperature = Temperature+2^(8-i)
end
end
for i = 1, 8, 1 do
if (bitStream[i+24] > 2) then
TemperatureDec = TemperatureDec+2^(8-i)
end
end
for i = 1, 8, 1 do
if (bitStream[i+32] > 2) then
Checksum = Checksum+2^(8-i)
end
end
ChecksumTest=(Humidity+HumidityDec+Temperature+TemperatureDec) % 0xFF
39
print ("ChecksumTest: "..ChecksumTest)
end
40
there is not occupied. This lesson is designed primarily a remote indicator toilet, do not need to
go to the toilet can also know whether someone is using the toilet, this greatly facilitates our
daily life. Of course, this design idea can be applied in many other occasions of life.
Material:
Crowtail- ESP8266 NodeMCU x 2
Crowtail- Magnetic Switch x1
Crowtail- LED x1
Hardware Connection
Open the LuaLoader, click the “Upload File…”cho ose ESP826 6 No deMC U kit demo code
-> P19_Wireless_Toliet_LED_Idicator-> Client->init.lua. After the upload completed then click
“Download ” to download the init.lua file.
Note!!! This init.lua file uploaded should choose the NodeMCU which connect with Magnetic
Switch.
Then open the LuaLoader, choose the correct serial port for the NodeMCU which connect with
two indicator LED click the “Upload File…”choo se ESP826 6 NodeM CU kit demo code ->
P19_Wireless_Toliet_LED_Idicator-> Server->init.lua. After the upload completed then click
“Download ” to download the init.lua file.
Client:
-- ESP8266 Client
print("ESP8266 Client1")
wifi.sta.disconnect()
wifi.setmode(wifi.STATION)
wifi.sta.config("test","12345678") -- connecting to server
wifi.sta.connect()
print("Looking for a connection")
pin=1
gpio.mode(pin, gpio.INPUT,gpio.FLOAT)
pin_value=0
41
cl=net.createConnection(net.TCP, 0)
cl:connect(80,"192.168.4.1")
tmr.alarm(2,1000, 1, function()
if(wifi.sta.getip()==nil) then
wifi.sta.disconnect()
wifi.sta.config("test","12345678") -- connecting to server
wifi.sta.connect()
print("reconnection")
tmr.start(1)
tmr.stop(2)
end
pin_value=gpio.read(pin)
if(pin_value==0) then
cl:send("10")
print("send 10")
else
cl:send("11")
print("send 11")
end
end)
else
print("Connecting...")
end
end)
Server:
-- ESP8266 Server
print("ESP8266 Server")
wifi.setmode(wifi.STATIONAP);
wifi.ap.config({ssid="test",pwd="12345678"});
print("Server IP Address:",wifi.ap.getip())
pin_value=0
pin=1
pin1=2
gpio.mode(pin, gpio.OUTPUT)
gpio.mode(pin1, gpio.OUTPUT)
gpio.write(pin, gpio.LOW) --Red LED
gpio.write(pin1, gpio.HIGH) --Green LED
sv = net.createServer(net.TCP)
sv:listen(80, function(conn)
conn:on("receive", function(conn, receivedData)
print("Received Data: " .. receivedData)
pin_value=receivedData
tmr.alarm(0, 100, 1, function()
42
print("inter tmr")
if(pin_value=="10") then
gpio.write(pin, gpio.HIGH)
gpio.write(pin1, gpio.LOW)
elseif(pin_value=="11") then
gpio.write(pin, gpio.LOW)
gpio.write(pin1, gpio.HIGH)
end
end)
end)
conn:on("sent", function(conn)
collectgarbage()
end)
end)
The lessons are over here, and you have learned the basic application of ESP8266 NodeMCU, but the usage of
ESP8266 is not only these. There are many other examples of the ESP8266 in the folder files, you can try them by
yourself And if you need other Crowtail modules you can visit:http://www.elecrow.com/ . Hope you have a enjoy
time.
43
44