Arduino Forum

Using Arduino => Interfacing w/ Software on the Computer => Topic started by: lukason96 on Jun 12, 2018, 09:52 am

Title: Rasperry Pi capture data from arduino
Post by: lukason96 on Jun 12, 2018, 09:52 am
Code: [Select]
import serial
import time
import MySQLdb as mdb

arduino = serial.Serial('/dev/ttyACM1', baudrate=9600)

data = arduino.readline()
time.sleep(1)
data = arduino.readline()#read data from arduino
pieces = data.split("\t")  #spl2t the data by the tab

temperature = pieces[0]
humidity = pieces[1]
flames = pieces[2]

if temperature > 0.00 and temperature <= 30.00 : temp =  "Normal"
elif temperature  > 30.00 and temperature <= 35.00 : temp =  "Hot"
elif temperature  > 35.00 : temp = "Dangerous"
if flames == 0 : flame =  "Close fire"
elif flames == 1 : flame = "Distance fire"
elif flames == 2 : flame =  "No fire"

con = mdb.connect("localhost", "root", "test", "FYPtest")or die ("could not connect to database")

with con:
    cursor =con.cursor()
    cursor.execute("INSERT INTO fyp (TEMP,HUM,T_STAT, F_STAT) VALUES (%s, %s,%s, %s)", (temperature,humidity,temp,flame ))
   
con.commit()

cursor.close()


Ok so basically this is the python code to let me get data from arduino and display in Raspberry Pi. Although i able to get the temperature value which is 30. However, the temp status is Dangerous, by right i should be "Normal" value is under 30. Can anyone help me to solve this issue? TQ

(http://file:///C:/Users/onjay/Desktop/aaaa.PNG)
Title: Re: Rasperry Pi capture data from arduino
Post by: PaulS on Jun 12, 2018, 10:47 am
Quote
Can anyone help me to solve this issue?
I'll bet someone could, if you asked your Python question on a Python forum.

Your post has NOTHING to do with the Arduino.
Title: Re: Rasperry Pi capture data from arduino
Post by: Robin2 on Jun 12, 2018, 11:31 am
Ok so basically this is the python code to let me get data from arduino and display in Raspberry Pi.
I don't see anything in your Python code to let you know what it actually receives from the Arduino.

This Python - Arduino demo (http://forum.arduino.cc/index.php?topic=225329.msg1810764#msg1810764) my be of interest.

...R
Title: Re: Rasperry Pi capture data from arduino
Post by: oqibidipo on Jun 12, 2018, 11:47 am
Code: [Select]
pieces = data.split("\t")
temperature = pieces[0]

if temperature > 0.00 and temperature <= 30.00 : temp =  "Normal"

temperature is a string and you are comparing it to a float. This raises a TypeError in Python 3.x, but is allowed in 2.x. However, the result is not what you expect.

Make sure you are using the right types:
Code: [Select]
temperature = float(pieces[0])
humidity = float(pieces[1])
flames = int(pieces[2])  # assuming this is indeed integer