UnoGarden

Arduino / TheThingsUno

Moisture sensor

Relais

Water level

(temperature)

CronTab:
*/1 * * * * sh /home/pi/GreenPi/LaunchArduino2DB.sh

Arduino2DB.sh:
import serial
import MySQLdb
port=”/dev/ttyUSB0″
connection = MySQLdb.connect (host = “localhost”,
user = “xxxx”,
passwd = “xxxx”,
db = “GreenPi”)
cursor = connection.cursor()
#cursor.execute (“SELECT VERSION()”)
cursor.execute (“SHOW TABLES LIKE ‘IncommingData’;”)
row = cursor.fetchone()
if not row:
print(“Table do not exist”)
cursor.execute(“””

CREATE TABLE IncommingData (
ID INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
Message VARCHAR(4000) NOT NULL,
Origin VARCHAR(200) NOT NULL,

CreatedDate TIMESTAMP
);
“””)
else:
print(“Table exist”)
cursor.close()
#connection.close()

# set up the serial connection speed
ser = serial.Serial(port, 9600)

# main loop
while 1:
# c = input(‘Enter a char: ‘)
# if len(c) == 1:
# send data to the Arduino
# ser.write(c.encode())
# receive data from the Arduino
response = ser.readline()
sql=”insert into IncommingData (Message,Origin) values (‘” + response.decode()$
print(sql)

cursor = connection.cursor()
cursor.execute(sql)
connection.commit()
cursor.close()

LaunchArduino2DB.sh:

!/bin/sh
# launcher.sh
# navigate to home directory, then to this directory, then execute python scrip$
cd /
cd home/pi/GreenPi
if pgrep -f “python Arduino2DB.py” > /dev/null
then
echo “was found running”
else
nohup python Arduino2DB.py >/dev/null 2>&1
echo “started”
fi
cd /