pleasedontcode

# Serial Monitor rev_02

Feb 28th, 2026
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: # Serial Monitor
  13.     - Source Code compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2026-02-28 09:46:17
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* **Proposal 2**: "Configure ESP32 DevKit V1 serial */
  21.     /* communication to send sensor data readings over */
  22.     /* UART at 115200 baud rate for real-time */
  23.     /* monitoring." */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26.  
  27.  
  28.  
  29. // Global variables for sensor data storage
  30. float sensorValue1 = 0.0;
  31. float sensorValue2 = 0.0;
  32. float sensorValue3 = 0.0;
  33.  
  34. // UART Configuration Constants
  35. #define UART_BAUD_RATE 115200
  36. #define SERIAL_TX_BUFFER_SIZE 256
  37. #define SENSOR_READ_INTERVAL 1000 // milliseconds
  38.  
  39. // Timing variables
  40. unsigned long lastSensorReadTime = 0;
  41.  
  42. void setup(void)
  43. {
  44.     // Initialize serial communication with 115200 baud rate
  45.     initSerialCommunication();
  46.    
  47.     // Small delay to ensure serial is ready
  48.     delay(500);
  49.    
  50.     // Send initialization message
  51.     Serial.println("\n===========================================");
  52.     Serial.println("ESP32 DevKit V1 - Sensor Data Monitor");
  53.     Serial.println("UART Configuration: 115200 baud rate");
  54.     Serial.println("System Ready for Real-time Monitoring");
  55.     Serial.println("===========================================\n");
  56. }
  57.  
  58. void loop(void)
  59. {
  60.     // Check if it's time to read sensor data
  61.     if (millis() - lastSensorReadTime >= SENSOR_READ_INTERVAL)
  62.     {
  63.         // Read sensor values
  64.         readSensorData();
  65.        
  66.         // Transmit sensor data over UART
  67.         transmitSensorData();
  68.        
  69.         // Update last read time
  70.         lastSensorReadTime = millis();
  71.     }
  72. }
  73.  
  74. /**
  75.  * Function: initSerialCommunication
  76.  * Purpose: Configure and initialize ESP32 UART serial communication
  77.  * Parameters: None
  78.  * Returns: None
  79.  * Description: Initializes the primary UART (Serial0) with 115200 baud rate
  80.  */
  81. void initSerialCommunication(void)
  82. {
  83.     // Initialize Serial (UART0) with 115200 baud rate
  84.     // ESP32 uses GPIO1 (TX) and GPIO3 (RX) by default for Serial
  85.     Serial.begin(UART_BAUD_RATE);
  86.    
  87.     // Wait for serial port to connect (required for some ESP32 boards)
  88.     // Timeout after 2 seconds if not connected
  89.     unsigned long startTime = millis();
  90.     while (!Serial && (millis() - startTime < 2000))
  91.     {
  92.         delay(10);
  93.     }
  94. }
  95.  
  96. /**
  97.  * Function: readSensorData
  98.  * Purpose: Read analog sensor values from ESP32 ADC pins
  99.  * Parameters: None
  100.  * Returns: None
  101.  * Description: Simulates reading sensor data. In real application, replace with actual ADC reads.
  102.  *              This example demonstrates the sensor data acquisition pattern.
  103.  */
  104. void readSensorData(void)
  105. {
  106.     // Read analog sensor values from ADC pins
  107.     // Sensor 1: ADC0 (GPIO36/VP)
  108.     sensorValue1 = analogRead(36) * (3.3 / 4095.0);
  109.    
  110.     // Sensor 2: ADC1 (GPIO39/VN)
  111.     sensorValue2 = analogRead(39) * (3.3 / 4095.0);
  112.    
  113.     // Sensor 3: ADC2 (GPIO34/GPIO4) - Note: ADC2 pins cannot be used while WiFi is on
  114.     sensorValue3 = analogRead(34) * (3.3 / 4095.0);
  115. }
  116.  
  117. /**
  118.  * Function: transmitSensorData
  119.  * Purpose: Send sensor data readings over UART at 115200 baud rate
  120.  * Parameters: None
  121.  * Returns: None
  122.  * Description: Formats and transmits sensor data in a structured format for real-time monitoring.
  123.  *              Includes timestamp, sensor values, and checksum verification capability.
  124.  */
  125. void transmitSensorData(void)
  126. {
  127.     // Get current timestamp
  128.     unsigned long currentTime = millis();
  129.    
  130.     // Format and transmit header
  131.     Serial.print("[");
  132.     Serial.print(currentTime);
  133.     Serial.print("ms] ");
  134.    
  135.     // Transmit Sensor Data
  136.     Serial.print("SENSORS: ");
  137.     Serial.print("CH1=");
  138.     Serial.print(sensorValue1, 3);
  139.     Serial.print("V ");
  140.     Serial.print("CH2=");
  141.     Serial.print(sensorValue2, 3);
  142.     Serial.print("V ");
  143.     Serial.print("CH3=");
  144.     Serial.print(sensorValue3, 3);
  145.     Serial.println("V");
  146.    
  147.     // Transmit raw ADC values for reference
  148.     Serial.print("[RAW VALUES] ");
  149.     Serial.print("ADC0=");
  150.     Serial.print(analogRead(36));
  151.     Serial.print(" ");
  152.     Serial.print("ADC1=");
  153.     Serial.print(analogRead(39));
  154.     Serial.print(" ");
  155.     Serial.print("ADC2=");
  156.     Serial.println(analogRead(34));
  157. }
  158.  
  159. /* END CODE */
  160.  
Advertisement
Add Comment
Please, Sign In to add comment