Socket Server stopped running from boot.py

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

Socket Server stopped running from boot.py

Post by greg_the_aussie » Sat Jan 15, 2022 10:30 pm

Hello folks
My project involves a board connected to an ultrasonic distance measurement device sitting on top of my water tank and taking a daily reading of the water level and writing the number to water_level.txt. The board also provided a socket server with an infinite loop that sends water_level.txt to a Kivy app with the help of json.

The boot.py file used to contain hard coded configuration information - wifi username and password and so on, and the last line is import server to run the socket server. Main.py looks after the water level measurement and logging.

When the Kivy app runs it sends a socket request to the board and server.py sends back the water_level.txt file. Kivy then graphs the data into a line chart so I can see the water level for the previous 100 days.

Last night I 'improved' my project by taking the configuration information and putting it into a config.txt file and referring to it from boot.py. The idea is to be able to update wifi information remotely.

But now Kivy gets "ConnectionRefusedError: [Errno 111] Connection refused". But when I use the webrepl to interrupt the continuous loop and import server there, Kivy then receives the data and loads the graph like it should. It seems pretty clear to me that boot.py is no longer starting server.py like it used to and I don't understand why.

I'll be really appreciative of any suggestions what I have done wrong in my boot.py, server.py, main.py and config.txt files that might be the cause. I suspect the problem is in boot.py and config.txt, but I don't have the knowledge to identify what is wrong.

boot.py

Code: Select all

import network
import webrepl
import gc
import json

webrepl.start()
gc.collect()

import json
c = open('config.txt')
config = c.read()
d = json.loads(config)
c.close()
ESSID = d.get(APSSID)
EPASSWORD = d.get(APPassword)
SSID = d.get(SSSID)
PASSWORD = d.get(SPassword)
APACTIVE = d.get(ap_if.active)
STACTIVE = d.get(sta_if.active)

sta_if = network.WLAN(network.STA_IF)
ap_if = network.WLAN(network.AP_IF)
ap_if.active(APACTIVE)
ap_if.config(essid=ESSID, password=EPASSWORD)


if not sta_if.isconnected():
    print('connecting to network...')
    sta_if.active(STACTIVE)
    sta_if.connect(SSID,PASSWORD)
    while not sta_if.isconnected():
        pass
print('network config:', sta_if.ifconfig())

import server
server.py

Code: Select all

import socket,json,gc

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
"""s.bind((socket.gethostname(), 4321))"""
s.bind('192.168.11.253', 4321)
s.listen(2)

while True:
	clientsocket, address  = s.accept()
	print("Connection with client established.")
	f = open('water_level.txt')
	depths = json.dumps(f.read())
	clientsocket.send(bytes((depths),'utf-8'))

gc.collect()
s.close()
main.py

Code: Select all

import machine

#Nominate a trigger pin on ESP8266 and declare it as an output pin
trigger = machine.Pin((14), machine.Pin.OUT)

#Nominate an echo pin and declare it as an input pin
echo = machine.Pin((12), machine.Pin.IN)

#Create a continuous loop to check water level once every 24 hours
while True:

	#Set and hold the trigger low.
	import utime
	trigger.value(1)
	utime.sleep(86400)
	#Set the trigger high for 15 microseconds
	trigger.value(0)
	utime.sleep_us(15)
	trigger.value(1)

	#Set a timer to monitor the echo pin for a couple of seconds
	pulse = machine.time_pulse_us(echo,1)

	#Return the duration of the echo pulse
	print ('Echo duration:', pulse)

	#Calculate the distance to the surface based on the speed of sound.
	distance = (pulse/1000000)*34000/2
	print('Distance:', distance,'cm')

	#Write the distance to the surface to a file
	water_level = open("water_level.txt","a")
	print(distance, file = water_level)
	water_level.close()
config.txt
{"APSSID":"WaterTank","APPassword":"*********","SSSID":"Knight","SPassword":"*********","sta_if.active":"False","ap_if.active":"True","depth of the water tank":"100"}

davef
Posts: 811
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: Socket Server stopped running from boot.py

Post by davef » Sat Jan 15, 2022 10:39 pm

Maybe, try blinking an LED after:

Code: Select all

print('network config:', sta_if.ifconfig())
I don't use webrepl anymore but maybe you could run that so that you can see your print statements

Post Reply