Commit 8bce202d authored by Nelso Jost's avatar Nelso Jost
Browse files

FIX: Refactoring and improving docs

parent 7e29b5b4
venv/ .venv/
*.pyc *.pyc
datalog.csv datalog.csv
outgoing.json outgoing.json
......
PY := python3 PY := python3
VENV := .venv
all: all:
@ echo "USAGE:" @ echo "USAGE:"
...@@ -6,30 +7,35 @@ all: ...@@ -6,30 +7,35 @@ all:
@ echo " make log -- Launch the logger (using Python 3 from make setup)" @ echo " make log -- Launch the logger (using Python 3 from make setup)"
@ echo " make clean -- remove all the generated files" @ echo " make clean -- remove all the generated files"
setup: installpy venv setup: install-deb venv
installpy: install-deb:
sudo apt-get install python3 sudo apt-get install python3 python3-pip
sudo apt-get install python3-pip
sudo pip3 install virtualenv sudo pip3 install virtualenv
venv: venv: clean-venv
@ echo "-------------------------------------------------------" @ echo "-------------------------------------------------------"
@ if [ ! -d "venv/" ]; then \ virtualenv -v --python='${PY}' ${VENV}
virtualenv -q --no-site-packages --python='${PY}' venv; \ @ echo "Virtualenv with interpreter '${PY}' was created at ${VENV}"
echo "Virtualenv with ${PY} created at venv/"; \ @ echo "-------------------------------------------------------"
echo "-------------------------------------------------------"; \ ${VENV}/bin/pip3 install --upgrade pip
fi @ echo "-------------------------------------------------------"
venv/bin/pip install -r requirements.pip ${VENV}/bin/pip3 install -r requirements.pip
@ echo "on" @ echo "-------------------------------------------------------"
@ venv/bin/pip --version @ echo "Virtualenv is ready at ${VENV}!"
@ echo " "
@ echo "TOTAL SIZE: "
@ du -sh ${VENV}
clean-venv:
rm -rf ${VENV}
log: log:
@ venv/bin/python meteorolog.py @ ${VENV}/bin/python meteorolog.py
testserial: testserial:
@ venv/bin/ipython test_serial.py @ ${VENV}/bin/ipython test_serial.py
clean: clean-log:
rm -rf datalog.csv rm -rf datalog.csv
rm -rf outgoing.json rm -rf outgoing.json
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
# Author: Nelso G. Jost (nelsojost@gmail.com) # Author: Nelso G. Jost (nelsojost@gmail.com)
# # License: GPL
# License: BEERWARE (http://en.wikipedia.org/wiki/Beerware) # Purpose: Get data from the board via serial and send it to the server.
#
# Purpose: Testing board comunication.
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
from __future__ import print_function, division from __future__ import print_function, division
...@@ -17,9 +15,19 @@ import json ...@@ -17,9 +15,19 @@ import json
import yaml import yaml
def make_current_file_path(filename): def make_current_file_path(filename):
''' Append filename to the current __file__ path. '''
return os.path.join(os.path.abspath(os.path.dirname(__file__)), filename) return os.path.join(os.path.abspath(os.path.dirname(__file__)), filename)
class Meteorologger: class Meteorologger:
'''
Provides a series of mechanisms to collect sensor data from the board
via serial port, save locally on the machine and upload to the server.
The functionality of this class relies heavily on the config file given
by the SETTINGS_FILENAME attribute, which uses YAML syntax.
Call the run() method to start the logging process.
'''
SETTINGS_FILENAME = make_current_file_path('settings.yaml') SETTINGS_FILENAME = make_current_file_path('settings.yaml')
CSV_SEP = ',' CSV_SEP = ','
...@@ -33,10 +41,16 @@ class Meteorologger: ...@@ -33,10 +41,16 @@ class Meteorologger:
self.loadSettings() self.loadSettings()
def _normalizePathFilename(self, key): def _normalizePathFilename(self, key):
''' Appends __file__ basedir to config keys that don't have a basedir.
'''
if not os.path.dirname(self.CFG[key]): if not os.path.dirname(self.CFG[key]):
self.CFG[key] = make_current_file_path(self.CFG[key]) self.CFG[key] = make_current_file_path(self.CFG[key])
def loadSettings(self): def loadSettings(self):
'''
Load the configuration file onto the self.CFG attribute.
Some keys will be tested and filenames will be normalized.
'''
with open(self.SETTINGS_FILENAME) as f: with open(self.SETTINGS_FILENAME) as f:
self.CFG = yaml.safe_load(f) self.CFG = yaml.safe_load(f)
...@@ -59,6 +73,10 @@ class Meteorologger: ...@@ -59,6 +73,10 @@ class Meteorologger:
self._normalizePathFilename('SERVER_OUTGOING_DATA_LOG_FILENAME') self._normalizePathFilename('SERVER_OUTGOING_DATA_LOG_FILENAME')
def create_json_raw_sensor_data(self, raw_line): def create_json_raw_sensor_data(self, raw_line):
'''
Given the raw serial line response (expected to be a CSV line), returns
a JSON dict with sensor data including the datetime field.
'''
raw_sensor_data = {'datetime': datetime.now().strftime("%Y%m%d%H%M%S"), raw_sensor_data = {'datetime': datetime.now().strftime("%Y%m%d%H%M%S"),
'sensors': {}} 'sensors': {}}
...@@ -77,6 +95,9 @@ class Meteorologger: ...@@ -77,6 +95,9 @@ class Meteorologger:
return raw_sensor_data return raw_sensor_data
def add_data_log(self, json_data): def add_data_log(self, json_data):
'''
'''
csv_line = json_data['datetime'] + self.CFG['EXPORT_CSV_SEP'] csv_line = json_data['datetime'] + self.CFG['EXPORT_CSV_SEP']
for sensor in self.CFG['SENSORS']: for sensor in self.CFG['SENSORS']:
if sensor['nickname'] in json_data['sensors']: if sensor['nickname'] in json_data['sensors']:
...@@ -168,6 +189,11 @@ class Meteorologger: ...@@ -168,6 +189,11 @@ class Meteorologger:
ser.close() ser.close()
def run(self): def run(self):
'''
Starts the logger main loop, which iterate over the procedures:
1. Read sensor data via serial port;
2. If successful, save data on
'''
serial_port = 0 serial_port = 0
try: try:
while True: while True:
......
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
String read_LDR() String read_LDR()
{ {
return String(100/1023.0 - analogRead(LDR_PIN)); return String(100/1023.0 * analogRead(LDR_PIN));
} }
// === DHT22 SETUP ======================================= // === DHT22 SETUP =======================================
...@@ -36,6 +36,11 @@ String read_DHT22_AH() ...@@ -36,6 +36,11 @@ String read_DHT22_AH()
// === BMP085 SETUP =============================================== // === BMP085 SETUP ===============================================
// https://github.com/adafruit/Adafruit-BMP085-Library // https://github.com/adafruit/Adafruit-BMP085-Library
/*------------------------------------------------------------------------------
* AUTO-GENERATED CODE FROM CTA-EMM-WEB
* EDIT ONLY IF YOU KNOW WHAT YOU ARE DOING
*----------------------------------------------------------------------------*/
#include "mysensors.h"
#include "Adafruit_BMP085.h" #include "Adafruit_BMP085.h"
Adafruit_BMP085 bmp; Adafruit_BMP085 bmp;
......
# server's full base address for sendind requests (must end with /)
BASE_URL: http://localhost:5000/ BASE_URL: http://localhost:5000/
# board to upload data baka
BOARD_ID: 2 BOARD_ID: 2
SENSORS: SENSORS:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment