Constantine Karamichalis
4 years ago
2 changed files with 112 additions and 0 deletions
@ -0,0 +1,43 @@ |
|||||
|
import smbus |
||||
|
import time |
||||
|
import re |
||||
|
from datetime import datetime |
||||
|
from influxdb import InfluxDBClient |
||||
|
|
||||
|
|
||||
|
client = InfluxDBClient('192.168.1.6', 8086, 'test', 'test', 'sensor') |
||||
|
|
||||
|
|
||||
|
bus = smbus.SMBus(1) |
||||
|
address = 0x16 |
||||
|
|
||||
|
while True: |
||||
|
try: |
||||
|
now = datetime.now() |
||||
|
data = "" |
||||
|
for i in range(0, 40): |
||||
|
data += chr(bus.read_byte(address)); |
||||
|
|
||||
|
date_time = now.strftime("%m/%d/%Y, %H:%M:%S") |
||||
|
file = open("sensor_logs", "a") |
||||
|
file.write(date_time +" "+ data + "\n") |
||||
|
file.close() |
||||
|
find_values = re.findall(r'[-]{0,1}[\d]*[\.][\d]+', data) |
||||
|
create_list = list(map(str,find_values)) |
||||
|
# print (create_list) |
||||
|
# print(create_list[0]) |
||||
|
# print(create_list[1]) |
||||
|
# print(create_list[2]) |
||||
|
x_value = (create_list[0]) |
||||
|
y_value = (create_list[1]) |
||||
|
z_value = (create_list[2]) |
||||
|
|
||||
|
client.write(["logg,sensor_type=lilypad x_value="+x_value+",y_value="+y_value+",z_value="+z_value+""],{'db':'sensor'},204,'line') |
||||
|
|
||||
|
print (date_time+ " " + data) |
||||
|
print("\n") |
||||
|
time.sleep(1); |
||||
|
|
||||
|
except KeyboardInterrupt: |
||||
|
print("\n"+"Interrupt detected Stopping program") |
||||
|
exit(0) |
@ -0,0 +1,69 @@ |
|||||
|
#include <Wire.h> |
||||
|
#include <stdlib.h> |
||||
|
|
||||
|
#define SLAVE_ADDRESS 0x16 |
||||
|
|
||||
|
const int xpin = A0; // x-axis of the accelerometer
|
||||
|
const int ypin = A1; // y-axis
|
||||
|
const int zpin = A2; // z-axis (only on 3-axis models)
|
||||
|
int index = 0; |
||||
|
char data[40]; |
||||
|
|
||||
|
void setup() { |
||||
|
//Serial.begin(9600);
|
||||
|
pinMode(xpin, INPUT); |
||||
|
pinMode(ypin, INPUT); |
||||
|
pinMode(zpin, INPUT); |
||||
|
// initialize i2c as slave
|
||||
|
Wire.begin(SLAVE_ADDRESS); |
||||
|
Wire.onRequest(sendData); |
||||
|
} |
||||
|
|
||||
|
void loop() { |
||||
|
// Nothing to do at last !!!!
|
||||
|
} |
||||
|
|
||||
|
void sendData() { |
||||
|
read_lilypad(); |
||||
|
Wire.write(data[index]); |
||||
|
index++; |
||||
|
if (index >= 40) { |
||||
|
index = 0; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
//Function that reads the data from the Lilypad sensor
|
||||
|
void read_lilypad () { |
||||
|
int x_input = analogRead(xpin); |
||||
|
int y_input = analogRead(ypin); |
||||
|
int z_input = analogRead(zpin); |
||||
|
|
||||
|
//zero_G is the reading we expect from the sensor when it detects
|
||||
|
//no acceleration. Subtract this value from the sensor reading to
|
||||
|
//get a shifted sensor reading.
|
||||
|
//float x_zero_G = 515.39;
|
||||
|
//float y_zero_G = 508.57;
|
||||
|
//float z_zero_G = 513.19;
|
||||
|
|
||||
|
float x_zero_G = 401.01; |
||||
|
float y_zero_G = 407.15; |
||||
|
float z_zero_G = 474.69; |
||||
|
|
||||
|
//scale is the number of units we expect the sensor reading to
|
||||
|
//change when the acceleration along an axis changes by 1G.
|
||||
|
//Divide the shifted sensor reading by scale to get acceleration in Gs.
|
||||
|
float scale = 102.3; |
||||
|
|
||||
|
//Calculate values and convert them to string
|
||||
|
|
||||
|
String x_value = String(((float)x_input - x_zero_G) / scale); |
||||
|
String y_value = String(((float)y_input - y_zero_G) / scale); |
||||
|
String z_value = String(((float)z_input - z_zero_G) / scale); |
||||
|
|
||||
|
//Putting all values to one string
|
||||
|
String lilypad = "Lilypad x: " + x_value + " y: " + y_value + " z: " + z_value; |
||||
|
|
||||
|
/* Saving The string to an array to be send to master */ |
||||
|
lilypad.toCharArray(data, 40); |
||||
|
|
||||
|
} |
Loading…
Reference in new issue