Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #from asyncio import exceptions
- import random
- import _thread
- from time import sleep
- # ************************
- # Configure the ESP32 wifi
- # as STAtion mode.
- import network
- #import wifi_credentials
- sta = network.WLAN(network.STA_IF)
- if not sta.isconnected():
- print('connecting to network...')
- sta.active(True)
- sta.connect('your wifi ssid', 'your wifi password')
- while not sta.isconnected():
- pass
- 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,...
- # ************************
- # Configure the socket connection
- # over TCP/IP
- import socket
- # AF_INET - use Internet Protocol v4 addresses
- # SOCK_STREAM means that it is a TCP socket.
- # SOCK_DGRAM means that it is a UDP socket.
- s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- s.bind(('',9000)) # specifies that the socket is reachable by any address the machine happens to have
- s.listen(5) # max of 5 socket connections
- import machine
- import time
- led = machine.Pin(2,machine.Pin.OUT)
- pwr = machine.Pin(5,machine.Pin.IN, machine.Pin.PULL_UP)
- # = machine.Pin(12,machine.Pin.IN, machine.Pin.PULL_UP)
- #led.off()
- state = 0
- h = 0
- # global variable x
- x = 0
- pcInfo = ""#"0|0|0|0|0|0|0|0"
- ThreadCount = 0
- s.listen(5)
- import gc
- def multi_threaded_TcpClient(connection,pcInfo):
- connection.send(str.encode('Server is working:'))
- try:
- global pcInfos
- while True:
- data = connection.recv(2048)
- response = 'Server message: ' + data.decode('utf-8')
- #if str(data).find("|") == False:
- if not data:
- break
- pcInfos = str(data)[15:-1]
- print(pcInfos)
- pcInfo = pcInfos
- connection.sendall(str.encode(response))
- connection.close()
- except Exception as e:
- connection.close()
- print("C_tcp Error: ",e)
- gc.collect()
- def multi_threaded_TcpRevclient(connection,state,pcInfo):
- #connection.send(str.encode('Server is working:'))
- try:
- while True:
- data = connection.recv(2048)
- data = str(data)
- #response = 'Server message: ' + data
- update = data.find('/powerUp')
- print(update)
- if update == 6 and state == 0:
- led.on()
- sleep(0.4)
- led.off()
- state = 1
- print("state ", state)
- elif update == 6 and state == 1:
- led.off()
- sleep(0.4)
- led.on()
- state = 0
- print("state ", state)
- else:
- #pass
- print("pcInfos",pcInfos)
- pot_value = 4096 #supposed to use analog pin 34 to check if computer is power
- print((pot_value * 3.3) / 4095)
- status_value = (pot_value * 3.3) / 4095
- #sleep(0.1)
- connection.send(str.encode("OK: " + pcInfos + "|" + str(status_value)))
- #if not data:
- break
- #connection.sendall(str.encode(response))
- connection.close()
- except Exception as e:
- connection.close()
- print("C_revTcp Error: ",e)
- gc.collect()
- while True:
- Client, address = s.accept()
- #Client.close()
- print('Connected to: ' + address[0] + ':' + str(address[1]))
- if 'xxx.xxx.xxx.xxx' in address: #checks if client request comes from the my computer
- #sleep(0.01)
- _thread.start_new_thread(multi_threaded_TcpClient, (Client,pcInfo ))
- else:
- #_thread.start_new_thread(multi_threaded_client, (Client, ))
- _thread.start_new_thread(multi_threaded_TcpRevclient, (Client,state,pcInfo ))
- ThreadCount += 1
- print('Thread Number: ' + str(ThreadCount))
- s.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement