# Documentation for ReRo 24hr Hackathon ## Introduction Welcome to the ReRo 24hr Hackathon! In this competition, you will be controlling a bot remotely by uploading your Python script to the competition website. The goal of the bot is to follow a line from the start position to the end. ## Input and Output Formats The input to your script will be a JSON object in the format {'s1':0, 's2':0, 's3':0, 's4':1, 's5':0}. This represents the sensor array from left to right, where 0 means no line detected and 1 means a line is detected. The output from your script should be in the format `'motor "f" "50" "b" "50"\n'`. Here, f means the motor runs forward and b means the motor runs backward, and the first f 50 is data for the left motor and b 50 is for the right motor. ## Connecting to the ESP32 Board You will need to connect to the ESP32 board using Python sockets. The IP address and the port will be provided during runtime as arguments to the main() function. ## Code Structure Your code should be placed inside a function called main(ip_address, port_no). This function will be called to control the bot. ## Exception Handling Please ensure that your code handles exceptions appropriately to avoid any runtime errors or crashes. ## Printing logs You can use print statements in your code and the logs will be delivered to you on the website. ## Conclusion We wish you the best of luck in the competition! Remember, the goal is not just to win, but to learn and have fun in the process. Happy coding! ## References ESP32 https://randomnerdtutorials.com/getting-started-with-esp32/ Arduino Programming https://www.tutorialspoint.com/arduino/ Python https://www.w3schools.com/python/ Python Sockets https://realpython.com/python-sockets/ Python JSON https://www.w3schools.com/python/python_json.asp ![image.png](https://hackmd.io/_uploads/r1h6_zMma.png) ![image.png](https://hackmd.io/_uploads/Sk01dGzQp.png) ## Sample Code ```python import typing def get_motor_values(sensor_data) -> typing.Tuple[int, int]: """ Pass sensor data from main() to get_motor_values. Sensor_data sample: {'s1': 1, 's2': 1, 's3': 1, 's4': 1, 's5': 1} """ print(f"Got sensor Data: {sensor_data}") return 10, 10 # sample motor left and motor right PWM values. def main(ip_addr, port) -> None: pass ``` points to remember: - Make sure to close the socket connection before your program ends. - Even after catching errors, the socket connection needs to be closed. - Use the "finally" block in python after the try/except to make sure the connection closes. - The protocol works in the way that your code needs to read the sensor data first and then send the response. This can be put in loop to run continuously or a certain number of iterations for testing. ## Testing your code locally Since you cannot test your code on your own with a real robot, you can simulate the TCP server and check your connection status. here is the sample dummpy_server.py ```python import socket import json # Define the server address and port HOST = '127.0.0.1' # Loopback address PORT = 12345 # You can choose any available port # Create a socket object server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Bind the socket to the server address and port server_socket.bind((HOST, PORT)) # Listen for incoming connections server_socket.listen(1) # Allow one connection at a time print(f"Server listening on {HOST}:{PORT}") while True: # Accept a connection from a client client_socket, client_address = server_socket.accept() print(f"Accepted connection from {client_address}") try: # Define a sample JSON data json_data = {'s1': 1, 's2': 1, 's3': 1, 's4': 1, 's5': 1} # Serialize the JSON data to a string json_string = json.dumps(json_data) # Send the JSON data to the client client_socket.sendall(json_string.encode('utf-8')) while True: # Receive data from the client received_data = client_socket.recv(1024) if not received_data: break # Connection closed # Deserialize the received JSON data print("--- Got data.") # Send the received JSON data back to the client client_socket.sendall(json.dumps(json_data).encode('utf-8')) except Exception as e: print(f"An error occurred: {e}") finally: # Close the client socket client_socket.close() print(f"Connection with {client_address} closed") ```