1

I would like some help. I'm trying to send a variable to T0. I've tried but I cannot send a variable value. Please help.

CODE

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) 
Roy Scheffers
  • 3,832
  • 11
  • 31
  • 36

2 Answers2

0

You have to wrap your string in quotation marks (") for Nextion to read the string.

Not a Python expert, but this should give you a clue:

Change ser.write(b't0.txt=ip') to something like ser.write(b't0.txt="' + ip + '"').

Hein Andre Grønnestad
  • 6,885
  • 2
  • 31
  • 43
0

This works for me.

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
    
Tom
  • 1
  • 1