commit
c56c523216
|
@ -0,0 +1,34 @@
|
|||
---------------------------------------------------------------
|
||||
-- MPR121 I2C module usage example for NodeMCU
|
||||
-- Based on code from: http://bildr.org/2011/05/mpr121_arduino/
|
||||
-- Tiago Custódio <tiagocustodio1@gmail.com>
|
||||
---------------------------------------------------------------
|
||||
|
||||
require("mpr121")
|
||||
|
||||
-- default address for the mpr121
|
||||
mpr121_address = 0x5A
|
||||
|
||||
id = 0
|
||||
sda = 3
|
||||
scl = 4
|
||||
|
||||
i2c.setup(id, sda, scl, i2c.SLOW)
|
||||
|
||||
-- needs only to be called once to configure the mpr121 registers
|
||||
mpr121.init(mpr121_address)
|
||||
|
||||
while true do -- WARNING: this is a infinite loop! don't set your init to execute this file
|
||||
touch = mpr121.readTouchInputs()
|
||||
touchString = ""
|
||||
for k, v in pairs(touch) do
|
||||
-- concatenate every value in a string
|
||||
touchString = touchString .. v
|
||||
end
|
||||
print(touchString)
|
||||
tmr.wdclr() -- clear the watchdog or the esp will reset!
|
||||
end
|
||||
|
||||
-- don't forget to release it after use
|
||||
mpr121 = nil
|
||||
package.loaded["mpr121"] = nil
|
|
@ -0,0 +1,74 @@
|
|||
---------------------------------------------------------------
|
||||
-- MPR121 I2C module for NodeMCU
|
||||
-- Based on code from: http://bildr.org/2011/05/mpr121_arduino/
|
||||
-- Tiago Custódio <tiagocustodio1@gmail.com>
|
||||
---------------------------------------------------------------
|
||||
|
||||
local moduleName = ...
|
||||
local M = {}
|
||||
_G[moduleName] = M
|
||||
|
||||
-- Default value for i2c communication
|
||||
local id = 0
|
||||
|
||||
--device address
|
||||
local devAddr = 0
|
||||
|
||||
--make it faster
|
||||
local i2c = i2c
|
||||
|
||||
function M.setRegister(address, r, v)
|
||||
i2c.start(id)
|
||||
local ans = i2c.address(id, address, i2c.TRANSMITTER)
|
||||
i2c.write(id, r)
|
||||
i2c.write(id, v)
|
||||
i2c.stop(id)
|
||||
end
|
||||
|
||||
function M.init(address)
|
||||
devAddr = address
|
||||
M.setRegister(devAddr, 0x5E, 0x00) -- ELE_CFG
|
||||
|
||||
-- Section A - Controls filtering when data is > baseline.
|
||||
M.setRegister(devAddr, 0x2B, 0x01) -- MHD_R
|
||||
M.setRegister(devAddr, 0x2C, 0x01) -- NHD_R
|
||||
M.setRegister(devAddr, 0x2D, 0x00) -- NCL_R
|
||||
M.setRegister(devAddr, 0x2E, 0x00) -- FDL_R
|
||||
|
||||
-- Section B - Controls filtering when data is < baseline.
|
||||
M.setRegister(devAddr, 0x2F, 0x01) -- MHD_F
|
||||
M.setRegister(devAddr, 0x30, 0x01) -- NHD_F
|
||||
M.setRegister(devAddr, 0x31, 0xFF) -- NCL_F
|
||||
M.setRegister(devAddr, 0x32, 0x02) -- FDL_F
|
||||
|
||||
-- Section C - Sets touch and release thresholds for each electrode
|
||||
for i = 0, 11 do
|
||||
M.setRegister(devAddr, 0x41+(i*2), 0x06) -- ELE0_T
|
||||
M.setRegister(devAddr, 0x42+(i*2), 0x0A) -- ELE0_R
|
||||
end
|
||||
|
||||
-- Section D - Set the Filter Configuration - Set ESI2
|
||||
M.setRegister(devAddr, 0x5D, 0x04) -- FIL_CFG
|
||||
|
||||
-- Section E - Electrode Configuration - Set 0x5E to 0x00 to return to standby mode
|
||||
M.setRegister(devAddr, 0x5E, 0x0C) -- ELE_CFG
|
||||
|
||||
tmr.delay(50000) -- Delay to let the electrodes settle after config
|
||||
end
|
||||
|
||||
function M.readTouchInputs()
|
||||
i2c.start(id)
|
||||
i2c.address(id, devAddr, i2c.RECEIVER)
|
||||
local c = i2c.read(id, 2)
|
||||
i2c.stop(id)
|
||||
local LSB = tonumber(string.byte(c, 1))
|
||||
local MSB = tonumber(string.byte(c, 2))
|
||||
local touched = bit.lshift(MSB, 8) + LSB
|
||||
local t = {}
|
||||
for i = 0, 11 do
|
||||
t[i+1] = bit.isset(touched, i) and 1 or 0
|
||||
end
|
||||
return t
|
||||
end
|
||||
|
||||
return M
|
|
@ -0,0 +1,140 @@
|
|||
# phant-nodemcu
|
||||
|
||||
The goal of this library is to provide a simple interface for logging data to phant.
|
||||
It should take any data type, convert it to a `String`, and build a url. This
|
||||
library will not handle interaction with WiFi or ethernet shields, it's only purpose is
|
||||
to make it easier for users to build phant compatible requests without worrying about data
|
||||
types and string concatenation.
|
||||
|
||||
## Installation
|
||||
|
||||
Save "phant.lua" to your nodemcu.
|
||||
|
||||
## Usage
|
||||
|
||||
To create a new instance of the request builder, pass the server's `hostname`, your `public key`,
|
||||
and your `private key` to the `phant.init()` method.
|
||||
|
||||
```
|
||||
phant.init("data.sparkfun.com", "VGb2Y1jD4VIxjX3x196z", "9YBaDk6yeMtNErDNq4YM")
|
||||
```
|
||||
|
||||
To add data, you can make calls to `phant.add(key, value)`. The library will convert any value data type
|
||||
to a string, so there is no need for conversion before sending data. For example, if you have a stream
|
||||
with fields titled `wind`, `temp`, and `raining`, your code would add data to the request like this:
|
||||
|
||||
```
|
||||
phant.add("wind", 12.0);
|
||||
phant.add("temp", 70.1);
|
||||
phant.add("raining", false);
|
||||
```
|
||||
|
||||
Remember that in order to use float data-type values you must use a version of nodemcu that has been compiled
|
||||
with support for float data-type.
|
||||
|
||||
To get the request string for adding data, you have two options `phant.url()` and `phant.post()`.
|
||||
Both methods will clear the current request data after building and returning the current request. Unless
|
||||
there is some compelling reason to do otherwise, you should always use `phant.post()` to get a
|
||||
[HTTP POST](http://en.wikipedia.org/wiki/POST_(HTTP)) request string. `phant.url()` will return a URL that you
|
||||
can use in your web browser to test data logging.
|
||||
|
||||
```
|
||||
print(phant.post())
|
||||
```
|
||||
or
|
||||
```
|
||||
conn = net.createConnection(net.TCP, 0)
|
||||
|
||||
conn:on("connection", function(conn, payload)
|
||||
conn:send(phant.post())
|
||||
end)
|
||||
|
||||
conn:connect(80, 'data.sparkfun.com')
|
||||
```
|
||||
|
||||
To clear all of the data in your stream, you can use `phant.clear()` to get a `HTTP DELETE` request string. Clearing the
|
||||
data will not delete the stream definition, it will only delete the logged data.
|
||||
|
||||
```
|
||||
print(phant.clear())
|
||||
```
|
||||
|
||||
If you would like to retrieve your logged data, `phant.get()` will return a [HTTP GET](http://en.wikipedia.org/wiki/GET_(HTTP))
|
||||
request string that will cause the server to respond with your logged data in [CSV](http://en.wikipedia.org/wiki/Comma-separated_values)
|
||||
format. Parsing CSV is outside of the scope of this library.
|
||||
|
||||
As an added functionality, if you which to retrieve your logged data in another format like JSON, you can supply that format
|
||||
in the method as a parameter. If no format is given to the method it will default to CSV.
|
||||
|
||||
```
|
||||
print(phant.get())
|
||||
|
||||
print(phant.get("json"))
|
||||
```
|
||||
|
||||
## Output Example
|
||||
|
||||
### Sketch
|
||||
|
||||
```lua
|
||||
require("phant")
|
||||
|
||||
hostName = "data.sparkfun.com"
|
||||
publicKey = "VGb2Y1jD4VIxjX3x196z"
|
||||
privateKey = "9YBaDk6yeMtNErDNq4YM"
|
||||
|
||||
phant.init(hostName, publicKey, privateKey)
|
||||
|
||||
phant.add("val1", "url")
|
||||
phant.add("val2", 22)
|
||||
phant.add("val3", 0.1234)
|
||||
|
||||
print("----TEST URL-----")
|
||||
print(phant.url() .. "\n")
|
||||
|
||||
phant.add("val1", "post")
|
||||
phant.add("val2", false)
|
||||
phant.add("val3", 98.6)
|
||||
|
||||
print("----HTTP POST----")
|
||||
print(phant.post() .. "\n")
|
||||
|
||||
print("----HTTP GET----")
|
||||
print(phant.get())
|
||||
|
||||
print("----HTTP DELETE----")
|
||||
print(phant.clear())
|
||||
|
||||
phant = nil
|
||||
package.loaded["phant"] = nil
|
||||
```
|
||||
|
||||
### Output
|
||||
|
||||
This example prints the following to the serial monitor:
|
||||
|
||||
```
|
||||
----TEST URL-----
|
||||
http://data.sparkfun.com/input/VGb2Y1jD4VIxjX3x196z.txt?private_key=9YBaDk6yeMtNErDNq4YM&val1=url&val2=22&val3=0.1234
|
||||
|
||||
----HTTP POST----
|
||||
POST /input/VGb2Y1jD4VIxjX3x196z.txt HTTP/1.1
|
||||
Host: data.sparkfun.com
|
||||
Phant-Private-Key: 9YBaDk6yeMtNErDNq4YM
|
||||
Connection: close
|
||||
Content-Type: application/x-www-form-urlencoded
|
||||
Content-Length: 30
|
||||
|
||||
val1=post&val2=false&val3=98.6
|
||||
|
||||
----HTTP GET----
|
||||
GET /output/VGb2Y1jD4VIxjX3x196z.csv HTTP/1.1
|
||||
Host: data.sparkfun.com
|
||||
Connection: close
|
||||
|
||||
----HTTP DELETE----
|
||||
DELETE /input/VGb2Y1jD4VIxjX3x196z.txt HTTP/1.1
|
||||
Host: data.sparkfun.com
|
||||
Phant-Private-Key: 9YBaDk6yeMtNErDNq4YM
|
||||
Connection: close
|
||||
```
|
|
@ -0,0 +1,36 @@
|
|||
-------------------------------------------------------------------------
|
||||
-- Phant module for NodeMCU
|
||||
-- Based on code from Sparkfun: https://github.com/sparkfun/phant-arduino
|
||||
-- Tiago Custódio <tiagocustodio1@gmail.com>
|
||||
-------------------------------------------------------------------------
|
||||
|
||||
require("phant")
|
||||
|
||||
hostName = "data.sparkfun.com"
|
||||
publicKey = "VGb2Y1jD4VIxjX3x196z"
|
||||
privateKey = "9YBaDk6yeMtNErDNq4YM"
|
||||
|
||||
phant.init(hostName, publicKey, privateKey)
|
||||
|
||||
phant.add("val1", "url")
|
||||
phant.add("val2", 22)
|
||||
phant.add("val3", 0.1234)
|
||||
|
||||
print("----TEST URL-----")
|
||||
print(phant.url() .. "\n")
|
||||
|
||||
phant.add("val1", "post")
|
||||
phant.add("val2", false)
|
||||
phant.add("val3", 98.6)
|
||||
|
||||
print("----HTTP POST----")
|
||||
print(phant.post() .. "\n")
|
||||
|
||||
print("----HTTP GET----")
|
||||
print(phant.get())
|
||||
|
||||
print("----HTTP DELETE----")
|
||||
print(phant.clear())
|
||||
|
||||
phant = nil
|
||||
package.loaded["phant"] = nil
|
|
@ -0,0 +1,68 @@
|
|||
-------------------------------------------------------------------------
|
||||
-- Phant module for NodeMCU
|
||||
-- Based on code from Sparkfun: https://github.com/sparkfun/phant-arduino
|
||||
-- Tiago Custódio <tiagocustodio1@gmail.com>
|
||||
-------------------------------------------------------------------------
|
||||
|
||||
local moduleName = ...
|
||||
local M = {}
|
||||
_G[moduleName] = M
|
||||
|
||||
local _pub;
|
||||
local _prv;
|
||||
local _host;
|
||||
local _params;
|
||||
|
||||
function M.init(host, publicKey, privateKey)
|
||||
_host = host
|
||||
_pub = publicKey
|
||||
_prv = privateKey
|
||||
_params = ""
|
||||
end
|
||||
|
||||
function M.add(field, data)
|
||||
_params = _params .. "&" .. field .. "=" .. tostring(data)
|
||||
end
|
||||
|
||||
function M.queryString()
|
||||
return _params
|
||||
end
|
||||
|
||||
function M.url()
|
||||
local result = "http://" .. _host .. "/input/" .. _pub .. ".txt" .. "?private_key=" .. _prv .. _params
|
||||
_params = ""
|
||||
return result;
|
||||
end
|
||||
|
||||
function M.get(format)
|
||||
if format == nil then
|
||||
format = "csv"
|
||||
end
|
||||
local result = "GET /output/" .. _pub .. "." .. format .. " HTTP/1.1\n" .. "Host: " .. _host .. "\n" .. "Connection: close\n"
|
||||
return result
|
||||
end
|
||||
|
||||
function M.post()
|
||||
local params = string.sub(_params, 2);
|
||||
local result = "POST /input/" .. _pub .. ".txt HTTP/1.1\n" .. "Host: " .. _host .. "\n"
|
||||
result = result .. "Phant-Private-Key: " .. _prv .. '\n'
|
||||
result = result .. "Connection: close\n"
|
||||
result = result .. "Content-Type: application/x-www-form-urlencoded\n"
|
||||
result = result .. "Content-Length: " .. string.len(params) .. "\n\n"
|
||||
result = result .. params
|
||||
|
||||
_params = "";
|
||||
return result;
|
||||
end
|
||||
|
||||
function M.clear()
|
||||
local result = "DELETE /input/" .. _pub .. ".txt HTTP/1.1\n"
|
||||
result = result .. "Host: " .. _host .. "\n"
|
||||
result = result .. "Phant-Private-Key: " .. _prv .."\n"
|
||||
result = result .. "Connection: close\n"
|
||||
|
||||
return result;
|
||||
end
|
||||
|
||||
|
||||
return M
|
Loading…
Reference in New Issue