├── FreeRTOS ├── .gitignore ├── .vscode │ ├── extensions.json │ └── settings.json ├── test │ └── README ├── platformio.ini ├── lib │ └── README ├── include │ ├── README │ └── Webserver.h └── src │ └── main.cpp ├── .vscode ├── settings.json └── c_cpp_properties.json ├── LICENSE └── README.md /FreeRTOS/.gitignore: -------------------------------------------------------------------------------- 1 | .pio 2 | .vscode/.browse.c_cpp.db* 3 | .vscode/c_cpp_properties.json 4 | .vscode/launch.json 5 | .vscode/ipch 6 | -------------------------------------------------------------------------------- /FreeRTOS/.vscode/extensions.json: -------------------------------------------------------------------------------- 1 | { 2 | // See http://go.microsoft.com/fwlink/?LinkId=827846 3 | // for the documentation about the extensions.json format 4 | "recommendations": [ 5 | "platformio.platformio-ide" 6 | ], 7 | "unwantedRecommendations": [ 8 | "ms-vscode.cpptools-extension-pack" 9 | ] 10 | } 11 | -------------------------------------------------------------------------------- /FreeRTOS/test/README: -------------------------------------------------------------------------------- 1 | 2 | This directory is intended for PlatformIO Test Runner and project tests. 3 | 4 | Unit Testing is a software testing method by which individual units of 5 | source code, sets of one or more MCU program modules together with associated 6 | control data, usage procedures, and operating procedures, are tested to 7 | determine whether they are fit for use. Unit testing finds problems early 8 | in the development cycle. 9 | 10 | More information about PlatformIO Unit Testing: 11 | - https://docs.platformio.org/en/latest/advanced/unit-testing/index.html 12 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "python.autoComplete.extraPaths": [ 3 | "/home/ibrahim/install/micro_ros_msgs/local/lib/python3.10/dist-packages", 4 | "/home/ibrahim/ros2_ws/install/py_pkg/lib/python3.10/site-packages", 5 | "/opt/ros/humble/lib/python3.10/site-packages", 6 | "/opt/ros/humble/local/lib/python3.10/dist-packages" 7 | ], 8 | "python.analysis.extraPaths": [ 9 | "/home/ibrahim/install/micro_ros_msgs/local/lib/python3.10/dist-packages", 10 | "/home/ibrahim/ros2_ws/install/py_pkg/lib/python3.10/site-packages", 11 | "/opt/ros/humble/lib/python3.10/site-packages", 12 | "/opt/ros/humble/local/lib/python3.10/dist-packages" 13 | ] 14 | } -------------------------------------------------------------------------------- /FreeRTOS/.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "python.autoComplete.extraPaths": [ 3 | "/home/ibrahim/install/micro_ros_msgs/local/lib/python3.10/dist-packages", 4 | "/home/ibrahim/ros2_ws/install/py_pkg/lib/python3.10/site-packages", 5 | "/opt/ros/humble/lib/python3.10/site-packages", 6 | "/opt/ros/humble/local/lib/python3.10/dist-packages" 7 | ], 8 | "python.analysis.extraPaths": [ 9 | "/home/ibrahim/install/micro_ros_msgs/local/lib/python3.10/dist-packages", 10 | "/home/ibrahim/ros2_ws/install/py_pkg/lib/python3.10/site-packages", 11 | "/opt/ros/humble/lib/python3.10/site-packages", 12 | "/opt/ros/humble/local/lib/python3.10/dist-packages" 13 | ] 14 | } -------------------------------------------------------------------------------- /FreeRTOS/platformio.ini: -------------------------------------------------------------------------------- 1 | ; PlatformIO Project Configuration File 2 | ; 3 | ; Build options: build flags, source filter 4 | ; Upload options: custom upload port, speed and extra flags 5 | ; Library options: dependencies, extra library storages 6 | ; Advanced options: extra scripting 7 | ; 8 | ; Please visit documentation for the other options and examples 9 | ; https://docs.platformio.org/page/projectconf.html 10 | 11 | [env:esp32doit-devkit-v1] 12 | platform = espressif32 13 | board = esp32doit-devkit-v1 14 | framework = arduino 15 | monitor_speed = 115200 16 | monitor_rts = 0 17 | monitor_dtr = 0 18 | monitor_port = /dev/ttyUSB0 19 | lib_deps = 20 | ottowinter/ESPAsyncWebServer-esphome@^3.1.0 21 | adafruit/DHT sensor library@^1.4.6 22 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 Ibrahim Bin Mansur 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /FreeRTOS/lib/README: -------------------------------------------------------------------------------- 1 | 2 | This directory is intended for project specific (private) libraries. 3 | PlatformIO will compile them to static libraries and link into executable file. 4 | 5 | The source code of each library should be placed in a an own separate directory 6 | ("lib/your_library_name/[here are source files]"). 7 | 8 | For example, see a structure of the following two libraries `Foo` and `Bar`: 9 | 10 | |--lib 11 | | | 12 | | |--Bar 13 | | | |--docs 14 | | | |--examples 15 | | | |--src 16 | | | |- Bar.c 17 | | | |- Bar.h 18 | | | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html 19 | | | 20 | | |--Foo 21 | | | |- Foo.c 22 | | | |- Foo.h 23 | | | 24 | | |- README --> THIS FILE 25 | | 26 | |- platformio.ini 27 | |--src 28 | |- main.c 29 | 30 | and a contents of `src/main.c`: 31 | ``` 32 | #include 33 | #include 34 | 35 | int main (void) 36 | { 37 | ... 38 | } 39 | 40 | ``` 41 | 42 | PlatformIO Library Dependency Finder will find automatically dependent 43 | libraries scanning project source files. 44 | 45 | More information about PlatformIO Library Dependency Finder 46 | - https://docs.platformio.org/page/librarymanager/ldf.html 47 | -------------------------------------------------------------------------------- /.vscode/c_cpp_properties.json: -------------------------------------------------------------------------------- 1 | { 2 | "configurations": [ 3 | { 4 | "browse": { 5 | "databaseFilename": "${default}", 6 | "limitSymbolsToIncludedHeaders": false 7 | }, 8 | "includePath": [ 9 | "/home/ibrahim/install/micro_ros_agent/include/**", 10 | "/home/ibrahim/install/micro_ros_msgs/include/**", 11 | "/opt/ros/humble/include/**", 12 | "/home/ibrahim/mros_ws/src/uros/micro-ROS-Agent/micro_ros_agent/include/**", 13 | "/home/ibrahim/robotisim_ws/src/drive_turtlesim/include/**", 14 | "/home/ibrahim/ros2_ws/src/cpp_pkg/include/**", 15 | "/home/ibrahim/ros_packages/basic/include/**", 16 | "/home/ibrahim/ros_packages/jani_bot/include/**", 17 | "/home/ibrahim/running_ws/src/mobile_robotics_ROS2/turtlebot3_simulations/turtlebot3_gazebo/include/**", 18 | "/home/ibrahim/src/uros/micro-ROS-Agent/micro_ros_agent/include/**", 19 | "/usr/include/**", 20 | "${workspaceFolder}/FreeRTOS/include" 21 | ], 22 | "name": "ROS", 23 | "intelliSenseMode": "gcc-x64", 24 | "compilerPath": "/usr/bin/gcc", 25 | "cStandard": "gnu11", 26 | "cppStandard": "c++14" 27 | } 28 | ], 29 | "version": 4 30 | } -------------------------------------------------------------------------------- /FreeRTOS/include/README: -------------------------------------------------------------------------------- 1 | 2 | This directory is intended for project header files. 3 | 4 | A header file is a file containing C declarations and macro definitions 5 | to be shared between several project source files. You request the use of a 6 | header file in your project source file (C, C++, etc) located in `src` folder 7 | by including it, with the C preprocessing directive `#include'. 8 | 9 | ```src/main.c 10 | 11 | #include "header.h" 12 | 13 | int main (void) 14 | { 15 | ... 16 | } 17 | ``` 18 | 19 | Including a header file produces the same results as copying the header file 20 | into each source file that needs it. Such copying would be time-consuming 21 | and error-prone. With a header file, the related declarations appear 22 | in only one place. If they need to be changed, they can be changed in one 23 | place, and programs that include the header file will automatically use the 24 | new version when next recompiled. The header file eliminates the labor of 25 | finding and changing all the copies as well as the risk that a failure to 26 | find one copy will result in inconsistencies within a program. 27 | 28 | In C, the usual convention is to give header files names that end with `.h'. 29 | It is most portable to use only letters, digits, dashes, and underscores in 30 | header file names, and at most one dot. 31 | 32 | Read more about using header files in official GCC documentation: 33 | 34 | * Include Syntax 35 | * Include Operation 36 | * Once-Only Headers 37 | * Computed Includes 38 | 39 | https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html 40 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ESP32 Web Server with FreeRTOS 2 | 3 | This project implements a web server on the ESP32 platform, utilizing FreeRTOS for efficient task management. The server is designed to handle tasks such as reading sensor data, updating a display, and responding to emergency situations. 4 | 5 | ## Table of Contents 6 | 7 | - [Prerequisites](#prerequisites) 8 | - [Installation](#installation) 9 | - [Features](#features) 10 | - [Code Structure](#code-structure) 11 | - [main.cpp](#maincpp) 12 | - [Webserver.h](#Webserverh) 13 | - [Usage](#usage) 14 | - [Testing](#testing) 15 | - [Live Demo](#live-demo) 16 | - [Contributing](#contributing) 17 | - [License](#license) 18 | - [Acknowledgements](#acknowledgements) 19 | 20 | ## Prerequisites 21 | 22 | Before you begin, ensure you have met the following requirements: 23 | 24 | - You have a ESP32 board. 25 | - You have installed the Arduino IDE or PlatformIO. 26 | - You have a stable internet connection. 27 | 28 | ## Installation 29 | 30 | 1. Clone the repository to your local machine. 31 | 2. Open the project in your preferred IDE. 32 | 3. Update the WiFi SSID and password in the `main.cpp` file to match your network. 33 | 4. Upload the code to your ESP32 board. 34 | 35 | ## Features 36 | 37 | - Web server running on ESP32 38 | - Efficient task management with FreeRTOS 39 | - Sensor data reading 40 | - Display updating 41 | - Emergency situation handling 42 | 43 | ## Code Structure 44 | 45 | ### main.cpp 46 | 47 | The `main.cpp` file is the main code file, containing setup and loop functions, as well as the definitions of FreeRTOS tasks. 48 | 49 | Tasks defined in `main.cpp`: 50 | 51 | 1. **`sensorTask`**: Reads data from sensors at regular intervals, updating global variables with the latest sensor readings. 52 | 53 | 2. **`displayTask`**: Updates the display with the latest sensor readings whenever there is a change in the sensor data. 54 | 55 | 3. **`emergencyTask`**: Checks for emergency situations, such as sensor readings exceeding a threshold. Takes appropriate action in case of an emergency. 56 | 57 | ```cpp 58 | 59 | #include 60 | 61 | // Global variables for sensor data 62 | int temperature = 0; 63 | int humidity = 0; 64 | 65 | void setup() { 66 | // Setup code goes here 67 | 68 | // Create tasks 69 | xTaskCreate(sensorTask, "Sensor Task", 1000, NULL, 1, NULL); 70 | xTaskCreate(displayTask, "Display Task", 1000, NULL, 1, NULL); 71 | xTaskCreate(emergencyTask, "Emergency Task", 1000, NULL, 1, NULL); 72 | } 73 | 74 | void loop() { 75 | // Empty loop 76 | } 77 | 78 | void sensorTask(void * parameter) { 79 | for (;;) { 80 | // Read data from sensors 81 | temperature = readTemperatureSensor(); 82 | humidity = readHumiditySensor(); 83 | 84 | // Delay for a regular interval 85 | vTaskDelay(1000 / portTICK_PERIOD_MS); 86 | } 87 | } 88 | 89 | void displayTask(void * parameter) { 90 | for (;;) { 91 | // Update display with latest sensor readings 92 | updateDisplay(temperature, humidity); 93 | 94 | // Delay until there is a change in the sensor data 95 | vTaskDelayUntil(&lastWakeTime, (1000 / portTICK_PERIOD_MS)); 96 | } 97 | } 98 | 99 | void emergencyTask(void * parameter) { 100 | for (;;) { 101 | // Check for emergency situations 102 | if (temperature > TEMPERATURE_THRESHOLD || humidity > HUMIDITY_THRESHOLD) { 103 | // Take appropriate action 104 | triggerEmergencyAlert(); 105 | } 106 | 107 | // Delay for a short time 108 | vTaskDelay(100 / portTICK_PERIOD_MS); 109 | } 110 | } 111 | ``` 112 | 113 | These code snippets show how the tasks might be defined and created in the `main.cpp` file. The `sensorTask` reads data from the sensors, the `displayTask` updates the display with the latest sensor readings, and the `emergencyTask` checks for emergency situations. The actual implementation of these tasks would depend on the specific sensors and display used in the project. 114 | 115 | ### Webserver.h 116 | 117 | The `Webserver.h` file is a header file that contains the HTML code for the web server's main page. This HTML code is served to the client when they connect to the ESP32's IP address in a web browser. 118 | 119 | This file is included in `main.cpp`, and it's responsible for displaying the current sensor readings and a section for emergency alerts. 120 | 121 | Here's a simplified example of what the HTML code might look like: 122 | 123 | ```cpp 124 | const char MAIN_page[] PROGMEM = R"=====( 125 | 126 | 127 |

ESP32 Web Server

128 |

Current Sensor Readings:

129 |

130 |

Emergency Alerts

131 |

132 | 135 | 136 | 137 | )====="; 138 | ``` 139 | 140 | In this example, the `sensorData` and `emergencyAlerts` elements are placeholders that will be updated with real data by JavaScript code. This JavaScript code would typically make AJAX requests to the ESP32 to get the latest sensor data and emergency alerts, and then update the HTML elements with this data. 141 | 142 | The `PROGMEM` keyword is used to store the HTML code in program memory, which is necessary because the ESP32 has limited RAM. 143 | 144 | This HTML code is served to the client in the `handleRoot()` function in `main.cpp`, which is called when the client makes a GET request to the root URL (`/`). 145 | 146 | ## Usage 147 | 148 | Once the code is uploaded to your ESP32 board, access the web server by entering the ESP32's IP address in a web browser. 149 | 150 | ## Testing 151 | 152 | To test the project, connect to the web server and verify that the sensor readings are displayed correctly and that the emergency alerts are working as expected. 153 | 154 | ## Live Demo 155 | 156 | [Screencast from 01-12-2024 01:15:53 AM.webm](https://github.com/ESP32-Work/Embedded-Systems-Semester-Project/assets/81290322/210c93d9-d37c-4b91-9d12-6f95ebebdda4) 157 | 158 | ## Contributing 159 | 160 | Contributions are welcome. Please open an issue or submit a pull request. 161 | 162 | ## License 163 | 164 | This project is licensed under the MIT [License](LICENSE). 165 | 166 | ## Acknowledgements 167 | 168 | - Original code can be found [here](https://github.com/KrisKasprzak/ESP32_WebPage). 169 | -------------------------------------------------------------------------------- /FreeRTOS/src/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "DHT.h" 6 | #include 7 | 8 | #include "Webserver.h" 9 | 10 | #define AP_SSID "TestWebSite" 11 | #define AP_PASS "12345678" 12 | 13 | #define PIN_OUTPUT 26 14 | #define PIN_FAN 27 15 | #define PIN_LED 2 16 | #define PIN_A0 34 17 | #define PIN_A1 35 18 | #define IR_SENSOR_PIN 33 19 | #define ULTRASONIC_TRIGGER_PIN 12 20 | #define ULTRASONIC_ECHO_PIN 13 21 | #define BUZZER_PIN 15 22 | #define DHT_PIN 14 23 | #define DHT_TYPE DHT11 24 | 25 | DHT dht(DHT_PIN, DHT_TYPE); 26 | 27 | int BitsA0 = 0, BitsA1 = 0; 28 | float VoltsA0 = 0, VoltsA1 = 0; 29 | int FanSpeed = 0; 30 | bool LED0 = false, SomeOutput = false; 31 | uint32_t SensorUpdate = 0; 32 | int FanRPM = 0; 33 | bool emergencyShutdownActive = false; 34 | int taskCount = 0; 35 | float temperatureDHT = 0.0; 36 | float humidityDHT = 0.0; 37 | 38 | char XML[2048]; 39 | char buf[32]; 40 | IPAddress Actual_IP; 41 | IPAddress PageIP(192, 168, 1, 1); 42 | IPAddress gateway(192, 168, 1, 1); 43 | IPAddress subnet(255, 255, 255, 0); 44 | IPAddress ip; 45 | 46 | WebServer server(80); 47 | 48 | TaskHandle_t mainTask; 49 | TaskHandle_t serverTask; 50 | TaskHandle_t Task1Handle = NULL; 51 | TaskHandle_t Task2Handle = NULL; 52 | TaskHandle_t Task3Handle = NULL; 53 | 54 | void IR_Sensor_Function(void *pvParameters); 55 | void DHT_Sensor_Function(void *pvParameters); 56 | void Emergency_Function(void *pvParameters); 57 | void ServerTask(void *pvParameters); 58 | void EmergencyShutdown(); 59 | 60 | void ADC_Function(void *pvParameters); 61 | 62 | void UpdateSlider(); 63 | void ProcessButton_1(); 64 | void ProcessButton_0(); 65 | void SendXML(); 66 | void SendWebsite(); 67 | 68 | void setup() { 69 | Serial.begin(115200); 70 | 71 | pinMode(PIN_FAN, OUTPUT); 72 | pinMode(PIN_LED, OUTPUT); 73 | LED0 = false; 74 | digitalWrite(PIN_LED, LED0); 75 | 76 | ledcSetup(0, 10000, 8); 77 | ledcAttachPin(PIN_FAN, 0); 78 | ledcWrite(0, FanSpeed); 79 | 80 | disableCore0WDT(); 81 | 82 | WiFi.softAP(AP_SSID, AP_PASS); 83 | delay(100); 84 | WiFi.softAPConfig(PageIP, gateway, subnet); 85 | delay(100); 86 | Actual_IP = WiFi.softAPIP(); 87 | Serial.print("IP address: "); 88 | Serial.println(Actual_IP); 89 | 90 | server.on("/", SendWebsite); 91 | server.on("/xml", SendXML); 92 | server.on("/UPDATE_SLIDER", UpdateSlider); 93 | server.on("/BUTTON_0", ProcessButton_0); 94 | server.on("/BUTTON_1", ProcessButton_1); 95 | 96 | server.begin(); 97 | dht.begin(); 98 | xTaskCreatePinnedToCore(ADC_Function, "mainTask", 8192, NULL, 1, &mainTask, 0); 99 | 100 | // Create FreeRTOS tasks 101 | xTaskCreatePinnedToCore(IR_Sensor_Function, "Task1", 8192, NULL, 2, &Task1Handle, 0); 102 | xTaskCreatePinnedToCore(DHT_Sensor_Function, "Task2", 8192, NULL, 2, &Task2Handle, 0); 103 | xTaskCreatePinnedToCore(Emergency_Function, "Task3", 8192, NULL, 3, &Task3Handle, 0); 104 | 105 | // Create a server task pinned to core 1 106 | xTaskCreatePinnedToCore(ServerTask, "serverTask", 4096, NULL, 3, &serverTask, 1); 107 | } 108 | 109 | void loop() { 110 | // Since the server is now in its task, loop() can be left empty 111 | // or used for other non-blocking tasks if needed. 112 | } 113 | 114 | void ADC_Function(void *pvParameters) { 115 | taskCount++; 116 | while (true) { 117 | if ((millis() - SensorUpdate) >= 50) { 118 | SensorUpdate = millis(); 119 | BitsA0 = analogRead(PIN_A0); 120 | BitsA1 = analogRead(PIN_A1); 121 | VoltsA0 = BitsA0 * 3.3 / 4096; 122 | VoltsA1 = BitsA1 * 3.3 / 4096; 123 | } 124 | vTaskDelay(pdMS_TO_TICKS(10)); // Adjust the delay as needed 125 | } 126 | 127 | } 128 | 129 | void UpdateSlider() { 130 | String t_state = server.arg("VALUE"); 131 | FanSpeed = t_state.toInt(); 132 | Serial.print("UpdateSlider "); 133 | Serial.println(FanSpeed); 134 | 135 | // Map FanSpeed to LED brightness 136 | int ledBrightness = map(FanSpeed, 0, 255, 0, 255); 137 | 138 | // Set LED brightness 139 | analogWrite(PIN_FAN, ledBrightness); 140 | 141 | // Respond with the updated RPM value 142 | server.send(200, "text/plain", String(FanSpeed)); 143 | } 144 | 145 | void ProcessButton_0() { 146 | LED0 = !LED0; 147 | digitalWrite(PIN_LED, LED0); 148 | Serial.print("Button 0 "); 149 | Serial.println(LED0); 150 | server.send(200, "text/plain", ""); 151 | } 152 | 153 | void ProcessButton_1() { 154 | SomeOutput = !SomeOutput; 155 | digitalWrite(PIN_OUTPUT, SomeOutput); 156 | Serial.print("Button 1 "); 157 | Serial.println(LED0); 158 | server.send(200, "text/plain", ""); 159 | } 160 | 161 | void SendWebsite() { 162 | Serial.println("sending web page"); 163 | server.send(200, "text/html", PAGE_MAIN); 164 | } 165 | 166 | void SendXML() { 167 | strcpy(XML, "\n\n"); 168 | sprintf(buf, "%d\n", BitsA0); 169 | strcat(XML, buf); 170 | sprintf(buf, "%d.%d\n", (int)(VoltsA0), abs((int)(VoltsA0 * 10) - ((int)(VoltsA0) * 10))); 171 | strcat(XML, buf); 172 | sprintf(buf, "%d\n", BitsA1); 173 | strcat(XML, buf); 174 | sprintf(buf, "%d.%d\n", (int)(VoltsA1), abs((int)(VoltsA1 * 10) - ((int)(VoltsA1) * 10))); 175 | strcat(XML, buf); 176 | 177 | if (LED0) { 178 | strcat(XML, "1\n"); 179 | } else { 180 | strcat(XML, "0\n"); 181 | } 182 | 183 | if (SomeOutput) { 184 | strcat(XML, "1\n"); 185 | } else { 186 | strcat(XML, "0\n"); 187 | } 188 | 189 | strcat(XML, ""); 190 | if (emergencyShutdownActive) { 191 | strcat(XML, "1"); 192 | } else { 193 | strcat(XML, "0"); 194 | } 195 | strcat(XML, "\n"); 196 | strcat(XML, ""); 197 | if (xTaskGetSchedulerState() == taskSCHEDULER_RUNNING && uxTaskGetNumberOfTasks() > 0) { 198 | strcat(XML, "1"); 199 | } else { 200 | strcat(XML, "0"); 201 | } 202 | strcat(XML, "\n"); 203 | 204 | strcat(XML, ""); 205 | if (xTaskGetSchedulerState() == taskSCHEDULER_RUNNING && uxTaskGetNumberOfTasks() > 1) { 206 | // char a= Serial.read(); 207 | // if(a == 'C'){ 208 | // strcat(XML, "0"); 209 | // } 210 | strcat(XML, "1"); 211 | } else { 212 | strcat(XML, "0"); 213 | } 214 | strcat(XML, "\n"); 215 | // Append temperature and humidity data to XML 216 | strcat(XML, "\n"); 217 | 218 | // Append temperature value 219 | strcat(XML, ""); 220 | sprintf(buf, "%.2f", temperatureDHT); 221 | strcat(XML, buf); 222 | strcat(XML, "\n"); 223 | 224 | // Append humidity value 225 | strcat(XML, ""); 226 | sprintf(buf, "%.2f", humidityDHT); 227 | strcat(XML, buf); 228 | strcat(XML, "\n"); 229 | 230 | strcat(XML, "\n"); 231 | strcat(XML, ""); 232 | strcat(XML, String(taskCount).c_str()); 233 | strcat(XML, "\n"); 234 | 235 | strcat(XML, "\n"); 236 | Serial.println(XML); 237 | server.send(200, "text/xml", XML); 238 | } 239 | 240 | void ServerTask(void *pvParameters) { 241 | taskCount++; 242 | while (true) { 243 | // Update the server task core indicators 244 | server.handleClient(); 245 | vTaskDelay(pdMS_TO_TICKS(10)); 246 | } 247 | } 248 | 249 | 250 | void IR_Sensor_Function(void *pvParameters) { 251 | taskCount++; 252 | pinMode(IR_SENSOR_PIN, INPUT); 253 | 254 | while (1) { 255 | // Read data from the analog IR sensor 256 | int irSensorValue = analogRead(IR_SENSOR_PIN); 257 | 258 | // Process the sensor data 259 | if (irSensorValue < 500) { 260 | Serial.println("IR sensor detected an obstacle!"); 261 | } else { 262 | Serial.println("No obstacle detected."); 263 | } 264 | vTaskDelay(pdMS_TO_TICKS(50)); // Adjust the delay as needed 265 | } 266 | 267 | } 268 | 269 | void DHT_Sensor_Function(void *pvParameters) { 270 | taskCount++; 271 | while (1) { 272 | temperatureDHT = dht.readTemperature(); 273 | humidityDHT = dht.readHumidity(); 274 | 275 | Serial.print("Task2 - Temperature: "); 276 | Serial.print(temperatureDHT); 277 | Serial.print(" °C, Humidity: "); 278 | Serial.print(humidityDHT); 279 | Serial.println(" %"); 280 | // Update the server with temperature and humidity values 281 | SendXML(); 282 | Serial.println("Task2 - display is updated"); 283 | vTaskDelay(pdMS_TO_TICKS(100)); // Adjust the delay as needed 284 | } 285 | 286 | } 287 | 288 | void Emergency_Function(void *pvParameters) { 289 | taskCount++; 290 | pinMode(ULTRASONIC_TRIGGER_PIN, OUTPUT); 291 | pinMode(ULTRASONIC_ECHO_PIN, INPUT); 292 | pinMode(BUZZER_PIN, OUTPUT); 293 | 294 | while (1) { 295 | // Trigger ultrasonic sensor 296 | digitalWrite(ULTRASONIC_TRIGGER_PIN, LOW); 297 | delayMicroseconds(2); 298 | digitalWrite(ULTRASONIC_TRIGGER_PIN, HIGH); 299 | delayMicroseconds(10); 300 | digitalWrite(ULTRASONIC_TRIGGER_PIN, LOW); 301 | 302 | // Read the ultrasonic sensor echo 303 | long duration = pulseIn(ULTRASONIC_ECHO_PIN, HIGH); 304 | // Calculate distance in centimeters 305 | float distance = duration * 0.034 / 2; 306 | Serial.print("Distance: "); 307 | Serial.println(distance); 308 | char a= Serial.read(); 309 | // Check if an obstacle is detected (adjust the distance threshold as needed) 310 | if (distance < 20) { 311 | // if (a == 'E') { 312 | // Activate emergency mode only if not already active 313 | if (!emergencyShutdownActive) { 314 | emergencyShutdownActive = true; 315 | EmergencyShutdown(); 316 | digitalWrite(BUZZER_PIN, HIGH); 317 | } 318 | } else { 319 | // Deactivate emergency mode only if it was active 320 | if (emergencyShutdownActive) { 321 | emergencyShutdownActive = false; 322 | digitalWrite(BUZZER_PIN, LOW); 323 | // Add any post-emergency code here if needed 324 | } 325 | } 326 | 327 | vTaskDelay(pdMS_TO_TICKS(100)); // Adjust the delay as needed 328 | } 329 | } 330 | 331 | void EmergencyShutdown() { 332 | // Execute emergency shutdown procedures 333 | Serial.println("Task3 - EMERGENCY protocol is being executed..."); 334 | // Send XML data to update the server 335 | server.send(200, "text/plain", ""); 336 | } 337 | -------------------------------------------------------------------------------- /FreeRTOS/include/Webserver.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | 4 | HTML + CSS styling + javascript all in and undebuggable environment 5 | 6 | get all your HTML code (from html to /html) and past it into this test site 7 | muck with the HTML and CSS code until it's what you want 8 | https://www.w3schools.com/html/tryit.asp?filename=tryhtml_intro 9 | 10 | No clue how to debug javascrip other that write, compile, upload, refresh, guess, repeat 11 | 12 | I'm using class designators to set styles and id's for data updating 13 | for example: 14 | the CSS class .tabledata defines with the cell will look like 15 |
16 | 17 | the XML code will update the data where id = "switch" 18 | java script then uses getElementById 19 | document.getElementById("switch").innerHTML="Switch is OFF"; 20 | 21 | 22 | .. now you can have the class define the look AND the class update the content, but you will then need 23 | a class for every data field that must be updated, here's what that will look like 24 |
25 | 26 | the XML code will update the data where class = "switch" 27 | java script then uses getElementsByClassName 28 | document.getElementsByClassName("switch")[0].style.color=text_color; 29 | 30 | 31 | the main general sections of a web page are the following and used here 32 | 33 | 34 | 37 | 38 |
39 | // put header code for cute banners here 40 |
41 |
42 | // the buld of your web page contents 43 |
44 |
45 | // put cute footer (c) 2021 xyz inc type thing 46 |
47 | 48 | 51 | 52 | 53 | 54 | */ 55 | 56 | 57 | const char PAGE_MAIN[] PROGMEM = R"=====( 58 | 59 | 60 | 61 | 62 | Web Page Update Demo 63 | 64 | 211 | 212 | 213 | 214 |
215 | 224 |
225 | 226 |
227 |
Sensor Readings
228 |
229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | 252 | 253 | 254 | 255 | 256 | 257 | 258 | 259 | 261 | 262 |
Pin
Bits
Volts
Analog pin 34
Analog pin 35
Digital switch
Emergency Mode
260 |
263 |
264 |
Cores Used
265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 | 274 | 275 | 276 | 277 | 278 | 279 | 280 | 281 | 282 |
Core
Status
Core 0
Core 1
283 | 284 |
285 |
Task Information
286 |
287 |
Total Tasks
288 |
289 |
290 |
291 |
292 | 293 |
294 |
Live Readings
295 | 296 | 297 | 298 | 299 | 300 | 301 | 302 | 303 | 304 |
Temp(°C)
Humidity (%)
305 |
306 |
307 | 308 | 327 |
328 |
329 |
330 |
Sensor Controls
331 |
332 |
Inbuilt LED
333 | 334 | 335 |
336 |
Switch
337 | 338 | 339 |
340 |
341 |
Fan Speed Control (RPM: )
342 |
343 | 344 |
345 |
346 |
347 | 348 |
Embedded System Design Semester Project
349 | 350 | 351 | 352 | 353 | 574 | 575 | 576 | 577 | 578 | 579 | )====="; --------------------------------------------------------------------------------