EnvironmentDataMeasurementSystem is a project that shows how to connect a ESP8266 / NodeMCU to an API service to store environment data in a database using the DHT22 and LM35 sensors.
- NodeMCU or ESP8266 compatible board
- DHT22 sensor
- LM35 sensor
- Breadboard
- Some jumper wires to connect
- Some kind of server (e.g. Raspberry Pi suites that, too)
- Some supported database fror Sails.JS: https://sailsjs.com/documentation/concepts/extending-sails/adapters/available-adapters
- Install Node.JS properly (administrator mode on Windows) on your server.
- Check if the environment variables are set properly. (See image below for more information)
- Adjust the parameters as follows:
- In the environmentsystem subfolder adjust the following:
- In the api\swagger\swagger.yaml file adjust the host variable to your server host (Let's assume 111.111.111.111):
host: 111.111.111.111:10011
- In the assets\index.html file file adjust the host variable to your server host (Let's assume 111.111.111.111):
url: "https://111.111.111.111:10011/swagger",
- In the config/ssl subfolder add your ssl files (I've put dummy ones in there): For generation of the files refer to https://github.com/SeppPenner/EnvironmentDataMeasurementSystem#generate-self-signed-ssl-files-openssl-needs-to-be-installed
- In the config/session.js file update your session secret (Let's assume 41e2608f92f8701a99c41e2608f05885):
secret: '41e2608f92f8701a99c41e2608f05885',
- In the config/policies.js file update your username/ password for basic authentication (Let's assume EnvironmentSystem as user and Test as password):
From this username/ password get the Base64 encoding in the form username:password, so e.g. EnvironmentSystem:Test results in RW52aXJvbm1lbnRTeXN0ZW06VGVzdA== (Using e.g. https://www.base64encode.org/)var auth = require('http-auth'); var basic = auth.basic({ realm: "EnvironmentSystem" }, function(username, password, callback) { callback(username === "EnvironmentSystem" && password === "Test"); });
- In the config/connections.js file update your database connection (You can use a local database, MySQL, MongoDB or PostgreSQL):
You can specifiy more than one connection here. However, only one will be used. Set the used database in the config/models.js file:localDiskDb: { adapter: 'sails-disk' }, mysqlServer: { adapter: 'sails-mysql', host: '111.111.111.111', user: 'username', password: 'password', port: 3306, database: 'environmentsystem' }, mongodbServer: { adapter: 'sails-mongo', host: '111.111.111.111', port: 27017, user: 'username', password: 'password', database: 'environmentsystem' }, postgresqlServer: { adapter: 'sails-postgresql', host: '111.111.111.111', user: 'username', password: 'password', database: 'environmentsystem' }
connection: 'mysqlServer',
- In the EnvironmentMeasurement subfolder adjust the following:
- In EnvironmentPost.h adjust the host variable to your server host (Let's assume 111.111.111.111):
#define HOST "111.111.111.111"
- In EnvironmentPost.cpp adjust the host variable to your server host (Let's assume 111.111.111.111):
client->println("Host: 111.111.111.111");
- In EnvironmentPost.cpp adjust the authorization variable as set in the service (Base64 encoded, see the config/policies.js configuration above) to your server host (Let's assume RW52aXJvbm1lbnRTeXN0ZW06VGVzdA==):
client->println("Authorization: Basic RW52aXJvbm1lbnRTeXN0ZW06VGVzdA==");
- Adjust the Wifi and password in the EnvironmentMeasurement.ino file:
const char* ssid = "MyWifi"; const char* password = "MyPassword";
-
Connect the LM35 tp pin A0 (or adjust the pin in the EnvironmentMeasurement.ino file) and the Dht22 to the pin A2 (or adjust the pin in the EnvironmentMeasurement.ino file).
Pins set in the EnvironmentMeasurement.ino file:
const int LM35Pin = A0; const int DhtPin = A2;
-
Install a database system on your server (e.g. MySQL and add the database environmentsystem) --> See other manuals on how to do that, please.
-
Copy the environmentsystem folder to your server (e.g. with FileZilla in binary mode as a zip file --> unpack it).
-
Install sails and swagger and start your service:
cd /environmentsystem
npm install sails -g
npm install -g swagger
swagger project start-
Your server should now be accessible at https://111.111.111.111:10011/ (Replace 111.111.111.111 with your host ip address) within your browser.
-
Compile the binaries onto the ESP8266/ NodeMCU using Arduino IDE.
-
The ESP8266/ NodeMCU posts data from the sensors to the database (Hopefully). Note that the ESP8266/ NodeMCU needs about 10 to 13 seconds to establish a SSL connection...
The Dockerfile for the service didn't work on my Raspberry Pi. Cou can give it a try but don't be sad if it doesn't do what expected.
export SERVER_NAME='localhost'
mkdir ssl
openssl req -nodes -x509 -newkey rsa:2048 \
-subj "/CN=$SERVER_NAME" \
-keyout ssl/default.key \
-out ssl/default.crtRemove the environmentsystem/package-lock.json
Go to the environmentsystem folder (e.g. cd environmentsystem)
Update via npm (In Windows use Powershell as Administrator):
npm i -g npm-check-updates
npm-check-updates -u
npm installSee the Changelog.
