1
0
Fork
You've already forked pi-sensor-reader
0
A simple Node application used for reading temperature and humidity from DHT11/DHT22/AM2302 sensors on a Raspberry Pi and posting the results to an HTTP endpoint.
  • TypeScript 96.2%
  • Dockerfile 3.8%
Find a file
2021年10月24日 17:41:29 +11:00
src Added try/catch for JSON file parsing from the queue directory, updated tests to match. 2020年11月08日 11:23:56 +11:00
test Updated dependencies, corrected invalid test description for sensor test. 2020年11月08日 11:30:11 +11:00
.dockerignore Added Dockerfile. Needs to be run with --privileged. 2020年04月03日 10:06:08 +01:00
.eslintrc.json Full refactor into a separate library module. 2019年06月22日 17:08:21 +10:00
.gitignore Converted project to TypeScript. 2019年07月02日 20:35:15 +10:00
config.example.json Updated dependencies, tidied code a bit, added optional checkTemperatureDifference and temperatureDifferenceThreshold configuration. 2020年03月21日 16:59:55 +11:00
docker-compose.yml Docker container no longer needs --privileged or to run as root. 2020年04月11日 01:18:20 +01:00
Dockerfile Slimmed down final Docker container. 2020年04月11日 11:20:01 +01:00
LICENSE Added license. 2021年10月24日 17:41:29 +11:00
nodemon.json Initial steps at adding a queue mechanism to handle failed API endpoint connections. 2019年06月23日 11:04:39 +10:00
package-lock.json Merge pull request #7 from VirtualWolf/dependabot/npm_and_yarn/glob-parent-5.1.2 2021年10月24日 17:40:43 +11:00
package.json Version bump to 3.2.4. 2020年11月08日 11:30:32 +11:00
README.md Added more comprehensive tests and the ability to enable additional debug logging. 2020年08月30日 11:02:10 +10:00
tsconfig.json Converted project to TypeScript. 2019年07月02日 20:35:15 +10:00

Pi Sensor Reader

This is my Raspberry Pi-powered replacement for my Ninja Block Serial Port Reader to read from an attached AM2303/DHT22/DHT11 temperature sensor and send the data via HTTP to a specified endpoint. It uses the node-dht-sensor library and assumes AM2302/DHT22 temperature/humidity sensors.

It requires a config file called config.json at the root of the repostory, multiple sensors can be added by adding to the sensors array. A fully-filled out file looks like this:

{
 "sensors": [
 {
 "name": "outdoor",
 "type": 22,
 "pin": 4
 },
 {
 "name": "indoor",
 "type": 22,
 "pin": 17,
 "checkTemperatureDifference": true,
 "temperatureDifferenceThreshold": 0.3
 }
 ],
 "endpoint": "http://example.com/update",
 "apiKey": "password",
 "sensorReadPeriod": 20000,
 "queueCheckPeriod": 300000,
}

It includes a local Express server running on port 3000 that returns the latest temperature, humidity, and timestamp of the latest sensor reading, and is located at http://localhost:3000/rest/<sensor-name-from-config-file>.

Per-sensor options

  • name — Required. The name of the sensor, used when sending updates and for the Express server.
  • pin — Required. The GPIO pin number on the Raspberry Pi the sensor is connected to, see node-dht-sensor for details.
  • type — Optional. The type of sensor, one of 11 (for DHT11) or 22 (for AM2302/DHT22), see node-dht-sensor for details. Defaults to 22 if not specified.
  • checkTemperatureDifference — Optional. Allows for checking and discarding of spuriously high or low readings when compared to the previous reading. (My indoor sensor will very occasionally decide the temperature is ten degrees or more higher or lower than what it actually is). Defaults to false if not specified.
  • temperatureDifferenceThreshold — Optional. The value in degrees above or below the previous reading at which a new reading should be discarded. Defaults to 0.3 if not specified and checkTemperatureDifference is true.

Global options

  • endpoint — Optional if the LOCAL_ONLY environment variable is set, required if not. The URL to post updates to, which will arrive as JSON in the form of {"sensor_name":"<name>","timestamp","<unix-timestamp">,"temperature","<temperature>","humidity":"<humidity>"}.
  • apiKey — Optional if the LOCAL_ONLY environment variable is set, required if not. Updates will be posted to the endpoint above with the X-Weather-API header set to this value.
  • sensorReadPeriod — Optional. The interval between sensor readings. Specified in milliseconds and defaults to 20000 (20 seconds) if not specified.
  • queueCheckPeriod — Optional. When the LOCAL_ONLY environment variable is not set, updates which fail to be sent to the URL given in endpoint (due to intermittent network flakiness for example) will be written to the queue directory at the root level of this repository. queueCheckPeriod configures how often this directory should be read and updates attempted to be re-sent. Specified in milliseconds and defaults to 300000 (300 seconds, five minutes) if not specified.

Environment variables

  • DEBUG can be set to enable additional debug logging.
  • LOCAL_ONLY can be set to skip sending updates to the HTTP endpoint, in which case the endpoint and apiKey options in config.json can be entirely omitted (the Express server at port 3000 remains running).
  • The endpoint, apiKey, sensorReadPeriod, and queueCheckPeriod options in config.json file can be overridden with API_ENDPOINT, API_KEY, SENSOR_READ_PERIOD, and QUEUE_CHECK_PERIOD respectively.

Running

With Docker

A docker-compose.yml file is included, run sudo docker-compose up -d to bring up the container and keep it running even after system reboots.

To run it as a local-only system without trying to send data to an HTTP endpoint, set LOCALONLY=true in the environment section of docker-compose.yml:

version: '3.3'
services:
 reader:
 [...]
 environment:
 - LOCAL_ONLY=true

The Dockerfile is based off arm32v6/node so it can be built on both a full-size Raspberry Pi and the Pi Zero/Zero W.

Without Docker

This project is written in TypeScript, compilation is done with npm run compile. Javascript files are output to the build directory and once compiled the application can be started with npm start.

Tests

Run with npm test.