- install python wireless module
sudo pip install wireless
| #!/usr/bin/python | |
| import pifacecad | |
| import socket | |
| import time | |
| from wireless import Wireless | |
| SLEEP_TIME=5 | |
| def main(): | |
| cad = pifacecad.PiFaceCAD() | |
| network_detected = False | |
| while True: | |
| try: | |
| try: | |
| hostname = socket.gethostname() | |
| ip_addr = socket.gethostbyname(hostname) | |
| if not network_detected: | |
| cad.lcd.write("Got network!") | |
| time.sleep(SLEEP_TIME) | |
| cad.lcd.clear() | |
| network_detected = True | |
| except socket.gaierror: | |
| cad.lcd.write("No network, waiting") | |
| time.sleep(SLEEP_TIME) | |
| continue | |
| cad.lcd.cursor_off() | |
| show_hostname(cad, hostname) | |
| show_ip(cad, ip_addr) | |
| show_network(cad) | |
| except KeyboardInterrupt: | |
| cad.lcd.clear() | |
| cad.lcd.write("Bye") | |
| time.sleep(SLEEP_TIME) | |
| cad.lcd.clear() | |
| break | |
| def show_hostname(cad, hostname): | |
| cad.lcd.home() | |
| cad.lcd.write("Hostname:") | |
| cad.lcd.set_cursor(0,1) | |
| cad.lcd.write(hostname) | |
| time.sleep(SLEEP_TIME) | |
| cad.lcd.clear() | |
| def show_ip(cad, ip_addr): | |
| cad.lcd.home() | |
| cad.lcd.write("IP Address:") | |
| cad.lcd.set_cursor(0,1) | |
| cad.lcd.write(ip_addr) | |
| time.sleep(SLEEP_TIME) | |
| cad.lcd.clear() | |
| def show_network(cad): | |
| wl = Wireless() | |
| essid = wl.current().split()[0] | |
| cad.lcd.home() | |
| cad.lcd.write("ESSID:") | |
| cad.lcd.set_cursor(0,1) | |
| cad.lcd.write(essid) | |
| time.sleep(SLEEP_TIME) | |
| cad.lcd.clear() | |
| if __name__ == "__main__": | |
| main() |