-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
61 lines (52 loc) · 1.73 KB
/
Copy pathmain.py
File metadata and controls
61 lines (52 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import socket
from machine import Pin
import network
pin=Pin(16,Pin.OUT) #Configuring D0 as Output
pin1=Pin(5,Pin.OUT) #Configuring D1 as Output
#Setting pins to OFF state as pins of NodeMCU are active low
pin.on()
pin1.on()
mes1="fan turned on"
mes2="fan turned off"
mes3="light turned on"
mes4="light turned off"
mes5="invalid command"
#Function to connect NodeMCU to WiFi
def do_connect():
import network
sta_if = network.WLAN(network.STA_IF)
if not sta_if.isconnected():
print('connecting to network...')
sta_if.active(True)
sta_if.connect('Your_WiFi_Name', 'Your_WiFi_Password') #Replace your WiFi name and Password accordingly, here within single quotes for both
while not sta_if.isconnected():
pass
print('network config:', sta_if.ifconfig())
do_connect()
host="Put_Server_IP_address_here" #put your Server IP address in Double quotes
port=9999
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM) #Creating Client Socket
s.connect((host,port)) #Client connects to Server
while True:
data=str(s.recv(1024),"utf-8") #Receiving data from Server
#Actions performed based on received data, can be configured as per your choice
if "on fan" in data:
s.send(str.encode(mes1))
pin.off()
print(data)
elif "off fan" in data:
s.send(str.encode(mes2))
pin.on()
print(data)
elif "on light" in data:
s.send(str.encode(mes3))
pin1.off()
print(data)
elif "off light" in data:
s.send(str.encode(mes4))
pin1.on()
print(data)
else:
print("host entered invalid command")
s.send(str.encode(mes5))
continue