Advertisement
Stripf-elektronik

"Traffic Detected" Remote Unit

Mar 12th, 2023 (edited)
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.35 KB | Source Code | 0 0
  1. from machine import Pin
  2. import time
  3. import network
  4. import socket
  5. import gc
  6. import machine
  7.  
  8. #Enable garbage collector
  9. gc.enable()
  10.  
  11. #Host-Adress and Port of the Feeder (Port should be 30003)
  12. HOST='192.168.178.73'
  13. PORT=30003
  14.  
  15. #WLAN SSID and Password
  16. wlan_ssid     = "YOUR SSID"
  17. wlan_passwort = "YOUR WIFI PASSWORD"
  18.  
  19. #-------------------------------------------------
  20.  
  21. LED_Traffic = Pin(13, Pin.OUT)
  22. LED_Traffic.off()
  23. LED_WLAN = Pin(12, Pin.OUT)
  24. LED_Traffic.off()
  25. LED_WLAN.off()
  26.  
  27. WLAN_Error_Flag = False
  28.  
  29. #-------------------------------------------------
  30.  
  31. wlan = network.WLAN(network.STA_IF)
  32.  
  33. def wlan_connect():
  34.     while not wlan.isconnected():
  35.         print("Connecting to WIFI: " , wlan_ssid)
  36.         start = time.ticks_ms();
  37.         wlan.active(False)
  38.         wlan.active(True);
  39.         time.sleep(0.5)
  40.         wlan.connect(wlan_ssid,wlan_passwort)
  41.         while not wlan.isconnected() and start + 5000 > time.ticks_ms():
  42.             pass
  43.    
  44. wlan_connect()
  45.  
  46. if wlan.isconnected():
  47.     LED_WLAN.on()
  48.     print("Connected to WIFI:  " , wlan_ssid)
  49.     WLAN_Error_Flag = False
  50.  
  51. else:
  52.     LED_WLAN.off()
  53.     print("WLAN Connection to failed")
  54.     WLAN_Error_Flag = True
  55.     wlan_connect()
  56.  
  57. #-------------------------------------------------
  58.  
  59. if wlan.isconnected():
  60.     sock = socket.socket()
  61.     sock.settimeout(60)
  62.     sock.connect((HOST, PORT))
  63.  
  64. print("Starting to listen to Port", PORT, "on Host" , HOST)
  65. print("The blue LED is on in case of traffic is detected")
  66.  
  67. while 1:
  68.    
  69.     try:
  70.            
  71.         if not wlan.isconnected():
  72.             print("WLAN ERROR - Reconnect")
  73.             wlan = network.WLAN(network.STA_IF)
  74.             WLAN_Error_Flag = True
  75.             LED_WLAN.off()
  76.             LED_Traffic.off()
  77.             time.sleep(10)
  78.             wlan_connect()
  79.                
  80.         if not WLAN_Error_Flag:
  81.             Data = 0
  82.             Data = sock.recv(8500)
  83.  
  84.             if not Data:
  85.                 LED_Traffic.off()
  86.                 time.sleep(10)
  87.                 sock.close()
  88.                 sock = socket.socket()
  89.                 sock.settimeout(60)
  90.                 sock.connect((HOST, PORT))
  91.  
  92.         LED_Traffic.on()
  93.         time.sleep(0.5)
  94.         LED_Traffic.off()
  95.        
  96.         gc.collect()
  97.              
  98.     except OSError as error :
  99.         machine.reset()
  100.        
  101.  
  102.  
  103.  
  104.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement