From 11928fa855fa270b970280805fbdc5a59939a630 Mon Sep 17 00:00:00 2001 From: Simonas Narbutas Date: Wed, 28 Nov 2018 15:50:45 +0100 Subject: [PATCH] first commit --- .gitignore | 1 + README.md | 25 +++++++++++++++++++++++++ application.lua | 42 ++++++++++++++++++++++++++++++++++++++++++ blinkDetector.lua | 20 ++++++++++++++++++++ counter.lua | 12 ++++++++++++ init.lua | 41 +++++++++++++++++++++++++++++++++++++++++ location_example.lua | 2 ++ 7 files changed, 143 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 application.lua create mode 100644 blinkDetector.lua create mode 100644 counter.lua create mode 100644 init.lua create mode 100644 location_example.lua diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1f2a43b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +location.lua diff --git a/README.md b/README.md new file mode 100644 index 0000000..bc0c9d1 --- /dev/null +++ b/README.md @@ -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. diff --git a/application.lua b/application.lua new file mode 100644 index 0000000..7f51a62 --- /dev/null +++ b/application.lua @@ -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 diff --git a/blinkDetector.lua b/blinkDetector.lua new file mode 100644 index 0000000..75f02e1 --- /dev/null +++ b/blinkDetector.lua @@ -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 diff --git a/counter.lua b/counter.lua new file mode 100644 index 0000000..774b5df --- /dev/null +++ b/counter.lua @@ -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 diff --git a/init.lua b/init.lua new file mode 100644 index 0000000..9434e4b --- /dev/null +++ b/init.lua @@ -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) diff --git a/location_example.lua b/location_example.lua new file mode 100644 index 0000000..4888708 --- /dev/null +++ b/location_example.lua @@ -0,0 +1,2 @@ +ssid = "rearcrack" +wifipassword = "DoNotEnter"