Advertisement
Guest User

Untitled

a guest
Aug 17th, 2022
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.94 KB | Source Code | 0 0
  1.  
  2.  
  3. #from asyncio import exceptions
  4. import random
  5. import _thread
  6. from time import sleep
  7.  
  8. # ************************
  9. # Configure the ESP32 wifi
  10. # as STAtion mode.
  11. import network
  12. #import wifi_credentials
  13.  
  14. sta = network.WLAN(network.STA_IF)
  15. if not sta.isconnected():
  16.   print('connecting to network...')
  17.   sta.active(True)
  18.   sta.connect('your wifi ssid', 'your wifi password')
  19.   while not sta.isconnected():
  20.     pass
  21.  
  22. print('network config:', sta.ifconfig(('xxx.xxx.xxx.xxx','255.255.255.0','xxx.xxx.xxx.xxx','8.8.8.8')))#ip, Subnet Mask, gateway,...
  23.  
  24. # ************************
  25. # Configure the socket connection
  26. # over TCP/IP
  27. import socket
  28. # AF_INET - use Internet Protocol v4 addresses
  29. # SOCK_STREAM means that it is a TCP socket.
  30. # SOCK_DGRAM means that it is a UDP socket.
  31. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  32. s.bind(('',9000)) # specifies that the socket is reachable by any address the machine happens to have
  33. s.listen(5)     # max of 5 socket connections
  34.  
  35.  
  36. import machine
  37. import time
  38. led = machine.Pin(2,machine.Pin.OUT)
  39. pwr = machine.Pin(5,machine.Pin.IN, machine.Pin.PULL_UP)
  40. # = machine.Pin(12,machine.Pin.IN, machine.Pin.PULL_UP)
  41. #led.off()
  42.  
  43. state = 0
  44. h = 0
  45.  
  46. # global variable x
  47.  
  48. x = 0
  49.  
  50. pcInfo = ""#"0|0|0|0|0|0|0|0"
  51. ThreadCount = 0
  52.  
  53. s.listen(5)
  54. import gc
  55.  
  56. def multi_threaded_TcpClient(connection,pcInfo):
  57.     connection.send(str.encode('Server is working:'))
  58.    
  59.     try:
  60.         global pcInfos
  61.         while True:
  62.             data = connection.recv(2048)
  63.            
  64.             response = 'Server message: ' + data.decode('utf-8')
  65.             #if str(data).find("|") == False:
  66.                
  67.             if not data:
  68.                 break
  69.             pcInfos = str(data)[15:-1]
  70.             print(pcInfos)
  71.             pcInfo = pcInfos
  72.             connection.sendall(str.encode(response))
  73.            
  74.         connection.close()
  75.     except Exception as e:
  76.             connection.close()
  77.             print("C_tcp Error: ",e)
  78.             gc.collect()
  79.  
  80. def multi_threaded_TcpRevclient(connection,state,pcInfo):
  81.     #connection.send(str.encode('Server is working:'))
  82.     try:
  83.         while True:
  84.             data = connection.recv(2048)
  85.             data = str(data)
  86.             #response = 'Server message: ' + data
  87.             update = data.find('/powerUp')
  88.             print(update)
  89.             if update == 6 and state == 0:
  90.                 led.on()
  91.                 sleep(0.4)
  92.                 led.off()
  93.                 state = 1
  94.                 print("state ", state)
  95.             elif update == 6 and state == 1:
  96.                 led.off()
  97.                 sleep(0.4)
  98.                 led.on()
  99.                 state = 0
  100.                 print("state ", state)
  101.             else:
  102.                 #pass
  103.                 print("pcInfos",pcInfos)
  104.                 pot_value = 4096 #supposed to use analog pin 34 to check if computer is power
  105.                 print((pot_value * 3.3) / 4095)
  106.                 status_value = (pot_value * 3.3) / 4095
  107.                 #sleep(0.1)
  108.                 connection.send(str.encode("OK: " + pcInfos + "|" + str(status_value)))
  109.             #if not data:
  110.             break
  111.             #connection.sendall(str.encode(response))
  112.            
  113.         connection.close()
  114.     except Exception as e:
  115.             connection.close()
  116.             print("C_revTcp Error: ",e)
  117.             gc.collect()
  118.    
  119. while True:
  120.     Client, address = s.accept()
  121.     #Client.close()
  122.     print('Connected to: ' + address[0] + ':' + str(address[1]))
  123.     if 'xxx.xxx.xxx.xxx' in address: #checks if client request comes from the my computer
  124.         #sleep(0.01)
  125.         _thread.start_new_thread(multi_threaded_TcpClient, (Client,pcInfo ))
  126.     else:
  127.         #_thread.start_new_thread(multi_threaded_client, (Client, ))
  128.         _thread.start_new_thread(multi_threaded_TcpRevclient, (Client,state,pcInfo ))
  129.     ThreadCount += 1
  130.     print('Thread Number: ' + str(ThreadCount))
  131. s.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement