Skip to content
Snippets Groups Projects
Commit fb922b4e authored by Václav Jelínek's avatar Václav Jelínek
Browse files

Add ESP wifi webserver menu program

parent 68731ce2
No related branches found
No related tags found
1 merge request!12Major update of menu visuals and example programs
Pipeline #106778 canceled
......@@ -42,6 +42,7 @@ def main():
DISPLAY_TITLE_POS = 18
DISPLAY_PROG_POS = 45
DISPLAY_ARROW_GAP = 10
ESP_MAX_RESET_FAILED = 5
menu_programs_functions = (('NXT Light', menu_programs.nxt_light_run), ('NXT Sound', menu_programs.nxt_sound_run),
('NXT Touch', menu_programs.nxt_touch_run), ('NXT Ultrasonic', menu_programs.nxt_ultra_run),
('OC Color', menu_programs.oc_color_run), ('OC AHRS', menu_programs.oc_gyro_run),
......@@ -68,10 +69,15 @@ def main():
robot.display.centered_text("cube is empty", DISPLAY_PROG_POS, 1)
# Fill framebuffer with bt menu
def display_fill_bt_menu(robot, esp_default_name):
def display_fill_bt_menu(robot, esp_default_name, esp_reset_failed_counter):
robot.display.fill(0)
robot.display.centered_text('< BT pair >', DISPLAY_TITLE_POS, 1)
robot.display.centered_text(esp_default_name, DISPLAY_PROG_POS, 1)
if esp_reset_failed_counter > ESP_MAX_RESET_FAILED:
robot.display.centered_text('ESP', DISPLAY_PROG_POS-12, 1)
robot.display.centered_text('not responding', DISPLAY_PROG_POS-4, 1)
robot.display.centered_text('restart cube', DISPLAY_PROG_POS+4, 1)
else:
robot.display.centered_text(esp_default_name, DISPLAY_PROG_POS, 1)
# Add bt connecting window to framebuffer
def display_fill_bt_pin(robot, pin, connecting):
......@@ -114,6 +120,8 @@ def main():
test_name = "Gyro Acc"
elif current_oth_test == 1:
test_name = "Utility"
elif current_oth_test == 2:
test_name = "Wifi webserver"
robot.display.centered_text(test_name, DISPLAY_PROG_POS, 1)
display_fill_arrows(robot)
......@@ -229,7 +237,7 @@ def main():
bt_pair = False
bt_connecting = False
button_values = robot.buttons.pressed()
esp_reset = False
esp_reset_failed_counter = 0
display_show_startup(robot)
# Get a list of user programs
......@@ -262,9 +270,12 @@ def main():
if not robot.esp.running() and counter % 10 == 0:
s = utime.ticks_ms()
robot.esp.reset(200)
if not robot.esp.reset(100):
esp_reset_failed_counter += 1
else:
esp_reset_failed_counter = 0
esp_reset_counter = counter
print("ESP32 reset time: ", utime.ticks_ms() - s)
esp_reset_counter = counter
if robot.esp.running() and counter - esp_reset_counter == 10:
robot.esp.set_name_nonblocking(esp_default_name, 100)
print("ESP BT name: ", esp_default_name)
......@@ -303,7 +314,7 @@ def main():
menu_debounce = 1
if current_menu == Menu.BT:
display_fill_bt_menu(robot, esp_default_name)
display_fill_bt_menu(robot, esp_default_name, esp_reset_failed_counter)
# Request BT pair pin every 10 cycles
if robot.esp.running() and counter % 10 == 0:
bt_pair, pin = send_esp_req(robot)
......@@ -375,6 +386,7 @@ def main():
elif menu_list[current_menu] == 2:
print("Wifi webserver test")
menu_programs.esp_wifi_run(robot, esp_default_name)
robot.esp.esp_running = False
robot.deinit_all()
robot_state = 0
menu_debounce = 1
......
'''
Example program for the Open-Cube ESP32 Wi-Fi mode with web server.
Start Wi-Fi acces point on the ESP32 with name 'Open-Cube-Wifi' and password '01234567'.
'''
import utime
import random
from lib.robot_consts import Button
def esp_wifi_run(robot, wifi_name):
wifi_password = "12345678"
counter = 0
indicators = [True, False, True, False, False]
labels = ["1", "test1", "test2", "", "123"]
btn_labels = ["", "up", "", "left", "ok", "right", "", "down", "" ]
run = True
robot.display.fill(0)
robot.display.centered_text(f"Wifi webserver", 0, 1)
robot.display.text(f"Name:{wifi_name}", 0, 16, 1)
robot.display.text(f"Pass:{wifi_password}", 0, 24, 1)
robot.display.text('<', 0, 54, 1)
robot.display.show()
time = utime.ticks_us()
wifi = robot.esp.wifi()
if wifi:
print("Wifi webserver start failed, restart cube")
robot.display.fill(0)
robot.display.centered_text(f"Wifi webserver", 16, 1)
robot.display.centered_text(f"start failed", 24, 1)
robot.display.centered_text(f"restart cube", 32, 1)
robot.display.show()
utime.sleep(2)
return
print("Name: ", robot.esp.set_name(wifi_name))
print("Pass: ", robot.esp.set_password(wifi_password))
robot.esp.wifi_set_indicators_labels(("1", "", "test2", "test3", "123"))
robot.esp.wifi_set_buttons_labels(btn_labels)
robot.esp.wifi_set_switches_labels(labels)
robot.esp.wifi_set_numbers_labels(("t:", "s:", "v:", "test1:", "", "test2:"))
while run:
utime.sleep(0.1)
if counter % 10 == 0:
robot.esp.wifi_set_indicators(indicators)
robot.esp.wifi_set_numbers((123456789, 0.123456, random.random(), 0.123456, random.random(), 5.2))
indicators = indicators[-1:]+indicators[:-1]
wifi_buttons = robot.esp.wifi_get_buttons()
wifi_switches = robot.esp.wifi_get_switches()
wifi_buttons_string = ''.join(['1' if value else '0' for value in wifi_buttons])
wifi_switches_string = ''.join(['1' if value else '0' for value in wifi_switches])
robot.display.fill(0)
robot.display.centered_text(f"Wifi webserver", 0, 1)
robot.display.text(f"Name:{wifi_name}", 0, 16, 1)
robot.display.text(f"Pass:{wifi_password}", 0, 24, 1)
robot.display.text(f"Button:{wifi_buttons_string}", 0, 32, 1)
robot.display.text(f"Switch:{wifi_switches_string}", 0, 40, 1)
robot.display.text('<', 0, 54, 1)
robot.display.show()
print("Buttons: ", wifi_buttons)
print("Switches: ", wifi_switches)
counter +=1
buttons = robot.buttons.pressed()
if buttons[Button.LEFT]:
run = False
\ No newline at end of file
......@@ -27,9 +27,9 @@ def motor_run(robot, chosen_motor_port):
print("Enc pos:", pos1,"Speed:", speed1)
robot.display.fill(0)
robot.display.centered_text(f"Motor M{chosen_motor_port+1}", 0, 1)
robot.display.text(f"Power: {pwr}", 0, 10, 1)
robot.display.text(f"Pos: {pos1}", 0, 18, 1)
robot.display.text(f"Speed: {speed1}", 0, 26, 1)
robot.display.text(f"Power: {pwr}", 0, 16, 1)
robot.display.text(f"Pos: {pos1}", 0, 24, 1)
robot.display.text(f"Speed: {speed1}", 0, 30, 1)
robot.display.text(f"power", 80, 44, 1)
robot.display.text('^', 95, 44 - 6, 1)
robot.display.text('v', 95, 44 + 10, 1)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment