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

FIX: Refactoring and improving docs

parent 7e29b5b4
venv/
.venv/
*.pyc
datalog.csv
outgoing.json
......
PY := python3
VENV := .venv
all:
@ echo "USAGE:"
......@@ -6,30 +7,35 @@ all:
@ echo " make log -- Launch the logger (using Python 3 from make setup)"
@ echo " make clean -- remove all the generated files"
setup: installpy venv
setup: install-deb venv
installpy:
sudo apt-get install python3
sudo apt-get install python3-pip
install-deb:
sudo apt-get install python3 python3-pip
sudo pip3 install virtualenv
venv:
venv: clean-venv
@ echo "-------------------------------------------------------"
@ if [ ! -d "venv/" ]; then \
virtualenv -q --no-site-packages --python='${PY}' venv; \
echo "Virtualenv with ${PY} created at venv/"; \
echo "-------------------------------------------------------"; \
fi
venv/bin/pip install -r requirements.pip
@ echo "on"
@ venv/bin/pip --version
virtualenv -v --python='${PY}' ${VENV}
@ echo "Virtualenv with interpreter '${PY}' was created at ${VENV}"
@ echo "-------------------------------------------------------"
${VENV}/bin/pip3 install --upgrade pip
@ echo "-------------------------------------------------------"
${VENV}/bin/pip3 install -r requirements.pip
@ echo "-------------------------------------------------------"
@ echo "Virtualenv is ready at ${VENV}!"
@ echo " "
@ echo "TOTAL SIZE: "
@ du -sh ${VENV}
clean-venv:
rm -rf ${VENV}
log:
@ venv/bin/python meteorolog.py
@ ${VENV}/bin/python meteorolog.py
testserial:
@ venv/bin/ipython test_serial.py
@ ${VENV}/bin/ipython test_serial.py
clean:
clean-log:
rm -rf datalog.csv
rm -rf outgoing.json
#-------------------------------------------------------------------------------
# Author: Nelso G. Jost (nelsojost@gmail.com)
#
# License: BEERWARE (http://en.wikipedia.org/wiki/Beerware)
#
# Purpose: Testing board comunication.
# License: GPL
# Purpose: Get data from the board via serial and send it to the server.
#-------------------------------------------------------------------------------
from __future__ import print_function, division
......@@ -17,9 +15,19 @@ import json
import yaml
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)
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')
CSV_SEP = ','
......@@ -33,10 +41,16 @@ class Meteorologger:
self.loadSettings()
def _normalizePathFilename(self, key):
''' Appends __file__ basedir to config keys that don't have a basedir.
'''
if not os.path.dirname(self.CFG[key]):
self.CFG[key] = make_current_file_path(self.CFG[key])
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:
self.CFG = yaml.safe_load(f)
......@@ -59,6 +73,10 @@ class Meteorologger:
self._normalizePathFilename('SERVER_OUTGOING_DATA_LOG_FILENAME')
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"),
'sensors': {}}
......@@ -77,6 +95,9 @@ class Meteorologger:
return raw_sensor_data
def add_data_log(self, json_data):
'''
'''
csv_line = json_data['datetime'] + self.CFG['EXPORT_CSV_SEP']
for sensor in self.CFG['SENSORS']:
if sensor['nickname'] in json_data['sensors']:
......@@ -168,6 +189,11 @@ class Meteorologger:
ser.close()
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
try:
while True:
......
......@@ -11,7 +11,7 @@
String read_LDR()
{
return String(100/1023.0 - analogRead(LDR_PIN));
return String(100/1023.0 * analogRead(LDR_PIN));
}
// === DHT22 SETUP =======================================
......@@ -36,6 +36,11 @@ String read_DHT22_AH()
// === BMP085 SETUP ===============================================
// 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"
Adafruit_BMP085 bmp;
......
# server's full base address for sendind requests (must end with /)
BASE_URL: http://localhost:5000/
# board to upload data baka
BOARD_ID: 2
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