Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ruanhaishen
Nodemcu Firmware
Commits
4cd2b554
Commit
4cd2b554
authored
Apr 24, 2015
by
devsaurus
Browse files
Merge remote-tracking branch 'upstream/dev' into dev
parents
fc19725f
6eb5b9c8
Changes
34
Hide whitespace changes
Inline
Side-by-side
app/user/Makefile
View file @
4cd2b554
...
@@ -44,6 +44,7 @@ INCLUDES += -I ../libc
...
@@ -44,6 +44,7 @@ INCLUDES += -I ../libc
INCLUDES
+=
-I
../platform
INCLUDES
+=
-I
../platform
INCLUDES
+=
-I
../lua
INCLUDES
+=
-I
../lua
INCLUDES
+=
-I
../wofs
INCLUDES
+=
-I
../wofs
INCLUDES
+=
-I
../spiffs
PDIR
:=
../
$(PDIR)
PDIR
:=
../
$(PDIR)
sinclude
$(PDIR)Makefile
sinclude
$(PDIR)Makefile
app/user/user_main.c
View file @
4cd2b554
...
@@ -14,8 +14,7 @@
...
@@ -14,8 +14,7 @@
#include "c_stdlib.h"
#include "c_stdlib.h"
#include "c_stdio.h"
#include "c_stdio.h"
#include "romfs.h"
#include "flash_fs.h"
#include "user_interface.h"
#include "user_interface.h"
#include "ets_sys.h"
#include "ets_sys.h"
...
@@ -44,7 +43,6 @@ void task_init(void){
...
@@ -44,7 +43,6 @@ void task_init(void){
system_os_task
(
task_lua
,
USER_TASK_PRIO_0
,
taskQueue
,
TASK_QUEUE_LEN
);
system_os_task
(
task_lua
,
USER_TASK_PRIO_0
,
taskQueue
,
TASK_QUEUE_LEN
);
}
}
extern
void
spiffs_mount
();
// extern void test_spiffs();
// extern void test_spiffs();
// extern int test_romfs();
// extern int test_romfs();
...
@@ -69,7 +67,16 @@ void nodemcu_init(void)
...
@@ -69,7 +67,16 @@ void nodemcu_init(void)
// Flash init data at FLASHSIZE - 0x04000 Byte.
// Flash init data at FLASHSIZE - 0x04000 Byte.
flash_init_data_default
();
flash_init_data_default
();
// Flash blank data at FLASHSIZE - 0x02000 Byte.
// Flash blank data at FLASHSIZE - 0x02000 Byte.
flash_init_data_blank
();
flash_init_data_blank
();
if
(
!
fs_format
()
)
{
NODE_ERR
(
"
\n
i*** ERROR ***: unable to format. FS might be compromised.
\n
"
);
NODE_ERR
(
"It is advised to re-flash the NodeMCU image.
\n
"
);
}
else
{
NODE_ERR
(
"format done.
\n
"
);
}
fs_unmount
();
// mounted by format.
}
}
#endif // defined(FLASH_SAFE_API)
#endif // defined(FLASH_SAFE_API)
...
@@ -94,7 +101,7 @@ void nodemcu_init(void)
...
@@ -94,7 +101,7 @@ void nodemcu_init(void)
// test_romfs();
// test_romfs();
#elif defined ( BUILD_SPIFFS )
#elif defined ( BUILD_SPIFFS )
spif
fs_mount
();
fs_mount
();
// test_spiffs();
// test_spiffs();
#endif
#endif
// endpoint_setup();
// endpoint_setup();
...
...
examples/fragment.lua
View file @
4cd2b554
...
@@ -14,7 +14,7 @@ sk:connect(80,"115.239.210.27")
...
@@ -14,7 +14,7 @@ sk:connect(80,"115.239.210.27")
sk
:
send
(
"GET / HTTP/1.1\r\nHost: 115.239.210.27\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n"
)
sk
:
send
(
"GET / HTTP/1.1\r\nHost: 115.239.210.27\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n"
)
sk
:
connect
(
80
,
"192.168.0.66"
)
sk
:
connect
(
80
,
"192.168.0.66"
)
sk
:
send
(
"GET / HTTP/1.1\r\nHost:
1
192.168.0.66\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n"
)
sk
:
send
(
"GET / HTTP/1.1\r\nHost: 192.168.0.66\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n"
)
i2c
.
setup
(
0
,
1
,
0
,
i2c
.
SLOW
)
i2c
.
setup
(
0
,
1
,
0
,
i2c
.
SLOW
)
function
read_bmp
(
addr
)
i2c
.
start
(
0
)
i2c
.
address
(
0
,
119
,
i2c
.
RECEIVER
)
c
=
i2c
.
read
(
0
,
1
)
i2c
.
stop
(
0
)
print
(
string.byte
(
c
))
end
function
read_bmp
(
addr
)
i2c
.
start
(
0
)
i2c
.
address
(
0
,
119
,
i2c
.
RECEIVER
)
c
=
i2c
.
read
(
0
,
1
)
i2c
.
stop
(
0
)
print
(
string.byte
(
c
))
end
...
@@ -381,3 +381,141 @@ function TestDNSLeak()
...
@@ -381,3 +381,141 @@ function TestDNSLeak()
tmr
.
alarm
(
1
,
3000
,
0
,
function
()
print
(
"hack socket close, MEM: "
..
node
.
heap
())
c
:
close
()
end
)
-- socket timeout hack
tmr
.
alarm
(
1
,
3000
,
0
,
function
()
print
(
"hack socket close, MEM: "
..
node
.
heap
())
c
:
close
()
end
)
-- socket timeout hack
print
(
"MEM: "
..
node
.
heap
())
print
(
"MEM: "
..
node
.
heap
())
end
end
v
=
"abc%0D%0Adef"
print
(
string.gsub
(
v
,
"%%(%x%x)"
,
function
(
x
)
return
string.char
(
tonumber
(
x
,
16
))
end
))
function
ex
(
x
)
string.find
(
"abc%0Ddef"
,
"bc"
)
return
's'
end
string.gsub
(
"abc%0Ddef"
,
"%%(%x%x)"
,
ex
)
function
ex
(
x
)
string.char
(
35
)
return
's'
end
string.gsub
(
"abc%0Ddef"
,
"%%(%x%x)"
,
ex
)
print
(
"hello"
)
function
ex
(
x
)
string.lower
(
'Ab'
)
return
's'
end
string.gsub
(
"abc%0Ddef"
,
"%%(%x%x)"
,
ex
)
print
(
"hello"
)
v
=
"abc%0D%0Adef"
pcall
(
function
()
print
(
string.gsub
(
v
,
"%%(%x%x)"
,
function
(
x
)
return
string.char
(
tonumber
(
x
,
16
))
end
))
end
)
mosca
-
v
|
bunyan
m
=
mqtt
.
Client
()
m
:
connect
(
"192.168.18.88"
,
1883
)
topic
=
{}
topic
[
"/topic1"
]
=
0
topic
[
"/topic2"
]
=
0
m
:
subscribe
(
topic
,
function
(
m
)
print
(
"sub done"
)
end
)
m
:
on
(
"message"
,
function
(
m
,
t
,
pl
)
print
(
t
..
":"
)
if
pl
~=
nil
then
print
(
pl
)
end
end
)
m
:
publish
(
"/topic1"
,
"hello"
,
0
,
0
)
m
:
publish
(
"/topic3"
,
"hello"
,
0
,
0
)
m
:
publish
(
"/topic4"
,
"hello"
,
0
,
0
)
m
=
mqtt
.
Client
()
m
:
connect
(
"192.168.18.88"
,
1883
)
m
:
subscribe
(
"/topic1"
,
0
,
function
(
m
)
print
(
"sub done"
)
end
)
m
:
subscribe
(
"/topic2"
,
0
,
function
(
m
)
print
(
"sub done"
)
end
)
m
:
on
(
"message"
,
function
(
m
,
t
,
pl
)
print
(
t
..
":"
)
if
pl
~=
nil
then
print
(
pl
)
end
end
)
m
:
publish
(
"/topic1"
,
"hello"
,
0
,
0
)
m
:
publish
(
"/topic3"
,
"hello"
,
0
,
0
)
m
:
publish
(
"/topic4"
,
"hello"
,
0
,
0
)
m
:
publish
(
"/topic1"
,
"hello1"
,
0
,
0
)
m
:
publish
(
"/topic2"
,
"hello2"
,
0
,
0
)
m
:
publish
(
"/topic1"
,
"hello"
,
1
,
0
)
m
:
subscribe
(
"/topic3"
,
0
,
function
(
m
)
print
(
"sub done"
)
end
)
m
:
publish
(
"/topic3"
,
"hello3"
,
2
,
0
)
m
=
mqtt
.
Client
()
m
:
connect
(
"192.168.18.88"
,
1883
,
function
(
con
)
print
(
"connected hello"
)
end
)
m
=
mqtt
.
Client
()
m
:
on
(
"connect"
,
function
(
m
)
print
(
"connection"
)
end
)
m
:
connect
(
"192.168.18.88"
,
1883
)
m
:
on
(
"offline"
,
function
(
m
)
print
(
"disconnection"
)
end
)
m
=
mqtt
.
Client
()
m
:
on
(
"connect"
,
function
(
m
)
print
(
"connection "
..
node
.
heap
())
end
)
m
:
on
(
"offline"
,
function
(
conn
)
if
conn
==
nil
then
print
(
"conn is nil"
)
end
print
(
"Reconnect to broker..."
)
print
(
node
.
heap
())
conn
:
connect
(
"192.168.18.88"
,
1883
,
0
,
1
)
end
)
m
:
connect
(
"192.168.18.88"
,
1883
,
0
,
1
)
m
=
mqtt
.
Client
()
m
:
on
(
"connect"
,
function
(
m
)
print
(
"connection "
..
node
.
heap
())
end
)
m
:
on
(
"offline"
,
function
(
conn
)
if
conn
==
nil
then
print
(
"conn is nil"
)
end
print
(
"Reconnect to broker..."
)
print
(
node
.
heap
())
conn
:
connect
(
"192.168.18.88"
,
1883
)
end
)
m
:
connect
(
"192.168.18.88"
,
1883
)
m
:
close
()
m
=
mqtt
.
Client
()
m
:
connect
(
"192.168.18.88"
,
1883
)
m
:
on
(
"message"
,
function
(
m
,
t
,
pl
)
print
(
t
..
":"
)
if
pl
~=
nil
then
print
(
pl
)
end
end
)
m
:
subscribe
(
"/topic1"
,
0
,
function
(
m
)
print
(
"sub done"
)
end
)
m
:
publish
(
"/topic1"
,
"hello3"
,
2
,
0
)
m
:
publish
(
"/topic1"
,
"hello2"
,
2
,
0
)
m
:
publish
(
"/topic1"
,
"hello3"
,
0
,
0
)
m
:
publish
(
"/topic1"
,
"hello2"
,
2
,
0
)
m
:
subscribe
(
"/topic2"
,
2
,
function
(
m
)
print
(
"sub done"
)
end
)
m
:
publish
(
"/topic2"
,
"hello3"
,
0
,
0
)
m
:
publish
(
"/topic2"
,
"hello2"
,
2
,
0
)
m
=
mqtt
.
Client
()
m
:
on
(
"connect"
,
function
(
m
)
print
(
"connection "
..
node
.
heap
())
m
:
subscribe
(
"/topic1"
,
0
,
function
(
m
)
print
(
"sub done"
)
end
)
m
:
publish
(
"/topic1"
,
"hello3"
,
0
,
0
)
m
:
publish
(
"/topic1"
,
"hello2"
,
2
,
0
)
end
)
m
:
on
(
"offline"
,
function
(
conn
)
print
(
"disconnect to broker..."
)
print
(
node
.
heap
())
end
)
m
:
connect
(
"192.168.18.88"
,
1883
,
0
,
1
)
-- serout( pin, firstLevel, delay_table, [repeatNum] )
gpio
.
mode
(
1
,
gpio
.
OUTPUT
,
gpio
.
PULLUP
)
gpio
.
serout
(
1
,
1
,{
30
,
30
,
60
,
60
,
30
,
30
})
-- serial one byte, b10110010
gpio
.
serout
(
1
,
1
,{
30
,
70
},
8
)
-- serial 30% pwm 10k, lasts 8 cycles
gpio
.
serout
(
1
,
1
,{
3
,
7
},
8
)
-- serial 30% pwm 100k, lasts 8 cycles
gpio
.
serout
(
1
,
1
,{
0
,
0
},
8
)
-- serial 50% pwm as fast as possible, lasts 8 cycles
gpio
.
mode
(
1
,
gpio
.
OUTPUT
,
gpio
.
PULLUP
)
gpio
.
serout
(
1
,
0
,{
20
,
10
,
10
,
20
,
10
,
10
,
10
,
100
})
-- sim uart one byte 0x5A at about 100kbps
gpio
.
serout
(
1
,
1
,{
8
,
18
},
8
)
-- serial 30% pwm 38k, lasts 8 cycles
-- Lua: mqtt.Client(clientid, keepalive, user, pass)
-- test with cloudmqtt.com
m_dis
=
{}
function
dispatch
(
m
,
t
,
pl
)
if
pl
~=
nil
and
m_dis
[
t
]
then
m_dis
[
t
](
pl
)
end
end
function
topic1func
(
pl
)
print
(
"get1: "
..
pl
)
end
function
topic2func
(
pl
)
print
(
"get2: "
..
pl
)
end
m_dis
[
"/topic1"
]
=
topic1func
m_dis
[
"/topic2"
]
=
topic2func
m
=
mqtt
.
Client
(
"nodemcu1"
,
60
,
"test"
,
"test123"
)
m
:
on
(
"connect"
,
function
(
m
)
print
(
"connection "
..
node
.
heap
())
m
:
subscribe
(
"/topic1"
,
0
,
function
(
m
)
print
(
"sub done"
)
end
)
m
:
subscribe
(
"/topic2"
,
0
,
function
(
m
)
print
(
"sub done"
)
end
)
m
:
publish
(
"/topic1"
,
"hello"
,
0
,
0
)
m
:
publish
(
"/topic2"
,
"world"
,
0
,
0
)
end
)
m
:
on
(
"offline"
,
function
(
conn
)
print
(
"disconnect to broker..."
)
print
(
node
.
heap
())
end
)
m
:
on
(
"message"
,
dispatch
)
m
:
connect
(
"m11.cloudmqtt.com"
,
11214
,
0
,
1
)
-- Lua: mqtt:connect( host, port, secure, auto_reconnect, function(client) )
tmr
.
alarm
(
0
,
10000
,
1
,
function
()
local
pl
=
"time: "
..
tmr
.
time
()
m
:
publish
(
"/topic1"
,
pl
,
0
,
0
)
end
)
examples/init.lua
0 → 100644
View file @
4cd2b554
--init.lua, something like this
countdown
=
3
tmr
.
alarm
(
0
,
1000
,
1
,
function
()
print
(
countdown
)
countdown
=
countdown
-
1
if
countdown
<
1
then
tmr
.
stop
(
0
)
countdown
=
nil
local
s
,
err
if
file
.
open
(
"user.lc"
)
then
file
.
close
()
s
,
err
=
pcall
(
function
()
dofile
(
"user.lc"
)
end
)
else
s
,
err
=
pcall
(
function
()
dofile
(
"user.lua"
)
end
)
end
if
not
s
then
print
(
err
)
end
end
end
)
examples/user.lua
0 → 100644
View file @
4cd2b554
print
(
"hello NodeMCU"
)
ld/eagle.app.v6.ld
View file @
4cd2b554
...
@@ -5,7 +5,7 @@ MEMORY
...
@@ -5,7 +5,7 @@ MEMORY
dport0_0_seg : org = 0x3FF00000, len = 0x10
dport0_0_seg : org = 0x3FF00000, len = 0x10
dram0_0_seg : org = 0x3FFE8000, len = 0x14000
dram0_0_seg : org = 0x3FFE8000, len = 0x14000
iram1_0_seg : org = 0x40100000, len = 0x8000
iram1_0_seg : org = 0x40100000, len = 0x8000
irom0_0_seg : org = 0x40210000, len = 0x
5A
000
irom0_0_seg : org = 0x40210000, len = 0x
60
000
}
}
PHDRS
PHDRS
...
...
lua_examples/make_phone_call.lua
0 → 100644
View file @
4cd2b554
--[[
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
]]
--
-- Your access point's SSID and password
local
SSID
=
"xxxxxx"
local
SSID_PASSWORD
=
"xxxxxx"
-- configure ESP as a station
wifi
.
setmode
(
wifi
.
STATION
)
wifi
.
sta
.
config
(
SSID
,
SSID_PASSWORD
)
wifi
.
sta
.
autoconnect
(
1
)
local
TWILIO_ACCOUNT_SID
=
"xxxxxx"
local
TWILIO_TOKEN
=
"xxxxxx"
local
HOST
=
"iot-https-relay.appspot.com"
-- visit http://iot-https-relay.appspot.com/ to learn more about this service
-- Please be sure to understand the security issues of using this relay app and use at your own risk.
local
URI
=
"/twilio/Calls.json"
function
build_post_request
(
host
,
uri
,
data_table
)
local
data
=
""
for
param
,
value
in
pairs
(
data_table
)
do
data
=
data
..
param
..
"="
..
value
..
"&"
end
request
=
"POST "
..
uri
..
" HTTP/1.1\r\n"
..
"Host: "
..
host
..
"
\r\n
"
..
"Connection: close\r\n"
..
"Content-Type: application/x-www-form-urlencoded\r\n"
..
"Content-Length: "
..
string.len
(
data
)
..
"
\r\n
"
..
"
\r\n
"
..
data
print
(
request
)
return
request
end
local
function
display
(
sck
,
response
)
print
(
response
)
end
-- When using send_sms: the "from" number HAS to be your twilio number.
-- If you have a free twilio account the "to" number HAS to be your twilio verified number.
local
function
make_call
(
from
,
to
,
body
)
local
data
=
{
sid
=
TWILIO_ACCOUNT_SID
,
token
=
TWILIO_TOKEN
,
Body
=
string.gsub
(
body
,
" "
,
"+"
),
From
=
from
,
To
=
to
}
socket
=
net
.
createConnection
(
net
.
TCP
,
0
)
socket
:
on
(
"receive"
,
display
)
socket
:
connect
(
80
,
HOST
)
socket
:
on
(
"connection"
,
function
(
sck
)
local
post_request
=
build_post_request
(
HOST
,
URI
,
data
)
sck
:
send
(
post_request
)
end
)
end
function
check_wifi
()
local
ip
=
wifi
.
sta
.
getip
()
if
(
ip
==
nil
)
then
print
(
"Connecting..."
)
else
tmr
.
stop
(
0
)
print
(
"Connected to AP!"
)
print
(
ip
)
-- make a call with a voice message "your house is on fire"
make_call
(
"15558976687"
,
"1334856679"
,
"Your house is on fire!"
)
end
end
tmr
.
alarm
(
0
,
2000
,
1
,
check_wifi
)
lua_examples/mqtt/mqtt2cloud.lua
0 → 100644
View file @
4cd2b554
-- test with cloudmqtt.com
m_dis
=
{}
function
dispatch
(
m
,
t
,
pl
)
if
pl
~=
nil
and
m_dis
[
t
]
then
m_dis
[
t
](
m
,
pl
)
end
end
function
topic1func
(
m
,
pl
)
print
(
"get1: "
..
pl
)
end
function
topic2func
(
m
,
pl
)
print
(
"get2: "
..
pl
)
end
m_dis
[
"/topic1"
]
=
topic1func
m_dis
[
"/topic2"
]
=
topic2func
-- Lua: mqtt.Client(clientid, keepalive, user, pass)
m
=
mqtt
.
Client
(
"nodemcu1"
,
60
,
"test"
,
"test123"
)
m
:
on
(
"connect"
,
function
(
m
)
print
(
"connection "
..
node
.
heap
())
m
:
subscribe
(
"/topic1"
,
0
,
function
(
m
)
print
(
"sub done"
)
end
)
m
:
subscribe
(
"/topic2"
,
0
,
function
(
m
)
print
(
"sub done"
)
end
)
m
:
publish
(
"/topic1"
,
"hello"
,
0
,
0
)
m
:
publish
(
"/topic2"
,
"world"
,
0
,
0
)
end
)
m
:
on
(
"offline"
,
function
(
conn
)
print
(
"disconnect to broker..."
)
print
(
node
.
heap
())
end
)
m
:
on
(
"message"
,
dispatch
)
-- Lua: mqtt:connect( host, port, secure, auto_reconnect, function(client) )
m
:
connect
(
"m11.cloudmqtt.com"
,
11214
,
0
,
1
)
tmr
.
alarm
(
0
,
10000
,
1
,
function
()
local
pl
=
"time: "
..
tmr
.
time
()
m
:
publish
(
"/topic1"
,
pl
,
0
,
0
)
end
)
lua_examples/mqtt/mqtt_file.lua
0 → 100644
View file @
4cd2b554
-- test transfer files over mqtt.
m_dis
=
{}
function
dispatch
(
m
,
t
,
pl
)
if
pl
~=
nil
and
m_dis
[
t
]
then
m_dis
[
t
](
m
,
pl
)
end
end
function
pubfile
(
m
,
filename
)
file
.
close
()
file
.
open
(
filename
)
repeat
local
pl
=
file
.
read
(
1024
)
if
pl
then
m
:
publish
(
"/topic2"
,
pl
,
0
,
0
)
end
until
not
pl
file
.
close
()
end
-- payload(json): {"cmd":xxx,"content":xxx}
function
topic1func
(
m
,
pl
)
print
(
"get1: "
..
pl
)
local
pack
=
cjson
.
decode
(
pl
)
if
pack
.
content
then
if
pack
.
cmd
==
"open"
then
file
.
open
(
pack
.
content
,
"w+"
)
elseif
pack
.
cmd
==
"write"
then
file
.
write
(
pack
.
content
)
elseif
pack
.
cmd
==
"close"
then
file
.
close
()
elseif
pack
.
cmd
==
"remove"
then
file
.
remove
(
pack
.
content
)
elseif
pack
.
cmd
==
"run"
then
dofile
(
pack
.
content
)
elseif
pack
.
cmd
==
"read"
then
pubfile
(
m
,
pack
.
content
)
end
end
end
m_dis
[
"/topic1"
]
=
topic1func
-- Lua: mqtt.Client(clientid, keepalive, user, pass)
m
=
mqtt
.
Client
()
m
:
on
(
"connect"
,
function
(
m
)
print
(
"connection "
..
node
.
heap
())
m
:
subscribe
(
"/topic1"
,
0
,
function
(
m
)
print
(
"sub done"
)
end
)
end
)
m
:
on
(
"offline"
,
function
(
conn
)
print
(
"disconnect to broker..."
)
print
(
node
.
heap
())
end
)
m
:
on
(
"message"
,
dispatch
)
-- Lua: mqtt:connect( host, port, secure, auto_reconnect, function(client) )
m
:
connect
(
192
.
168
.
18
.
88
,
1883
,
0
,
1
)
-- usage:
-- another client(pc) subscribe to /topic2, will receive the test.lua content.
-- and publish below message to /topic1
-- {"cmd":"open","content":"test.lua"}
-- {"cmd":"write","content":"print([[hello world]])\n"}
-- {"cmd":"write","content":"print(\"hello2 world2\")\n"}
-- {"cmd":"write","content":"test.lua"}
-- {"cmd":"run","content":"test.lua"}
-- {"cmd":"read","content":"test.lua"}
lua_examples/send_text_message.lua
0 → 100644
View file @
4cd2b554
--[[
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
]]
--
-- Your access point's SSID and password
local
SSID
=
"xxxxxx"
local
SSID_PASSWORD
=
"xxxxxx"
-- configure ESP as a station
wifi
.
setmode
(
wifi
.
STATION
)
wifi
.
sta
.
config
(
SSID
,
SSID_PASSWORD
)
wifi
.
sta
.
autoconnect
(
1
)
local
TWILIO_ACCOUNT_SID
=
"xxxxxx"
local
TWILIO_TOKEN
=
"xxxxxx"
local
HOST
=
"iot-https-relay.appspot.com"
-- visit http://iot-https-relay.appspot.com/ to learn more about this service
-- Please be sure to understand the security issues of using this relay app and use at your own risk.
local
URI
=
"/twilio/Messages.json"
function
build_post_request
(
host
,
uri
,
data_table
)
local
data
=
""
for
param
,
value
in
pairs
(
data_table
)
do
data
=
data
..
param
..
"="
..
value
..
"&"
end
request
=
"POST "
..
uri
..
" HTTP/1.1\r\n"
..
"Host: "
..
host
..
"
\r\n
"
..
"Connection: close\r\n"
..
"Content-Type: application/x-www-form-urlencoded\r\n"
..
"Content-Length: "
..
string.len
(
data
)
..
"
\r\n
"
..
"
\r\n
"
..
data
print
(
request
)
return
request
end
local
function
display
(
sck
,
response
)
print
(
response
)
end
-- When using send_sms: the "from" number HAS to be your twilio number.
-- If you have a free twilio account the "to" number HAS to be your twilio verified number.
local
function
send_sms
(
from
,
to
,
body
)
local
data
=
{
sid
=
TWILIO_ACCOUNT_SID
,
token
=
TWILIO_TOKEN
,
Body
=
string.gsub
(
body
,
" "
,
"+"
),
From
=
from
,
To
=
to
}
socket
=
net
.
createConnection
(
net
.
TCP
,
0
)
socket
:
on
(
"receive"
,
display
)
socket
:
connect
(
80
,
HOST
)
socket
:
on
(
"connection"
,
function
(
sck
)
local
post_request
=
build_post_request
(
HOST
,
URI
,
data
)
sck
:
send
(
post_request
)
end
)
end
function
check_wifi
()
local
ip
=
wifi
.
sta
.
getip
()
if
(
ip
==
nil
)
then
print
(
"Connecting..."
)
else
tmr
.
stop
(
0
)
print
(
"Connected to AP!"
)
print
(
ip
)
-- send a text message with the text "Hello from your esp8266"
send_sms
(
"15558889944"
,
"15559998845"
,
"Hello from your ESP8266"
)
end
end
tmr
.
alarm
(
0
,
7000
,
1
,
check_wifi
)
lua_modules/dht
22
/README.md
→
lua_modules/dht
_lib
/README.md
View file @
4cd2b554
# DHT22 module
# DHTxx module
This module is compatible with DHT11, DHT21 and DHT22.
And is able to auto-select wheather you are using DHT11 or DHT2x
This module is compatible with DHT22 and DHT21.
Supports nodemcu with or without floating point.
No need to use a resistor to connect the pin data of DHT22 to ESP8266.
No need to use a resistor to connect the pin data of DHT22 to ESP8266.
## Example
##Integer Verison[When using DHT11, Float version is useless...]
### Example
```
lua
```
lua
PIN
=
4
-- data pin, GPIO2
PIN
=
4
-- data pin, GPIO2
dht22
=
require
(
"dht22"
)
DHT
=
require
(
"dht_lib"
)
dht22
.
read
(
PIN
)
t
=
dht22
.
getTemperature
()
DHT
.
read
(
PIN
)
h
=
dht22
.
getHumidity
()
t
=
DHT
.
getTemperature
()
h
=
DHT
.
getHumidity
()
if
h
==
nil
then
if
h
==
nil
then
print
(
"Error reading from DHT
22
"
)
print
(
"Error reading from DHT
xx
"
)
else
else
-- temperature in degrees Celsius and Farenheit
-- temperature in degrees Celsius and Farenheit
-- floating point and integer version:
print
(
"Temperature: "
..
((
t
-
(
t
%
10
))
/
10
)
..
"."
..
(
t
%
10
)
..
" deg C"
)
print
(
"Temperature: "
..
((
t
-
(
t
%
10
))
/
10
)
..
"."
..
(
t
%
10
)
..
" deg C"
)
-- only integer version:
print
(
"Temperature: "
..
(
9
*
t
/
50
+
32
)
..
"."
..
(
9
*
t
/
5
%
10
)
..
" deg F"
)
print
(
"Temperature: "
..
(
9
*
t
/
50
+
32
)
..
"."
..
(
9
*
t
/
5
%
10
)
..
" deg F"
)
-- only float point version:
print
(
"Temperature: "
..
(
9
*
t
/
50
+
32
)
..
" deg F"
)
-- humidity
-- humidity
-- floating point and integer version
print
(
"Humidity: "
..
((
h
-
(
h
%
10
))
/
10
)
..
"."
..
(
h
%
10
)
..
"%"
)
print
(
"Humidity: "
..
((
h
-
(
h
%
10
))
/
10
)
..
"."
..
(
h
%
10
)
..
"%"
)
end
end
-- release module
-- release module
dht22
=
nil
DHT
=
nil
package.loaded
[
"dht22"
]
=
nil
package.loaded
[
"dht_lib"
]
=
nil
```
##Float Verison
###Example
```
lua
PIN
=
4
-- data pin, GPIO2
DHT
=
require
(
"dht_lib"
)
DHT
.
read
(
PIN
)
t
=
DHT
.
getTemperature
()
h
=
DHT
.
getHumidity
()
if
h
==
nil
then
print
(
"Error reading from DHT11/22"
)
else
-- temperature in degrees Celsius and Farenheit
-- floating point and integer version:
print
(
"Temperature: "
..
(
t
/
10
)
..
" deg C"
)
print
(
"Temperature: "
..
(
9
*
t
/
50
+
32
)
..
" deg F"
)
-- humidity
print
(
"Humidity: "
..
(
h
/
10
)
..
"%"
)
end
-- release module
DHT
=
nil
package.loaded
[
"dht_lib"
]
=
nil
```
```
## Functions
## Functions
### read
read(pin)
Read humidity and temperature from DHT22.
###read
read(pin)
Read humidity and temperature from DHTxx(11,21,22...).
**Parameters:**
**Parameters:**
*
pin - ESP8266 pin connect to data pin
in DHT22
*
pin - ESP8266 pin connect to data pin
### getHumidity
### getHumidity
getHumidity()
getHumidity()
...
@@ -54,5 +85,5 @@ getTemperature()
...
@@ -54,5 +85,5 @@ getTemperature()
Returns the temperature of the last reading.
Returns the temperature of the last reading.
**Returns:**
**Returns:**
*
last temperature reading in
0.
1ºC
*
last temperature reading in
(dht22) 0.1ºC (dht11)
1ºC
*
lua_modules/dht
22/dht22
.lua
→
lua_modules/dht
_lib/dht_lib
.lua
View file @
4cd2b554
-- ***************************************************************************
-- ***************************************************************************
-- DHT22 module for ESP8266 with nodeMCU
-- DHT
xx(11,21,
22
)
module for ESP8266 with nodeMCU
--
--
-- Written by Javier Yanez
-- Written by Javier Yanez
mod by Martin
-- but based on a script of Pigs Fly from ESP8266.com forum
-- but based on a script of Pigs Fly from ESP8266.com forum
--
--
-- MIT license, http://opensource.org/licenses/MIT
-- MIT license, http://opensource.org/licenses/MIT
-- ***************************************************************************
-- ***************************************************************************
--Support list:
--DHT11 Tested
--DHT21 Not Test yet
--DHT22(AM2302) Tested
--AM2320 Not Test yet
--Output format-> Real temperature times 10(or DHT22 will miss it float part in Int Version)
--==========================Module Part======================
local
moduleName
=
...
local
moduleName
=
...
local
M
=
{}
local
M
=
{}
_G
[
moduleName
]
=
M
_G
[
moduleName
]
=
M
--==========================Local the UMI and TEMP===========
local
humidity
local
humidity
local
temperature
local
temperature
--==========================Local the bitStream==============
local
bitStream
=
{}
function
M
.
read
(
pin
)
---------------------------Read bitStream from DHTXX--------------------------
local
checksum
local
function
read
(
pin
)
local
checksumTest
local
bitlength
=
0
humidity
=
0
humidity
=
0
temperature
=
0
temperature
=
0
checksum
=
0
-- Use Markus Gritsch trick to speed up read/write on GPIO
-- Use Markus Gritsch trick to speed up read/write on GPIO
local
gpio_read
=
gpio
.
read
local
gpio_read
=
gpio
.
read
local
bitStream
=
{}
for
j
=
1
,
40
,
1
do
for
j
=
1
,
40
,
1
do
bitStream
[
j
]
=
0
bitStream
[
j
]
=
0
end
end
local
bitlength
=
0
-- Step 1: send out start signal to DHT22
-- Step 1: send out start signal to DHT22
gpio
.
mode
(
pin
,
gpio
.
OUTPUT
)
gpio
.
mode
(
pin
,
gpio
.
OUTPUT
)
...
@@ -39,7 +49,7 @@ function M.read(pin)
...
@@ -39,7 +49,7 @@ function M.read(pin)
gpio
.
write
(
pin
,
gpio
.
HIGH
)
gpio
.
write
(
pin
,
gpio
.
HIGH
)
gpio
.
mode
(
pin
,
gpio
.
INPUT
)
gpio
.
mode
(
pin
,
gpio
.
INPUT
)
-- Step 2:
DHT22 send response signal
-- Step 2:
Receive bitStream from DHT11/22
-- bus will always let up eventually, don't bother with timeout
-- bus will always let up eventually, don't bother with timeout
while
(
gpio_read
(
pin
)
==
0
)
do
end
while
(
gpio_read
(
pin
)
==
0
)
do
end
local
c
=
0
local
c
=
0
...
@@ -48,7 +58,7 @@ function M.read(pin)
...
@@ -48,7 +58,7 @@ function M.read(pin)
while
(
gpio_read
(
pin
)
==
0
)
do
end
while
(
gpio_read
(
pin
)
==
0
)
do
end
c
=
0
c
=
0
while
(
gpio_read
(
pin
)
==
1
and
c
<
500
)
do
c
=
c
+
1
end
while
(
gpio_read
(
pin
)
==
1
and
c
<
500
)
do
c
=
c
+
1
end
-- Step 3: DHT22 send data
-- Step 3: DHT22 send data
for
j
=
1
,
40
,
1
do
for
j
=
1
,
40
,
1
do
while
(
gpio_read
(
pin
)
==
1
and
bitlength
<
10
)
do
while
(
gpio_read
(
pin
)
==
1
and
bitlength
<
10
)
do
...
@@ -59,24 +69,80 @@ function M.read(pin)
...
@@ -59,24 +69,80 @@ function M.read(pin)
-- bus will always let up eventually, don't bother with timeout
-- bus will always let up eventually, don't bother with timeout
while
(
gpio_read
(
pin
)
==
0
)
do
end
while
(
gpio_read
(
pin
)
==
0
)
do
end
end
end
end
---------------------------Check out the data--------------------------
----Auto Select the DHT11/DHT22 By check the byte[1] && byte[3] -------
---------------Which is empty when using DHT11-------------------------
function
M
.
read
(
pin
)
read
(
pin
)
--DHT data acquired, process.
local
byte_0
=
0
for
i
=
1
,
16
,
1
do
local
byte_1
=
0
local
byte_2
=
0
local
byte_3
=
0
local
byte_4
=
0
for
i
=
1
,
8
,
1
do
-- Byte[0]
if
(
bitStream
[
i
]
>
3
)
then
if
(
bitStream
[
i
]
>
3
)
then
humidity
=
humidity
+
2
^
(
16
-
i
)
byte_0
=
byte_0
+
2
^
(
8
-
i
)
end
end
end
end
for
i
=
1
,
16
,
1
do
if
(
bitStream
[
i
+
16
]
>
3
)
then
for
i
=
1
,
8
,
1
do
-- Byte[1]
temperature
=
temperature
+
2
^
(
16
-
i
)
if
(
bitStream
[
i
+
8
]
>
3
)
then
byte_1
=
byte_1
+
2
^
(
8
-
i
)
end
end
end
end
for
i
=
1
,
8
,
1
do
if
(
bitStream
[
i
+
32
]
>
3
)
then
for
i
=
1
,
8
,
1
do
-- Byte[2]
checksum
=
checksum
+
2
^
(
8
-
i
)
if
(
bitStream
[
i
+
16
]
>
3
)
then
byte_2
=
byte_2
+
2
^
(
8
-
i
)
end
end
end
end
for
i
=
1
,
8
,
1
do
-- Byte[3]
if
(
bitStream
[
i
+
24
]
>
3
)
then
byte_2
=
byte_2
+
2
^
(
8
-
i
)
end
end
for
i
=
1
,
8
,
1
do
-- Byte[4]
if
(
bitStream
[
i
+
32
]
>
3
)
then
byte_4
=
byte_4
+
2
^
(
8
-
i
)
end
end
if
byte_1
==
0
and
byte_3
==
0
then
---------------------------Convert the bitStream into Number through DHT11's Way--------------------------
--As for DHT11 40Bit is consisit of 5Bytes
--First byte->Humidity Data's Int part
--Sencond byte->Humidity Data's Float Part(Which should be empty)
--Third byte->Temp Data;s Intpart
--Forth byte->Temp Data's Float Part(Which should be empty)
--Fifth byte->SUM Byte, Humi+Temp
if
(
byte_4
~=
byte_0
+
byte_2
)
then
humidity
=
nil
temperature
=
nil
else
humidity
=
byte_0
*
10
-- In order to universe with the DHT22
temperature
=
byte_2
*
10
end
else
---------------------------Convert the bitStream into Number through DHT22's Way--------------------------
--As for DHT22 40Bit is consisit of 5Bytes
--First byte->Humidity Data's High Bit
--Sencond byte->Humidity Data's Low Bit(And if over 0x8000, use complement)
--Third byte->Temp Data's High Bit
--Forth byte->Temp Data's Low Bit
--Fifth byte->SUM Byte
humidity
=
byte_0
*
256
+
byte_1
temperature
=
byte_2
*
256
+
byte_3
checksum
=
byte_4
checksumTest
=
(
bit
.
band
(
humidity
,
0xFF
)
+
bit
.
rshift
(
humidity
,
8
)
+
bit
.
band
(
temperature
,
0xFF
)
+
bit
.
rshift
(
temperature
,
8
))
checksumTest
=
(
bit
.
band
(
humidity
,
0xFF
)
+
bit
.
rshift
(
humidity
,
8
)
+
bit
.
band
(
temperature
,
0xFF
)
+
bit
.
rshift
(
temperature
,
8
))
checksumTest
=
bit
.
band
(
checksumTest
,
0xFF
)
checksumTest
=
bit
.
band
(
checksumTest
,
0xFF
)
...
@@ -89,7 +155,17 @@ function M.read(pin)
...
@@ -89,7 +155,17 @@ function M.read(pin)
if
(
checksumTest
-
checksum
>=
1
)
or
(
checksum
-
checksumTest
>=
1
)
then
if
(
checksumTest
-
checksum
>=
1
)
or
(
checksum
-
checksumTest
>=
1
)
then
humidity
=
nil
humidity
=
nil
end
end
end
byte_0
=
nil
byte_1
=
nil
byte_2
=
nil
byte_3
=
nil
byte_4
=
nil
end
end
--------------API for geting the data out------------------
function
M
.
getTemperature
()
function
M
.
getTemperature
()
return
temperature
return
temperature
...
@@ -98,5 +174,5 @@ end
...
@@ -98,5 +174,5 @@ end
function
M
.
getHumidity
()
function
M
.
getHumidity
()
return
humidity
return
humidity
end
end
-------------Return Index------------------------------------
return
M
return
M
lua_modules/yeelink/yeelink_lib.lua
0 → 100644
View file @
4cd2b554
-- ***************************************************************************
-- Yeelink Updata Libiary
--
-- Written by Martin
-- but based on a script of zhouxu_o from bbs.nodemcu.com
--
-- MIT license, http://opensource.org/licenses/MIT
-- ***************************************************************************
if
wifi
.
sta
.
getip
()
==
nil
then
print
(
"Please Connect WIFI First"
)
return
nil
end
--==========================Module Part======================
local
moduleName
=
...
local
M
=
{}
_G
[
moduleName
]
=
M
--=========================Local Args=======================
local
dns
=
"0.0.0.0"
local
device
=
""
local
sensor
=
""
local
apikey
=
""
--================================
local
debug
=
true
--<<<<<<<<<<<<< Don't forget to "false" it before using
--================================
local
sk
=
net
.
createConnection
(
net
.
TCP
,
0
)
local
datapoint
=
0
--====DNS the yeelink ip advance(in order to save RAM)=====
sk
:
dns
(
"api.yeelink.net"
,
function
(
conn
,
ip
)
dns
=
ip
print
(
"DNS YEELINK OK... IP: "
..
dns
)
end
)
--========Set the init function===========
--device->number
--sensor->number
-- apikey must be -> string <-
-- e.g. xxx.init(00000,00000,"123j12b3jkb12k4b23bv54i2b5b3o4")
--========================================
function
M
.
init
(
_device
,
_sensor
,
_apikey
)
device
=
tostring
(
_device
)
sensor
=
tostring
(
_sensor
)
apikey
=
_apikey
if
dns
==
"0.0.0.0"
then
return
false
else
return
dns
end
end
--=====Update to Yeelink Sever(At least 10s per sencods))=====
-- datapoint->number
--
--e.g. xxx.update(233.333)
--============================================================
function
M
.
update
(
_datapoint
)
datapoint
=
tostring
(
_datapoint
)
sk
:
on
(
"connection"
,
function
(
conn
)
print
(
"connect OK..."
)
local
a
=
[[{"value":]]
local
b
=
[[}]]
local
st
=
a
..
datapoint
..
b
sk
:
send
(
"POST /v1.0/device/"
..
device
..
"/sensor/"
..
sensor
..
"/datapoints HTTP/1.1\r\n"
..
"Host: www.yeelink.net\r\n"
..
"Content-Length: "
..
string.len
(
st
)
..
"
\r\n
"
--the length of json is important
..
"Content-Type: application/x-www-form-urlencoded\r\n"
..
"U-ApiKey:"
..
apikey
..
"
\r\n
"
..
"Cache-Control: no-cache\r\n\r\n"
..
st
..
"
\r\n
"
)
end
)
sk
:
on
(
"receive"
,
function
(
sck
,
content
)
if
debug
then
print
(
"
\r\n
"
..
content
..
"
\r\n
"
)
else
print
(
"Date Receive"
)
end
end
)
sk
:
connect
(
80
,
dns
)
end
--================end==========================
return
M
tools/esptool.py
View file @
4cd2b554
...
@@ -41,7 +41,7 @@ class ESPROM:
...
@@ -41,7 +41,7 @@ class ESPROM:
# Maximum block sized for RAM and Flash writes, respectively.
# Maximum block sized for RAM and Flash writes, respectively.
ESP_RAM_BLOCK
=
0x1800
ESP_RAM_BLOCK
=
0x1800
ESP_FLASH_BLOCK
=
0x
1
00
ESP_FLASH_BLOCK
=
0x
4
00
# Default baudrate. The ROM auto-bauds, so we can use more or less whatever we want.
# Default baudrate. The ROM auto-bauds, so we can use more or less whatever we want.
ESP_ROM_BAUD
=
115200
ESP_ROM_BAUD
=
115200
...
@@ -56,6 +56,12 @@ class ESPROM:
...
@@ -56,6 +56,12 @@ class ESPROM:
ESP_OTP_MAC0
=
0x3ff00050
ESP_OTP_MAC0
=
0x3ff00050
ESP_OTP_MAC1
=
0x3ff00054
ESP_OTP_MAC1
=
0x3ff00054
# Sflash stub: an assembly routine to read from spi flash and send to host
SFLASH_STUB
=
"
\x80\x3c\x00\x40\x1c\x4b\x00\x40\x21\x11\x00\x40\x00\x80
"
\
"
\xfe\x3f\xc1\xfb\xff\xd1\xf8\xff\x2d\x0d\x31\xfd\xff\x41\xf7\xff\x4a
"
\
"
\xdd\x51\xf9\xff\xc0\x05\x00\x21\xf9\xff\x31\xf3\xff\x41\xf5\xff\xc0
"
\
"
\x04\x00\x0b\xcc\x56\xec\xfd\x06\xff\xff\x00\x00
"
def
__init__
(
self
,
port
=
0
,
baud
=
ESP_ROM_BAUD
):
def
__init__
(
self
,
port
=
0
,
baud
=
ESP_ROM_BAUD
):
self
.
_port
=
serial
.
Serial
(
port
,
baud
)
self
.
_port
=
serial
.
Serial
(
port
,
baud
)
...
@@ -78,15 +84,7 @@ class ESPROM:
...
@@ -78,15 +84,7 @@ class ESPROM:
""" Write bytes to the serial port while performing SLIP escaping """
""" Write bytes to the serial port while performing SLIP escaping """
def
write
(
self
,
packet
):
def
write
(
self
,
packet
):
buf
=
'
\xc0
'
buf
=
'
\xc0
'
+
(
packet
.
replace
(
'
\xdb
'
,
'
\xdb\xdd
'
).
replace
(
'
\xc0
'
,
'
\xdb\xdc
'
))
+
'
\xc0
'
for
b
in
packet
:
if
b
==
'
\xc0
'
:
buf
+=
'
\xdb\xdc
'
elif
b
==
'
\xdb
'
:
buf
+=
'
\xdb\xdd
'
else
:
buf
+=
b
buf
+=
'
\xc0
'
self
.
_port
.
write
(
buf
)
self
.
_port
.
write
(
buf
)
""" Calculate checksum of a blob, as it is defined by the ROM """
""" Calculate checksum of a blob, as it is defined by the ROM """
...
@@ -132,11 +130,25 @@ class ESPROM:
...
@@ -132,11 +130,25 @@ class ESPROM:
# RTS = CH_PD (i.e reset)
# RTS = CH_PD (i.e reset)
# DTR = GPIO0
# DTR = GPIO0
# self._port.setRTS(True)
# self._port.setDTR(True)
# self._port.setRTS(False)
# time.sleep(0.1)
# self._port.setDTR(False)
# NodeMCU devkit
self
.
_port
.
setRTS
(
True
)
self
.
_port
.
setRTS
(
True
)
self
.
_port
.
setDTR
(
True
)
self
.
_port
.
setDTR
(
True
)
self
.
_port
.
setRTS
(
False
)
time
.
sleep
(
0.1
)
time
.
sleep
(
0.1
)
self
.
_port
.
setRTS
(
False
)
self
.
_port
.
setDTR
(
False
)
self
.
_port
.
setDTR
(
False
)
time
.
sleep
(
0.1
)
self
.
_port
.
setRTS
(
True
)
time
.
sleep
(
0.1
)
self
.
_port
.
setDTR
(
True
)
self
.
_port
.
setRTS
(
False
)
time
.
sleep
(
0.3
)
self
.
_port
.
setDTR
(
True
)
self
.
_port
.
timeout
=
0.5
self
.
_port
.
timeout
=
0.5
for
i
in
xrange
(
10
):
for
i
in
xrange
(
10
):
...
@@ -209,16 +221,78 @@ class ESPROM:
...
@@ -209,16 +221,78 @@ class ESPROM:
self
.
flash_begin
(
0
,
0
)
self
.
flash_begin
(
0
,
0
)
self
.
flash_finish
(
reboot
)
self
.
flash_finish
(
reboot
)
""" Read MAC from OTP ROM """
def
read_mac
(
self
):
mac0
=
esp
.
read_reg
(
esp
.
ESP_OTP_MAC0
)
mac1
=
esp
.
read_reg
(
esp
.
ESP_OTP_MAC1
)
if
((
mac1
>>
16
)
&
0xff
)
==
0
:
oui
=
(
0x18
,
0xfe
,
0x34
)
elif
((
mac1
>>
16
)
&
0xff
)
==
1
:
oui
=
(
0xac
,
0xd0
,
0x74
)
else
:
raise
Exception
(
"Unknown OUI"
)
return
oui
+
((
mac1
>>
8
)
&
0xff
,
mac1
&
0xff
,
(
mac0
>>
24
)
&
0xff
)
""" Read SPI flash manufacturer and device id """
def
flash_id
(
self
):
self
.
flash_begin
(
0
,
0
)
self
.
write_reg
(
0x60000240
,
0x0
,
0xffffffff
)
self
.
write_reg
(
0x60000200
,
0x10000000
,
0xffffffff
)
flash_id
=
esp
.
read_reg
(
0x60000240
)
self
.
flash_finish
(
False
)
return
flash_id
""" Read SPI flash """
def
flash_read
(
self
,
offset
,
size
,
count
=
1
):
# Create a custom stub
stub
=
struct
.
pack
(
'<III'
,
offset
,
size
,
count
)
+
self
.
SFLASH_STUB
# Trick ROM to initialize SFlash
self
.
flash_begin
(
0
,
0
)
# Download stub
self
.
mem_begin
(
len
(
stub
),
1
,
len
(
stub
),
0x40100000
)
self
.
mem_block
(
stub
,
0
)
self
.
mem_finish
(
0x4010001c
)
# Fetch the data
data
=
''
for
_
in
xrange
(
count
):
if
self
.
_port
.
read
(
1
)
!=
'
\xc0
'
:
raise
Exception
(
'Invalid head of packet (sflash read)'
)
data
+=
self
.
read
(
size
)
if
self
.
_port
.
read
(
1
)
!=
chr
(
0xc0
):
raise
Exception
(
'Invalid end of packet (sflash read)'
)
return
data
""" Perform a chip erase of SPI flash """
def
flash_erase
(
self
):
# Trick ROM to initialize SFlash
self
.
flash_begin
(
0
,
0
)
# This is hacky: we don't have a custom stub, instead we trick
# the bootloader to jump to the SPIEraseChip() routine and then halt/crash
# when it tries to boot an unconfigured system.
self
.
mem_begin
(
0
,
0
,
0
,
0x40100000
)
self
.
mem_finish
(
0x40004984
)
# Yup - there's no good way to detect if we succeeded.
# It it on the other hand unlikely to fail.
class
ESPFirmwareImage
:
class
ESPFirmwareImage
:
def
__init__
(
self
,
filename
=
None
):
def
__init__
(
self
,
filename
=
None
):
self
.
segments
=
[]
self
.
segments
=
[]
self
.
entrypoint
=
0
self
.
entrypoint
=
0
self
.
flash_mode
=
0
self
.
flash_size_freq
=
0
if
filename
is
not
None
:
if
filename
is
not
None
:
f
=
file
(
filename
,
'rb'
)
f
=
file
(
filename
,
'rb'
)
(
magic
,
segments
,
_
,
_
,
self
.
entrypoint
)
=
struct
.
unpack
(
'<BBBBI'
,
f
.
read
(
8
))
(
magic
,
segments
,
self
.
flash_mode
,
self
.
flash_size_freq
,
self
.
entrypoint
)
=
struct
.
unpack
(
'<BBBBI'
,
f
.
read
(
8
))
# some sanity check
# some sanity check
if
magic
!=
ESPROM
.
ESP_IMAGE_MAGIC
or
segments
>
16
:
if
magic
!=
ESPROM
.
ESP_IMAGE_MAGIC
or
segments
>
16
:
...
@@ -246,7 +320,8 @@ class ESPFirmwareImage:
...
@@ -246,7 +320,8 @@ class ESPFirmwareImage:
def
save
(
self
,
filename
):
def
save
(
self
,
filename
):
f
=
file
(
filename
,
'wb'
)
f
=
file
(
filename
,
'wb'
)
f
.
write
(
struct
.
pack
(
'<BBBBI'
,
ESPROM
.
ESP_IMAGE_MAGIC
,
len
(
self
.
segments
),
0
,
0
,
self
.
entrypoint
))
f
.
write
(
struct
.
pack
(
'<BBBBI'
,
ESPROM
.
ESP_IMAGE_MAGIC
,
len
(
self
.
segments
),
self
.
flash_mode
,
self
.
flash_size_freq
,
self
.
entrypoint
))
checksum
=
ESPROM
.
ESP_CHECKSUM_MAGIC
checksum
=
ESPROM
.
ESP_CHECKSUM_MAGIC
for
(
offset
,
size
,
data
)
in
self
.
segments
:
for
(
offset
,
size
,
data
)
in
self
.
segments
:
...
@@ -346,6 +421,12 @@ if __name__ == '__main__':
...
@@ -346,6 +421,12 @@ if __name__ == '__main__':
'write_flash'
,
'write_flash'
,
help
=
'Write a binary blob to flash'
)
help
=
'Write a binary blob to flash'
)
parser_write_flash
.
add_argument
(
'addr_filename'
,
nargs
=
'+'
,
help
=
'Address and binary file to write there, separated by space'
)
parser_write_flash
.
add_argument
(
'addr_filename'
,
nargs
=
'+'
,
help
=
'Address and binary file to write there, separated by space'
)
parser_write_flash
.
add_argument
(
'--flash_freq'
,
'-ff'
,
help
=
'SPI Flash frequency'
,
choices
=
[
'40m'
,
'26m'
,
'20m'
,
'80m'
],
default
=
'40m'
)
parser_write_flash
.
add_argument
(
'--flash_mode'
,
'-fm'
,
help
=
'SPI Flash mode'
,
choices
=
[
'qio'
,
'qout'
,
'dio'
,
'dout'
],
default
=
'qio'
)
parser_write_flash
.
add_argument
(
'--flash_size'
,
'-fs'
,
help
=
'SPI Flash size in Mbit'
,
choices
=
[
'4m'
,
'2m'
,
'8m'
,
'16m'
,
'32m'
],
default
=
'4m'
)
parser_run
=
subparsers
.
add_parser
(
parser_run
=
subparsers
.
add_parser
(
'run'
,
'run'
,
...
@@ -369,11 +450,32 @@ if __name__ == '__main__':
...
@@ -369,11 +450,32 @@ if __name__ == '__main__':
help
=
'Create an application image from ELF file'
)
help
=
'Create an application image from ELF file'
)
parser_elf2image
.
add_argument
(
'input'
,
help
=
'Input ELF file'
)
parser_elf2image
.
add_argument
(
'input'
,
help
=
'Input ELF file'
)
parser_elf2image
.
add_argument
(
'--output'
,
'-o'
,
help
=
'Output filename prefix'
,
type
=
str
)
parser_elf2image
.
add_argument
(
'--output'
,
'-o'
,
help
=
'Output filename prefix'
,
type
=
str
)
parser_elf2image
.
add_argument
(
'--flash_freq'
,
'-ff'
,
help
=
'SPI Flash frequency'
,
choices
=
[
'40m'
,
'26m'
,
'20m'
,
'80m'
],
default
=
'40m'
)
parser_elf2image
.
add_argument
(
'--flash_mode'
,
'-fm'
,
help
=
'SPI Flash mode'
,
choices
=
[
'qio'
,
'qout'
,
'dio'
,
'dout'
],
default
=
'qio'
)
parser_elf2image
.
add_argument
(
'--flash_size'
,
'-fs'
,
help
=
'SPI Flash size in Mbit'
,
choices
=
[
'4m'
,
'2m'
,
'8m'
,
'16m'
,
'32m'
],
default
=
'4m'
)
parser_read_mac
=
subparsers
.
add_parser
(
parser_read_mac
=
subparsers
.
add_parser
(
'read_mac'
,
'read_mac'
,
help
=
'Read MAC address from OTP ROM'
)
help
=
'Read MAC address from OTP ROM'
)
parser_flash_id
=
subparsers
.
add_parser
(
'flash_id'
,
help
=
'Read SPI flash manufacturer and device ID'
)
parser_read_flash
=
subparsers
.
add_parser
(
'read_flash'
,
help
=
'Read SPI flash content'
)
parser_read_flash
.
add_argument
(
'address'
,
help
=
'Start address'
,
type
=
arg_auto_int
)
parser_read_flash
.
add_argument
(
'size'
,
help
=
'Size of region to dump'
,
type
=
arg_auto_int
)
parser_read_flash
.
add_argument
(
'filename'
,
help
=
'Name of binary dump'
)
parser_erase_flash
=
subparsers
.
add_parser
(
'erase_flash'
,
help
=
'Perform Chip Erase on SPI flash'
)
args
=
parser
.
parse_args
()
args
=
parser
.
parse_args
()
# Create the ESPROM connection object, if needed
# Create the ESPROM connection object, if needed
...
@@ -421,6 +523,12 @@ if __name__ == '__main__':
...
@@ -421,6 +523,12 @@ if __name__ == '__main__':
elif
args
.
operation
==
'write_flash'
:
elif
args
.
operation
==
'write_flash'
:
assert
len
(
args
.
addr_filename
)
%
2
==
0
assert
len
(
args
.
addr_filename
)
%
2
==
0
flash_mode
=
{
'qio'
:
0
,
'qout'
:
1
,
'dio'
:
2
,
'dout'
:
3
}[
args
.
flash_mode
]
flash_size_freq
=
{
'4m'
:
0x00
,
'2m'
:
0x10
,
'8m'
:
0x20
,
'16m'
:
0x30
,
'32m'
:
0x40
}[
args
.
flash_size
]
flash_size_freq
+=
{
'40m'
:
0
,
'26m'
:
1
,
'20m'
:
2
,
'80m'
:
0xf
}[
args
.
flash_freq
]
flash_info
=
struct
.
pack
(
'BB'
,
flash_mode
,
flash_size_freq
)
while
args
.
addr_filename
:
while
args
.
addr_filename
:
address
=
int
(
args
.
addr_filename
[
0
],
0
)
address
=
int
(
args
.
addr_filename
[
0
],
0
)
filename
=
args
.
addr_filename
[
1
]
filename
=
args
.
addr_filename
[
1
]
...
@@ -434,7 +542,11 @@ if __name__ == '__main__':
...
@@ -434,7 +542,11 @@ if __name__ == '__main__':
print
'
\r
Writing at 0x%08x... (%d %%)'
%
(
address
+
seq
*
esp
.
ESP_FLASH_BLOCK
,
100
*
(
seq
+
1
)
/
blocks
),
print
'
\r
Writing at 0x%08x... (%d %%)'
%
(
address
+
seq
*
esp
.
ESP_FLASH_BLOCK
,
100
*
(
seq
+
1
)
/
blocks
),
sys
.
stdout
.
flush
()
sys
.
stdout
.
flush
()
block
=
image
[
0
:
esp
.
ESP_FLASH_BLOCK
]
block
=
image
[
0
:
esp
.
ESP_FLASH_BLOCK
]
block
=
block
+
'
\xe0
'
*
(
esp
.
ESP_FLASH_BLOCK
-
len
(
block
))
# Fix sflash config data
if
address
==
0
and
seq
==
0
and
block
[
0
]
==
'
\xe9
'
:
block
=
block
[
0
:
2
]
+
flash_info
+
block
[
4
:]
# Pad the last block
block
=
block
+
'
\xff
'
*
(
esp
.
ESP_FLASH_BLOCK
-
len
(
block
))
esp
.
flash_block
(
block
,
seq
)
esp
.
flash_block
(
block
,
seq
)
image
=
image
[
esp
.
ESP_FLASH_BLOCK
:]
image
=
image
[
esp
.
ESP_FLASH_BLOCK
:]
seq
+=
1
seq
+=
1
...
@@ -478,6 +590,11 @@ if __name__ == '__main__':
...
@@ -478,6 +590,11 @@ if __name__ == '__main__':
for
section
,
start
in
((
".text"
,
"_text_start"
),
(
".data"
,
"_data_start"
),
(
".rodata"
,
"_rodata_start"
)):
for
section
,
start
in
((
".text"
,
"_text_start"
),
(
".data"
,
"_data_start"
),
(
".rodata"
,
"_rodata_start"
)):
data
=
e
.
load_section
(
section
)
data
=
e
.
load_section
(
section
)
image
.
add_segment
(
e
.
get_symbol_addr
(
start
),
data
)
image
.
add_segment
(
e
.
get_symbol_addr
(
start
),
data
)
image
.
flash_mode
=
{
'qio'
:
0
,
'qout'
:
1
,
'dio'
:
2
,
'dout'
:
3
}[
args
.
flash_mode
]
image
.
flash_size_freq
=
{
'4m'
:
0x00
,
'2m'
:
0x10
,
'8m'
:
0x20
,
'16m'
:
0x30
,
'32m'
:
0x40
}[
args
.
flash_size
]
image
.
flash_size_freq
+=
{
'40m'
:
0
,
'26m'
:
1
,
'20m'
:
2
,
'80m'
:
0xf
}[
args
.
flash_freq
]
image
.
save
(
args
.
output
+
"0x00000.bin"
)
image
.
save
(
args
.
output
+
"0x00000.bin"
)
data
=
e
.
load_section
(
".irom0.text"
)
data
=
e
.
load_section
(
".irom0.text"
)
off
=
e
.
get_symbol_addr
(
"_irom0_text_start"
)
-
0x40200000
off
=
e
.
get_symbol_addr
(
"_irom0_text_start"
)
-
0x40200000
...
@@ -487,6 +604,17 @@ if __name__ == '__main__':
...
@@ -487,6 +604,17 @@ if __name__ == '__main__':
f
.
close
()
f
.
close
()
elif
args
.
operation
==
'read_mac'
:
elif
args
.
operation
==
'read_mac'
:
mac0
=
esp
.
read_reg
(
esp
.
ESP_OTP_MAC0
)
mac
=
esp
.
read_mac
()
mac1
=
esp
.
read_reg
(
esp
.
ESP_OTP_MAC1
)
print
'MAC: %s'
%
':'
.
join
(
map
(
lambda
x
:
'%02x'
%
x
,
mac
))
print
'MAC: 18:fe:34:%02x:%02x:%02x'
%
((
mac1
>>
8
)
&
0xff
,
mac1
&
0xff
,
(
mac0
>>
24
)
&
0xff
)
elif
args
.
operation
==
'flash_id'
:
flash_id
=
esp
.
flash_id
()
print
'Manufacturer: %02x'
%
(
flash_id
&
0xff
)
print
'Device: %02x%02x'
%
((
flash_id
>>
8
)
&
0xff
,
(
flash_id
>>
16
)
&
0xff
)
elif
args
.
operation
==
'read_flash'
:
print
'Please wait...'
file
(
args
.
filename
,
'wb'
).
write
(
esp
.
flash_read
(
args
.
address
,
1024
,
int
(
math
.
ceil
(
args
.
size
/
1024.
)))[:
args
.
size
])
elif
args
.
operation
==
'erase_flash'
:
esp
.
flash_erase
()
Prev
1
2
Next
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment