mirror of
https://github.com/simisimis/blinkmeter.git
synced 2025-12-06 08:42:26 +02:00
first commit
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
location.lua
|
||||
25
README.md
Normal file
25
README.md
Normal file
@@ -0,0 +1,25 @@
|
||||
#Blinkmeter
|
||||
|
||||
Electricity meter blink counter nodemcu lua code for ESP8266.
|
||||
|
||||
## What it does:
|
||||
- Upon starting it establishes connection to provided wifi ssid
|
||||
- It counts electricity meter flashes and stores them in counter variable
|
||||
- It opens a socket and exposes counter.
|
||||
- Once prometheus scrapes the value, counter gets reset to 0.
|
||||
|
||||
## Installation
|
||||
|
||||
### Download nodemcu firmware
|
||||
- I use this site to build custom nodemcu firmware: [nodency-build,com](https://nodemcu-build.com)
|
||||
|
||||
### Flash your ESP8266 controller
|
||||
```bash $ esptool.py --port /dev/ttyUSB0 --baud 115200 write_flash --flash_size=detect 0 /path/to/nodemcu.bin ```
|
||||
|
||||
### Download ESPlorer
|
||||
- You can get it at [Esplorer site](https://esp8266.ru/esplorer/)
|
||||
- Extract and launch with java 1.7+ ```bash $ java -jar ESPlorer.jar```
|
||||
|
||||
### Upload lua files
|
||||
- move location_example.lua to location.lua and change to your ssid/psk
|
||||
- upload files through esplorer.
|
||||
42
application.lua
Normal file
42
application.lua
Normal file
@@ -0,0 +1,42 @@
|
||||
dofile("counter.lua")
|
||||
dofile("blinkDetector.lua")
|
||||
|
||||
local counter = Counter
|
||||
local blinkDetector = BlinkDetector
|
||||
|
||||
function countBlinks()
|
||||
local value = adc.read(0)
|
||||
if blinkDetector:isBlinking(value) then
|
||||
counter:increment()
|
||||
end
|
||||
end
|
||||
|
||||
function metrics()
|
||||
local buffer = {"# TYPE watts gauge\n", "watts ", counter:get(), "\n"}
|
||||
local body = table.concat(buffer)
|
||||
counter:reset()
|
||||
return body
|
||||
end
|
||||
|
||||
function response()
|
||||
local header = "HTTP/1.1 200 OK\r\nServer: NodeMCU on ESP8266\r\nContent-Type: text/plain; version=0.0.4\r\n\r\n"
|
||||
local response = header .. metrics()
|
||||
print("> " .. response)
|
||||
return response
|
||||
end
|
||||
|
||||
-- Initiate blink counting thread
|
||||
tmr.create():alarm(10, tmr.ALARM_AUTO, countBlinks)
|
||||
|
||||
srv = net.createServer(net.TCP, 20) -- 20s timeout
|
||||
|
||||
if srv then
|
||||
srv:listen(80, function(conn)
|
||||
conn:on("receive", function(conn, data)
|
||||
print("< " .. data)
|
||||
conn:send(response(), function(finishConn)
|
||||
finishConn:close()
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
end
|
||||
20
blinkDetector.lua
Normal file
20
blinkDetector.lua
Normal file
@@ -0,0 +1,20 @@
|
||||
dofile("counter.lua")
|
||||
|
||||
BlinkDetector = {
|
||||
ON_THRESHOLD = 200,
|
||||
OFF_THRESHOLD = 100,
|
||||
ON = 1,
|
||||
OFF = 0,
|
||||
state = 0
|
||||
}
|
||||
|
||||
function BlinkDetector:isBlinking(value)
|
||||
if self.state == self.ON and value < self.OFF_THRESHOLD then
|
||||
self.state = self.OFF
|
||||
return true
|
||||
end
|
||||
if value > self.ON_THRESHOLD then
|
||||
self.state = self.ON
|
||||
end
|
||||
return false
|
||||
end
|
||||
12
counter.lua
Normal file
12
counter.lua
Normal file
@@ -0,0 +1,12 @@
|
||||
Counter = {counter = 0}
|
||||
function Counter:increment()
|
||||
self.counter = self.counter + 1
|
||||
end
|
||||
|
||||
function Counter:reset()
|
||||
self.counter = 0
|
||||
end
|
||||
|
||||
function Counter:get()
|
||||
return self.counter
|
||||
end
|
||||
41
init.lua
Normal file
41
init.lua
Normal file
@@ -0,0 +1,41 @@
|
||||
-- load location specific data 'altitude', 'ssid' and 'wifipassword'
|
||||
dofile("location.lua")
|
||||
|
||||
function startup()
|
||||
if file.open("init.lua") == nil then
|
||||
print("init.lua deleted or renamed")
|
||||
else
|
||||
print("Running")
|
||||
file.close("init.lua")
|
||||
-- the actual application is stored in 'application.lua'
|
||||
dofile("application.lua")
|
||||
end
|
||||
end
|
||||
|
||||
function read()
|
||||
print("Reading value")
|
||||
end
|
||||
|
||||
function flushing()
|
||||
print("Flushing value")
|
||||
end
|
||||
|
||||
print("Connecting to WiFi access point...")
|
||||
wifi.setmode(wifi.STATION)
|
||||
station_cfg={}
|
||||
station_cfg.ssid=ssid
|
||||
station_cfg.pwd=wifipassword
|
||||
wifi.sta.config(station_cfg)
|
||||
print("Connected to wifi:" .. ssid)
|
||||
-- wifi.sta.connect() not necessary because config() uses auto-connect=true by default
|
||||
tmr.create():alarm(1000, tmr.ALARM_AUTO, function(cb_timer)
|
||||
if wifi.sta.getip() == nil then
|
||||
print("Waiting for IP address...")
|
||||
else
|
||||
cb_timer:unregister()
|
||||
print("WiFi connection established, IP address: " .. wifi.sta.getip())
|
||||
print("You have 3 seconds to abort")
|
||||
print("Waiting...")
|
||||
tmr.create():alarm(3000, tmr.ALARM_SINGLE, startup)
|
||||
end
|
||||
end)
|
||||
2
location_example.lua
Normal file
2
location_example.lua
Normal file
@@ -0,0 +1,2 @@
|
||||
ssid = "rearcrack"
|
||||
wifipassword = "DoNotEnter"
|
||||
Reference in New Issue
Block a user