天热了给鱼缸来个自动增氧🐟

给小朋友买了几条金鱼,天热总是死掉,周末就想着来整个智能定时供氧,通过之前折腾物联网手上现有 esp8266 芯片的板子Heltec WiFi Kit 8,带 0.91英寸 oled,刷个nodemcu固件连个WiFi就能简单满足需求,不用动用树莓派,顺便把之前一直想弄却拖了很久的跨界玩法《树莓派+Prometheus+Grafana来实现可视化传感器数据展示》尝试一下。

IMG_0086

需求

Heltec WiFi Kit 8

ESP8266 芯片没有硬件I²C,因此模块使用软件I²C驱动程序。可以在包括GPIO16在内的任何GPIO引脚上进行设置。

image-20200520010608887

image-20200518222907546

NodeMCU IO index ESP8266 pin IO index ESP8266 pin
0 [*] GPIO16 7 GPIO13
1 GPIO5 8 GPIO15
2 GPIO4 9 GPIO3
3 GPIO0 10 GPIO1
4 GPIO2 11 GPIO9
5 GPIO14 12 GPIO10
6 GPIO12

Heltec WiFi Kit 8 原理图

配合之前闲置的12v小水泵用5v驱动100~200mAh的电流(根据负载情况,一般100多)。

刷nodemcu固件

OLED 是 SSD1306 128x32 I2C,选择u8g2和字体,U8g2是我用过最简单的oled屏幕驱动库,兼容市面上你能买到的绝大多数oled模块。

根据需求定制固件,云端编译 https://nodemcu-build.com/

构建完会收到邮件,里面有下载链接

刷入固件

ESP8266EX + 4MB Flash,选择 dio 模式

pip install esptool
esptool.py --port /dev/tty.SLAB_USBtoUART --baud 115200 write_flash -fm dio 0x00000 nodemcu-master-10-modules-2020-05-16-16-27-31-float.bin

esptool.py v2.8 Serial port /dev/tty.SLAB_USBtoUART Connecting…….._ Detecting chip type… ESP8266 Chip is ESP8266EX Features: WiFi Crystal is 26MHz MAC: 60:01:94:xx:xx:xx Uploading stub… Running stub… Stub running… Configuring flash size… Auto-detected Flash size: 4MB Flash params set to 0x0240 Compressed 618496 bytes to 406545… Wrote 618496 bytes (406545 compressed) at 0x00000000 in 35.7 seconds (effective 138.4 kbit/s)… Hash of data verified.

Leaving… Hard resetting via RTS pin…

验证一下

screen /dev/tty.SLAB_USBtoUART 115200

NodeMCU 3.0.0.0 built on nodemcu-build.com provided by frightanic.com branch: master commit: 3d917850180f67adc7f2c6b5d00f27c152e7194c release: release DTS: 202002231746 SSL: true build type: float LFS: 0x0 modules: dht,file,gpio,i2c,net,node,tmr,uart,wifi,tls build 2020-05-16 16:26 powered by Lua 5.1.4 on SDK 3.0.1-dev(fce080e) lua: cannot open init.lua

-- connect to WiFi access point
wifi.setmode(wifi.STATION)
wifi.sta.config({ssid='JGuest', pwd='xxxxxx'})
print(wifi.sta.getip())

192.168.1.7 255.255.255.0 192.168.1.1

-- a simple HTTP server
srv = net.createServer(net.TCP)
srv:listen(80, function(conn)
	conn:on("receive", function(sck, payload)
		print(payload)
		sck:send("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n<h1> Hello, NodeMCU.</h1>")
	end)
	conn:on("sent", function(sck) sck:close() end)
end)

curl -v http://192.168.1.7 … OK!

构建电路和代码

电路

nodemcu + 继电器,略…

温度传感器DS18B20

img

从上图可以看出DS18B20的接线得却十分简单,但需要注意的是,当传感器与单片机相连时,要加一个4.7K的上拉电阻(建议4.7k~10k)。

编码

略…

上传

npm install nodemcu-tool -g

nodemcu-tool upload -p /dev/tty.SLAB_USBtoUART *.lua

image-20200517013112180

image-20200517013248486

其他问题

如何避免init.lua中出现PANIC循环?

保持init.lua尽可能简单-例如配置wifi,然后tmr.alarm()在2-3秒的延迟后使用一次启动您的应用程序。此延迟足够长,可以file.remove("init.lua")通过串行端口发出信号并以这种方式恢复控制。

Prometheus + Grafana 监控

定时采集指标数据,设置告警规则,Grafana 查看图表

docker run \
    -p 9090:9090 \
    -v $(pwd)/prometheus.yml:/etc/prometheus/prometheus.yml \
    prom/prometheus

prometheus.yml:

global:
  scrape_interval:     15s # By default, scrape targets every 15 seconds.

  # Attach these labels to any time series or alerts when communicating with
  # external systems (federation, remote storage, Alertmanager).
  external_labels:
    monitor: 'codelab-monitor'

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
  # The job name is added as a label  to any timeseries scraped from this config.
  - job_name: 'prometheus'

    # Override the global default and scrape targets from this job every 5 seconds.
    scrape_interval: 5s

    static_configs:
      - targets: ['localhost:9090']
  - job_name: 'iot'

    # Override the global default and scrape targets from this job every 5 seconds.
    scrape_interval: 5s

    static_configs:
      - targets: ['192.168.x.102:8080']

Grafana:

docker run -d -p 3000:3000 grafana/grafana

附录

Technical Parameters

Source Parameter
MCU ESP8266 (160MHz Tensilica L06 32-bits,SRAM 80KB, Wi-Fi)
Hardware Resources UART x 2; SPI x 1; I2C x 1; I2S x 1
10 – bits ADC
17 general GPIO
Display 0.91寸 128*32 OLED display
FLASH 4MB(32M-bits) SPI FLASH
Interface Micro USB x 1
Size 50.8 x 18.2 x 6.69 mm
USB to UART bridge CP2104
Battery Type 3.7V Lithium (SH1.25 x 2 socket)
Working Temperature Range -40 ~ 85 ℃

Electrical characteristics

Electrical characteristics Condition Minimum Typical Maximum
Power Supply USB Powered (≥500mA) 4.7V 5V 6V
Lithium powered (≥250mA) 3.3V 3.7V 4.2V
3.3V (Header) powered(≥150mA) 2.7V 3.3V 3.5V
5V (Header) powered(≥500mA) 4.7V 5V 6V
Power consumption(mA) WIFI Scan 115mA
WIFI AP 135mA
Output 3.3V Header Output 500mA
5V Header output (USB powered only) Equal to input current

相关参考

https://platformio.org/platformio-ide

https://docs.platformio.org/en/latest/boards/espressif8266/heltec_wifi_kit_8.html

https://heltec.org/project/wifi-kit-8/

https://github.com/nodemcu/nodemcu-firmware

https://nodemcu.readthedocs.io/en/master/getting-started/

https://nodemcu.readthedocs.io/en/master/modules/u8g2/

GPIO输入与输出设置

https://heltec-automation-docs.readthedocs.io/en/latest/esp8266+arduino/quick_start.html