Merge branch 'dev' of https://github.com/nodemcu/nodemcu-firmware
Conflicts: app/include/user_version.h app/modules/node.c app/modules/wifi.c
This commit is contained in:
commit
71469dd70f
17
README.md
17
README.md
|
@ -36,6 +36,12 @@ Tencent QQ group: 309957875<br />
|
|||
- cross compiler (done)
|
||||
|
||||
# Change log
|
||||
2015-03-31<br />
|
||||
polish mqtt module, add queue for mqtt module.<br />
|
||||
add reconnect option to mqtt.connect api, :connect( host, port, secure, auto_reconnect, function(client) )<br />
|
||||
move node.readvdd33 to adc.readvdd33.<br />
|
||||
tools/esptool.py supported NodeMCU devkit automatic flash.
|
||||
|
||||
2015-03-18<br />
|
||||
update u8glib.<br />
|
||||
merge everything to master.
|
||||
|
@ -225,8 +231,10 @@ m:on("message", function(conn, topic, data)
|
|||
end
|
||||
end)
|
||||
|
||||
-- for secure: m:connect("192.168.11.118", 1880, 1)
|
||||
m:connect("192.168.11.118", 1880, 0, function(conn) print("connected") end)
|
||||
-- m:connect( host, port, secure, auto_reconnect, function(client) )
|
||||
-- for secure: m:connect("192.168.11.118", 1880, 1, 0)
|
||||
-- for auto-reconnect: m:connect("192.168.11.118", 1880, 0, 1)
|
||||
m:connect("192.168.11.118", 1880, 0, 0, function(conn) print("connected") end)
|
||||
|
||||
-- subscribe topic with qos = 0
|
||||
m:subscribe("/topic",0, function(conn) print("subscribe success") end)
|
||||
|
@ -235,7 +243,7 @@ m:subscribe("/topic",0, function(conn) print("subscribe success") end)
|
|||
-- publish a message with data = hello, QoS = 0, retain = 0
|
||||
m:publish("/topic","hello",0,0, function(conn) print("sent") end)
|
||||
|
||||
m:close();
|
||||
m:close(); -- if auto-reconnect == 1, will disable auto-reconnect and then disconnect from host.
|
||||
-- you can call m:connect again
|
||||
|
||||
```
|
||||
|
@ -402,7 +410,8 @@ u8glib comes with a wide range of fonts for small displays. Since they need to b
|
|||
They'll be available as `u8g.<font_name>` in Lua.
|
||||
|
||||
#####Bitmaps
|
||||
Bitmaps and XBMs are supplied as strings to `drawBitmap()` and `drawXBM()`. This off-loads all data handling from the u8g module to generic methods for binary files. See `lua_examples/u8glib/u8g_bitmaps.lua`. Binary files can be uploaded with [nodemcu-uploader.py](https://github.com/kmpm/nodemcu-uploader).
|
||||
Bitmaps and XBMs are supplied as strings to `drawBitmap()` and `drawXBM()`. This off-loads all data handling from the u8g module to generic methods for binary files. See `lua_examples/u8glib/u8g_bitmaps.lua`.
|
||||
In contrast to the source code based inclusion of XBMs into u8glib, it's required to provide precompiled binary files. This can be performed online with [Online-Utility's Image Converter](http://www.online-utility.org/image_converter.jsp): Convert from XBM to MONO format and upload the binary result with [nodemcu-uploader.py](https://github.com/kmpm/nodemcu-uploader).
|
||||
|
||||
#####Unimplemented functions
|
||||
- [ ] Cursor handling
|
||||
|
|
|
@ -46,7 +46,7 @@ int strbuf_init(strbuf_t *s, int len)
|
|||
s->reallocs = 0;
|
||||
s->debug = 0;
|
||||
|
||||
s->buf = c_malloc(size);
|
||||
s->buf = (char *)c_malloc(size);
|
||||
if (!s->buf){
|
||||
NODE_ERR("not enough memory\n");
|
||||
return -1;
|
||||
|
@ -60,7 +60,7 @@ strbuf_t *strbuf_new(int len)
|
|||
{
|
||||
strbuf_t *s;
|
||||
|
||||
s = c_malloc(sizeof(strbuf_t));
|
||||
s = (strbuf_t *)c_malloc(sizeof(strbuf_t));
|
||||
if (!s){
|
||||
NODE_ERR("not enough memory\n");
|
||||
return NULL;
|
||||
|
|
|
@ -365,7 +365,7 @@ pwm_init(uint16 freq, uint16 *duty)
|
|||
for (i = 0; i < PWM_CHANNEL; i++) {
|
||||
// pwm_gpio |= (1 << pwm_out_io_num[i]);
|
||||
pwm_gpio = 0;
|
||||
pwm.duty[0] = 0;
|
||||
pwm.duty[i] = 0;
|
||||
}
|
||||
|
||||
pwm_set_freq(500, 0);
|
||||
|
|
|
@ -7,6 +7,6 @@
|
|||
#define NODE_VERSION_INTERNAL 0U
|
||||
|
||||
#define NODE_VERSION "NodeMCU 0.9.6"
|
||||
#define BUILD_DATE "build 20150318"
|
||||
#define BUILD_DATE "build 20150405"
|
||||
|
||||
#endif /* __USER_VERSION_H__ */
|
||||
|
|
|
@ -47,9 +47,9 @@ extern int c_stderr;
|
|||
#define SEEK_END 2 /* set file offset to EOF plus offset */
|
||||
#endif
|
||||
|
||||
#define c_malloc os_malloc
|
||||
#define c_zalloc os_zalloc
|
||||
#define c_free os_free
|
||||
// #define c_malloc os_malloc
|
||||
// #define c_zalloc os_zalloc
|
||||
// #define c_free os_free
|
||||
|
||||
extern void output_redirect(const char *str);
|
||||
#define c_puts output_redirect
|
||||
|
|
|
@ -29,9 +29,9 @@
|
|||
#define os_realloc(p, s) mem_realloc((p), (s))
|
||||
#endif
|
||||
|
||||
// #define c_free os_free
|
||||
// #define c_malloc os_malloc
|
||||
// #define c_zalloc os_zalloc
|
||||
#define c_free os_free
|
||||
#define c_malloc os_malloc
|
||||
#define c_zalloc os_zalloc
|
||||
#define c_realloc os_realloc
|
||||
|
||||
#define c_abs abs
|
||||
|
@ -47,9 +47,9 @@
|
|||
// c_getenv() get env "LUA_INIT" string for lua initialization.
|
||||
const char *c_getenv(const char *__string);
|
||||
|
||||
void *c_malloc(size_t __size);
|
||||
void *c_zalloc(size_t __size);
|
||||
void c_free(void *);
|
||||
// void *c_malloc(size_t __size);
|
||||
// void *c_zalloc(size_t __size);
|
||||
// void c_free(void *);
|
||||
|
||||
// int c_rand(void);
|
||||
// void c_srand(unsigned int __seed);
|
||||
|
|
|
@ -541,8 +541,12 @@ extern int readline4lua(const char *prompt, char *buffer, int length);
|
|||
|
||||
/*
|
||||
@@ LUAL_BUFFERSIZE is the buffer size used by the lauxlib buffer system.
|
||||
** Attention: This value should probably not be set higher than 1K.
|
||||
** The size has direct impact on the C stack size needed be auxlib functions.
|
||||
** For example: If set to 4K a call to string.gsub will need more than
|
||||
** 5k C stack space.
|
||||
*/
|
||||
#define LUAL_BUFFERSIZE ((BUFSIZ)*4)
|
||||
#define LUAL_BUFFERSIZE BUFSIZ
|
||||
|
||||
/* }================================================================== */
|
||||
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
#include "lrotable.h"
|
||||
|
||||
#include "c_types.h"
|
||||
#include "user_interface.h"
|
||||
|
||||
// Lua: read(id) , return system adc
|
||||
static int adc_sample( lua_State* L )
|
||||
|
@ -19,12 +20,33 @@ static int adc_sample( lua_State* L )
|
|||
return 1;
|
||||
}
|
||||
|
||||
// Lua: readvdd33()
|
||||
static int adc_readvdd33( lua_State* L )
|
||||
{
|
||||
uint32_t vdd33 = 0;
|
||||
if(STATION_MODE == wifi_get_opmode())
|
||||
{
|
||||
// Bug fix
|
||||
wifi_set_opmode( STATIONAP_MODE );
|
||||
vdd33 = readvdd33();
|
||||
wifi_set_opmode( STATION_MODE );
|
||||
}
|
||||
else
|
||||
{
|
||||
vdd33 = readvdd33();
|
||||
}
|
||||
|
||||
lua_pushinteger(L, vdd33);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Module function map
|
||||
#define MIN_OPT_LEVEL 2
|
||||
#include "lrodefs.h"
|
||||
const LUA_REG_TYPE adc_map[] =
|
||||
{
|
||||
{ LSTRKEY( "read" ), LFUNCVAL( adc_sample ) },
|
||||
{ LSTRKEY( "readvdd33" ), LFUNCVAL( adc_readvdd33) },
|
||||
#if LUA_OPTIMIZE_MEMORY > 0
|
||||
|
||||
#endif
|
||||
|
|
|
@ -80,7 +80,7 @@ LUALIB_API int ( luaopen_file )( lua_State *L );
|
|||
LUALIB_API int ( luaopen_ow )( lua_State *L );
|
||||
|
||||
#define AUXLIB_CJSON "cjson"
|
||||
LUALIB_API int ( luaopen_ow )( lua_State *L );
|
||||
LUALIB_API int ( luaopen_cjson )( lua_State *L );
|
||||
|
||||
// Helper macros
|
||||
#define MOD_CHECK_ID( mod, id )\
|
||||
|
|
|
@ -146,6 +146,82 @@ static int lgpio_write( lua_State* L )
|
|||
return 0;
|
||||
}
|
||||
|
||||
#define DELAY_TABLE_MAX_LEN 256
|
||||
#define noInterrupts os_intr_lock
|
||||
#define interrupts os_intr_unlock
|
||||
#define delayMicroseconds os_delay_us
|
||||
#define DIRECT_WRITE(pin, level) (GPIO_OUTPUT_SET(GPIO_ID_PIN(pin_num[pin]), level))
|
||||
// Lua: serout( pin, firstLevel, delay_table, [repeatNum] )
|
||||
// -- 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
|
||||
static int lgpio_serout( lua_State* L )
|
||||
{
|
||||
unsigned level;
|
||||
unsigned pin;
|
||||
unsigned table_len = 0;
|
||||
unsigned repeat = 0;
|
||||
int delay_table[DELAY_TABLE_MAX_LEN];
|
||||
|
||||
pin = luaL_checkinteger( L, 1 );
|
||||
MOD_CHECK_ID( gpio, pin );
|
||||
level = luaL_checkinteger( L, 2 );
|
||||
if ( level!=HIGH && level!=LOW )
|
||||
return luaL_error( L, "wrong arg type" );
|
||||
if( lua_istable( L, 3 ) )
|
||||
{
|
||||
table_len = lua_objlen( L, 3 );
|
||||
if (table_len <= 0 || table_len>DELAY_TABLE_MAX_LEN)
|
||||
return luaL_error( L, "wrong arg range" );
|
||||
int i;
|
||||
for( i = 0; i < table_len; i ++ )
|
||||
{
|
||||
lua_rawgeti( L, 3, i + 1 );
|
||||
delay_table[i] = ( int )luaL_checkinteger( L, -1 );
|
||||
lua_pop( L, 1 );
|
||||
if( delay_table[i] < 0 || delay_table[i] > 1000000 ) // can not delay more than 1000000 us
|
||||
return luaL_error( L, "delay must < 1000000 us" );
|
||||
}
|
||||
} else {
|
||||
return luaL_error( L, "wrong arg range" );
|
||||
}
|
||||
|
||||
if(lua_isnumber(L, 4))
|
||||
repeat = lua_tointeger( L, 4 );
|
||||
if( repeat < 0 || repeat > DELAY_TABLE_MAX_LEN )
|
||||
return luaL_error( L, "delay must < 256" );
|
||||
|
||||
if(repeat==0)
|
||||
repeat = 1;
|
||||
int j;
|
||||
bool skip_loop = true;
|
||||
do
|
||||
{
|
||||
if(skip_loop){ // skip the first loop.
|
||||
skip_loop = false;
|
||||
continue;
|
||||
}
|
||||
for(j=0;j<table_len;j++){
|
||||
noInterrupts();
|
||||
// platform_gpio_write(pin, level);
|
||||
DIRECT_WRITE(pin, level);
|
||||
interrupts();
|
||||
delayMicroseconds(delay_table[j]);
|
||||
level=!level;
|
||||
}
|
||||
repeat--;
|
||||
} while (repeat>0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
#undef DELAY_TABLE_MAX_LEN
|
||||
|
||||
// Module function map
|
||||
#define MIN_OPT_LEVEL 2
|
||||
#include "lrodefs.h"
|
||||
|
@ -154,6 +230,7 @@ const LUA_REG_TYPE gpio_map[] =
|
|||
{ LSTRKEY( "mode" ), LFUNCVAL( lgpio_mode ) },
|
||||
{ LSTRKEY( "read" ), LFUNCVAL( lgpio_read ) },
|
||||
{ LSTRKEY( "write" ), LFUNCVAL( lgpio_write ) },
|
||||
{ LSTRKEY( "serout" ), LFUNCVAL( lgpio_serout ) },
|
||||
#ifdef GPIO_INTERRUPT_ENABLE
|
||||
{ LSTRKEY( "trig" ), LFUNCVAL( lgpio_trig ) },
|
||||
#endif
|
||||
|
|
1007
app/modules/mqtt.c
1007
app/modules/mqtt.c
File diff suppressed because it is too large
Load Diff
|
@ -100,14 +100,15 @@ static int node_chipid( lua_State* L )
|
|||
lua_pushinteger(L, id);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// deprecated, moved to adc module
|
||||
// Lua: readvdd33()
|
||||
static int node_readvdd33( lua_State* L )
|
||||
{
|
||||
// uint32_t vdd33 = system_get_vdd33();
|
||||
uint32_t vdd33 = readvdd33();
|
||||
lua_pushinteger(L, vdd33);
|
||||
return 1;
|
||||
}
|
||||
// static int node_readvdd33( lua_State* L )
|
||||
// {
|
||||
// uint32_t vdd33 = readvdd33();
|
||||
// lua_pushinteger(L, vdd33);
|
||||
// return 1;
|
||||
// }
|
||||
|
||||
// Lua: flashid()
|
||||
static int node_flashid( lua_State* L )
|
||||
|
@ -431,7 +432,8 @@ const LUA_REG_TYPE node_map[] =
|
|||
#endif
|
||||
{ LSTRKEY( "input" ), LFUNCVAL( node_input ) },
|
||||
{ LSTRKEY( "output" ), LFUNCVAL( node_output ) },
|
||||
{ LSTRKEY( "readvdd33" ), LFUNCVAL( node_readvdd33) },
|
||||
// Moved to adc module, use adc.readvdd33()
|
||||
// { LSTRKEY( "readvdd33" ), LFUNCVAL( node_readvdd33) },
|
||||
{ LSTRKEY( "compile" ), LFUNCVAL( node_compile) },
|
||||
{ LSTRKEY( "CPU80MHZ" ), LNUMVAL( CPU80MHZ ) },
|
||||
{ LSTRKEY( "CPU160MHZ" ), LNUMVAL( CPU160MHZ ) },
|
||||
|
|
|
@ -167,8 +167,8 @@ static int wifi_getmode( lua_State* L )
|
|||
/**
|
||||
* wifi.setphymode()
|
||||
* Description:
|
||||
* Set wifi physical mode(802.11 b/g/n)
|
||||
* Note: SoftAP only supports 802.11 b/g.
|
||||
* Set wifi physical mode(802.11 b/g/n)
|
||||
* Note: SoftAP only supports 802.11 b/g.
|
||||
* Syntax:
|
||||
* wifi.setphymode(mode)
|
||||
* Parameters:
|
||||
|
@ -197,7 +197,7 @@ static int wifi_setphymode( lua_State* L )
|
|||
/**
|
||||
* wifi.getphymode()
|
||||
* Description:
|
||||
* Get wifi physical mode(802.11 b/g/n)
|
||||
* Get wifi physical mode(802.11 b/g/n)
|
||||
* Syntax:
|
||||
* wifi.getphymode()
|
||||
* Parameters:
|
||||
|
@ -456,8 +456,8 @@ static int wifi_station_config( lua_State* L )
|
|||
if (sl>32 || ssid == NULL)
|
||||
return luaL_error( L, "ssid:<32" );
|
||||
const char *password = luaL_checklstring( L, 2, &pl );
|
||||
if (pl<8 || pl>64 || password == NULL)
|
||||
return luaL_error( L, "pwd:8~64" );
|
||||
if (pl!=0 && (pl<8 || pl>64) || password == NULL)
|
||||
return luaL_error( L, "pwd:0,8~64" );
|
||||
|
||||
if(lua_isnumber(L, 3))
|
||||
{
|
||||
|
|
|
@ -3,6 +3,8 @@
|
|||
#include "platform.h"
|
||||
#include "auxmods.h"
|
||||
#include "lrotable.h"
|
||||
#include "c_stdlib.h"
|
||||
#include "c_string.h"
|
||||
/**
|
||||
* All this code is mostly from http://www.esp8266.com/viewtopic.php?f=21&t=1143&sid=a620a377672cfe9f666d672398415fcb
|
||||
* from user Markus Gritsch.
|
||||
|
@ -35,7 +37,10 @@ static int ICACHE_FLASH_ATTR ws2812_writergb(lua_State* L)
|
|||
{
|
||||
const uint8_t pin = luaL_checkinteger(L, 1);
|
||||
size_t length;
|
||||
char *buffer = (char *)luaL_checklstring(L, 2, &length); // Cast away the constness.
|
||||
const char *rgb = luaL_checklstring(L, 2, &length);
|
||||
// dont modify lua-internal lstring - make a copy instead
|
||||
char *buffer = (char *)c_malloc(length);
|
||||
c_memcpy(buffer, rgb, length);
|
||||
|
||||
// Initialize the output pin:
|
||||
platform_gpio_mode(pin, PLATFORM_GPIO_OUTPUT, PLATFORM_GPIO_FLOAT);
|
||||
|
@ -59,17 +64,17 @@ static int ICACHE_FLASH_ATTR ws2812_writergb(lua_State* L)
|
|||
|
||||
// Send the buffer:
|
||||
os_intr_lock();
|
||||
const char * const end = buffer + length;
|
||||
while (buffer != end) {
|
||||
for (i = 0; i < length; i++) {
|
||||
uint8_t mask = 0x80;
|
||||
while (mask) {
|
||||
(*buffer & mask) ? send_ws_1(pin_num[pin]) : send_ws_0(pin_num[pin]);
|
||||
(buffer[i] & mask) ? send_ws_1(pin_num[pin]) : send_ws_0(pin_num[pin]);
|
||||
mask >>= 1;
|
||||
}
|
||||
++buffer;
|
||||
}
|
||||
os_intr_unlock();
|
||||
|
||||
c_free(buffer);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
|
@ -29,7 +29,7 @@
|
|||
*
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include "c_string.h"
|
||||
#include "mqtt_msg.h"
|
||||
|
||||
#define MQTT_MAX_FIXED_HEADER_SIZE 3
|
||||
|
@ -61,7 +61,7 @@ static int append_string(mqtt_connection_t* connection, const char* string, int
|
|||
|
||||
connection->buffer[connection->message.length++] = len >> 8;
|
||||
connection->buffer[connection->message.length++] = len & 0xff;
|
||||
memcpy(connection->buffer + connection->message.length, string, len);
|
||||
c_memcpy(connection->buffer + connection->message.length, string, len);
|
||||
connection->message.length += len;
|
||||
|
||||
return len + 2;
|
||||
|
@ -121,7 +121,7 @@ static mqtt_message_t* fini_message(mqtt_connection_t* connection, int type, int
|
|||
|
||||
void mqtt_msg_init(mqtt_connection_t* connection, uint8_t* buffer, uint16_t buffer_length)
|
||||
{
|
||||
memset(connection, 0, sizeof(connection));
|
||||
c_memset(connection, 0, sizeof(connection));
|
||||
connection->buffer = buffer;
|
||||
connection->buffer_length = buffer_length;
|
||||
}
|
||||
|
@ -294,7 +294,7 @@ mqtt_message_t* mqtt_msg_connect(mqtt_connection_t* connection, mqtt_connect_inf
|
|||
|
||||
variable_header->lengthMsb = 0;
|
||||
variable_header->lengthLsb = 4;
|
||||
memcpy(variable_header->magic, "MQTT", 4);
|
||||
c_memcpy(variable_header->magic, "MQTT", 4);
|
||||
variable_header->version = 4;
|
||||
variable_header->flags = 0;
|
||||
variable_header->keepaliveMsb = info->keepalive >> 8;
|
||||
|
@ -305,7 +305,7 @@ mqtt_message_t* mqtt_msg_connect(mqtt_connection_t* connection, mqtt_connect_inf
|
|||
|
||||
if(info->client_id != NULL && info->client_id[0] != '\0')
|
||||
{
|
||||
if(append_string(connection, info->client_id, strlen(info->client_id)) < 0)
|
||||
if(append_string(connection, info->client_id, c_strlen(info->client_id)) < 0)
|
||||
return fail_message(connection);
|
||||
}
|
||||
else
|
||||
|
@ -313,10 +313,10 @@ mqtt_message_t* mqtt_msg_connect(mqtt_connection_t* connection, mqtt_connect_inf
|
|||
|
||||
if(info->will_topic != NULL && info->will_topic[0] != '\0')
|
||||
{
|
||||
if(append_string(connection, info->will_topic, strlen(info->will_topic)) < 0)
|
||||
if(append_string(connection, info->will_topic, c_strlen(info->will_topic)) < 0)
|
||||
return fail_message(connection);
|
||||
|
||||
if(append_string(connection, info->will_message, strlen(info->will_message)) < 0)
|
||||
if(append_string(connection, info->will_message, c_strlen(info->will_message)) < 0)
|
||||
return fail_message(connection);
|
||||
|
||||
variable_header->flags |= MQTT_CONNECT_FLAG_WILL;
|
||||
|
@ -327,7 +327,7 @@ mqtt_message_t* mqtt_msg_connect(mqtt_connection_t* connection, mqtt_connect_inf
|
|||
|
||||
if(info->username != NULL && info->username[0] != '\0')
|
||||
{
|
||||
if(append_string(connection, info->username, strlen(info->username)) < 0)
|
||||
if(append_string(connection, info->username, c_strlen(info->username)) < 0)
|
||||
return fail_message(connection);
|
||||
|
||||
variable_header->flags |= MQTT_CONNECT_FLAG_USERNAME;
|
||||
|
@ -335,7 +335,7 @@ mqtt_message_t* mqtt_msg_connect(mqtt_connection_t* connection, mqtt_connect_inf
|
|||
|
||||
if(info->password != NULL && info->password[0] != '\0')
|
||||
{
|
||||
if(append_string(connection, info->password, strlen(info->password)) < 0)
|
||||
if(append_string(connection, info->password, c_strlen(info->password)) < 0)
|
||||
return fail_message(connection);
|
||||
|
||||
variable_header->flags |= MQTT_CONNECT_FLAG_PASSWORD;
|
||||
|
@ -351,7 +351,7 @@ mqtt_message_t* mqtt_msg_publish(mqtt_connection_t* connection, const char* topi
|
|||
if(topic == NULL || topic[0] == '\0')
|
||||
return fail_message(connection);
|
||||
|
||||
if(append_string(connection, topic, strlen(topic)) < 0)
|
||||
if(append_string(connection, topic, c_strlen(topic)) < 0)
|
||||
return fail_message(connection);
|
||||
|
||||
if(qos > 0)
|
||||
|
@ -364,7 +364,7 @@ mqtt_message_t* mqtt_msg_publish(mqtt_connection_t* connection, const char* topi
|
|||
|
||||
if(connection->message.length + data_length > connection->buffer_length)
|
||||
return fail_message(connection);
|
||||
memcpy(connection->buffer + connection->message.length, data, data_length);
|
||||
c_memcpy(connection->buffer + connection->message.length, data, data_length);
|
||||
connection->message.length += data_length;
|
||||
|
||||
return fini_message(connection, MQTT_MSG_TYPE_PUBLISH, 0, qos, retain);
|
||||
|
@ -412,7 +412,7 @@ mqtt_message_t* mqtt_msg_subscribe(mqtt_connection_t* connection, const char* to
|
|||
if((*message_id = append_message_id(connection, 0)) == 0)
|
||||
return fail_message(connection);
|
||||
|
||||
if(append_string(connection, topic, strlen(topic)) < 0)
|
||||
if(append_string(connection, topic, c_strlen(topic)) < 0)
|
||||
return fail_message(connection);
|
||||
|
||||
if(connection->message.length + 1 > connection->buffer_length)
|
||||
|
@ -432,7 +432,7 @@ mqtt_message_t* mqtt_msg_unsubscribe(mqtt_connection_t* connection, const char*
|
|||
if((*message_id = append_message_id(connection, 0)) == 0)
|
||||
return fail_message(connection);
|
||||
|
||||
if(append_string(connection, topic, strlen(topic)) < 0)
|
||||
if(append_string(connection, topic, c_strlen(topic)) < 0)
|
||||
return fail_message(connection);
|
||||
|
||||
return fini_message(connection, MQTT_MSG_TYPE_SUBSCRIBE, 0, 1, 0);
|
||||
|
|
|
@ -0,0 +1,82 @@
|
|||
#include "c_string.h"
|
||||
#include "c_stdlib.h"
|
||||
#include "c_stdio.h"
|
||||
#include "msg_queue.h"
|
||||
|
||||
msg_queue_t *msg_enqueue(msg_queue_t **head, mqtt_message_t *msg, uint16_t msg_id, int msg_type, int publish_qos){
|
||||
if(!head){
|
||||
return NULL;
|
||||
}
|
||||
if (!msg || !msg->data || msg->length == 0){
|
||||
NODE_DBG("empty message\n");
|
||||
return NULL;
|
||||
}
|
||||
msg_queue_t *node = (msg_queue_t *)c_zalloc(sizeof(msg_queue_t));
|
||||
if(!node){
|
||||
NODE_DBG("not enough memory\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
node->msg.data = (uint8_t *)c_zalloc(msg->length);
|
||||
if(!node->msg.data){
|
||||
NODE_DBG("not enough memory\n");
|
||||
c_free(node);
|
||||
return NULL;
|
||||
}
|
||||
c_memcpy(node->msg.data, msg->data, msg->length);
|
||||
node->msg.length = msg->length;
|
||||
node->next = NULL;
|
||||
node->msg_id = msg_id;
|
||||
node->msg_type = msg_type;
|
||||
node->publish_qos = publish_qos;
|
||||
|
||||
msg_queue_t *tail = *head;
|
||||
if(tail){
|
||||
while(tail->next!=NULL) tail = tail->next;
|
||||
tail->next = node;
|
||||
} else {
|
||||
*head = node;
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
void msg_destroy(msg_queue_t *node){
|
||||
if(!node) return;
|
||||
if(node->msg.data){
|
||||
c_free(node->msg.data);
|
||||
node->msg.data = NULL;
|
||||
}
|
||||
c_free(node);
|
||||
}
|
||||
|
||||
msg_queue_t * msg_dequeue(msg_queue_t **head){
|
||||
if(!head || !*head){
|
||||
return NULL;
|
||||
}
|
||||
msg_queue_t *node = *head; // fetch head.
|
||||
*head = node->next; // update head.
|
||||
node->next = NULL;
|
||||
return node;
|
||||
}
|
||||
|
||||
msg_queue_t * msg_peek(msg_queue_t **head){
|
||||
if(!head || !*head){
|
||||
return NULL;
|
||||
}
|
||||
return *head; // fetch head.
|
||||
}
|
||||
|
||||
int msg_size(msg_queue_t **head){
|
||||
if(!head || !*head){
|
||||
return 0;
|
||||
}
|
||||
int i = 1;
|
||||
msg_queue_t *tail = *head;
|
||||
if(tail){
|
||||
while(tail->next!=NULL){
|
||||
tail = tail->next;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
return i;
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
#ifndef _MSG_QUEUE_H
|
||||
#define _MSG_QUEUE_H 1
|
||||
#include "mqtt_msg.h"
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct msg_queue_t;
|
||||
|
||||
typedef struct msg_queue_t {
|
||||
struct msg_queue_t *next;
|
||||
mqtt_message_t msg;
|
||||
uint16_t msg_id;
|
||||
int msg_type;
|
||||
int publish_qos;
|
||||
} msg_queue_t;
|
||||
|
||||
msg_queue_t * msg_enqueue(msg_queue_t **head, mqtt_message_t *msg, uint16_t msg_id, int msg_type, int publish_qos);
|
||||
void msg_destroy(msg_queue_t *node);
|
||||
msg_queue_t * msg_dequeue(msg_queue_t **head);
|
||||
msg_queue_t * msg_peek(msg_queue_t **head);
|
||||
int msg_size(msg_queue_t **head);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
|
@ -321,7 +321,7 @@ bool flash_init_data_written(void)
|
|||
// FLASH SEC - 4
|
||||
uint32_t data[2] ICACHE_STORE_ATTR;
|
||||
#if defined(FLASH_SAFE_API)
|
||||
if (SPI_FLASH_RESULT_OK == flash_safe_read((flash_rom_get_sec_num() - 4) * SPI_FLASH_SEC_SIZE, (uint32 *)data, sizeof(data)))
|
||||
if (SPI_FLASH_RESULT_OK == flash_safe_read((flash_safe_get_sec_num() - 4) * SPI_FLASH_SEC_SIZE, (uint32 *)data, sizeof(data)))
|
||||
#else
|
||||
if (SPI_FLASH_RESULT_OK == spi_flash_read((flash_rom_get_sec_num() - 4) * SPI_FLASH_SEC_SIZE, (uint32 *)data, sizeof(data)))
|
||||
#endif // defined(FLASH_SAFE_API)
|
||||
|
@ -369,8 +369,8 @@ bool flash_init_data_blank(void)
|
|||
// It will init system config to blank!
|
||||
bool result = false;
|
||||
#if defined(FLASH_SAFE_API)
|
||||
if ((SPI_FLASH_RESULT_OK == flash_safe_erase_sector((flash_rom_get_sec_num() - 2))) &&
|
||||
(SPI_FLASH_RESULT_OK == flash_safe_erase_sector((flash_rom_get_sec_num() - 1))))
|
||||
if ((SPI_FLASH_RESULT_OK == flash_safe_erase_sector((flash_safe_get_sec_num() - 2))) &&
|
||||
(SPI_FLASH_RESULT_OK == flash_safe_erase_sector((flash_safe_get_sec_num() - 1))))
|
||||
#else
|
||||
if ((SPI_FLASH_RESULT_OK == spi_flash_erase_sector((flash_rom_get_sec_num() - 2))) &&
|
||||
(SPI_FLASH_RESULT_OK == spi_flash_erase_sector((flash_rom_get_sec_num() - 1))))
|
||||
|
@ -396,11 +396,25 @@ uint8_t byte_of_aligned_array(const uint8_t *aligned_array, uint32_t index)
|
|||
NODE_DBG("aligned_array is not 4-byte aligned.\n");
|
||||
return 0;
|
||||
}
|
||||
uint32_t v = ((uint32_t *)aligned_array)[ index / 4 ];
|
||||
volatile uint32_t v = ((uint32_t *)aligned_array)[ index / 4 ];
|
||||
uint8_t *p = (uint8_t *) (&v);
|
||||
return p[ (index % 4) ];
|
||||
}
|
||||
|
||||
uint16_t word_of_aligned_array(const uint16_t *aligned_array, uint32_t index)
|
||||
{
|
||||
if ( (((uint32_t)aligned_array) % 4) != 0 )
|
||||
{
|
||||
NODE_DBG("aligned_array is not 4-byte aligned.\n");
|
||||
return 0;
|
||||
}
|
||||
volatile uint32_t v = ((uint32_t *)aligned_array)[ index / 2 ];
|
||||
uint16_t *p = (uint16_t *) (&v);
|
||||
return (index % 2 == 0) ? p[ 0 ] : p[ 1 ];
|
||||
// return p[ (index % 2) ]; // -- why error???
|
||||
// (byte_of_aligned_array((uint8_t *)aligned_array, index * 2 + 1) << 8) | byte_of_aligned_array((uint8_t *)aligned_array, index * 2);
|
||||
}
|
||||
|
||||
// uint8_t flash_rom_get_checksum(void)
|
||||
// {
|
||||
// // SPIFlashInfo spi_flash_info ICACHE_STORE_ATTR = flash_rom_getinfo();
|
||||
|
|
|
@ -107,6 +107,7 @@ bool flash_init_data_default(void);
|
|||
bool flash_init_data_blank(void);
|
||||
bool flash_self_destruct(void);
|
||||
uint8_t byte_of_aligned_array(const uint8_t* aligned_array, uint32_t index);
|
||||
uint16_t word_of_aligned_array(const uint16_t *aligned_array, uint32_t index);
|
||||
// uint8_t flash_rom_get_checksum(void);
|
||||
// uint8_t flash_rom_calc_checksum(void);
|
||||
|
||||
|
|
|
@ -71,6 +71,9 @@
|
|||
#define fs_rename myspiffs_rename
|
||||
#define fs_size myspiffs_size
|
||||
|
||||
#define fs_mount myspiffs_mount
|
||||
#define fs_unmount myspiffs_unmount
|
||||
|
||||
#define FS_NAME_MAX_LENGTH SPIFFS_OBJ_NAME_LEN
|
||||
|
||||
#endif
|
||||
|
|
|
@ -42,7 +42,7 @@ The small 4KB sectors allow for greater flexibility in applications th
|
|||
|
||||
********************/
|
||||
|
||||
void spiffs_mount() {
|
||||
void myspiffs_mount() {
|
||||
spiffs_config cfg;
|
||||
cfg.phys_addr = ( u32_t )platform_flash_get_first_free_block_address( NULL );
|
||||
cfg.phys_addr += 0x3000;
|
||||
|
@ -69,6 +69,10 @@ void spiffs_mount() {
|
|||
NODE_DBG("mount res: %i\n", res);
|
||||
}
|
||||
|
||||
void myspiffs_unmount() {
|
||||
SPIFFS_unmount(&fs);
|
||||
}
|
||||
|
||||
// FS formatting function
|
||||
// Returns 1 if OK, 0 for error
|
||||
int myspiffs_format( void )
|
||||
|
@ -85,7 +89,7 @@ int myspiffs_format( void )
|
|||
while( sect_first <= sect_last )
|
||||
if( platform_flash_erase_sector( sect_first ++ ) == PLATFORM_ERR )
|
||||
return 0;
|
||||
spiffs_mount();
|
||||
myspiffs_mount();
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
|
|
@ -477,6 +477,8 @@ u32_t SPIFFS_buffer_bytes_for_cache(spiffs *fs, u32_t num_pages);
|
|||
#if SPIFFS_CACHE
|
||||
#endif
|
||||
|
||||
void myspiffs_mount();
|
||||
void myspiffs_unmount();
|
||||
int myspiffs_open(const char *name, int flags);
|
||||
int myspiffs_close( int fd );
|
||||
size_t myspiffs_write( int fd, const void* ptr, size_t len );
|
||||
|
|
|
@ -44,6 +44,7 @@ INCLUDES += -I ../libc
|
|||
INCLUDES += -I ../platform
|
||||
INCLUDES += -I ../lua
|
||||
INCLUDES += -I ../wofs
|
||||
INCLUDES += -I ../spiffs
|
||||
PDIR := ../$(PDIR)
|
||||
sinclude $(PDIR)Makefile
|
||||
|
||||
|
|
|
@ -14,8 +14,7 @@
|
|||
#include "c_stdlib.h"
|
||||
#include "c_stdio.h"
|
||||
|
||||
#include "romfs.h"
|
||||
|
||||
#include "flash_fs.h"
|
||||
#include "user_interface.h"
|
||||
|
||||
#include "ets_sys.h"
|
||||
|
@ -44,7 +43,6 @@ void task_init(void){
|
|||
system_os_task(task_lua, USER_TASK_PRIO_0, taskQueue, TASK_QUEUE_LEN);
|
||||
}
|
||||
|
||||
extern void spiffs_mount();
|
||||
// extern void test_spiffs();
|
||||
// extern int test_romfs();
|
||||
|
||||
|
@ -69,7 +67,16 @@ void nodemcu_init(void)
|
|||
// Flash init data at FLASHSIZE - 0x04000 Byte.
|
||||
flash_init_data_default();
|
||||
// Flash blank data at FLASHSIZE - 0x02000 Byte.
|
||||
flash_init_data_blank();
|
||||
flash_init_data_blank();
|
||||
if( !fs_format() )
|
||||
{
|
||||
NODE_ERR( "\ni*** 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)
|
||||
|
||||
|
@ -94,7 +101,7 @@ void nodemcu_init(void)
|
|||
|
||||
// test_romfs();
|
||||
#elif defined ( BUILD_SPIFFS )
|
||||
spiffs_mount();
|
||||
fs_mount();
|
||||
// test_spiffs();
|
||||
#endif
|
||||
// endpoint_setup();
|
||||
|
|
|
@ -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
|
||||
print("MEM: "..node.heap())
|
||||
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)
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
--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)
|
|
@ -0,0 +1 @@
|
|||
print("hello NodeMCU")
|
|
@ -5,7 +5,7 @@ MEMORY
|
|||
dport0_0_seg : org = 0x3FF00000, len = 0x10
|
||||
dram0_0_seg : org = 0x3FFE8000, len = 0x14000
|
||||
iram1_0_seg : org = 0x40100000, len = 0x8000
|
||||
irom0_0_seg : org = 0x40210000, len = 0x5A000
|
||||
irom0_0_seg : org = 0x40210000, len = 0x60000
|
||||
}
|
||||
|
||||
PHDRS
|
||||
|
|
|
@ -0,0 +1,98 @@
|
|||
--[[
|
||||
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)
|
|
@ -0,0 +1,33 @@
|
|||
-- 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)
|
|
@ -0,0 +1,56 @@
|
|||
-- 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"}
|
|
@ -0,0 +1,98 @@
|
|||
--[[
|
||||
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)
|
|
@ -2,9 +2,9 @@
|
|||
-- setup I2c and connect display
|
||||
function init_i2c_display()
|
||||
-- SDA and SCL can be assigned freely to available GPIOs
|
||||
sda = 5 -- GPIO14
|
||||
scl = 6 -- GPIO12
|
||||
sla = 0x3c
|
||||
local sda = 5 -- GPIO14
|
||||
local scl = 6 -- GPIO12
|
||||
local sla = 0x3c
|
||||
i2c.setup(0, sda, scl, i2c.SLOW)
|
||||
disp = u8g.ssd1306_128x64_i2c(sla)
|
||||
end
|
||||
|
@ -15,9 +15,9 @@ function init_spi_display()
|
|||
-- Hardware SPI MOSI = GPIO13
|
||||
-- Hardware SPI MISO = GPIO12 (not used)
|
||||
-- CS, D/C, and RES can be assigned freely to available GPIOs
|
||||
cs = 8 -- GPIO15, pull-down 10k to GND
|
||||
dc = 4 -- GPIO2
|
||||
res = 0 -- GPIO16
|
||||
local cs = 8 -- GPIO15, pull-down 10k to GND
|
||||
local dc = 4 -- GPIO2
|
||||
local res = 0 -- GPIO16
|
||||
|
||||
spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, spi.DATABITS_8, 0)
|
||||
disp = u8g.ssd1306_128x64_spi(cs, dc, res)
|
||||
|
|
|
@ -2,9 +2,9 @@
|
|||
-- setup I2c and connect display
|
||||
function init_i2c_display()
|
||||
-- SDA and SCL can be assigned freely to available GPIOs
|
||||
sda = 5 -- GPIO14
|
||||
scl = 6 -- GPIO12
|
||||
sla = 0x3c
|
||||
local sda = 5 -- GPIO14
|
||||
local scl = 6 -- GPIO12
|
||||
local sla = 0x3c
|
||||
i2c.setup(0, sda, scl, i2c.SLOW)
|
||||
disp = u8g.ssd1306_128x64_i2c(sla)
|
||||
end
|
||||
|
@ -15,9 +15,9 @@ function init_spi_display()
|
|||
-- Hardware SPI MOSI = GPIO13
|
||||
-- Hardware SPI MISO = GPIO12 (not used)
|
||||
-- CS, D/C, and RES can be assigned freely to available GPIOs
|
||||
cs = 8 -- GPIO15, pull-down 10k to GND
|
||||
dc = 4 -- GPIO2
|
||||
res = 0 -- GPIO16
|
||||
local cs = 8 -- GPIO15, pull-down 10k to GND
|
||||
local dc = 4 -- GPIO2
|
||||
local res = 0 -- GPIO16
|
||||
|
||||
spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, spi.DATABITS_8, 0)
|
||||
disp = u8g.ssd1306_128x64_spi(cs, dc, res)
|
||||
|
@ -91,17 +91,6 @@ function ascii_1()
|
|||
end
|
||||
end
|
||||
|
||||
function ascii_2()
|
||||
local x, y, s
|
||||
disp:drawStr(0, 0, "ASCII page 2")
|
||||
for y = 0, 5, 1 do
|
||||
for x = 0, 15, 1 do
|
||||
s = y*16 + x + 160
|
||||
disp:drawStr(x*7, y*10+10, string.char(s))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function extra_page(a)
|
||||
disp:drawStr(0, 12, "setScale2x2")
|
||||
disp:setScale2x2()
|
||||
|
@ -131,8 +120,6 @@ function draw(draw_state)
|
|||
elseif (component == 6) then
|
||||
ascii_1()
|
||||
elseif (component == 7) then
|
||||
ascii_2()
|
||||
elseif (component == 8) then
|
||||
extra_page(bit.band(draw_state, 7))
|
||||
end
|
||||
end
|
||||
|
|
|
@ -2,9 +2,9 @@
|
|||
-- setup I2c and connect display
|
||||
function init_i2c_display()
|
||||
-- SDA and SCL can be assigned freely to available GPIOs
|
||||
sda = 5 -- GPIO14
|
||||
scl = 6 -- GPIO12
|
||||
sla = 0x3c
|
||||
local sda = 5 -- GPIO14
|
||||
local scl = 6 -- GPIO12
|
||||
local sla = 0x3c
|
||||
i2c.setup(0, sda, scl, i2c.SLOW)
|
||||
disp = u8g.ssd1306_128x64_i2c(sla)
|
||||
end
|
||||
|
@ -15,9 +15,9 @@ function init_spi_display()
|
|||
-- Hardware SPI MOSI = GPIO13
|
||||
-- Hardware SPI MISO = GPIO12 (not used)
|
||||
-- CS, D/C, and RES can be assigned freely to available GPIOs
|
||||
cs = 8 -- GPIO15, pull-down 10k to GND
|
||||
dc = 4 -- GPIO2
|
||||
res = 0 -- GPIO16
|
||||
local cs = 8 -- GPIO15, pull-down 10k to GND
|
||||
local dc = 4 -- GPIO2
|
||||
local res = 0 -- GPIO16
|
||||
|
||||
spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, spi.DATABITS_8, 0)
|
||||
disp = u8g.ssd1306_128x64_spi(cs, dc, res)
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
# DHTxx module
|
||||
|
||||
This module is compatible with DHT11, DHT21 and DHT22.
|
||||
And is able to auto-select wheather you are using DHT11 or DHT2x
|
||||
|
||||
No need to use a resistor to connect the pin data of DHT22 to ESP8266.
|
||||
|
||||
##Integer Verison[When using DHT11, Float version is useless...]
|
||||
|
@ -10,14 +12,13 @@ PIN = 4 -- data pin, GPIO2
|
|||
|
||||
DHT= require("dht_lib")
|
||||
|
||||
--dht.read11(PIN)
|
||||
DHT.read22(PIN)
|
||||
DHT.read(PIN)
|
||||
|
||||
t = DHT.getTemperature()
|
||||
h = DHT.getHumidity()
|
||||
|
||||
if h == nil then
|
||||
print("Error reading from DHT11/22")
|
||||
print("Error reading from DHTxx")
|
||||
else
|
||||
-- temperature in degrees Celsius and Farenheit
|
||||
|
||||
|
@ -41,8 +42,7 @@ PIN = 4 -- data pin, GPIO2
|
|||
|
||||
DHT= require("dht_lib")
|
||||
|
||||
--dht.read11(PIN)
|
||||
DHT.read22(PIN)
|
||||
DHT.read(PIN)
|
||||
|
||||
t = DHT.getTemperature()
|
||||
h = DHT.getHumidity()
|
||||
|
@ -52,11 +52,12 @@ if h == nil then
|
|||
else
|
||||
-- temperature in degrees Celsius and Farenheit
|
||||
-- floating point and integer version:
|
||||
print("Temperature: "..t.." deg C")
|
||||
|
||||
print("Temperature: "..(t/10).." deg C")
|
||||
print("Temperature: "..(9 * t / 50 + 32).." deg F")
|
||||
|
||||
-- humidity
|
||||
print("Humidity: "..h.."%")
|
||||
print("Humidity: "..(h/10).."%")
|
||||
end
|
||||
|
||||
-- release module
|
||||
|
@ -64,12 +65,10 @@ DHT = nil
|
|||
package.loaded["dht_lib"]=nil
|
||||
```
|
||||
## Functions
|
||||
### read11
|
||||
read11(pin)
|
||||
Read humidity and temperature from DHT11.
|
||||
###read22
|
||||
read22(pin)
|
||||
Read humidity and temperature from DHT22/21.
|
||||
|
||||
###read
|
||||
read(pin)
|
||||
Read humidity and temperature from DHTxx(11,21,22...).
|
||||
**Parameters:**
|
||||
|
||||
* pin - ESP8266 pin connect to data pin
|
||||
|
@ -88,4 +87,3 @@ Returns the temperature of the last reading.
|
|||
**Returns:**
|
||||
* last temperature reading in(dht22) 0.1ºC (dht11)1ºC
|
||||
*
|
||||
|
|
@ -8,10 +8,13 @@
|
|||
-- ***************************************************************************
|
||||
|
||||
--Support list:
|
||||
--DHT11 Tested ->read11
|
||||
--DHT21 Not Tested->read22
|
||||
--DHT22 Tested->read22
|
||||
|
||||
--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 M = {}
|
||||
|
@ -37,8 +40,6 @@ local function read(pin)
|
|||
bitStream[j] = 0
|
||||
end
|
||||
|
||||
|
||||
|
||||
-- Step 1: send out start signal to DHT22
|
||||
gpio.mode(pin, gpio.OUTPUT)
|
||||
gpio.write(pin, gpio.HIGH)
|
||||
|
@ -57,7 +58,7 @@ local function read(pin)
|
|||
while (gpio_read(pin) == 0 ) do end
|
||||
c=0
|
||||
while (gpio_read(pin) == 1 and c < 500) do c = c + 1 end
|
||||
|
||||
|
||||
-- Step 3: DHT22 send data
|
||||
for j = 1, 40, 1 do
|
||||
while (gpio_read(pin) == 1 and bitlength < 10 ) do
|
||||
|
@ -69,68 +70,79 @@ local function read(pin)
|
|||
while (gpio_read(pin) == 0) do end
|
||||
end
|
||||
end
|
||||
---------------------------Convert the bitStream into Number through DHT11 Ways--------------------------
|
||||
function M.read11(pin)
|
||||
--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
|
||||
|
||||
---------------------------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)
|
||||
local checksum = 0
|
||||
local checksumTest
|
||||
--DHT data acquired, process.
|
||||
|
||||
local byte_0 = 0
|
||||
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
|
||||
humidity = humidity + 2 ^ (8 - i)
|
||||
end
|
||||
end
|
||||
for i = 1, 8, 1 do -- Byte[2]
|
||||
if (bitStream[i + 16] > 3) then
|
||||
temperature = temperature + 2 ^ (8 - i)
|
||||
end
|
||||
end
|
||||
for i = 1, 8, 1 do --Byte[4]
|
||||
if (bitStream[i + 32] > 3) then
|
||||
checksum = checksum + 2 ^ (8 - i)
|
||||
byte_0 = byte_0 + 2 ^ (8 - i)
|
||||
end
|
||||
end
|
||||
|
||||
if(checksum ~= humidity+temperature) then
|
||||
humidity = nil
|
||||
temperature = nil
|
||||
end
|
||||
|
||||
end
|
||||
---------------------------Convert the bitStream into Number through DHT22 Ways--------------------------
|
||||
function M.read22( pin )
|
||||
--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
|
||||
read(pin)
|
||||
local checksum = 0
|
||||
local checksumTest
|
||||
--DHT data acquired, process.
|
||||
for i = 1, 16, 1 do
|
||||
if (bitStream[i] > 3) then
|
||||
humidity = humidity + 2 ^ (16 - i)
|
||||
for i = 1, 8, 1 do -- Byte[1]
|
||||
if (bitStream[i+8] > 3) then
|
||||
byte_1 = byte_1 + 2 ^ (8 - i)
|
||||
end
|
||||
end
|
||||
for i = 1, 16, 1 do
|
||||
if (bitStream[i + 16] > 3) then
|
||||
temperature = temperature + 2 ^ (16 - i)
|
||||
|
||||
for i = 1, 8, 1 do -- Byte[2]
|
||||
if (bitStream[i+16] > 3) then
|
||||
byte_2 = byte_2 + 2 ^ (8 - i)
|
||||
end
|
||||
end
|
||||
for i = 1, 8, 1 do
|
||||
if (bitStream[i + 32] > 3) then
|
||||
checksum = checksum + 2 ^ (8 - i)
|
||||
|
||||
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(checksumTest, 0xFF)
|
||||
|
||||
|
@ -143,9 +155,18 @@ function M.read22( pin )
|
|||
if (checksumTest - checksum >= 1) or (checksum - checksumTest >= 1) then
|
||||
humidity = nil
|
||||
end
|
||||
end
|
||||
|
||||
---------------------------Check out the data--------------------------
|
||||
end
|
||||
|
||||
byte_0 = nil
|
||||
byte_1 = nil
|
||||
byte_2 = nil
|
||||
byte_3 = nil
|
||||
byte_4 = nil
|
||||
|
||||
end
|
||||
--------------API for geting the data out------------------
|
||||
|
||||
function M.getTemperature()
|
||||
return temperature
|
||||
end
|
||||
|
@ -153,5 +174,5 @@ end
|
|||
function M.getHumidity()
|
||||
return humidity
|
||||
end
|
||||
|
||||
-------------Return Index------------------------------------
|
||||
return M
|
|
@ -0,0 +1,25 @@
|
|||
-- ***************************************************************************
|
||||
-- Example for Yeelink Lib
|
||||
--
|
||||
-- Written by Martin
|
||||
--
|
||||
--
|
||||
-- MIT license, http://opensource.org/licenses/MIT
|
||||
-- ***************************************************************************
|
||||
|
||||
wifi.setmode(wifi.STATION) --Step1: Connect to Wifi
|
||||
wifi.sta.config("SSID","Password")
|
||||
|
||||
dht = require("dht_lib") --Step2: "Require" the libs
|
||||
yeelink = require("yeelink_lib")
|
||||
|
||||
yeelink.init(23333,23333,"You api-key",function() --Step3: Register the callback function
|
||||
|
||||
print("Yeelink Init OK...")
|
||||
tmr.alarm(1,60000,1,function() --Step4: Have fun~ (Update your data)
|
||||
|
||||
dht.read(4)
|
||||
yeelink.update(dht.getTemperature())
|
||||
|
||||
end)
|
||||
end)
|
|
@ -0,0 +1,134 @@
|
|||
|
||||
-- ***************************************************************************
|
||||
-- Yeelink Updata Libiary Version 0.1.2 r1
|
||||
--
|
||||
-- Written by Martin
|
||||
-- but based on a script of zhouxu_o from bbs.nodemcu.com
|
||||
--
|
||||
-- MIT license, http://opensource.org/licenses/MIT
|
||||
-- ***************************************************************************
|
||||
--==========================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)=====
|
||||
|
||||
if wifi.sta.getip() == nil then
|
||||
print("Please Connect WIFI First")
|
||||
tmr.alarm(1,1000,1,function ()
|
||||
if wifi.sta.getip() ~= nil then
|
||||
tmr.stop(1)
|
||||
sk:dns("api.yeelink.net",function(conn,ip)
|
||||
dns=ip
|
||||
print("DNS YEELINK OK... IP: "..dns)
|
||||
end)
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
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
|
||||
tmr.alarm(2,5000,1,function ()
|
||||
if dns == "0.0.0.0" then
|
||||
print("Waiting for DNS...")
|
||||
end
|
||||
end)
|
||||
return false
|
||||
else
|
||||
return dns
|
||||
end
|
||||
end
|
||||
--========Check the DNS Status===========
|
||||
--if DNS success, return the address(string)
|
||||
--if DNS fail(or processing), return nil
|
||||
--
|
||||
--
|
||||
--========================================
|
||||
function M.getDNS()
|
||||
|
||||
if dns == "0.0.0.0" then
|
||||
return nil
|
||||
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
|
162
tools/esptool.py
162
tools/esptool.py
|
@ -41,7 +41,7 @@ class ESPROM:
|
|||
|
||||
# Maximum block sized for RAM and Flash writes, respectively.
|
||||
ESP_RAM_BLOCK = 0x1800
|
||||
ESP_FLASH_BLOCK = 0x100
|
||||
ESP_FLASH_BLOCK = 0x400
|
||||
|
||||
# Default baudrate. The ROM auto-bauds, so we can use more or less whatever we want.
|
||||
ESP_ROM_BAUD = 115200
|
||||
|
@ -56,6 +56,12 @@ class ESPROM:
|
|||
ESP_OTP_MAC0 = 0x3ff00050
|
||||
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):
|
||||
self._port = serial.Serial(port, baud)
|
||||
|
||||
|
@ -78,15 +84,7 @@ class ESPROM:
|
|||
|
||||
""" Write bytes to the serial port while performing SLIP escaping """
|
||||
def write(self, packet):
|
||||
buf = '\xc0'
|
||||
for b in packet:
|
||||
if b == '\xc0':
|
||||
buf += '\xdb\xdc'
|
||||
elif b == '\xdb':
|
||||
buf += '\xdb\xdd'
|
||||
else:
|
||||
buf += b
|
||||
buf += '\xc0'
|
||||
buf = '\xc0'+(packet.replace('\xdb','\xdb\xdd').replace('\xc0','\xdb\xdc'))+'\xc0'
|
||||
self._port.write(buf)
|
||||
|
||||
""" Calculate checksum of a blob, as it is defined by the ROM """
|
||||
|
@ -132,11 +130,25 @@ class ESPROM:
|
|||
|
||||
# RTS = CH_PD (i.e reset)
|
||||
# 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.setDTR(True)
|
||||
self._port.setRTS(False)
|
||||
time.sleep(0.1)
|
||||
self._port.setRTS(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
|
||||
for i in xrange(10):
|
||||
|
@ -209,16 +221,78 @@ class ESPROM:
|
|||
self.flash_begin(0, 0)
|
||||
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:
|
||||
|
||||
def __init__(self, filename = None):
|
||||
self.segments = []
|
||||
self.entrypoint = 0
|
||||
self.flash_mode = 0
|
||||
self.flash_size_freq = 0
|
||||
|
||||
if filename is not None:
|
||||
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
|
||||
if magic != ESPROM.ESP_IMAGE_MAGIC or segments > 16:
|
||||
|
@ -246,7 +320,8 @@ class ESPFirmwareImage:
|
|||
|
||||
def save(self, filename):
|
||||
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
|
||||
for (offset, size, data) in self.segments:
|
||||
|
@ -346,6 +421,12 @@ if __name__ == '__main__':
|
|||
'write_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('--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(
|
||||
'run',
|
||||
|
@ -369,11 +450,32 @@ if __name__ == '__main__':
|
|||
help = 'Create an application image from 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('--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(
|
||||
'read_mac',
|
||||
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()
|
||||
|
||||
# Create the ESPROM connection object, if needed
|
||||
|
@ -421,6 +523,12 @@ if __name__ == '__main__':
|
|||
|
||||
elif args.operation == 'write_flash':
|
||||
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:
|
||||
address = int(args.addr_filename[0], 0)
|
||||
filename = args.addr_filename[1]
|
||||
|
@ -434,7 +542,11 @@ if __name__ == '__main__':
|
|||
print '\rWriting at 0x%08x... (%d %%)' % (address + seq*esp.ESP_FLASH_BLOCK, 100*(seq+1)/blocks),
|
||||
sys.stdout.flush()
|
||||
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)
|
||||
image = image[esp.ESP_FLASH_BLOCK:]
|
||||
seq += 1
|
||||
|
@ -478,6 +590,11 @@ if __name__ == '__main__':
|
|||
for section, start in ((".text", "_text_start"), (".data", "_data_start"), (".rodata", "_rodata_start")):
|
||||
data = e.load_section(section)
|
||||
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")
|
||||
data = e.load_section(".irom0.text")
|
||||
off = e.get_symbol_addr("_irom0_text_start") - 0x40200000
|
||||
|
@ -487,6 +604,17 @@ if __name__ == '__main__':
|
|||
f.close()
|
||||
|
||||
elif args.operation == 'read_mac':
|
||||
mac0 = esp.read_reg(esp.ESP_OTP_MAC0)
|
||||
mac1 = esp.read_reg(esp.ESP_OTP_MAC1)
|
||||
print 'MAC: 18:fe:34:%02x:%02x:%02x' % ((mac1 >> 8) & 0xff, mac1 & 0xff, (mac0 >> 24) & 0xff)
|
||||
mac = esp.read_mac()
|
||||
print 'MAC: %s' % ':'.join(map(lambda x: '%02x'%x, mac))
|
||||
|
||||
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()
|
||||
|
|
Loading…
Reference in New Issue