LCD屏幕出错

时间:2018-01-05 20:35:49

标签: python i2c

我遇到了液晶屏HD44780的问题。屏幕通过I2C连接到RPi3。我准备了一个显示数据的代码。

from thread import *
import socket
import sys
import string
import select
import lcddriver
import MySQLdb
import time
import mysql.connector 

BUFFER_SIZE = 1024
display = lcddriver.lcd()
database = 'data'
username = 'admin'
password = 'mypasss'

date_now = time.strftime("%Y-%m-%d %H:%M:%S")

def clientthread(conn):
    received = []
    while True:
                data = conn.recv(BUFFER_SIZE)
                if data == ";" :
                    conn.close()
                    if received[0] == '1':
                    esp_id = int(received[0])
                    temperature = float (received[1] + received[2] + received[3] + received[4])
                    humidity = float (received[5] + received[6] + received[7] + received[8])
                    esp1 = received[0] + ":T=" + received[1] + received[2] + received[3] + received[4] + "H=" + received[5] + received[6] + received[7] + received[8]
                    display.lcd_display_string(str(esp1), 1)
                    print esp1

                    datehour = time.strftime("%Y-%m-%d %H:%M:%S")
                    db = MySQLdb.connect("localhost","root","raspberry","projekt_grupowy")
                    cursor = db.cursor()
                    sql = "INSERT INTO pomiary(esp_id, temperature, humidity, datehour) VALUES('%d', '%.2f', '%.2f', '%s')" % (esp_id, temperature, humidity, datehour)
                    try: 
                        cursor.execute(sql)
                        db.commit()
                    except:
                        print "Nieudana proba wpisu"
                        db.rollback()
                    else:
                        print "Data sent"
                    received = []
                    break
                elif received[0] == '2':

                    esp_id = int (received[0])
                    temperature = float(received[1] + received[2] + received[3] + received[4])
                    humidity = float(received[5] + received[6] + received[7] + received[8])

                    esp2 = received[0] + ":T=" + received[1] + received[2] + received[3] + received[4] + "H=" + received[5] + received[6] + received[7] + received[8]
                    print esp2

                    display.lcd_display_string(str(esp2), 2)
                    datehour = time.strftime("%Y-%m-%d %H:%M:%S")
                    db = MySQLdb.connect("localhost","root","raspberry","projekt_grupowy")
                    cursor = db.cursor()
                    sql = "INSERT INTO pomiary(esp_id, temperature, humidity, datehour) VALUES('%d', '%.2f', '%.2f', '%s')" % (esp_id, temperature, humidity, datehour)
                    try:
                        cursor.execute(sql)
                        db.commit()
                    except:
                        print "Nieudana proba wpisu"
                        db.rollback()
                    else:
                        print "Data sent"
                    received = []
                    break

                else:
                    print "error sensor"
                received = []
            else:                    
                received.append(data)
conn.close()




def main():
  try:
    host = '192.168.42.1'
    port = 8888
    tot_socket = 1

    list_sock = []
    for i in range(tot_socket):
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
        s.bind((host, port+i))
        s.listen(10)
        list_sock.append(s)
        print "[*] Server listening on %s %d" %(host, (port+i))

    while 1:
        for j in range(len(list_sock)):
            conn, addr = list_sock[j].accept()
            print '[*] Connected with ' + addr[0] + ':' + str(addr[1])
            start_new_thread(clientthread ,(conn,))
    s.close()

except KeyboardInterrupt as msg:
    sys.exit(0)

if __name__ == "__main__":
     main()

我从两个ESP模块接收数据并在屏幕上显示。 3

display.lcd_display_string(str(esp1), 1)
display.lcd_display_string(str(esp2), 2)

有时会有两个模块同时发送数据。结果,即使我实现了多线程,屏幕也显示错误。我该怎么做才能避免这种情况?

0 个答案:

没有答案