Skip to content

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.

License

Notifications You must be signed in to change notification settings

SeppPenner/EnvironmentDataMeasurementSystem

Repository files navigation

EnvironmentDataMeasurementSystem

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.

Build status GitHub issues GitHub forks GitHub stars License: MIT

What you need:

Installation:

  1. Install Node.JS properly (administrator mode on Windows) on your server.
  2. Check if the environment variables are set properly. (See image below for more information)

Screenshot of the environment variables

  1. 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",
     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):
     var auth = require('http-auth');
     var basic = auth.basic({
     		realm: "EnvironmentSystem"
     	}, function(username, password, callback) {
     		callback(username === "EnvironmentSystem" && password === "Test");
     });
    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/)
    • In the config/connections.js file update your database connection (You can use a local database, MySQL, MongoDB or PostgreSQL):
     	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'
     	}
    You can specifiy more than one connection here. However, only one will be used. Set the used database in the config/models.js file:
     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"; 
  1. 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;
  2. Install a database system on your server (e.g. MySQL and add the database environmentsystem) --> See other manuals on how to do that, please.

  3. Copy the environmentsystem folder to your server (e.g. with FileZilla in binary mode as a zip file --> unpack it).

  4. Install sails and swagger and start your service:

cd /environmentsystem
npm install sails -g
npm install -g swagger
swagger project start
  1. 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.

  2. Compile the binaries onto the ESP8266/ NodeMCU using Arduino IDE.

  3. 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...

Additional information:

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.

Generate self signed ssl files (openssl needs to be installed):

export SERVER_NAME='localhost'
mkdir ssl

openssl req -nodes -x509 -newkey rsa:2048 \
  -subj "/CN=$SERVER_NAME" \
  -keyout ssl/default.key \
  -out ssl/default.crt

Updating dependencies:

Remove 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 install

Change history

See the Changelog.

About

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.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •