Inclusion of socket in main.py prevents further edits

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
greg_the_aussie
Posts: 8
Joined: Wed Jul 03, 2019 8:32 pm

Inclusion of socket in main.py prevents further edits

Post by greg_the_aussie » Wed Jul 03, 2019 8:53 pm

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

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

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

Post by jimmo » Wed Jul 03, 2019 10:29 pm

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

cyberlab
Posts: 56
Joined: Sat Apr 21, 2018 5:02 am

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

Post by cyberlab » Thu Jul 04, 2019 2:59 am

hi, if you are using windows 64 bits I recommend you use upyloader
https://github.com/BetaRavener/uPyLoade ... tag/v0.1.4

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

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

Post by Roberthh » Thu Jul 04, 2019 5:56 am

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.

greg_the_aussie
Posts: 8
Joined: Wed Jul 03, 2019 8:32 pm

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

Post by greg_the_aussie » Fri Jul 05, 2019 8:29 pm

Thanks for the links and guidance folks. Back on track again.

greg_the_aussie
Posts: 8
Joined: Wed Jul 03, 2019 8:32 pm

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

Post by greg_the_aussie » Sat Jul 06, 2019 1:12 pm

Thanks for the assistance folks. I have a micro python prompt again. Also directions to the couple of libraries are hugely appreciated.

Post Reply