如何使用python在nextion上发送数据

时间:2018-09-01 03:42:13

标签: python raspberry-pi nextion

我需要一些帮助。我正在尝试将变量发送到T0。我已经尝试过,但是无法发送变量值。请帮忙。

代码

import serial 
import time 
import struct 
from requests import get 

ip = get('https://api.ipify.org').text 
ser = serial.Serial("/dev/ttyAMA0") 
time.sleep(1) 
k=struct.pack('B', 0xff ) 
while True: 
    ser.write(b't0.txt=ip') 
    ser.write(k) 
    ser.write(k) 
    ser.write(k) 

2 个答案:

答案 0 :(得分:0)

您必须将字符串用引号("括起来,以便Nextion读取字符串。

不是Python专家,但这应该可以给您一个提示:

ser.write(b't0.txt=ip')更改为ser.write(b't0.txt="' + ip + '"')

答案 1 :(得分:0)

这对我有用。

port=serial.Serial(port='/dev/ttyAMA0',baudrate=9600, timeout=1.0)
eof = "\xff\xff\xff"
tn = str(datetime.now().time())
alt = 'page0.T0.txt="'+tn+'"'+eof
dimCmd = "dim=0"
undimCmd = "dim=100"
cmd1 = "https://api.thingspeak.com/apps/thinghttp/send_request?api_key=YOUR_API_KEY" #IF USING THINGSPEAK

#get temp Sub - USING THINGSPEAK GET LOCAL WEATHER
def GetTemp():
global temp
response = urllib.urlopen(cmd1)
temp = response.read()
temp = temp[:-2] #gets all characters in temp except the last 2

while True:
	port.write(alt) #writes time to T0 textbox on screen
    port.write(undimCmd + eof) #set screen brightness to 100%
    port.write("page 1")#set screen to page 1 for multiple page setups