saving sensor data raspberry pi to data base - python

I'm currently working with raspberry pi and using DHT11 to read temperature and humidity values every second. I have to save these values into a database in real time.
here's my code that showing sensor data every second, i don't know how and where do i insert lines of connection to database.
import RPi.GPIO as GPIO
import dht11
import time
import datetime
# initialize GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.cleanup()
# read data using pin 7
instance = dht11.DHT11(pin=4)
while True:
result = instance.read()
if result.is_valid():
print("Last valid input: " + str(datetime.datetime.now()))
print("Temperature: %d C" % result.temperature)
#print("Temperature: %d F" % ((result.temperature * 9/5) + 32))
print("Humidity: %d %%" % result.humidity)
time.sleep(1)

first install MySQL db on your system then use PyMySQL or any other library for connection to MySQL from python if using PyMySQL go through this DOC
install library using
pip install PyMySQL
Make connection once so put connection codes on top your while loop
db = PyMySQL.connect("localhost:port","username","password","database_name" )
cursor = db.cursor()
while True:
use cursor for SQL-QUERY execution inside while loop where you get valid results to be stored
sql = "insert into table_name(id,feild1,feild2) values (1,value1,value2);"
# Execute the SQL command
cursor.execute(sql)
# Commit your changes in the database
db.commit()
change the fields table name and connection information as in database and replace values in INSERT statement to your sensor values

Related

Is it possible to continuously read a database in python?

I am developing a 3D Scanner using a Raspberry Pi and a Canon SLR. To control the reflex and the turntable, I use a simple web interface that I developed.
But I have to make the link between a "Start" or "Pause" buttons between the HTML interface and the Python control program. So I create a database to store the settings for the SLR (ISO, Shutterspeed ...).
For example, is it possible to have a box in the database with a state true or false for the pause button and then read it continuously on the python program?
I made a little test code like this below :
import mysql.connector
import time
mydb = mysql.connector.connect(
host="localhost",
user="scanner",
passwd="valentin",
database="Scanner3D"
)
print(mydb)
mycursor = mydb.cursor()
mycursor.execute("SELECT a FROM test")
myresult = mycursor.fetchall()
print(myresult)
myresult = str(myresult)
print(myresult)
x = myresult[2:4]
print(x)
while x == "10":
print("Hello World")
time.sleep(1)
mycursor.execute("SELECT a FROM test")
myresult = mycursor.fetchall()
myresult = str(myresult)
x = myresult[2:4]
print(x)
Problem is when I change the value of A in the database, the program never displays the new value and never leaves the loop, what is wrong in here?
Consider to use real-time communication like websocket.
The simplest way is: When scanner scans some value and saves it to DB, send via websocket command to client, to pull new data from server. Or you can send new data right in websocket message.
Very nice tutorial is here: https://realpython.com/python-sockets/

raspberry pi sending data to a XAMPP database

I have the following code in python for sending data to a mysql database
import time
import datetime
import MySQLdb
from time import strftime
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
PIR_PIN = 21
GPIO.setup(PIR_PIN, GPIO.IN)
# Variables for MySQL
db = MySQLdb.connect(host="*******", user="root",passwd="*****", db="sensor1")
cur = db.cursor()
while True:
i = GPIO.input(PIR_PIN)
print i
datetimeWrite = (time.strftime("%Y-%m-%d ") + time.strftime("%H:%M:%S"))
print datetimeWrite
sql = ("""INSERT INTO templog (datetime,temperature) VALUES (%s,%s)""",(datetimeWrite,i))
try:
print "Writing to database..."
# Execute the SQL command
cur.execute(*sql)
# Commit your changes in the database
db.commit()
print "Write Complete"
except:
# Rollback in case there is any error
db.rollback()
print "Failed writing to database"
cur.close()
db.close()
break
My problem is that my XAMPP server is installed in pc where I want to view the the data from raspberry pi in mysql database.
So for getting the connection established what should I write in host= "?"
The connection-string should be like this:
db = MySQLdb.connect(host="192.168.0.xxx", user="root",passwd="*****", db="sensor1")
See this Question
Host will be IP address of your system on which XAMPP is installed e.g. 192.168.x.x

getting data from ibm bluemix into python code

I need code for getting data from bluemix which is sent by this code
import RPi.GPIO as GPIO
import dht11
import time
import datetime
import ibmiotf.device
# initialize GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.cleanup()
1
client.connect()
# read data using pin GPIO4
instance = dht11.DHT11(pin=4)
while True:
result = instance.read()
if result.is_valid():
print("Last valid input: " + str(datetime.datetime.now()))
temp=(" %d C" % result.temperature)
print("temperature:" +temp)
humid=("%d %%" % result.humidity)
print("Humidity:" +humid)
# print("Temperature: %d C" % result.temperature)
# print("Humidity: %d %%" % result.humidity)
print("Data is published into the cloued")
Data={'Temperature' :temp, 'Humidity' :humid }
client.publishEvent("status", "json", Data)
time.sleep(1)
you can use NodeRed to add some node(insert, function, iotout) to publish the data to iot platform(publish it as device type) into a topic then in the PI with pyhton subscribe to that topic as an application... that should work just fine. All the connection/publish/subscribing/topics details can be found here:
https://console.ng.bluemix.net/docs/services/IoT/index.html
You connect as a device -> import ibmiotf.device. A device can send events and receive commands. It can't receive events. To do this you need to connect as an application. You will import ibmiotf.application and have something like:
options = ibmiotf.application.ParseConfigFile(configFilePath)
appClient = ibmiotf.application.Client(options)
appClient.connect()
appClient.subscribeToDeviceEvents()
The config file should be like:
[application]
org=orgId
id=myApplication
auth-method=apikey
auth-key=key
auth-token=token
Where the auth-key/auth-token comes from the Apps part of your platform. You'll have to generate a set to connect as an application. The auth-method should be left as apikey.
That will list every event that arrives to your platform. You can make it more granular. See the api guide which also references a sample app on github.

