· 8 years ago · Nov 22, 2017, 05:50 AM
1#!/usr/bin/python
2# -*- coding: utf-8 -*-
3
4import sqlite3
5from sqlite3 import Error
6import datetime
7import time
8import json
9import platform
10import config
11import sensors
12import requests
13
14# Setup the Ubidots stuff
15
16from ubidots import ApiClient
17
18
19def main():
20 print '--------------------------------------------------------------------------------'
21 print ' Brijn Onion datalogger v0.1'
22 print '--------------------------------------------------------------------------------'
23
24 database = '/root/BOD/data_logger.db'
25 hostname = None
26 ubidotsOnline = False
27 sensorData = None
28 dataCount = 0
29
30 api = ApiClient(token=config.ubidots_token)
31
32 hostname = platform.node().upper()
33 logline('INFO', 'Detected our hostname as [' + hostname + ']')
34
35 # create a database connection
36
37 logline('INFO', 'Creating database connection')
38 conn = create_connection(database)
39
40 # And just to be sure we always run the table create code
41
42 create_table(conn)
43
44 logline('INFO', '.. Done')
45
46 # Check if Ubidots is online
47
48 logline('INFO', 'Check Ubidots connection')
49 ubidotsOnline = ubidotsIsReachable(api)
50 #ubidotsOnline = False
51
52 if ubidotsOnline:
53 logline('INFO', '.. Ubidots is reachable, submit data directly')
54
55 dataCount = count_datalog(conn)
56
57 if dataCount > 0:
58
59 # We have some data to push out
60
61 logline('INFO',
62 '.... We have locally cached data to push out to Ubidots ('
63 + str(dataCount) + ' records)')
64 dataFromDb(conn)
65 else:
66 logline('WARN',
67 '.. Ubidots not reachable, submit data to local cache'
68 )
69
70 # Doing a sensor read is what we are all about, if that fails, no point in doing anything else
71
72 logline('INFO', 'Reading sensor data')
73
74 for sensor in config.sensor_list:
75
76 # Loop over the sensors
77
78 logline('INFO', '.. Reading data from sensor ' + sensor.upper())
79 get_sensor = getattr(sensors, sensor.upper())
80
81 if ubidotsOnline:
82 logline('INFO', '.... Sending data to Ubidots')
83 dataToUbidots(hostname, sensor.upper(), get_sensor())
84 else:
85 logline('INFO', '.... Sending data to local database')
86 data = (sensor.upper(), str(get_sensor()))
87 print data
88 create_datalog(conn, data)
89
90 logline('INFO', '.. Sensor read complete')
91
92 conn.close()
93
94
95######################################################################
96# Functions
97######################################################################
98
99def ubidotsIsReachable(api):
100 try:
101
102 # Get all datasources
103
104 all_datasources = api.get_datasources()
105 except Exception, e:
106 return False
107
108 # We should be good at this point
109
110 return True
111
112
113def dataToUbidots(hostname, sensor_name, data):
114 url = 'http://things.ubidots.com/'
115 url = url + 'api/v1.6/devices/' + hostname + '_' + sensor_name
116 headers = {'X-Auth-Token': config.ubidots_token,
117 'Content-Type': 'application/json'}
118
119 try:
120 print data
121 req = requests.post(url=url, headers=headers, json=data)
122 #print req.json()
123 except requests.exceptions.RequestException, e:
124 print e
125
126
127def dataFromDb(conn):
128 """
129 Pull all cached sensor data from the local database and push it to Ubidots
130 """
131
132 hostname = platform.node().upper()
133
134 cur = conn.cursor()
135 cur.execute('SELECT sensor_name,sensor_data FROM log_data')
136
137 rows = cur.fetchall()
138
139 for row in rows:
140 print 'Results %s %s' % (row[0], row[1])
141 dataToUbidots(hostname, row[0], row[1])
142
143 # Now that it has been uploaded, wipe the data from the DB
144
145 #cur.execute('DELETE FROM log_data')
146 #cur.execute('VACUUM')
147
148
149def create_connection(db_file):
150 """ create a database connection to the SQLite database
151 specified by db_file
152 :param db_file: database file
153 :return: Connection object or None
154 """
155
156 try:
157 conn = sqlite3.connect(db_file)
158 return conn
159 except Error, e:
160 print e
161
162 return None
163
164
165def create_datalog(conn, data):
166
167 sql = \
168 '''INSERT INTO log_data(sensor_name,sensor_data)
169 VALUES(?,?) '''
170 cur = conn.cursor()
171 cur.execute(sql, data)
172 conn.commit()
173 return cur.lastrowid
174
175
176def count_datalog(conn):
177 """
178 Return the number of rows (as int) that are stored in the database
179 """
180
181 sql = 'SELECT COUNT(*) FROM log_data'
182 try:
183 c = conn.cursor()
184 c.execute(sql)
185 count = c.fetchone()
186 return int(count[0])
187 except Error, e:
188 print e
189
190
191def create_table(conn):
192 sql_create = \
193 """CREATE TABLE IF NOT EXISTS log_data (
194 id integer PRIMARY KEY,
195 sensor_name text NOT NULL,
196 sensor_data text NOT NULL
197);"""
198
199 try:
200 c = conn.cursor()
201 c.execute(sql_create)
202 except Error, e:
203 print e
204
205
206def logline(type, message):
207 timestamp = datetime.datetime.utcnow().strftime('%Y%m%d %H:%M:%S')
208 print '[' + timestamp + '] ' + type + ' ' + message
209
210
211if __name__ == '__main__':
212 main()