PowerMeter

From Stu2
Revision as of 21:01, 3 September 2019 by Stu2 (talk | contribs)
Jump to navigation Jump to search

ESP32 Programming. The 'new' ESP32 dev boards are preprogrammed with a LUA script. To get an Arduino to load, press and hold the 'Boot' button while uploading the Arduino code. Hold the boot button until finished loading the code.

Docker Notes

Some Planning:

  • mosquitto - can use base image. No customization really required. If moved to cloud, may want to figure out TLS and add passwords, etc.
  • node-red - customize Dockerfile to load modules. Update flows manually.
  • influxdb - need to store data in external volume
  • grafana
  • docker - create bridge, try docker compose to bundle all this stuff
  • cloud - cloud is expensive, but we could move mosquitto, influx and grafana to cloud, use flow in node red to push updates

Cheat sheet

docker ps -a    -lists all containers
docker container prune  -remove all containers in exit status
docker images
docker rm (name) - get rid of a container
docker-compose logs --tail

Setup the network.

docker network create wwi-net

Interesting to note that we can use the docker names on this network. e.g. the MQTT server can be mqtt.


Some mosquitto help.

mkdir ~/mosquitto/config
mkdir ~/mosquitto/data
mkdir ~/mosquitto/log

Edit ~/mosquitto/config/mosquitto.conf

 persistence true
 persistence_location /mosquitto/data/
 log_dest file /mosquitto/log/mosquitto.log
 allow_anonymous true

docker run --name mqtt -d -p 1883:1883 --net wwi-net -v $(pwd)/mosquitto/config/mosquitto.conf:/mosquitto/config/mosquitto.conf -v $(pwd)/mosquitto/data:/mosquitto/data -v $(pwd)/mosquitto/log:/mosquitto/log eclipse-mosquitto

mosquitto_pub -h 127.0.0.1 -m "test" -t test/switch -d

https://blog.container-solutions.com/understanding-volumes-docker - notes on volumes. The eclipse-mosquitto readme should be more explicit about how to use these volumes. 


Node Red help

git node-red-docker as per this: https://nodered.org/docs/getting-started/docker
Create Dockfile in ~/node-red-docker/v1.0/Dockerfile. I wish I could use Alpine (:slim) :latest is 900MB. Dockerfile:

FROM nodered/node-red-docker:latest

EXPOSE 1880

RUN npm install node-red-dashboard
RUN npm install node-red-contrib-influxdb


Then build image:

docker build -f v1.0/Dockerfile -t rpt-nodered:1.0 .

Run to build container:
docker run -d --net wwi-net --name rpt-nodered -p 1880:1880 rpt-nodered:1.0

Once built, start and stop as usual. (docker stop rpt-nodered, docker start rpt-nodered)

Import flows into container via node-red development dashboard. I think these are stored in the container. We'll see when I move the container.


Some influxdb help:

Create directory for data. e.g. ~/influxdb
Pull out the configuration file to the present working directory (PWD) using:

docker run --rm influxdb influxd config > influxdb.conf

Modify config file, if needed.

Create container and run, which uses ~/influxdb.conf and stores data in ~/influxdb

docker run --name rpt-influxdb --net wwi-net -d -p 8086:8086 -v $(pwd)/influxdb/influxdb:/var/lib/influxdb -v $(pwd)/influxdb.conf:/etc/influxdb/influxdb.conf:ro influxdb -config /etc/influxdb/influxdb.conf

Create measurement database:

docker exec -it rpt-influxdb influx

create database meters

After I figured out how to create stand alone containers, I figured out docker-composer. Also, create a file in the same directory as docker-composer.yml called .env. Put your secret stuff there. e.g USERDIR.

Do the following:

  • Copy ~/dc/docker-compose.yml to new computer
  • Copy all the data directories over to the new computer. (grafana,influxdb,node-red-data,node-red-docker,mosquitto)
  • Build node-red-docker, first. This creates rpt-nodeRed image we need, which has the influxdb and dashboard modules
  • add user grafana
  • fix permission issues.


version: "3"
services:
  mqtt:
    image: eclipse-mosquitto
    container_name: rpt-mqtt
    networks:
      - wwi
    ports:
      - "1883:1883"
    volumes:
      - ${USERDIR}/mosquitto/config/mosquitto.conf:/mosquitto/config/mosquitto.conf
      - ${USERDIR}/mosquitto/data:/mosquitto/data
      - ${USERDIR}/mosquitto/log:/mosquitto/log 
  influxdb:
    image: influxdb
    container_name: rpt-influxdb
    networks:
      - wwi
    ports:
      - "8086:8086"
    volumes:
      - ${USERDIR}/influxdb/influxdb:/var/lib/influxdb
      - ${USERDIR}/influxdb/influxdb.conf:/etc/influxdb/influxdb.conf:ro
  postfix:
    networks:
      - wwi
    image: simenduev/postfix-relay
    container_name: rpt-postfix
    ports:
      - "25:25"
    environment:
      - SMTP_LOGIN=apikey
      - SMTP_PASSWORD=${SENDGRID_API_KEY}
      - SMTP_HOST=smtp.sendgrid.net
      - SMTP_PORT=587
  grafana:
    user: grafana
    networks:
      - wwi
    image: grafana/grafana
    depends_on:
      - influxdb
      - postfix
    environment:
      - GF_SMTP_ENABLED=true
      - GF_SMTP_HOST=rpt-postfix:25
      - GF_LOG_level=warn
    container_name: rpt-grafana
    ports:
      - "3000:3000"
    volumes:
      - ${USERDIR}/grafana:/var/lib/grafana
  nodered:
    container_name: rpt-nodered
    restart: always
    image: rpt-nodered:1.0
    user: root
    depends_on:
      - influxdb
      - mqtt
    volumes:
      - ${USERDIR}/node-red-data:/data
      - /etc/localtime:/etc/localtime:ro
    ports:
      - "1880:1880"
    networks:
      - wwi   
networks:
  wwi:
    driver: bridge