From f19e24c7ac77e8023525556ac00e5ceb24dd5741 Mon Sep 17 00:00:00 2001 From: Kirill Date: Fri, 10 Apr 2015 14:56:17 +0500 Subject: [PATCH 1/7] Update tmr.c According to NodeMCU team recommendations. --- app/modules/tmr.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/app/modules/tmr.c b/app/modules/tmr.c index 1a0fa478..ee92f4d5 100644 --- a/app/modules/tmr.c +++ b/app/modules/tmr.c @@ -71,6 +71,12 @@ static int tmr_delay( lua_State* L ) us = luaL_checkinteger( L, 1 ); if ( us <= 0 ) return luaL_error( L, "wrong arg range" ); + if(us<1000000) + { + os_delay_us( us ); + WRITE_PERI_REG(0x60000914, 0x73); + return 0; + } unsigned sec = (unsigned)us / 1000000; unsigned remain = (unsigned)us % 1000000; int i = 0; From 4437c46825b34ced12a5e6babd4d681067c79db1 Mon Sep 17 00:00:00 2001 From: Sander Boele Date: Sat, 11 Apr 2015 11:26:44 +0200 Subject: [PATCH 2/7] fixed floating point problem in bcd conversions --- lua_modules/ds3231/ds3231.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lua_modules/ds3231/ds3231.lua b/lua_modules/ds3231/ds3231.lua index 48b0fd86..d13b89e8 100644 --- a/lua_modules/ds3231/ds3231.lua +++ b/lua_modules/ds3231/ds3231.lua @@ -16,11 +16,11 @@ local id = 0 local dev_addr = 0x68 local function decToBcd(val) - return((val/10*16) + (val%10)) + return((((val/10) - ((val/10)%1)) *16) + (val%10)) end local function bcdToDec(val) - return((val/16*10) + (val%16)) + return((((val/16) - ((val/16)%1)) *10) + (val%16)) end -- initialize i2c From f68d41608e173332be3b2daa95e03d901d817124 Mon Sep 17 00:00:00 2001 From: Kevin Uhlir Date: Wed, 15 Apr 2015 03:41:13 -0500 Subject: [PATCH 3/7] Add multicastJoin and multicastLeave to net module net.multicastJoin(if_ip, multicast_ip) net.multicastLeave(if_ip, multicast_ip) if_ip is a string containing the interface ip to join/leave the multicast group. multicast_ip is the multicast ip of the group to join/leave. if_ip can be "" or "any" to affect all interfaces. --- app/modules/net.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/app/modules/net.c b/app/modules/net.c index 7fd785ab..dbd072f1 100644 --- a/app/modules/net.c +++ b/app/modules/net.c @@ -1119,7 +1119,6 @@ static int net_dns( lua_State* L, const char* mt ) return 0; } - // Lua: s = net.createServer(type, function(server)) static int net_createServer( lua_State* L ) { @@ -1284,6 +1283,55 @@ static int net_socket_dns( lua_State* L ) return net_dns(L, mt); } +static int net_multicastJoinLeave( lua_State *L, int join) +{ + size_t il; + ip_addr_t multicast_addr; + ip_addr_t if_addr; + const char *multicast_ip; + const char *if_ip; + + NODE_DBG("net_multicastJoin is called.\n"); + if(! lua_isstring(L,1) ) return luaL_error( L, "wrong arg type" ); + if_ip = luaL_checklstring( L, 1, &il ); + if (if_ip != NULL) + if ( if_ip[0] == '\0' || stricmp(if_ip,"any") == 0) + { + if_ip = "0.0.0.0"; + il = 7; + } + if (if_ip == NULL || il > 15 || il < 7) return luaL_error( L, "invalid if ip" ); + if_addr.addr = ipaddr_addr(if_ip); + + if(! lua_isstring(L,2) ) return luaL_error( L, "wrong arg type" ); + multicast_ip = luaL_checklstring( L, 2, &il ); + if (multicast_ip == NULL || il > 15 || il < 7) return luaL_error( L, "invalid multicast ip" ); + multicast_addr.addr = ipaddr_addr(multicast_ip); + if (join) + { + espconn_igmp_join(&if_addr, &multicast_addr); + } + else + { + espconn_igmp_leave(&if_addr, &multicast_addr); + } + return 0; +} +// Lua: net.multicastJoin(ifip, multicastip) +// if ifip "" or "any" all interfaces are affected +static int net_multicastJoin( lua_State* L ) +{ + return net_multicastJoinLeave(L,1); +} + +// Lua: net.multicastLeave(ifip, multicastip) +// if ifip "" or "any" all interfaces are affected +static int net_multicastLeave( lua_State* L ) +{ + return net_multicastJoinLeave(L,0); +} + + #if 0 static int net_array_index( lua_State* L ) { @@ -1358,6 +1406,8 @@ const LUA_REG_TYPE net_map[] = { { LSTRKEY( "createServer" ), LFUNCVAL ( net_createServer ) }, { LSTRKEY( "createConnection" ), LFUNCVAL ( net_createConnection ) }, + { LSTRKEY( "multicastJoin"), LFUNCVAL( net_multicastJoin ) }, + { LSTRKEY( "multicastLeave"), LFUNCVAL( net_multicastLeave ) }, #if LUA_OPTIMIZE_MEMORY > 0 { LSTRKEY( "TCP" ), LNUMVAL( TCP ) }, { LSTRKEY( "UDP" ), LNUMVAL( UDP ) }, From 4c2ad0f54545cf0bfd5c29a1b0a7e754f65edbb1 Mon Sep 17 00:00:00 2001 From: robertfoss Date: Thu, 14 May 2015 11:27:43 -0400 Subject: [PATCH 4/7] Add -Os flag to release and debug builds To fit the build on boards with 512K of flash, build using -Os flag. --- app/Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/Makefile b/app/Makefile index 4d04d7f5..a1c0d66a 100644 --- a/app/Makefile +++ b/app/Makefile @@ -53,11 +53,11 @@ TARGET_LDFLAGS = \ --text-section-literals ifeq ($(FLAVOR),debug) - TARGET_LDFLAGS += -g -O2 + TARGET_LDFLAGS += -g -Os endif ifeq ($(FLAVOR),release) - TARGET_LDFLAGS += -g -O0 + TARGET_LDFLAGS += -Os endif LD_FILE = $(LDDIR)/eagle.app.v6.ld From bcfe75334118a8706a5f52c90ceefcb1f36f6816 Mon Sep 17 00:00:00 2001 From: BruceMe Date: Sun, 31 May 2015 09:08:07 -0500 Subject: [PATCH 5/7] ADC driving RGB LED; electronic mood ring It's as simple as ADC (analog to digital converter); easy as RGB (Color LED)... This example can be run on any ESP8266, but it runs out of the box on the ebay search for.. "ESP8266 Battery Holder" with the light sensor on ADC0 and RGB LED. Fun and easy way to do more than blink a light. --- lua_examples/adc_rgb.lua | 43 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 lua_examples/adc_rgb.lua diff --git a/lua_examples/adc_rgb.lua b/lua_examples/adc_rgb.lua new file mode 100644 index 00000000..ff8b70e6 --- /dev/null +++ b/lua_examples/adc_rgb.lua @@ -0,0 +1,43 @@ +-- +-- Light sensor on ADC(0), RGB LED connected to gpio12(6) Green, gpio13(7) Blue & gpio15(8) Red. +-- This works out of the box on the typical ESP8266 evaluation boards with Battery Holder +-- +-- It uses the input from the sensor to drive a "rainbow" effect on the RGB LED +-- Includes a very "pseudoSin" function +-- + +function led(r,Sg,b) + pwm.setduty(8,r) + pwm.setduty(6,g) + pwm.setduty(7,b) +end + +-- this is perhaps the lightest weight sin function in existance +-- Given an integer from 0..128, 0..512 appximating 256 + 256 * sin(idx*Pi/256) +-- This is first order square approximation of sin, it's accurate around 0 and any multiple of 128 (Pi/2), +-- 92% accurate at 64 (Pi/4). +function pseudoSin (idx) + idx = idx % 128 + lookUp = 32 - idx % 64 + val = 256 - (lookUp * lookUp) / 4 + if (idx > 64) then + val = - val; + end + return 256+val +end + +pwm.setup(6,500,512) +pwm.setup(7,500,512) +pwm.setup(8,500,512) +pwm.start(6) +pwm.start(7) +pwm.start(8) + +tmr.alarm(1,20,1,function() + idx = 3 * adc.read(0) / 2 + r = pseudoSin(idx) + g = pseudoSin(idx + 43) + b = pseudoSin(idx + 85) + led(r,g,b) + idx = (idx + 1) % 128 + end) From d1fe4e620a5438c5bff503b814ffe2116507e6fe Mon Sep 17 00:00:00 2001 From: BruceMe Date: Sun, 31 May 2015 09:09:05 -0500 Subject: [PATCH 6/7] HTTP Server for LED/Servo A simple yet robust HTTP server that can parse GET parameters and turn on/off a light or set the position of a servo. --- lua_examples/http_server | 121 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 lua_examples/http_server diff --git a/lua_examples/http_server b/lua_examples/http_server new file mode 100644 index 00000000..c9ad275a --- /dev/null +++ b/lua_examples/http_server @@ -0,0 +1,121 @@ +-- +-- Simple NodeMCU web server (done is a not so nodeie fashion :-) +-- +-- Highly modified by Bruce Meacham, based on work by Scott Beasley 2015 +-- Open and free to change and use. Enjoy. [Beasley/Meacham 2015] +-- +-- Meacham Update: I streamlined/improved the parsing to focus on simple HTTP GET request and their simple parameters +-- Also added the code to drive a servo/light. Comment out as you see fit. +-- +-- Usage: +-- Change SSID and SSID_PASSPHRASE for your wifi network +-- Download to NodeMCU +-- node.compile("http_server.lua") +-- dofile("http_server.lc") +-- When the server is esablished it will output the IP address. +-- http://{ip address}/?s0=1200&light=1 +-- s0 is the servo position (actually the PWM hertz), 500 - 2000 are all good values +-- light chanel high(1)/low(0), some evaluation boards have LEDs pre-wired in a "pulled high" confguration, so '0' ground the emitter and turns it on backwards. +-- +-- Add to init.lua if you want it to autoboot. +-- + +-- Your Wifi connection data +local SSID = "YOUR WIFI SSID" +local SSID_PASSWORD = "YOUR SSID PASSPHRASE" + +-- General setup +local pinLight = 2 -- this is GPIO4 +gpio.mode(pinLight,gpio.OUTPUT) +gpio.write(pinLight,gpio.HIGH) + +servo = {} +servo.pin = 4 --this is GPIO2 +servo.value = 1500 +servo.id = "servo" +gpio.mode(servo.pin, gpio.OUTPUT) +gpio.write(servo.pin, gpio.LOW) + +-- This alarm drives the servo +tmr.alarm(0,10,1,function() -- 50Hz + if servo.value then -- generate pulse + gpio.write(servo.pin, gpio.HIGH) + tmr.delay(servo.value) + gpio.write(servo.pin, gpio.LOW) + end +end) + +local function connect (conn, data) + local query_data + + conn:on ("receive", + function (cn, req_data) + params = get_http_req (req_data) + cn:send("HTTP/1.1 200/OK\r\nServer: NodeLuau\r\nContent-Type: text/html\r\n\r\n") + cn:send ("

ESP8266 Servo & Light Server

\r\n") + if (params["light"] ~= nil) then + if ("0" == params["light"]) then + gpio.write(pinLight, gpio.LOW) + else + gpio.write(pinLight, gpio.HIGH) + end + end + + if (params["s0"] ~= nil) then + servo.value = tonumber(params["s0"]); + end + + -- Close the connection for the request + cn:close ( ) + end) +end + +-- Build and return a table of the http request data +function get_http_req (instr) + local t = {} + local str = string.sub(instr, 0, 200) + local v = string.gsub(split(str, ' ')[2], '+', ' ') + parts = split(v, '?') + local params = {} + if (table.maxn(parts) > 1) then + for idx,part in ipairs(split(parts[2], '&')) do + parmPart = split(part, '=') + params[parmPart[1]] = parmPart[2] + end + end + return params +end + +-- Source: http://lua-users.org/wiki/MakingLuaLikePhp +-- Credit: http://richard.warburton.it/ +function split(str, splitOn) + if (splitOn=='') then return false end + local pos,arr = 0,{} + for st,sp in function() return string.find(str,splitOn,pos,true) end do + table.insert(arr,string.sub(str,pos,st-1)) + pos = sp + 1 + end + table.insert(arr,string.sub(str,pos)) + return arr +end + +-- Configure the ESP as a station (client) +wifi.setmode (wifi.STATION) +wifi.sta.config (SSID, SSID_PASSWORD) +wifi.sta.autoconnect (1) + +-- Hang out until we get a wifi connection before the httpd server is started. +tmr.alarm (1, 800, 1, function ( ) + if wifi.sta.getip ( ) == nil then + print ("Waiting for Wifi connection") + else + tmr.stop (1) + print ("Config done, IP is " .. wifi.sta.getip ( )) + end +end) + +-- Create the httpd server +svr = net.createServer (net.TCP, 30) + +-- Server listening on port 80, call connect function if a request is received +svr:listen (80, connect) From 46a07700fbb551d79f5d18a802665f2e5b30ad98 Mon Sep 17 00:00:00 2001 From: BruceMe Date: Sun, 31 May 2015 09:09:23 -0500 Subject: [PATCH 7/7] Rename http_server to http_server.lua --- lua_examples/{http_server => http_server.lua} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename lua_examples/{http_server => http_server.lua} (100%) diff --git a/lua_examples/http_server b/lua_examples/http_server.lua similarity index 100% rename from lua_examples/http_server rename to lua_examples/http_server.lua