Cannot fetch data from MySQL database with Python

I'm trying to write a program that receives data from an XBee and then uploads the data to a database. The program is written in Python. I got the XBee part right, but when I try to execute a SQL command I get an error.
Here is the code:
from xbee import ZigBee
import serial
import MySQLdb
PORT = 'COM4'
BAUD_RATE = 9600
# Open serial port
ser = serial.Serial(PORT, BAUD_RATE)
zb = ZigBee(ser, escaped = True)
#DATA BASE CONNECTION
db = MySQLdb.connect("localhost","b3")
# prepare a cursor object using cursor() method
cursor = db.cursor()
# execute SQL query using execute() method.
cursor.execute("SELECT VERSION()")
# Fetch a single row using fetchone() method.
data = cursor.fetchone()
print "Database version : %s " % data
def checkSistem(sistemCode):
sql = "SELECT count(*) FROM sistema WHERE Codigo = '"+sistemCode+"'"
try:
# Execute the SQL command
cursor.execute(sql)
# Fetch all the rows in a list of lists.
results = cursor.fetchone()
print results[0]
except:
print "Error: unable to fecth data"
while True:
try:
dataXBee = zb.wait_read_frame()
data = dataXBee['rf_data'].decode("utf-8").split(",")
checkSistem(data[0])
print (data[1])
except KeyboardInterrupt:
break
And when I run the program I get this:
Database version : 5.6.26
Error: unable to fecth data
S1

Unknown Database Error python mysql

This link contains shows the database I've created in the mysql workbench and the connection I have established in the code but the database is unknown for some reason. Is there a step I've missed?
http://gyazo.com/d995c4da99043da43bfbd057a0a839c7
__author__ = 'avi'
from TwitterSearch import *
import json
twtsearch = TwitterSearch(
consumer_key='PXTUrlRfgC1zSTsAPU9z6EHtD',
consumer_secret='qM9F4FVj1qLFc6f795r96DQPNAJO8hkbWy4PXWYLfQcYyNGY7D',
access_token='2943116292-wVHEjbfjX7OFqaOURBqim5o7Vs6lZyjxsoto8nD',
access_token_secret='CJAppSRY9TZ5cwYTABZhH2YTd0rm5IzBDqPder6v4qLBA'
)
twtsearchorder = TwitterSearchOrder()
twtsearchorder.set_keywords(['iphone6'])
twtsearchorder.set_language('en')
twtsearchorder.set_include_entities(True)
tweet_limit=50
parsed_tweets= {}
table="twtinfo"
import MySQLdb as mdb
con = mdb.connect('localhost', 'root','root','tweetinfo')
cur=con.cursor()
for tweet in twtsearch.search_tweets_iterable(twtsearchorder):
if tweet_limit > 0 :
parsed_tweets['name'] = tweet['user']['screen_name']
parsed_tweets['content'] = tweet['text']
parsed_tweets['user_id'] = tweet['user']['id']
parsed_tweets['fav_count'] = tweet['favorite_count']
parsed_tweets['location'] = tweet['user']['location']
parsed_tweets['retweet_count'] = tweet['retweet_count']
placeholders= ', '.join(['%s'] *len(parsed_tweets))
columns = ', '.join(parsed_tweets.keys())
sql="INSERT into %s ( %s ) VALUES ( %s )" % (table, columns, placeholders)
cur.execute(sql,parsed_tweets.values())
tweet_limit -= 1
The MySQL process is complaining about the database you are trying to access, namely tweetinfo isn't existing. MySQL error 1049 is usually an indication of having to forgot to select a database, but you did as forth argument to mdb.connect()
Possible errors could be:
That you have several MySQL processes running, with the one with the proper database not being on the default MySQL port.
That somehow your database GUI application hasn't actually submitted your database and table to the MySQL process.
That MySQL isn't running? You would probably get a different error message for that, but it could be an idea to make sure it is just in case.
Just to check if your tables exists and that your database is in place, open a terminal and write the following commands:
mysql -u root -proot
use tweetinfo;
show create table twtinfo;
Another thing to try could be to ask the MySQL process about which database it thinks you are using. Try adding something like the following to your code:
cur.execute("SELECT DATABASE() FROM DUAL;")
print("Database is: %s.", cur.fetchone()[0])
I'm not a python programmer, so I'm not entirely confident that will work without some adjustments.
If none of this gave you a good lead, I'm not quite sure what's wrong.

Categories

Resources