Merge pull request #365 from nodemcu/master

Add ICMP ping to net module
This commit is contained in:
Vowstar 2015-05-01 14:57:50 +08:00
commit 1923280572
6 changed files with 181 additions and 37 deletions

View File

@ -1119,7 +1119,6 @@ static int net_dns( lua_State* L, const char* mt )
return 0; return 0;
} }
// Lua: s = net.createServer(type, function(server)) // Lua: s = net.createServer(type, function(server))
static int net_createServer( lua_State* L ) static int net_createServer( lua_State* L )
{ {
@ -1284,6 +1283,55 @@ static int net_socket_dns( lua_State* L )
return net_dns(L, mt); 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 #if 0
static int net_array_index( lua_State* L ) static int net_array_index( lua_State* L )
{ {
@ -1358,6 +1406,8 @@ const LUA_REG_TYPE net_map[] =
{ {
{ LSTRKEY( "createServer" ), LFUNCVAL ( net_createServer ) }, { LSTRKEY( "createServer" ), LFUNCVAL ( net_createServer ) },
{ LSTRKEY( "createConnection" ), LFUNCVAL ( net_createConnection ) }, { LSTRKEY( "createConnection" ), LFUNCVAL ( net_createConnection ) },
{ LSTRKEY( "multicastJoin"), LFUNCVAL( net_multicastJoin ) },
{ LSTRKEY( "multicastLeave"), LFUNCVAL( net_multicastLeave ) },
#if LUA_OPTIMIZE_MEMORY > 0 #if LUA_OPTIMIZE_MEMORY > 0
{ LSTRKEY( "TCP" ), LNUMVAL( TCP ) }, { LSTRKEY( "TCP" ), LNUMVAL( TCP ) },
{ LSTRKEY( "UDP" ), LNUMVAL( UDP ) }, { LSTRKEY( "UDP" ), LNUMVAL( UDP ) },

View File

@ -71,6 +71,12 @@ static int tmr_delay( lua_State* L )
us = luaL_checkinteger( L, 1 ); us = luaL_checkinteger( L, 1 );
if ( us <= 0 ) if ( us <= 0 )
return luaL_error( L, "wrong arg range" ); 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 sec = (unsigned)us / 1000000;
unsigned remain = (unsigned)us % 1000000; unsigned remain = (unsigned)us % 1000000;
int i = 0; int i = 0;

View File

@ -14,7 +14,7 @@ sk:connect(80,"115.239.210.27")
sk:send("GET / HTTP/1.1\r\nHost: 115.239.210.27\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n") sk:send("GET / HTTP/1.1\r\nHost: 115.239.210.27\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n")
sk:connect(80,"192.168.0.66") sk:connect(80,"192.168.0.66")
sk:send("GET / HTTP/1.1\r\nHost: 1192.168.0.66\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n") sk:send("GET / HTTP/1.1\r\nHost: 192.168.0.66\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n")
i2c.setup(0,1,0,i2c.SLOW) i2c.setup(0,1,0,i2c.SLOW)
function read_bmp(addr) i2c.start(0) i2c.address(0,119,i2c.RECEIVER) c=i2c.read(0,1) i2c.stop(0) print(string.byte(c)) end function read_bmp(addr) i2c.start(0) i2c.address(0,119,i2c.RECEIVER) c=i2c.read(0,1) i2c.stop(0) print(string.byte(c)) end

View File

@ -1,46 +1,78 @@
# DHT22 module # DHTxx module
This module is compatible with DHT22 and DHT21. This module is compatible with DHT11, DHT21 and DHT22.
Supports nodemcu with or without floating point.
No need to use a resistor to connect the pin data of DHT22 to ESP8266. No need to use a resistor to connect the pin data of DHT22 to ESP8266.
## Example ##Integer Verison[When using DHT11, Float version is useless...]
### Example
```lua ```lua
PIN = 4 -- data pin, GPIO2 PIN = 4 -- data pin, GPIO2
dht22 = require("dht22") DHT= require("dht_lib")
dht22.read(PIN)
t = dht22.getTemperature() --dht.read11(PIN)
h = dht22.getHumidity() DHT.read22(PIN)
t = DHT.getTemperature()
h = DHT.getHumidity()
if h == nil then if h == nil then
print("Error reading from DHT22") print("Error reading from DHT11/22")
else else
-- temperature in degrees Celsius and Farenheit -- temperature in degrees Celsius and Farenheit
-- floating point and integer version:
print("Temperature: "..((t-(t % 10)) / 10).."."..(t % 10).." deg C") print("Temperature: "..((t-(t % 10)) / 10).."."..(t % 10).." deg C")
-- only integer version:
print("Temperature: "..(9 * t / 50 + 32).."."..(9 * t / 5 % 10).." deg F") print("Temperature: "..(9 * t / 50 + 32).."."..(9 * t / 5 % 10).." deg F")
-- only float point version:
print("Temperature: "..(9 * t / 50 + 32).." deg F")
-- humidity -- humidity
-- floating point and integer version
print("Humidity: "..((h - (h % 10)) / 10).."."..(h % 10).."%") print("Humidity: "..((h - (h % 10)) / 10).."."..(h % 10).."%")
end end
-- release module -- release module
dht22 = nil DHT = nil
package.loaded["dht22"]=nil package.loaded["dht_lib"]=nil
```
##Float Verison
###Example
```lua
PIN = 4 -- data pin, GPIO2
DHT= require("dht_lib")
--dht.read11(PIN)
DHT.read22(PIN)
t = DHT.getTemperature()
h = DHT.getHumidity()
if h == nil then
print("Error reading from DHT11/22")
else
-- temperature in degrees Celsius and Farenheit
-- floating point and integer version:
print("Temperature: "..t.." deg C")
print("Temperature: "..(9 * t / 50 + 32).." deg F")
-- humidity
print("Humidity: "..h.."%")
end
-- release module
DHT = nil
package.loaded["dht_lib"]=nil
``` ```
## Functions ## Functions
### read ### read11
read(pin) read11(pin)
Read humidity and temperature from DHT22. Read humidity and temperature from DHT11.
###read22
read22(pin)
Read humidity and temperature from DHT22/21.
**Parameters:** **Parameters:**
* pin - ESP8266 pin connect to data pin in DHT22 * pin - ESP8266 pin connect to data pin
### getHumidity ### getHumidity
getHumidity() getHumidity()
@ -54,5 +86,6 @@ getTemperature()
Returns the temperature of the last reading. Returns the temperature of the last reading.
**Returns:** **Returns:**
* last temperature reading in 0.1ºC * last temperature reading in(dht22) 0.1ºC (dht11)1ºC
*

View File

@ -1,34 +1,43 @@
-- *************************************************************************** -- ***************************************************************************
-- DHT22 module for ESP8266 with nodeMCU -- DHTxx(11,21,22) module for ESP8266 with nodeMCU
-- --
-- Written by Javier Yanez -- Written by Javier Yanez mod by Martin
-- but based on a script of Pigs Fly from ESP8266.com forum -- but based on a script of Pigs Fly from ESP8266.com forum
-- --
-- MIT license, http://opensource.org/licenses/MIT -- MIT license, http://opensource.org/licenses/MIT
-- *************************************************************************** -- ***************************************************************************
--Support list
--DHT11 Tested ->read11
--DHT21 Not Tested->read22
--DHT22 Tested->read22
--==========================Module Part======================
local moduleName = ... local moduleName = ...
local M = {} local M = {}
_G[moduleName] = M _G[moduleName] = M
--==========================Local the UMI and TEMP===========
local humidity local humidity
local temperature local temperature
--==========================Local the bitStream==============
local bitStream = {}
function M.read(pin) ---------------------------Read bitStream from DHTXX--------------------------
local checksum local function read(pin)
local checksumTest
local bitlength = 0
humidity = 0 humidity = 0
temperature = 0 temperature = 0
checksum = 0
-- Use Markus Gritsch trick to speed up read/write on GPIO -- Use Markus Gritsch trick to speed up read/write on GPIO
local gpio_read = gpio.read local gpio_read = gpio.read
local bitStream = {}
for j = 1, 40, 1 do for j = 1, 40, 1 do
bitStream[j] = 0 bitStream[j] = 0
end end
local bitlength = 0
-- Step 1: send out start signal to DHT22 -- Step 1: send out start signal to DHT22
gpio.mode(pin, gpio.OUTPUT) gpio.mode(pin, gpio.OUTPUT)
@ -39,7 +48,7 @@ function M.read(pin)
gpio.write(pin, gpio.HIGH) gpio.write(pin, gpio.HIGH)
gpio.mode(pin, gpio.INPUT) gpio.mode(pin, gpio.INPUT)
-- Step 2: DHT22 send response signal -- Step 2: Receive bitStream from DHT11/22
-- bus will always let up eventually, don't bother with timeout -- bus will always let up eventually, don't bother with timeout
while (gpio_read(pin) == 0 ) do end while (gpio_read(pin) == 0 ) do end
local c=0 local c=0
@ -59,8 +68,53 @@ function M.read(pin)
-- bus will always let up eventually, don't bother with timeout -- bus will always let up eventually, don't bother with timeout
while (gpio_read(pin) == 0) do end while (gpio_read(pin) == 0) do end
end end
end
---------------------------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
read(pin)
local checksum = 0
local checksumTest
--DHT data acquired, process. --DHT data acquired, process.
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)
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 for i = 1, 16, 1 do
if (bitStream[i] > 3) then if (bitStream[i] > 3) then
humidity = humidity + 2 ^ (16 - i) humidity = humidity + 2 ^ (16 - i)
@ -91,6 +145,7 @@ function M.read(pin)
end end
end end
---------------------------Check out the data--------------------------
function M.getTemperature() function M.getTemperature()
return temperature return temperature
end end

View File

@ -16,11 +16,11 @@ local id = 0
local dev_addr = 0x68 local dev_addr = 0x68
local function decToBcd(val) local function decToBcd(val)
return((val/10*16) + (val%10)) return((((val/10) - ((val/10)%1)) *16) + (val%10))
end end
local function bcdToDec(val) local function bcdToDec(val)
return((val/16*10) + (val%16)) return((((val/16) - ((val/16)%1)) *10) + (val%16))
end end
-- initialize i2c -- initialize i2c