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
12a546ce
Commit
12a546ce
authored
Feb 03, 2015
by
devsaurus
Browse files
Merge remote-tracking branch 'upstream/master'
parents
6dfb82db
a1dc023c
Changes
7
Hide whitespace changes
Inline
Side-by-side
README.md
View file @
12a546ce
...
@@ -220,7 +220,8 @@ m:connect("192.168.11.118", 1880, 0, function(conn) print("connected") end)
...
@@ -220,7 +220,8 @@ m:connect("192.168.11.118", 1880, 0, function(conn) print("connected") end)
-- subscribe topic with qos = 0
-- subscribe topic with qos = 0
m
:
subscribe
(
"/topic"
,
0
,
function
(
conn
)
print
(
"subscribe success"
)
end
)
m
:
subscribe
(
"/topic"
,
0
,
function
(
conn
)
print
(
"subscribe success"
)
end
)
-- or subscribe multiple topic (topic/0, qos = 0; topic/1, qos = 1; topic2 , qos = 2)
-- m:subscribe({["topic/0"]=0,["topic/1"]=1,topic2=2}, function(conn) print("subscribe success") end)
-- publish a message with data = hello, QoS = 0, retain = 0
-- publish a message with data = hello, QoS = 0, retain = 0
m
:
publish
(
"/topic"
,
"hello"
,
0
,
0
,
function
(
conn
)
print
(
"sent"
)
end
)
m
:
publish
(
"/topic"
,
"hello"
,
0
,
0
,
function
(
conn
)
print
(
"sent"
)
end
)
...
...
app/include/driver/uart.h
View file @
12a546ce
...
@@ -33,6 +33,9 @@ typedef enum {
...
@@ -33,6 +33,9 @@ typedef enum {
}
UartExistParity
;
}
UartExistParity
;
typedef
enum
{
typedef
enum
{
BIT_RATE_1200
=
1200
,
BIT_RATE_2400
=
2400
,
BIT_RATE_4800
=
4800
,
BIT_RATE_9600
=
9600
,
BIT_RATE_9600
=
9600
,
BIT_RATE_19200
=
19200
,
BIT_RATE_19200
=
19200
,
BIT_RATE_38400
=
38400
,
BIT_RATE_38400
=
38400
,
...
...
app/modules/mqtt.c
View file @
12a546ce
...
@@ -529,23 +529,35 @@ static int mqtt_delete( lua_State* L )
...
@@ -529,23 +529,35 @@ static int mqtt_delete( lua_State* L )
}
}
if
(
mud
->
connect_info
.
will_topic
){
if
(
mud
->
connect_info
.
will_topic
){
c_free
(
mud
->
connect_info
.
client_id
);
c_free
(
mud
->
connect_info
.
will_topic
);
mud
->
connect_info
.
will_topic
=
NULL
;
}
}
if
(
mud
->
connect_info
.
will_message
){
if
(
mud
->
connect_info
.
will_message
){
c_free
(
mud
->
connect_info
.
will_message
);
c_free
(
mud
->
connect_info
.
will_message
);
mud
->
connect_info
.
will_message
=
NULL
;
}
}
if
(
mud
->
connect_info
.
client_id
)
if
(
mud
->
connect_info
.
client_id
)
{
c_free
(
mud
->
connect_info
.
client_id
);
c_free
(
mud
->
connect_info
.
client_id
);
if
(
mud
->
connect_info
.
username
)
mud
->
connect_info
.
client_id
=
NULL
;
}
if
(
mud
->
connect_info
.
username
){
c_free
(
mud
->
connect_info
.
username
);
c_free
(
mud
->
connect_info
.
username
);
if
(
mud
->
connect_info
.
password
)
mud
->
connect_info
.
username
=
NULL
;
}
if
(
mud
->
connect_info
.
password
){
c_free
(
mud
->
connect_info
.
password
);
c_free
(
mud
->
connect_info
.
password
);
if
(
mud
->
mqtt_state
.
in_buffer
)
mud
->
connect_info
.
password
=
NULL
;
}
if
(
mud
->
mqtt_state
.
in_buffer
){
c_free
(
mud
->
mqtt_state
.
in_buffer
);
c_free
(
mud
->
mqtt_state
.
in_buffer
);
if
(
mud
->
mqtt_state
.
out_buffer
)
mud
->
mqtt_state
.
in_buffer
=
NULL
;
}
if
(
mud
->
mqtt_state
.
out_buffer
){
c_free
(
mud
->
mqtt_state
.
out_buffer
);
c_free
(
mud
->
mqtt_state
.
out_buffer
);
mud
->
mqtt_state
.
out_buffer
=
NULL
;
}
// free (unref) callback ref
// free (unref) callback ref
if
(
LUA_NOREF
!=
mud
->
cb_connect_ref
){
if
(
LUA_NOREF
!=
mud
->
cb_connect_ref
){
...
@@ -879,52 +891,111 @@ static int mqtt_socket_on( lua_State* L )
...
@@ -879,52 +891,111 @@ static int mqtt_socket_on( lua_State* L )
}
}
// Lua: mqtt:subscribe(topic, qos, function())
// Lua: mqtt:subscribe(topic, qos, function())
static
int
mqtt_socket_subscribe
(
lua_State
*
L
)
static
int
mqtt_socket_subscribe
(
lua_State
*
L
)
{
{
NODE_DBG
(
"mqtt_socket_subscribe is called.
\n
"
);
NODE_DBG
(
"mqtt_socket_subscribe is called.
\n
"
);
typedef
struct
SUB_STORAGE
{
uint8_t
stack
=
1
,
qos
=
0
,
retain
=
0
;
uint32_t
length
;
const
char
*
topic
;
uint8_t
*
data
;
size_t
il
;
struct
SUB_STORAGE
*
next
;
lmqtt_userdata
*
mud
;
}
SUB_STORAGE
;
mud
=
(
lmqtt_userdata
*
)
luaL_checkudata
(
L
,
stack
,
"mqtt.socket"
);
uint8_t
stack
=
1
,
qos
=
0
;
luaL_argcheck
(
L
,
mud
,
stack
,
"mqtt.socket expected"
);
const
char
*
topic
;
stack
++
;
size_t
il
;
lmqtt_userdata
*
mud
;
if
(
mud
->
send_timeout
!=
0
)
return
luaL_error
(
L
,
"sending in process"
);
mud
=
(
lmqtt_userdata
*
)
luaL_checkudata
(
L
,
stack
,
"mqtt.socket"
);
luaL_argcheck
(
L
,
mud
,
stack
,
"mqtt.socket expected"
);
if
(
!
mud
->
connected
)
stack
++
;
return
luaL_error
(
L
,
"not connected"
);
topic
=
luaL_checklstring
(
L
,
stack
,
&
il
);
if
(
mud
->
send_timeout
!=
0
)
stack
++
;
return
luaL_error
(
L
,
"sending in process"
);
if
(
topic
==
NULL
)
return
luaL_error
(
L
,
"need topic name"
);
if
(
!
mud
->
connected
)
return
luaL_error
(
L
,
"not connected"
);
qos
=
luaL_checkinteger
(
L
,
stack
);
stack
++
;
if
(
lua_istable
(
L
,
stack
)
)
{
NODE_DBG
(
"subscribe table
\n
"
);
lua_pushnil
(
L
);
/* first key */
SUB_STORAGE
*
first
,
*
last
,
*
curr
;
first
=
(
SUB_STORAGE
*
)
c_zalloc
(
sizeof
(
SUB_STORAGE
));
if
(
first
==
NULL
)
return
luaL_error
(
L
,
"not enough memory"
);
first
->
length
=
0
;
last
=
first
;
first
->
next
=
NULL
;
while
(
lua_next
(
L
,
stack
)
!=
0
)
{
curr
=
(
SUB_STORAGE
*
)
c_zalloc
(
sizeof
(
SUB_STORAGE
));
if
(
curr
==
NULL
)
return
luaL_error
(
L
,
"not enough memory"
);
topic
=
luaL_checkstring
(
L
,
-
2
);
qos
=
luaL_checkinteger
(
L
,
-
1
);
mud
->
mqtt_state
.
outbound_message
=
mqtt_msg_subscribe
(
&
mud
->
mqtt_state
.
mqtt_connection
,
topic
,
qos
,
&
mud
->
mqtt_state
.
pending_msg_id
);
NODE_DBG
(
"topic: %s - qos: %d, length: %d
\n
"
,
topic
,
qos
,
mud
->
mqtt_state
.
outbound_message
->
length
);
curr
->
data
=
(
uint8_t
*
)
c_zalloc
(
mud
->
mqtt_state
.
outbound_message
->
length
);
if
(
curr
->
data
==
NULL
)
return
luaL_error
(
L
,
"not enough memory"
);
c_memcpy
(
curr
->
data
,
mud
->
mqtt_state
.
outbound_message
->
data
,
mud
->
mqtt_state
.
outbound_message
->
length
);
curr
->
length
=
mud
->
mqtt_state
.
outbound_message
->
length
;
curr
->
next
=
NULL
;
last
->
next
=
curr
;
last
=
curr
;
lua_pop
(
L
,
1
);
}
curr
=
first
;
uint32_t
ptr
=
0
;
while
(
curr
!=
NULL
)
{
if
(
curr
->
length
==
0
)
{
curr
=
curr
->
next
;
continue
;
}
if
(
ptr
+
curr
->
length
<
mud
->
mqtt_state
.
out_buffer_length
)
{
c_memcpy
(
mud
->
mqtt_state
.
out_buffer
+
ptr
,
curr
->
data
,
curr
->
length
);
ptr
+=
curr
->
length
;
}
c_free
(
curr
->
data
);
c_free
(
curr
);
curr
=
curr
->
next
;
}
c_free
(
first
);
if
(
ptr
==
0
)
{
return
luaL_error
(
L
,
"invalid data"
);
}
mud
->
mqtt_state
.
outbound_message
->
data
=
mud
->
mqtt_state
.
out_buffer
;
mud
->
mqtt_state
.
outbound_message
->
length
=
ptr
;
stack
++
;
}
else
{
NODE_DBG
(
"subscribe string
\n
"
);
topic
=
luaL_checklstring
(
L
,
stack
,
&
il
);
stack
++
;
if
(
topic
==
NULL
)
return
luaL_error
(
L
,
"need topic name"
);
qos
=
luaL_checkinteger
(
L
,
stack
);
mud
->
mqtt_state
.
outbound_message
=
mqtt_msg_subscribe
(
&
mud
->
mqtt_state
.
mqtt_connection
,
topic
,
qos
,
&
mud
->
mqtt_state
.
pending_msg_id
);
stack
++
;
}
mud
->
mqtt_state
.
outbound_message
=
mqtt_msg_subscribe
(
&
mud
->
mqtt_state
.
mqtt_connection
,
mud
->
send_timeout
=
MQTT_SEND_TIMEOUT
;
topic
,
qos
,
mud
->
mqtt_state
.
pending_msg_type
=
MQTT_MSG_TYPE_SUBSCRIBE
;
&
mud
->
mqtt_state
.
pending_msg_id
);
mud
->
mqtt_state
.
pending_publish_qos
=
mqtt_get_qos
(
mud
->
mqtt_state
.
outbound_message
->
data
);
mud
->
send_timeout
=
MQTT_SEND_TIMEOUT
;
mud
->
mqtt_state
.
pending_msg_type
=
MQTT_MSG_TYPE_SUBSCRIBE
;
mud
->
mqtt_state
.
pending_publish_qos
=
mqtt_get_qos
(
mud
->
mqtt_state
.
outbound_message
->
data
);
if
(
lua_type
(
L
,
stack
)
==
LUA_TFUNCTION
||
lua_type
(
L
,
stack
)
==
LUA_TLIGHTFUNCTION
){
if
(
lua_type
(
L
,
stack
)
==
LUA_TFUNCTION
||
lua_type
(
L
,
stack
)
==
LUA_TLIGHTFUNCTION
)
{
lua_pushvalue
(
L
,
stack
);
// copy argument (func) to the top of stack
lua_pushvalue
(
L
,
stack
);
// copy argument (func) to the top of stack
if
(
mud
->
cb_suback_ref
!=
LUA_NOREF
)
if
(
mud
->
cb_suback_ref
!=
LUA_NOREF
)
luaL_unref
(
L
,
LUA_REGISTRYINDEX
,
mud
->
cb_suback_ref
);
luaL_unref
(
L
,
LUA_REGISTRYINDEX
,
mud
->
cb_suback_ref
);
mud
->
cb_suback_ref
=
luaL_ref
(
L
,
LUA_REGISTRYINDEX
);
mud
->
cb_suback_ref
=
luaL_ref
(
L
,
LUA_REGISTRYINDEX
);
}
}
NODE_DBG
(
"Sent: %d
\n
"
,
mud
->
mqtt_state
.
outbound_message
->
length
);
if
(
mud
->
secure
)
espconn_secure_sent
(
mud
->
pesp_conn
,
mud
->
mqtt_state
.
outbound_message
->
data
,
mud
->
mqtt_state
.
outbound_message
->
length
);
else
espconn_sent
(
mud
->
pesp_conn
,
mud
->
mqtt_state
.
outbound_message
->
data
,
mud
->
mqtt_state
.
outbound_message
->
length
);
if
(
mud
->
secure
)
return
0
;
espconn_secure_sent
(
mud
->
pesp_conn
,
mud
->
mqtt_state
.
outbound_message
->
data
,
mud
->
mqtt_state
.
outbound_message
->
length
);
else
espconn_sent
(
mud
->
pesp_conn
,
mud
->
mqtt_state
.
outbound_message
->
data
,
mud
->
mqtt_state
.
outbound_message
->
length
);
return
0
;
}
}
// Lua: mqtt:publish( topic, payload, qos, retain, function() )
// Lua: mqtt:publish( topic, payload, qos, retain, function() )
...
...
app/platform/platform.c
View file @
12a546ce
...
@@ -185,6 +185,9 @@ uint32_t platform_uart_setup( unsigned id, uint32_t baud, int databits, int pari
...
@@ -185,6 +185,9 @@ uint32_t platform_uart_setup( unsigned id, uint32_t baud, int databits, int pari
{
{
switch
(
baud
)
switch
(
baud
)
{
{
case
BIT_RATE_1200
:
case
BIT_RATE_2400
:
case
BIT_RATE_4800
:
case
BIT_RATE_9600
:
case
BIT_RATE_9600
:
case
BIT_RATE_19200
:
case
BIT_RATE_19200
:
case
BIT_RATE_38400
:
case
BIT_RATE_38400
:
...
...
lua_modules/dht22/README.md
View file @
12a546ce
# DHT22 module
# DHT22 module
This module is compatible with DHT22 and DHT21.
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
## Example
...
@@ -12,15 +13,20 @@ dht22.read(PIN)
...
@@ -12,15 +13,20 @@ dht22.read(PIN)
t
=
dht22
.
getTemperature
()
t
=
dht22
.
getTemperature
()
h
=
dht22
.
getHumidity
()
h
=
dht22
.
getHumidity
()
if
h
==
-
1
then
if
h
==
nil
then
print
(
"Error reading from DHT22"
)
print
(
"Error reading from DHT22"
)
else
else
-- temperature in degrees Celsius and Farenheit
-- temperature in degrees Celsius and Farenheit
print
(
"Temperature: "
..
(
t
/
10
)
..
"."
..
(
t
%
10
)
..
" deg C"
)
-- floating point and integer version:
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
print
(
"Humidity: "
..
(
h
/
10
)
..
"."
..
(
h
%
10
)
..
"%"
)
-- floating point and integer version
print
(
"Humidity: "
..
((
h
-
(
h
%
10
))
/
10
)
..
"."
..
(
h
%
10
)
..
"%"
)
end
end
-- release module
-- release module
...
@@ -29,7 +35,7 @@ package.loaded["dht22"]=nil
...
@@ -29,7 +35,7 @@ package.loaded["dht22"]=nil
```
```
## Functions
## Functions
### read
### read
read(pin)
read(pin)
Read humidity and temperature from DHT22.
Read humidity and temperature from DHT22.
**Parameters:**
**Parameters:**
...
...
lua_modules/dht22/dht22.lua
View file @
12a546ce
...
@@ -77,15 +77,17 @@ function M.read(pin)
...
@@ -77,15 +77,17 @@ function M.read(pin)
end
end
end
end
checksumTest
=
((
humidity
/
256
)
+
(
humidity
%
256
)
+
(
temperature
/
256
)
+
(
temperature
%
256
))
%
256
checksumTest
=
(
bit
.
band
(
humidity
,
0xFF
)
+
bit
.
rshift
(
humidity
,
8
)
+
bit
.
band
(
temperature
,
0xFF
)
+
bit
.
rshift
(
temperature
,
8
))
checksumTest
=
bit
.
band
(
checksumTest
,
0xFF
)
if
temperature
>
0x8000
then
if
temperature
>
0x8000
then
-- convert to negative format
-- convert to negative format
temperature
=
-
(
temperature
-
0x8000
)
temperature
=
-
(
temperature
-
0x8000
)
end
end
if
checksum
~=
checksumTest
then
-- conditions compatible con float point and integer
humidity
=
-
1
if
(
checksumTest
-
checksum
>=
1
)
or
(
checksum
-
checksumTest
>=
1
)
then
humidity
=
nil
end
end
end
end
...
...
lua_modules/ds18b20/ds18b20.lua
View file @
12a546ce
...
@@ -105,11 +105,10 @@ function readNumber(addr, unit)
...
@@ -105,11 +105,10 @@ function readNumber(addr, unit)
else
else
return
nil
return
nil
end
end
t1
=
t
/
10000
t
=
t
/
10000
t2
=
t
%
10000
-- print("Temperature="..t1.."."..t2.." Centigrade")
-- print("Temperature="..t1.."."..t2.." Centigrade")
-- result = t1.."."..t2
-- result = t1.."."..t2
return
t
1
,
t2
return
t
end
end
tmr
.
wdclr
()
tmr
.
wdclr
()
else
else
...
@@ -122,11 +121,11 @@ function readNumber(addr, unit)
...
@@ -122,11 +121,11 @@ function readNumber(addr, unit)
end
end
function
read
(
addr
,
unit
)
function
read
(
addr
,
unit
)
t
1
,
t2
=
readNumber
(
addr
,
unit
)
t
=
readNumber
(
addr
,
unit
)
if
(
(
t
1
==
nil
)
or
(
t2
==
nil
)
)
then
if
(
t
==
nil
)
then
return
nil
return
nil
else
else
return
t
1
..
"."
..
string.format
(
"%04u"
,
t2
)
return
t
end
end
end
end
...
...
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