I found some code to establish a socket and let me switch the led on and off over wifi. (code at the end). Works correctly. But now I cannot edit the main.py file. Using webREPL I get an immediate Disconnecting message when I click connect. Screen, picocom will not return a python prompt no matter how many times I hit enter.
I suspect the conn.close() at the end of the script is to blame, but I'm very new to both the micropython and nodemcu I'm using so happy to be corrected. I think that before I can interact with my board the connection is closed.
My problem is that I would like to edit main.py as I want to use this socket feature to connect to my local server and mysql database and interact with many boards around my home.
So please, if you have a way for me to edit or replace main.py point me to some instructions or tell me what to do. Happy to reflash if necessary, but as a last resort.
And thank you in advance for even reading this far (hi mum).
main.py:
Code: Select all
# Complete project details at https://RandomNerdTutorials.com
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 80))
s.listen(5)
while True:
conn, addr = s.accept()
print('Got a connection from %s' % str(addr))
request = conn.recv(1024)
request = str(request)
print('Content = %s' % request)
led_on = request.find('/?led=on')
led_off = request.find('/?led=off')
if led_on == 6:
print('LED ON')
led.value(1)
if led_off == 6:
print('LED OFF')
led.value(0)
conn.send('HTTP/1.1 200 OK\n')
conn.send('Content-Type: text/html\n')
conn.send('Connection: close\n\n')
conn.sendall(response)
conn.close()