# Esp32 Brain

Code running on the main controller.

# Working Environment

<H4>Tools:</H4>

* Enveronment: ``PlatformIO``

* Framework: ``Arduino``

* Code editor: ``Visual Studio Code``

* MQTT Broker: ``shiftr.io``

<H4>Hardware:</H4>

* Mcu: ``ESP 32``


















<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>

# How to build and Flash

Setup Visual studio Code
--------------------------
The VS code setup Instructions are available in <a href="https://code.visualstudio.com/docs">Getting Started</a>.
* Setup Visual studio code according to the Host operating system. </a>
* Install <a href="https://docs.platformio.org/en/latest/integration/ide/vscode.html#ide-vscode">PlatformIo</a> extension inside VS code
* Make sure that ESP32 Dev Module is selected, and Framework as Arduino

Building and Flashing Application:
----------------------
To build the application, clone the `affbotics_software` repo and go to the folder `ESP32_AGV_brain`
* Pull the <a href="https://github.com/baseapp/affbotics_software">repository</a> locally
* Navigate to the directory ``affbotics_software/firmware/avg/ESP32_AGV_brain/``
* Select the COM port
* Click Upload

# Program Flow

Basic Workflow:
----------------

Firmware is present in ``affbotics_software/firmware/avg/ESP32_AGV_brain/src/main.cpp``
* Esp32 will connect to the wifi when booted 
* It will read the line sensor and Ultrasonic sensor calculate the values and sends commands to the motor via Modbus

![flowchart.png](https://docs.baseapp.com/uploads/images/drawio/2022-03/drawing-4-1646979290.png)

Am a Geek:
------------
* WiFi ssid and password can be changed by editing ``const char ssid[]`` and ``const char pass[]``
* MQTT broker can be changed by editing the line ``client.connect("esp32", "public", "public" ))`` and ``client.begin("public.cloud.shiftr.io", net);``
* PID values can be tweaked by changing values of variabled ``kp, ki, kd``