# Termi2 v0.5# by Cameron Coward# www.cameroncoward.com# 11/10/22## ESP32-based serial virtual assistant for TI Silent 700 terminals# Ask questions, get answers!# Utilizes Wolfram Alpha Simple Questions APIfrom machine import UARTfrom machine import Pinimport machineimport espimport esp32import urequests as requestsimport osimport reimport timereading_uart = False #becomes True when receiving bytes from UARTonboard_led = machine.Pin(2, machine.Pin.OUT) #LED for indicating WiFi connectiononboard_led.value(0) #start with LED offuart2 = UART(2, baudrate=300, bits=7, parity=1, stop=1, timeout=5000, tx=17, rx=16, flow=0, txbuf=0, invert=UART.INV_TX | UART.INV_RX) #create UARTdef do_connect():    #open text file to pull network credentials, if they exist    file = open("creds.txt")    cred_string = file.read()    creds = cred_string.split(",")    file.close()        #activate and config WLAN    import network    wlan = network.WLAN(network.STA_IF)    wlan.active(True)    wlan.config(reconnects=50)            if (len(cred_string) > 2): #check if at least two characters were in file        if not wlan.isconnected():            wlan.connect(creds[0], creds[1])    else:        connect_failed()    tries = 0 #reset connection attempt counter    while not wlan.isconnected(): #loops until WiFi connection established or after 100 loops        time.sleep(0.25) #wait a quarter second        tries = tries + 1        if tries > 100:            connect_failed()        onboard_led.value(1) #turns LED on to indicate WiFi connected    enter_question() #let user ask a questiondef ask_question(query):    res = requests.get(url=query) #get results from Wolfram Alpha    uart_output(res.text) #print results    enter_question() #let user ask another questiondef enter_question():    question = uart_input("Ask Termi2: ") #get question string from user    question = re.sub(r"[^a-zA-Z0-9 ]", "", question) #remove all special characters    question = question.replace(' ', '+') #change to Wolfram Alpha API URL format    # ****************** ENTER YOUR APP ID ON THE LINE BELOW!!!     query = 'http://api.wolframalpha.com/v1/result?appid=YOUR APP ID HERE&i=' + question #add question to API app URL... ENTER YOUR APP ID!    ask_question(query) #pass query string to function for sending API request    def uart_input(prompt_str):    uart_output(prompt_str) #print the passed string    reading_uart = True    uart_input_str = '' #start with an empty string    while reading_uart == True:        input_byte = uart2.read(1) #read a single byte                if input_byte:            cr_str = "\r" #carriage return            cr_byte = cr_str.encode('utf-8') #encode carriage return as byte            if input_byte == cr_byte: #check if the received byte is a carriage return                reading_uart = False            else:                input_str = input_byte.decode('utf-8') #decode input byte                uart_input_str = uart_input_str + input_str #add character to string with full input text    return uart_input_strdef uart_output(message):    uart2.write('   \n \n \r')    line_count = 0    for c in message:        if line_count > 74:            uart2.write('   \n \n \r')            line_count = 0        uart2.write(c)        line_count+=1    uart2.write('   \n \n \r')    def connect_failed():    uart_output("Unable to connect to WiFi. Enter new credentials by opening creds.txt file in flash memory and format as 'SSID,password' ")    pause_indefinitely = True    blink_led_counter = 0    while pause_indefinitely:        blink_led_counter+=1        time.sleep(0.1)        if blink_led_counter < 5:            onboard_led.value(1)        elif blink_led_counter < 10:            onboard_led.value(0)        else:            blink_led_counter = 0    do_connect()