/*
Niladri Podder
*/
#include "HX711.h" //You must have this library in your arduino library folder
const int LOADCELL_DOUT_PIN = 2;
const int LOADCELL_SCK_PIN = 3;
HX711 scale;
//Change this calibration factor as per your load cell once it is found you may need to vary it in thousands
float calibration_factor = 69525;
//=============================================================================================
// SETUP
//=============================================================================================
void setup() {
Serial.begin(9600);
//softSerial.begin(9600);
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
//Serial.println("HX711 Calibration");
//Serial.println("Remove all weight from scale");
//Serial.println("After readings begin, place known weight on scale");
//Serial.println("Press a,s,d,f to increase calibration factor by 10,100,1000,10000 respectively");
//Serial.println("Press z,x,c,v to decrease calibration factor by 10,100,1000,10000 respectively");
//Serial.println("Press t for tare");
scale.set_scale();
scale.tare(); //Reset the scale to 0
long zero_factor = scale.read_average(); //Get a baseline reading
//Serial.print("Zero factor: "); //This can be used to remove the need to tare the scale. Useful in permanent scale projects.
//Serial.println(zero_factor);
}
//=============================================================================================
// LOOP
//=============================================================================================
void loop() {
scale.set_scale(calibration_factor); //Adjust to this calibration factor
float load = scale.get_units()/10 ;
if (load >= 0.000)
{
//Serial.print("Reading: ");
Serial.println(load);
//Serial.print(" kg"); //Change this to kg and re-adjust the calibration factor if you follow SI units like a sane person
//Serial.print(" calibration_factor: ");
//Serial.print(calibration_factor);
//Serial.println();
}
else {
//don't print
}
delay(600000); //10 minute
//delay(5000); //5 sec for testing
if(Serial.available())
{
char temp = Serial.read();
if(temp == '+' || temp == 'a')
calibration_factor += 10;
else if(temp == '-' || temp == 'z')
calibration_factor -= 10;
else if(temp == 's')
calibration_factor += 100;
else if(temp == 'x')
calibration_factor -= 100;
else if(temp == 'd')
calibration_factor += 1000;
else if(temp == 'c')
calibration_factor -= 1000;
else if(temp == 'f')
calibration_factor += 10000;
else if(temp == 'v')
calibration_factor -= 10000;
else if(temp == 't')
scale.tare(); //Reset the scale to zero
}
}
//=============================================================================================
pi@raspberrypi ~ $ dmesg | grep tty
Install the SQLite package:
---> sudo apt-get install sqlite3
Then you can install SQLite browser:
---> sudo apt-get install sqlitebrowser
import numpy as np
import sqlite3
import json
import requests
import ssl
import http.client
import serial
import ast
def job():
# this port address is for the serial communication
SERIAL_PORT = '/dev/ttyUSB0' # 'COM7'
# be sure to set the baudrate to the same used on the Arduino nano
SERIAL_RATE = 9600
ser = serial.Serial(SERIAL_PORT, SERIAL_RATE)
# reading = ser.readline().decode('utf-8')
# reading is a string...do whatever you want from here
data = []
certificate_file = 'certificate.pem'
certificate_secret = 'inPjqfPN3RJt4KBcUE39SVj3RszyJziQ0rS3'
host = '<your tenant id>.eu10.cp.iot.sap'
# SAP IoT Cockpit Values
device_alternate_id = '77'
capability_alternate_id = '99'
sensor_alternate_id = '66'
# iterating till the range as we are collecting 6 values in every 1 hr for 10 min interval
for i in range(6):
reading = ser.readline().decode('utf-8')
val = ast.literal_eval(reading)
print(val)
line = data.append(val)
array1 = np.array(data)
sensor = np.ptp(array1)
if sensor == 0:
r = requests.post(url="https://maker.ifttt.com/trigger/<event name>/with/key/<key from IFTTT webhook>")
quit()
else:
print(sensor)
conn = sqlite3.connect('/home/pi/Database/sensor_new.db')
cur = conn.cursor()
cur.execute("update water_table set new_value = ? where id = 1", (sensor,))
cur.executescript("""
update water_table set total = (select sum(new_value + old_value) from water_table) where id = 1;
update water_table set old_value = (select total from water_table) where id = 1;
""")
select = cur.execute("select new_value, total from water_table;")
for row in select:
request_body_dict = {
'capabilityAlternateId': capability_alternate_id,
'sensorAlternateId': sensor_alternate_id,
'measures': [{
'consumed': row[0],
'total': row[1]
}]
}
print(json.dumps(request_body_dict))
# Defining parts of the HTTP request
request_url = '/iot/gateway/rest/measures/%s' % device_alternate_id
request_headers = {
'Content-Type': 'application/json'
}
# Declare client certificate settings for https connection
context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
context.load_cert_chain(certfile=certificate_file, password=certificate_secret)
connection = http.client.HTTPSConnection(host, port=443, context=context)
# HTTP POST request
connection.request(method="POST", url=request_url,headers=request_headers, body=json.dumps(request_body_dict))
# Print the HTTP response
response = connection.getresponse()
print(response.status, response.reason)
if response.status == 202:
print("Go on")
while True:
job()
pi@raspberrypi ~ $ python smart_bottle.py
0.33
0.33
0.27
0.27
0.27
0.27
0.06
{"capabilityAlternateId": "99", "sensorAlternateId": "66", "measures": [{"consumed": 0.06, "total": 4.81}]}
202 Accepted
Go on
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
26 | |
15 | |
14 | |
14 | |
13 | |
8 | |
8 | |
7 | |
6 | |
5 |