Thoughts or suggestions?
Thanks and keep up the great work.
Board: ESP8266 Node MCU LoLin 1.0
Micropython Version 1.14
Connected via USB/Serial
Code: Select all
# http_get() from https://docs.micropython.org/en/latest/esp8266/tutorial/network_tcp.html
import time
from machine import Pin
import gc
def http_get(url):
byte_count = 0
import socket
_, _, host, path = url.split('/', 3)
print(' A ',end='') # Make it here?
addr = socket.getaddrinfo(host, 80)[0][-1]
print(' B ',end='') # Make it here?
s = socket.socket()
print(' C ',end='') # Make it here?
s.connect(addr)
print(' D ',end='') # Make it here?
s.send(bytes('GET /%s HTTP/1.0\r\nHost: %s\r\n\r\n' % (path, host), 'utf8'))
print(' E ',end='') # Make it here?
while True:
print(' (',gc.mem_free(),') ',end='') # free mem
print(' F ',end='') # Make it here?
data = s.recv(100) # Crashes here eventually <<<<<<<<<
print(' G ',end='') # Make it here? (Not if it crashes!)
if data:
byte_count = byte_count + len(str(data, 'utf8')) # count bytes read
print(' H ',end='') # Make it here?
else:
break
print(' I ',end='') # Make it here?
s.close()
print(' J ',end='') # Make it here?
print(' Bytes:',byte_count)
gc.collect() # do a garbage collection to clean thing up just for kicks...
# Set up LED activity indicator
led=Pin(2,Pin.OUT) # This will turn the on-board LED on as it is active LOW
led_stat = 0
led.value(0)
exit_button = Pin(0, Pin.IN) ## GPIO0 attached to 'Flash' button
counter = 0 # Count cycles
while exit_button.value() != 0 : #Until Crash or 'Flash' button is pressed and held
counter += 1 # count cycles
print(counter,'> ',end = '')
try:
http_get('http://www.www.com/') # website that doesn't return much, but is real
except Exception as e:
print('Error ',e)
pass
# Flash LED to show still alive
led_stat = led_stat ^ 1
led.value(led_stat)
time.sleep(5) # wait 5 seconds before trying again