Page 1 of 1

Inclusion of socket in main.py prevents further edits

Posted: Wed Jul 03, 2019 8:53 pm
by greg_the_aussie
Hello

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()
  

Re: Inclusion of socket in main.py prevents further edits

Posted: Wed Jul 03, 2019 10:29 pm
by jimmo
Is webrepl configured, i.e. is that how you got main.py on there in the first place?

In picocom, you'll need to hit Ctrl-C to stop the program running so that you can get the prompt.

Instead of using sockets directly, if you plan to add more features you'll find it much easier to use a library to do the HTTp stuff for you. e.g. https://github.com/miguelgrinberg/microdot and https://blog.miguelgrinberg.com/post/mi ... -i-welcome

Also, sounds like your project might get a lot of value out of something even more high level e.g. https://github.com/peterhinch/micropython-iot

Re: Inclusion of socket in main.py prevents further edits

Posted: Thu Jul 04, 2019 2:59 am
by cyberlab
hi, if you are using windows 64 bits I recommend you use upyloader
https://github.com/BetaRavener/uPyLoade ... tag/v0.1.4

Re: Inclusion of socket in main.py prevents further edits

Posted: Thu Jul 04, 2019 5:56 am
by Roberthh
You do not get a prompt any more, because you start an infinite loop in main.py. The REPL prompt is shown when main,py finishes. You can try to cancel main,py when connected via picocom or webrepl by pushing Ctrl-C. At that point you can rename main.py with:

Code: Select all

import uos
uos.rename("main.py", "mymain.py")
Alternatively, you can erase the device completely and start over.

Re: Inclusion of socket in main.py prevents further edits

Posted: Fri Jul 05, 2019 8:29 pm
by greg_the_aussie
Thanks for the links and guidance folks. Back on track again.

Re: Inclusion of socket in main.py prevents further edits

Posted: Sat Jul 06, 2019 1:12 pm
by greg_the_aussie
Thanks for the assistance folks. I have a micro python prompt again. Also directions to the couple of libraries are hugely appreciated.