pleasedontcode

# Smart Cannabis Monitor rev_08

Jan 17th, 2026
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 10.12 KB | None | 0 0
  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: # Smart Cannabis Monitor
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2026-01-17 17:50:29
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Gunakan ESP32 AsyncWebServer untuk memantau */
  21.     /* pembacaan encoder putar dan mengontrol parameter */
  22.     /* mekanisme THC dari jarak jauh melalui koneksi */
  23.     /* WebSocket, menyediakan visualisasi pengukur secara */
  24.     /* real-time dan eksekusi perintah dengan umpan balik */
  25.     /* status. */
  26. /****** END SYSTEM REQUIREMENTS *****/
  27.  
  28.  
  29. /* START CODE */
  30.  
  31. // SYSTEM REQUIREMENTS:
  32. // Gunakan ESP32 AsyncWebServer untuk memantau pembacaan encoder putar
  33. // dan mengontrol parameter mekanisme THC dari jarak jauh melalui koneksi
  34. // WebSocket, menyediakan visualisasi pengukur secara real-time dan
  35. // eksekusi perintah dengan umpan balik status.
  36.  
  37. #include <EEPROM.h>
  38. #include "_webserver.h"
  39.  
  40. // ============ VARIABLE DECLARATIONS ============
  41. // EEPROM address variables
  42. int SetVa, DTa, HySa, StVa;
  43.  
  44. // THC Parameters
  45. int SetV = 100;      // Set voltage (0-250V)
  46. int DT = 5;          // Delay time (0.5s = value 5)
  47. int HyS = 80;        // Hysteresis (8.0V = value 80)
  48. int StV = 100;       // Start voltage (100V)
  49. int ArcV = 0;        // Arc voltage (read from sensor)
  50.  
  51. // Encoder variables
  52. int encoderVal = 0;  // Current encoder position value
  53. int oldValue = 0;    // Previous encoder value for change detection
  54.  
  55. // Program selection
  56. int program = 1;     // Currently selected program (1, 2, or 3)
  57. const int ParamItem = 4;  // Number of parameters per program
  58. int Param[ParamItem]; // Array to store program parameters
  59.  
  60. // LCD menu control
  61. int menu = 0;        // Current menu state
  62. int pos = 0;         // Position in menu
  63. int show = 0;        // LCD update counter
  64. int LCDtime = 0;     // LCD timeout counter
  65. int defaultLCDtime = 30;  // LCD timeout in 50ms intervals (1.5s)
  66.  
  67. // Timer and control variables
  68. esp_timer_handle_t timer_handle;
  69. bool Do = false;     // Timer trigger flag
  70.  
  71. // Pin definitions
  72. const int outputUpPin = 27;   // GPIO 27 - Torch UP control
  73. const int outputDnPin = 26;   // GPIO 26 - Torch DOWN control
  74. const int outputOkPin = 25;   // GPIO 25 - Torch OK control
  75.  
  76. // WebSocket update timing
  77. unsigned long lastWebSocketUpdate = 0;
  78. const unsigned long WEBSOCKET_UPDATE_INTERVAL = 100;  // 100ms update interval
  79.  
  80. // ============ SETUP FUNCTION ============
  81. void setup() {
  82.     // Initialize Serial communication
  83.     Serial.begin(115200);
  84.     delay(1000);
  85.     Serial.println("\n\n========== ESP32 THC System Starting ==========");
  86.    
  87.     // Initialize EEPROM
  88.     EEPROM.begin(128);
  89.     delay(100);
  90.    
  91.     // Read program selection from EEPROM
  92.     ReadProg();
  93.    
  94.     // Load selected program parameters
  95.     if (program == 1) {
  96.         doProgramSet(1);
  97.     } else if (program == 2) {
  98.         doProgramSet(2);
  99.     } else {
  100.         doProgramSet(3);
  101.     }
  102.    
  103.     // Initialize LCD display
  104.     Setup_LCD();
  105.    
  106.     // Initialize THC control outputs
  107.     Setup_THC();
  108.    
  109.     // Initialize timer for periodic tasks
  110.     Setup_Timer2();
  111.    
  112.     // Initialize WiFi Access Point
  113.     startWiFiAP();
  114.    
  115.     // Initialize Web Server and WebSocket
  116.     setupWebServer();
  117.    
  118.     Serial.println("Setup complete!");
  119.     Serial.println("Connect to WiFi SSID: " WIFI_SSID);
  120.     Serial.println("Access web interface at: http://<ESP32_IP>");
  121.     Serial.println("==========================================\n");
  122. }
  123.  
  124. // ============ MAIN LOOP ============
  125. void loop() {
  126.     // Process THC control logic
  127.     doTHC();
  128.    
  129.     // Update LCD display
  130.     doLCD();
  131.    
  132.     // Send WebSocket updates at regular intervals
  133.     unsigned long currentTime = millis();
  134.     if (currentTime - lastWebSocketUpdate >= WEBSOCKET_UPDATE_INTERVAL) {
  135.         lastWebSocketUpdate = currentTime;
  136.        
  137.         // Send encoder update via WebSocket
  138.         sendEncoderUpdate();
  139.        
  140.         // Send gauge update via WebSocket
  141.         sendGaugeUpdate();
  142.        
  143.         // Send THC status via WebSocket
  144.         sendTHCStatus();
  145.     }
  146.    
  147.     // Small delay to prevent watchdog timeout
  148.     delay(10);
  149. }
  150.  
  151. // ============ EEPROM FUNCTIONS ============
  152. // Initialize EEPROM with default values for all 3 programs
  153. void Default() {
  154.     // Set parameter addresses for Program 1 as default
  155.     SetVa = 0;
  156.     DTa = 1;
  157.     HySa = 2;
  158.     StVa = 3;
  159.  
  160.     // Set default parameter values
  161.     SetV = 100;   // Set voltage
  162.     DT = 5;       // Delay time (0.5s)
  163.     HyS = 80;     // Hysteresis (8.0V)
  164.     StV = 100;    // Start voltage (100V)
  165.    
  166.     // Store Program 1 default values at addresses 0-3
  167.     EEPROM.write(0, SetV);
  168.     EEPROM.write(1, DT);
  169.     EEPROM.write(2, HyS);
  170.     EEPROM.write(3, StV);
  171.  
  172.     // Store Program 2 default values at addresses 4-7
  173.     EEPROM.write(4, SetV);
  174.     EEPROM.write(5, DT);
  175.     EEPROM.write(6, HyS);
  176.     EEPROM.write(7, StV);
  177.  
  178.     // Store Program 3 default values at addresses 8-11
  179.     EEPROM.write(8, SetV);
  180.     EEPROM.write(9, DT);
  181.     EEPROM.write(10, HyS);
  182.     EEPROM.write(11, StV);
  183.  
  184.     // Store selected program at address 12 (default to Program 1)
  185.     EEPROM.write(12, 1);
  186.  
  187.     // Commit changes to EEPROM
  188.     EEPROM.commit();
  189. }
  190.  
  191. // Read the last selected program from EEPROM
  192. void ReadProg() {
  193.     // Initialize EEPROM access (required for ESP32)
  194.     // Size of 14 bytes covers addresses 0-13
  195.     EEPROM.begin(14);
  196.    
  197.     // Read the last selected program from address 12
  198.     program = EEPROM.read(12);
  199.    
  200.     // Validate program value
  201.     if (program < 1 || program > 3) {
  202.         program = 1;
  203.     }
  204. }
  205.  
  206. // Read Program 1 parameters from EEPROM addresses 0-3
  207. void ReadDataProg_1() {
  208.     for (int j = 0; j < ParamItem; j++) {
  209.         Param[j] = EEPROM.read(j);
  210.     }
  211. }
  212.  
  213. // Read Program 2 parameters from EEPROM addresses 4-7
  214. void ReadDataProg_2() {
  215.     for (int j = 0; j < ParamItem; j++) {
  216.         Param[j] = EEPROM.read(j + 4);
  217.     }
  218. }
  219.  
  220. // Read Program 3 parameters from EEPROM addresses 8-11
  221. void ReadDataProg_3() {
  222.     for (int j = 0; j < ParamItem; j++) {
  223.         Param[j] = EEPROM.read(j + 8);
  224.     }
  225. }
  226.  
  227. // Save a single parameter value to EEPROM
  228. void SaveData(int add, int value) {
  229.     EEPROM.write(add, value);
  230.     EEPROM.commit();
  231. }
  232.  
  233. // Helper function to set program and load its parameters
  234. void doProgramSet(int prg) {
  235.     if (prg == 1) {
  236.         SetVa = 0;
  237.         DTa = 1;
  238.         HySa = 2;
  239.         StVa = 3;
  240.         ReadDataProg_1();
  241.     } else if (prg == 2) {
  242.         SetVa = 4;
  243.         DTa = 5;
  244.         HySa = 6;
  245.         StVa = 7;
  246.         ReadDataProg_2();
  247.     } else {
  248.         SetVa = 8;
  249.         DTa = 9;
  250.         HySa = 10;
  251.         StVa = 11;
  252.         ReadDataProg_3();
  253.     }
  254.  
  255.     SaveData(100, prg);
  256.     program = prg;
  257.  
  258.     SetV = Param[0];
  259.     DT = Param[1];
  260.     HyS = Param[2];
  261.     StV = Param[3];
  262.    
  263.     // Preset value for encoder
  264.     encoderVal = SetV;
  265.     menu = 0;
  266. }
  267.  
  268. // ============ TIMER CALLBACK ============
  269. // Timer callback function for periodic task triggering
  270. void onTimerCallback(void* arg) {
  271.     Do = true;
  272. }
  273.  
  274. // Setup ESP32 timer for periodic callbacks
  275. void Setup_Timer2() {
  276.     // Setting structure for the timer
  277.     esp_timer_create_args_t timer_args = {
  278.         .callback = &onTimerCallback,
  279.         .name = "thc_periodic_timer"
  280.     };
  281.  
  282.     // Create the timer
  283.     ESP_ERROR_CHECK(esp_timer_create(&timer_args, &timer_handle));
  284.  
  285.     // Start the timer. It will repeat every 10,000 microseconds (10ms)
  286.     ESP_ERROR_CHECK(esp_timer_start_periodic(timer_handle, 10000));
  287. }
  288.  
  289. // ============ THC CONTROL LOGIC ============
  290. unsigned int delayTime;
  291. unsigned int SetVx10;
  292.  
  293. // Initialize THC control pins
  294. void Setup_THC() {
  295.     pinMode(outputUpPin, OUTPUT);   // GPIO 27 - Torch UP
  296.     pinMode(outputOkPin, OUTPUT);   // GPIO 25 - Torch OK
  297.     pinMode(outputDnPin, OUTPUT);   // GPIO 26 - Torch DOWN
  298.    
  299.     // Ensure all outputs are initially OFF
  300.     digitalWrite(outputUpPin, LOW);
  301.     digitalWrite(outputOkPin, LOW);
  302.     digitalWrite(outputDnPin, LOW);
  303. }
  304.  
  305. // Main THC control function - monitors arc voltage and adjusts torch height
  306. void doTHC() {
  307.     if (Do) {
  308.         Do = false;
  309.         LCDtime++;
  310.         show++;
  311.  
  312.         // Reset to default menu if timeout occurs
  313.         if (LCDtime > defaultLCDtime) {
  314.             menu = 0;
  315.             pos = 0;
  316.             LCDtime = 0;
  317.             encoderVal = SetV;
  318.         }
  319.        
  320.         // Check if arc voltage is within valid range (500mV to 2500mV)
  321.         if ((500 < ArcV) && (ArcV < 2500)) {
  322.             // Increment delay counter while waiting for arc stabilization
  323.             if (ArcV > StV * 10) {
  324.                 delayTime++;
  325.             }
  326.  
  327.             // Once delay threshold is reached, enable torch control
  328.             if (delayTime >= DT * 10) {
  329.                 SetVx10 = SetV * 10;
  330.                 delayTime = DT * 10;
  331.  
  332.                 // Signal that arc is OK
  333.                 digitalWrite(outputOkPin, HIGH);
  334.  
  335.                 // Hysteresis control - move torch up if voltage too high
  336.                 if (ArcV >= SetVx10 + HyS) {
  337.                     digitalWrite(outputUpPin, LOW);
  338.                     digitalWrite(outputDnPin, HIGH);
  339.                 }
  340.                 // Hysteresis control - move torch down if voltage too low
  341.                 else if (ArcV <= SetVx10 - HyS) {
  342.                     digitalWrite(outputDnPin, LOW);
  343.                     digitalWrite(outputUpPin, HIGH);
  344.                 }
  345.                 // Voltage is within acceptable range - hold position
  346.                 else {
  347.                     digitalWrite(outputUpPin, LOW);
  348.                     digitalWrite(outputDnPin, LOW);
  349.                 }
  350.             }
  351.         }
  352.         // Arc voltage out of range or not in test mode - turn off all outputs
  353.         else if (menu != 12) {
  354.             delayTime = 0;
  355.             digitalWrite(outputUpPin, LOW);
  356.             digitalWrite(outputOkPin, LOW);
  357.             digitalWrite(outputDnPin, LOW);
  358.         }
  359.     }
  360. }
  361.  
  362. /* END CODE */
  363.  
Advertisement
Add Comment
Please, Sign In to add comment