简体   繁体   中英

i2c LCD connected to the internet

i am trying to display internet connection if it is connected or not in the i2c lcd but when i try to run the code it will not show/display anything. Tried other codes to see if my lcd is working but it does.

import I2C_LCD_driver
import urllib.request
mylcd = I2C_LCD_driver.lcd()
def connect(host='http://google.com'):
    try:
        urllib.request.urlopen(host).read() #Python 3.x
        return True
    except:
        return False
# test
    mylcd.lcd_display_string ('connected' if connect() else 'no internet!')

It is better to place the function call outside the function body and if statement not as an argument. Check this:

import I2C_LCD_driver
import urllib.request
mylcd = I2C_LCD_driver.lcd()
def connect(host='http://google.com'):
    try:
        urllib.request.urlopen(host).read() #Python 3.x
        return True
    except:
        return False
# test
if connect() :
    mylcd.lcd_display_string ('connected' )
else :
    mylcd.lcd_display_string ('no internet!')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM