skip to Main Content

As soon as I run this python code that includes some mysql, all I get are the three greater or less signs(>>>). Any help would be much appreciated!
My code consists of trying to obtain temperatures through a ds18b20 connected to my raspberry pi 3 and sending that data into a mysql database that I have created.

Here is the python/mysql code :

import os
import glob
import time
import MySQLdb
import datetime

i = datetime.datetime.now()

db = MySQLdb.connect(host = "127.0.0.1", user = "root", passwd = "test", db = "temp_pi")
cur = db.cursor()

os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')

base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'

def read_temp_raw():
    f = open(device_file, 'r')
    lines = f.readlines()
    f.close()
    return lines

def read_temp():
    lines = read_temp_raw()
    while lines[0].strip()[-3:] != 'YES':
        time.sleep(0.2)
        lines = read_temp_raw()
    equals_pos = lines[1].find('t=')
    if equals_pos != -1:
        temp_string = lines[1][equals_pos+2:]
        temp_c = float(temp_string) / 1000.0
        temp_f = temp_c * 9.0 / 5.0 + 32.0
        return temp_c
    while True:
          print ("recording data into database(period = 5s.)....press ctrl+Z to stop!")

          valT = str(read_temp())

          year = str(i.year)
          month = str(i.month)
          day = str(i.day)
          date = day + "-" + month + "-" + year

          hour = str(i.hour)
          minute = str(i.minute)
          second = str(i.second)
          timestr = hour + ":" + minute + ":" + second

          try:
             cur.execute("""INSERT INTO TAB_CLASSROOM(temp_c,T_Date,T_Time) VALUES(%s,%s,%s)""",(valT,date,time))
             db.commit()
          except:
             db.rollback()

          time.sleep(10)

    cur.close()  
    db.close() 

2

Answers


  1. You’ve never run anything inside that code. The bulk of your code is inside read_temp() and it isn’t called anywhere in it. The >>> means the program has ended and it’s entered interpreter mode. Add some code after it for it to work the way you want it to.

    Login or Signup to reply.
  2. You are not, in fact running anything. Assuming you do

    python myscript.py
    

    python will load the file and run it – which means execute the code line-by-line. You will find that the first few lines outside of functions are simply executed. Then you define some functions, but never call them.

    You should probably add

    read_temp()
    

    at the end of the script.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search