Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from machine import Pin
- import time
- import network
- import socket
- import gc
- import machine
- #Enable garbage collector
- gc.enable()
- #Host-Adress and Port of the Feeder (Port should be 30003)
- HOST='192.168.178.73'
- PORT=30003
- #WLAN SSID and Password
- wlan_ssid = "YOUR SSID"
- wlan_passwort = "YOUR WIFI PASSWORD"
- #-------------------------------------------------
- LED_Traffic = Pin(13, Pin.OUT)
- LED_Traffic.off()
- LED_WLAN = Pin(12, Pin.OUT)
- LED_Traffic.off()
- LED_WLAN.off()
- WLAN_Error_Flag = False
- #-------------------------------------------------
- wlan = network.WLAN(network.STA_IF)
- def wlan_connect():
- while not wlan.isconnected():
- print("Connecting to WIFI: " , wlan_ssid)
- start = time.ticks_ms();
- wlan.active(False)
- wlan.active(True);
- time.sleep(0.5)
- wlan.connect(wlan_ssid,wlan_passwort)
- while not wlan.isconnected() and start + 5000 > time.ticks_ms():
- pass
- wlan_connect()
- if wlan.isconnected():
- LED_WLAN.on()
- print("Connected to WIFI: " , wlan_ssid)
- WLAN_Error_Flag = False
- else:
- LED_WLAN.off()
- print("WLAN Connection to failed")
- WLAN_Error_Flag = True
- wlan_connect()
- #-------------------------------------------------
- if wlan.isconnected():
- sock = socket.socket()
- sock.settimeout(60)
- sock.connect((HOST, PORT))
- print("Starting to listen to Port", PORT, "on Host" , HOST)
- print("The blue LED is on in case of traffic is detected")
- while 1:
- try:
- if not wlan.isconnected():
- print("WLAN ERROR - Reconnect")
- wlan = network.WLAN(network.STA_IF)
- WLAN_Error_Flag = True
- LED_WLAN.off()
- LED_Traffic.off()
- time.sleep(10)
- wlan_connect()
- if not WLAN_Error_Flag:
- Data = 0
- Data = sock.recv(8500)
- if not Data:
- LED_Traffic.off()
- time.sleep(10)
- sock.close()
- sock = socket.socket()
- sock.settimeout(60)
- sock.connect((HOST, PORT))
- LED_Traffic.on()
- time.sleep(0.5)
- LED_Traffic.off()
- gc.collect()
- except OSError as error :
- machine.reset()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement