1

我想要一些帮助。我正在尝试将变量发送到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) 
4

2 回答 2

0

您必须将字符串括在引号 ( ") 中,以便 Nextion 读取字符串。

不是 Python 专家,但这应该会给你一个线索:

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

于 2018-10-29T11:19:38.327 回答
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
    

于 2018-11-18T13:17:32.207 回答