-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.py
More file actions
1082 lines (864 loc) · 35.3 KB
/
deploy.py
File metadata and controls
1082 lines (864 loc) · 35.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
ESP32 S3 Deployment Generator - Enhanced 12-Feature Model
Generates complete C++ code for production deployment
"""
import pickle
import json
import numpy as np
print("="*80)
print("ESP32 S3 DEPLOYMENT - ENHANCED 12-FEATURE MODEL")
print("="*80)
# Load the trained model
print("\nLoading trained model...")
with open('irrigation_esp32_enhanced.pkl', 'rb') as f:
model = pickle.load(f)
with open('model_metadata_enhanced.json', 'r') as f:
metadata = json.load(f)
print(f"✓ Model loaded: {metadata['model_name']}")
print(f" Accuracy: {metadata['accuracy']*100:.2f}%")
print(f" Size: {metadata['model_size_kb']:.2f} KB")
print(f" Features: {metadata['n_features']}")
print("\n" + "="*80)
print("GENERATING C++ CODE")
print("="*80)
features = metadata['features']
print(f"\nFeatures to implement ({len(features)}):")
for i, feat in enumerate(features, 1):
print(f" {i:2}. {feat}")
def export_tree_to_c(tree, tree_idx):
"""Convert sklearn decision tree to C code"""
tree_struct = tree.tree_
code = f"// Tree {tree_idx}\n"
code += f"float tree_{tree_idx}(float features[N_FEATURES]) {{\n"
def recurse(node, depth=1):
indent = " " * depth
if tree_struct.feature[node] != -2: # Not a leaf
feature_idx = tree_struct.feature[node]
threshold = tree_struct.threshold[node]
left_child = tree_struct.children_left[node]
right_child = tree_struct.children_right[node]
code_str = f"{indent}if (features[{feature_idx}] <= {threshold:.6f}f) {{\n"
code_str += recurse(left_child, depth + 1)
code_str += f"{indent}}} else {{\n"
code_str += recurse(right_child, depth + 1)
code_str += f"{indent}}}\n"
return code_str
else: # Leaf node
value = tree_struct.value[node][0]
class_0_votes = value[0]
class_1_votes = value[1]
total = class_0_votes + class_1_votes
prob = class_1_votes / total if total > 0 else 0.0
return f"{indent}return {prob:.6f}f;\n"
code += recurse(0)
code += "}\n\n"
return code
print("Generating tree functions...")
# Generate C++ header file
cpp_code = f"""/*
* ESP32 S3 Enhanced Irrigation Model - Auto-Generated
*
* Model: {metadata['model_name']}
* Accuracy: {metadata['accuracy']*100:.2f}%
* Balanced Accuracy: {metadata['balanced_accuracy']*100:.2f}%
* Model Size: {metadata['model_size_kb']:.2f} KB
* False Negative Rate: {metadata['false_negative_rate']*100:.1f}%
* Cross-Validation: {metadata['cv_mean']*100:.2f}% ± {metadata['cv_std']*100:.2f}%
*
* Features: 12 total
* Base Sensors (4): soil_moisture, temperature, humidity, water_level
* Engineered (8): et_rate, soil_temp_stress, moisture_deficit, stress_index,
* vpd_index, temp_hum_ratio, critical_dry, optimal_moisture
*
* Memory: {metadata['model_size_kb']:.2f} KB / 520 KB ({metadata['model_size_kb']/520*100:.1f}%)
* Available: {520 - metadata['model_size_kb']:.1f} KB for application
*/
#ifndef IRRIGATION_MODEL_ENHANCED_H
#define IRRIGATION_MODEL_ENHANCED_H
#define N_FEATURES {metadata['n_features']}
#define N_TREES {metadata['configuration']['n_estimators']}
#define MODEL_VERSION "Enhanced-12F-v1.0"
// Feature indices
"""
for i, feat in enumerate(features):
cpp_code += f"#define FEAT_{feat.upper()} {i}\n"
cpp_code += "\n// Decision tree functions\n\n"
# Add all tree functions
for idx, tree in enumerate(model.estimators_):
cpp_code += export_tree_to_c(tree, idx)
# Add feature engineering and prediction functions
cpp_code += """
/*
* Feature Engineering Functions
* Compute engineered features from raw sensor readings
*/
void compute_features(float soil_moisture, float temperature, float humidity,
float water_level, float features[N_FEATURES]) {
// Base sensor readings
features[FEAT_SOIL_MOISTURE] = soil_moisture;
features[FEAT_TEMPERATURE] = temperature;
features[FEAT_HUMIDITY] = humidity;
features[FEAT_WATER_LEVEL] = water_level;
// Engineered Feature 1: Evapotranspiration rate
// Represents water loss through evaporation and plant transpiration
features[FEAT_ET_RATE] = (temperature * (100.0f - humidity)) / 1000.0f;
// Engineered Feature 2: Soil-temperature stress
// Combines dry soil with heat stress
features[FEAT_SOIL_TEMP_STRESS] = ((100.0f - soil_moisture) * temperature) / 100.0f;
// Engineered Feature 3: Moisture deficit
// How far soil moisture is below optimal (50%)
features[FEAT_MOISTURE_DEFICIT] = max(0.0f, 50.0f - soil_moisture);
// Engineered Feature 4: Combined stress index
// Multi-factor plant stress indicator
features[FEAT_STRESS_INDEX] = ((100.0f - soil_moisture) * temperature) / (humidity + 1.0f);
// Engineered Feature 5: Vapor Pressure Deficit index
// Simplified VPD - measures "thirstiness" of air
features[FEAT_VPD_INDEX] = temperature - (humidity / 5.0f);
// Engineered Feature 6: Temperature-humidity ratio
// Air dryness indicator
features[FEAT_TEMP_HUM_RATIO] = temperature / (humidity + 1.0f);
// Engineered Feature 7: Critical dry threshold
// Binary flag for very dry soil (<30%)
features[FEAT_CRITICAL_DRY] = (soil_moisture < 30.0f) ? 1.0f : 0.0f;
// Engineered Feature 8: Optimal moisture flag
// Binary flag for ideal moisture range (60-80%)
features[FEAT_OPTIMAL_MOISTURE] = (soil_moisture >= 60.0f && soil_moisture <= 80.0f) ? 1.0f : 0.0f;
}
/*
* Main Prediction Function
* Returns: 1 = irrigation needed, 0 = no action
*/
int predict_irrigation(float soil_moisture, float temperature, float humidity, float water_level) {
float features[N_FEATURES];
// Compute all features from sensor readings
compute_features(soil_moisture, temperature, humidity, water_level, features);
// Accumulate predictions from all trees
float sum = 0.0f;
"""
for idx in range(len(model.estimators_)):
cpp_code += f" sum += tree_{idx}(features);\n"
cpp_code += """
// Average and apply threshold
float avg = sum / N_TREES;
return (avg > 0.5f) ? 1 : 0;
}
/*
* Get Prediction Probability
* Returns: probability of needing irrigation (0.0 to 1.0)
*/
float predict_irrigation_probability(float soil_moisture, float temperature,
float humidity, float water_level) {
float features[N_FEATURES];
compute_features(soil_moisture, temperature, humidity, water_level, features);
float sum = 0.0f;
"""
for idx in range(len(model.estimators_)):
cpp_code += f" sum += tree_{idx}(features);\n"
cpp_code += """
return sum / N_TREES;
}
/*
* Get Feature Values (for debugging)
* Fills the provided array with computed feature values
*/
void get_feature_values(float soil_moisture, float temperature, float humidity,
float water_level, float features[N_FEATURES]) {
compute_features(soil_moisture, temperature, humidity, water_level, features);
}
/*
* Model Information
*/
void print_model_info() {
Serial.println("========================================");
Serial.println("Enhanced Irrigation Model");
Serial.println("========================================");
Serial.print("Version: ");
Serial.println(MODEL_VERSION);
""" + f""" Serial.print("Accuracy: ");
Serial.print({metadata['accuracy']*100:.2f});
Serial.println("%");
Serial.print("Model Size: ");
Serial.print({metadata['model_size_kb']:.2f});
Serial.println(" KB");
Serial.print("Trees: ");
Serial.println(N_TREES);
Serial.print("Features: ");
Serial.println(N_FEATURES);
""" + """ Serial.println("========================================");
}
#endif // IRRIGATION_MODEL_ENHANCED_H
"""
# Save header file
with open('IrrigationModel.h', 'w', encoding='utf-8') as f:
f.write(cpp_code)
print("✓ Generated: IrrigationModel.h")
# Generate Arduino main sketch
arduino_sketch = f"""/*
* ESP32 S3 Smart Irrigation System
* Enhanced ML Model: {metadata['accuracy']*100:.2f}% Accuracy
*
* Hardware:
* - ESP32 S3 N16R8 Dev Kit
* - Capacitive soil moisture sensor (GPIO 34)
* - DHT11 temperature/humidity (GPIO 4)
* - Water level sensor (GPIO 35)
* - Relay module (GPIO 25)
*/
#include "IrrigationModel.h"
#include <DHT.h>
// Pin Definitions
#define SOIL_MOISTURE_PIN 34
#define DHT_PIN 4
#define WATER_LEVEL_PIN 35
#define PUMP_RELAY_PIN 25
#define LED_PIN 2
// Sensor Configuration
#define DHT_TYPE DHT11
DHT dht(DHT_PIN, DHT_TYPE);
// Timing Configuration
#define READING_INTERVAL 60000 // Read sensors every 60 seconds
#define IRRIGATION_DURATION 30000 // Irrigate for 30 seconds
#define MIN_WATER_LEVEL 10 // Minimum 10% water to irrigate
// Sensor Calibration (adjust these for your sensors)
#define SOIL_DRY_VALUE 4095 // ADC value when sensor is in dry soil
#define SOIL_WET_VALUE 1500 // ADC value when sensor is in water
#define WATER_EMPTY_VALUE 4095 // ADC value when tank is empty
#define WATER_FULL_VALUE 1000 // ADC value when tank is full
// State variables
unsigned long lastReadingTime = 0;
unsigned long irrigationStartTime = 0;
bool isIrrigating = false;
void setup() {{
Serial.begin(115200);
// Initialize pins
pinMode(PUMP_RELAY_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
digitalWrite(PUMP_RELAY_PIN, LOW);
digitalWrite(LED_PIN, LOW);
// Initialize sensors
dht.begin();
// Print model information
print_model_info();
Serial.println("\\nSystem initialized. Starting monitoring...");
delay(2000);
}}
void loop() {{
unsigned long currentTime = millis();
// If irrigating, check if duration elapsed
if (isIrrigating) {{
if (currentTime - irrigationStartTime >= IRRIGATION_DURATION) {{
stopIrrigation();
}}
return;
}}
// Check if it's time to read sensors
if (currentTime - lastReadingTime >= READING_INTERVAL) {{
lastReadingTime = currentTime;
checkAndIrrigate();
}}
}}
void checkAndIrrigate() {{
Serial.println("\\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
Serial.println("SENSOR READING");
Serial.println("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
// Read sensors
float soilMoisture = readSoilMoisture();
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
float waterLevel = readWaterLevel();
// Handle DHT sensor errors
if (isnan(temperature) || isnan(humidity)) {{
Serial.println("[ERROR] DHT sensor failed to read!");
temperature = 25.0;
humidity = 70.0;
Serial.println("[INFO] Using default values");
}}
// Display readings
Serial.println("\\nSensor Values:");
Serial.print(" Soil Moisture: ");
Serial.print(soilMoisture, 1);
Serial.println("%");
Serial.print(" Temperature: ");
Serial.print(temperature, 1);
Serial.println(" °C");
Serial.print(" Humidity: ");
Serial.print(humidity, 1);
Serial.println("%");
Serial.print(" Water Level: ");
Serial.print(waterLevel, 1);
Serial.println("%");
// Check water availability
if (waterLevel < MIN_WATER_LEVEL) {{
Serial.println("\\n[WARNING] Water level too low for irrigation!");
Serial.println("[ACTION] Please refill water tank");
blinkLED(3, 200);
return;
}}
// ML Prediction
Serial.println("\\nML Prediction:");
int needsIrrigation = predict_irrigation(soilMoisture, temperature, humidity, waterLevel);
float probability = predict_irrigation_probability(soilMoisture, temperature, humidity, waterLevel);
Serial.print(" Decision: ");
Serial.println(needsIrrigation ? "IRRIGATE" : "NO ACTION");
Serial.print(" Confidence: ");
Serial.print(probability * 100, 1);
Serial.println("%");
// Take action
if (needsIrrigation) {{
startIrrigation();
}} else {{
Serial.println("\\n[STATUS] Conditions optimal, no irrigation needed");
}}
}}
float readSoilMoisture() {{
int raw = analogRead(SOIL_MOISTURE_PIN);
float percentage = map(raw, SOIL_DRY_VALUE, SOIL_WET_VALUE, 0, 100);
return constrain(percentage, 0, 100);
}}
float readWaterLevel() {{
int raw = analogRead(WATER_LEVEL_PIN);
float percentage = map(raw, WATER_EMPTY_VALUE, WATER_FULL_VALUE, 0, 100);
return constrain(percentage, 0, 100);
}}
void startIrrigation() {{
Serial.println("\\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
Serial.println("🌊 STARTING IRRIGATION");
Serial.println("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
Serial.print("Duration: ");
Serial.print(IRRIGATION_DURATION / 1000);
Serial.println(" seconds");
digitalWrite(PUMP_RELAY_PIN, HIGH);
digitalWrite(LED_PIN, HIGH);
isIrrigating = true;
irrigationStartTime = millis();
}}
void stopIrrigation() {{
Serial.println("\\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
Serial.println("🛑 STOPPING IRRIGATION");
Serial.println("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
digitalWrite(PUMP_RELAY_PIN, LOW);
digitalWrite(LED_PIN, LOW);
isIrrigating = false;
Serial.println("Irrigation cycle complete\\n");
}}
void blinkLED(int times, int delayMs) {{
for (int i = 0; i < times; i++) {{
digitalWrite(LED_PIN, HIGH);
delay(delayMs);
digitalWrite(LED_PIN, LOW);
delay(delayMs);
}}
}}
"""
with open('ESP32_Irrigation.ino', 'w', encoding='utf-8') as f:
f.write(arduino_sketch)
print("✓ Generated: ESP32_Irrigation.ino")
# Generate test sketch
test_sketch = """/*
* Test Suite for Enhanced Irrigation Model
* Tests various scenarios to verify model behavior
*/
#include "IrrigationModel.h"
void setup() {
Serial.begin(115200);
delay(1000);
Serial.println("\\n\\n");
Serial.println("========================================");
Serial.println("ENHANCED MODEL TEST SUITE");
Serial.println("12-Feature Random Forest");
Serial.println("========================================");
print_model_info();
Serial.println("\\nRunning test scenarios...");
Serial.println("========================================\\n");
// Test scenarios
testPrediction(15.0, 33.0, 60.0, 80.0, "Critical: Very dry + very hot");
testPrediction(20.0, 32.0, 62.0, 75.0, "Critical: Dry + hot + low humidity");
testPrediction(30.0, 26.0, 70.0, 60.0, "Moderate: Dry soil, normal weather");
testPrediction(35.0, 31.0, 61.0, 85.0, "Borderline: Hot + moderate dry");
testPrediction(45.0, 29.0, 64.0, 80.0, "Borderline: Warm + borderline dry");
testPrediction(50.0, 28.0, 68.0, 70.0, "Normal: Moderate conditions");
testPrediction(60.0, 25.0, 72.0, 50.0, "Good: Optimal moisture");
testPrediction(70.0, 24.0, 75.0, 40.0, "Good: High moisture");
testPrediction(85.0, 24.0, 75.0, 30.0, "Wet: Very high moisture");
testPrediction(25.0, 30.0, 65.0, 5.0, "Critical: Low water level");
testPrediction(20.0, 22.0, 78.0, 90.0, "Edge: Dry but cool & humid");
testPrediction(40.0, 32.0, 60.0, 95.0, "Edge: Hot & dry air but okay soil");
Serial.println("\\n========================================");
Serial.println("All tests completed!");
Serial.println("Review results above to verify behavior");
Serial.println("========================================\\n");
}
void loop() {
// Nothing in loop for test suite
}
void testPrediction(float soil, float temp, float hum, float water, String scenario) {
int prediction = predict_irrigation(soil, temp, hum, water);
float probability = predict_irrigation_probability(soil, temp, hum, water);
Serial.println("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
Serial.println("Test: " + scenario);
Serial.println("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
Serial.print("Inputs: Soil=");
Serial.print(soil, 1);
Serial.print("% | Temp=");
Serial.print(temp, 1);
Serial.print("°C | Humid=");
Serial.print(hum, 1);
Serial.print("% | Water=");
Serial.print(water, 1);
Serial.println("%");
Serial.print("Result: ");
Serial.print(prediction ? "✓ IRRIGATE" : "○ NO ACTION");
Serial.print(" | Confidence: ");
Serial.print(probability * 100, 1);
Serial.println("%");
// Show some feature values for debugging
float features[N_FEATURES];
get_feature_values(soil, temp, hum, water, features);
Serial.print("Features: ET=");
Serial.print(features[FEAT_ET_RATE], 3);
Serial.print(" | Stress=");
Serial.print(features[FEAT_STRESS_INDEX], 2);
Serial.print(" | VPD=");
Serial.print(features[FEAT_VPD_INDEX], 2);
Serial.println();
Serial.println();
}
"""
with open('Test_Model.ino', 'w', encoding='utf-8') as f:
f.write(test_sketch)
print("✓ Generated: Test_Model.ino")
# Generate comprehensive README
readme = f"""# ESP32 S3 Smart Irrigation System
## Enhanced 12-Feature ML Model
### 🎯 Outstanding Performance
```
Accuracy: {metadata['accuracy']*100:.2f}%
Balanced Accuracy: {metadata['balanced_accuracy']*100:.2f}%
CV Accuracy: {metadata['cv_mean']*100:.2f}% ± {metadata['cv_std']*100:.2f}%
Model Size: {metadata['model_size_kb']:.2f} KB
Memory Usage: {metadata['model_size_kb']/520*100:.1f}% of ESP32 SRAM
False Negatives: {metadata['false_negative_rate']*100:.1f}% (missed irrigation)
False Positives: {metadata['false_positive_rate']*100:.1f}% (over-watering)
```
### 🧠 Advanced Feature Engineering
This model uses **12 intelligent features** instead of just raw sensor data:
#### Base Sensors (4)
1. **Soil Moisture** - Capacitive sensor (0-100%)
2. **Temperature** - DHT11 sensor (°C)
3. **Humidity** - DHT11 sensor (%)
4. **Water Level** - Tank level sensor (0-100%)
#### Engineered Features (8)
5. **ET Rate** - Evapotranspiration calculation
6. **Soil-Temp Stress** - Combined dry soil + heat stress indicator
7. **Moisture Deficit** - Distance below optimal moisture (50%)
8. **Stress Index** - Multi-factor plant stress metric
9. **VPD Index** - Vapor Pressure Deficit (air "thirstiness")
10. **Temp-Humidity Ratio** - Air dryness indicator
11. **Critical Dry Flag** - Binary alert for very dry soil (<30%)
12. **Optimal Moisture Flag** - Binary indicator for ideal range (60-80%)
### 📊 Why This Approach Works
The engineered features capture **complex interactions** that raw sensors miss:
- **ET Rate** models water loss through evaporation and transpiration
- **VPD Index** captures how "thirsty" the air is
- **Stress Index** combines multiple stress factors
- **Critical flags** provide clear decision boundaries
Result: **{metadata['accuracy']*100:.2f}% accuracy** with only **{metadata['model_size_kb']:.2f} KB** of memory!
### 🔧 Hardware Requirements
#### Components
- **ESP32 S3 N16R8** Development Kit (520 KB SRAM)
- **Capacitive Soil Moisture Sensor** (analog)
- **DHT11** Temperature & Humidity Sensor
- **Water Level Sensor** (analog)
- **5V Relay Module** (for pump control)
- **Water Pump** (12V DC recommended)
- **Jumper Wires** and breadboard
#### Wiring Diagram
```
ESP32 Pin Component
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
GPIO 34 → Soil Moisture (Analog)
GPIO 4 → DHT11 Data Pin
GPIO 35 → Water Level (Analog)
GPIO 25 → Relay Control (IN)
3.3V → Sensors VCC
GND → Sensors GND
VIN (5V) → Relay VCC
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Relay Output:
COM → Pump +
NO → Power Supply +
Pump - → Power Supply -
```
### 🚀 Quick Start Guide
#### 1. Arduino IDE Setup
```bash
# Install ESP32 board support
File → Preferences → Additional Board URLs:
https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json
# Install libraries
Tools → Manage Libraries:
- DHT sensor library by Adafruit
- Adafruit Unified Sensor
```
#### 2. Test Without Hardware
```cpp
1. Open Test_Model.ino
2. Select: Tools → Board → ESP32 S3 Dev Module
3. Upload to ESP32
4. Open Serial Monitor (115200 baud)
5. Verify predictions make sense
```
Expected output for test cases:
- Very dry (20%) + hot (32°C) → **IRRIGATE**
- Good moisture (60%) → **NO ACTION**
- Low water (5%) → **NO ACTION** (safety)
#### 3. Deploy Full System
```cpp
1. Connect all sensors per wiring diagram
2. Place IrrigationModel.h and ESP32_Irrigation.ino in same folder
3. Open ESP32_Irrigation.ino
4. Upload to ESP32
5. Open Serial Monitor (115200 baud)
6. Calibrate sensors (see below)
```
### ⚙️ Sensor Calibration
#### Soil Moisture Sensor
```cpp
// Put sensor in DRY soil/air, read Serial Monitor:
// Note the ADC value shown
#define SOIL_DRY_VALUE 4095 // Your value here
// Put sensor completely in WATER, read Serial Monitor:
// Note the ADC value shown
#define SOIL_WET_VALUE 1500 // Your value here
```
#### Water Level Sensor
```cpp
// Empty tank, read Serial Monitor:
#define WATER_EMPTY_VALUE 4095 // Your value here
// Full tank, read Serial Monitor:
#define WATER_FULL_VALUE 1000 // Your value here
```
#### DHT11 Temperature Sensor
No calibration needed! The DHT11 is pre-calibrated.
Just ensure:
- Good connection on GPIO 4
- 10kΩ pull-up resistor on data line (many modules have this built-in)
### 🎛️ Configuration
Edit these values in `ESP32_Irrigation.ino`:
```cpp
// Timing
#define READING_INTERVAL 60000 // Check every 60 seconds
#define IRRIGATION_DURATION 30000 // Water for 30 seconds
// Safety
#define MIN_WATER_LEVEL 10 // Don't irrigate below 10%
```
### 📈 Feature Importance
The model prioritizes features in this order:
{chr(10).join([f"{i+1}. **{feat}** - {imp*100:.1f}%"
for i, (feat, imp) in enumerate(sorted(metadata['feature_importance'].items(),
key=lambda x: x[1], reverse=True)[:8])])}
### 🧪 Test Scenarios
The test suite validates these scenarios:
| Scenario | Soil | Temp | Humid | Water | Expected |
|----------|------|------|-------|-------|----------|
| Critical Dry & Hot | 15% | 33°C | 60% | 80% | IRRIGATE (high) |
| Dry + Hot + Low Humidity | 20% | 32°C | 62% | 75% | IRRIGATE (high) |
| Moderate Dry | 30% | 26°C | 70% | 60% | IRRIGATE (medium) |
| Borderline | 45% | 29°C | 64% | 80% | VARIABLE |
| Good Moisture | 60% | 25°C | 72% | 50% | NO ACTION |
| Wet Soil | 85% | 24°C | 75% | 40% | NO ACTION |
| Low Water Safety | 25% | 30°C | 65% | 5% | NO ACTION (safety) |
### 💾 Memory Usage
```
Total ESP32 S3 SRAM: 520 KB
───────────────────────────────────
Model Size: {metadata['model_size_kb']:.1f} KB ({metadata['model_size_kb']/520*100:.1f}%)
Arduino Core: ~60 KB
DHT Library: ~5 KB
Your Variables: ~5 KB
───────────────────────────────────
Available for Features: ~{520 - metadata['model_size_kb'] - 70:.0f} KB
Future-ready for:
✓ WiFi connectivity (~100 KB)
✓ OLED display (~20 KB)
✓ Data logging (~50 KB)
✓ OTA updates (~100 KB)
```
### 🐛 Troubleshooting
#### DHT11 Not Reading
```
Symptoms: "DHT sensor failed to read!" in Serial Monitor
Solutions:
✓ Check wiring: Data to GPIO 4, VCC to 3.3V, GND to GND
✓ Add 10kΩ pull-up resistor between Data and VCC
✓ Try different GPIO pin (update DHT_PIN)
✓ Ensure DHT11 gets stable 3.3V power
```
#### Wrong Predictions
```
Symptoms: Model irrigates when soil is wet, or doesn't irrigate when dry
Solutions:
✓ Calibrate sensors properly (see Calibration section)
✓ Check sensor readings in Serial Monitor
✓ Ensure soil sensor is inserted 2-3 inches deep
✓ Verify temperature/humidity readings are realistic
✓ Test with known conditions (dry soil in sun)
```
#### Pump Not Activating
```
Symptoms: Relay clicks but pump doesn't run
Solutions:
✓ Check pump power supply (12V DC, adequate current)
✓ Test relay separately: digitalWrite(PUMP_RELAY_PIN, HIGH)
✓ Verify relay trigger level (HIGH vs LOW active)
✓ Check relay COM/NO/NC connections
✓ Ensure pump is not burned out (test directly)
```
#### Random Restarts
```
Symptoms: ESP32 keeps resetting
Solutions:
✓ Use separate power supply for pump (don't use ESP32 power)
✓ Add capacitor (100µF) across ESP32 power pins
✓ Check for short circuits in wiring
✓ Ensure USB cable is good quality
✓ Use external 5V power adapter (>1A)
```
### 📊 Performance Metrics
```
Target Achieved Status
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Accuracy >90% {metadata['accuracy']*100:.2f}% {'✅' if metadata['accuracy'] >= 0.90 else '⚠️'}
Model Size <30KB {metadata['model_size_kb']:.2f} KB {'✅' if metadata['model_size_kb'] < 30 else '⚠️'}
False Negatives <10% {metadata['false_negative_rate']*100:.1f}% {'✅' if metadata['false_negative_rate'] < 0.10 else '⚠️'}
Balanced Accuracy >88% {metadata['balanced_accuracy']*100:.2f}% ✅
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
```
### 🔬 Model Architecture
```
Random Forest Classifier
├─ Trees: {metadata['configuration']['n_estimators']}
├─ Max Depth: {metadata['configuration']['max_depth']}
├─ Min Samples Leaf: {metadata['configuration']['min_samples_leaf']}
├─ Min Samples Split: {metadata['configuration']['min_samples_split']}
├─ Max Features: {metadata['configuration']['max_features']}
└─ Class Weight: balanced
Training Data: {metadata['training_samples']} samples
Test Data: {metadata['test_samples']} samples
Cross-Validation: 5-fold
```
### 🌐 Advanced Features (Optional)
#### WiFi Connectivity
```cpp
#include <WiFi.h>
// In setup():
WiFi.begin("YOUR_SSID", "YOUR_PASSWORD");
while (WiFi.status() != WL_CONNECTED) {{
delay(500);
Serial.print(".");
}}
Serial.println("\\nWiFi Connected!");
```
#### OTA (Over-The-Air) Updates
```cpp
#include <ArduinoOTA.h>
// In setup():
ArduinoOTA.setHostname("irrigation-system");
ArduinoOTA.begin();
// In loop():
ArduinoOTA.handle();
```
#### MQTT for IoT Integration
```cpp
#include <PubSubClient.h>
WiFiClient espClient;
PubSubClient mqtt(espClient);
// Publish sensor data
mqtt.publish("irrigation/soil", String(soilMoisture).c_str());
mqtt.publish("irrigation/action", needsIrrigation ? "ON" : "OFF");
```
#### Data Logging to SD Card
```cpp
#include <SD.h>
File logFile = SD.open("/irrigation_log.txt", FILE_APPEND);
logFile.printf("%lu,%f,%f,%f,%f,%d\\n",
millis(), soilMoisture, temperature, humidity, waterLevel, needsIrrigation);
logFile.close();
```
### 📈 Model Improvement
Want even better accuracy? Here's how to retrain with your data:
1. **Collect Real Data**
- Let system run for 2-4 weeks
- Log sensor readings and manual observations
- Note when irrigation was actually needed
2. **Label Your Data**
- Mark each data point: "needed irrigation" or "didn't need"
- Be honest about results
3. **Retrain Model**
```python
# Add your data to training dataset
# Run train_enhanced_model.py again
# Deploy new model
```
4. **Compare Results**
- Old accuracy vs new accuracy
- Fewer false negatives?
- Deploy if improved!
### 🎓 Understanding the Decisions
The model considers multiple factors simultaneously:
**Example 1: Dry but Cool**
- Soil: 30% (dry)
- Temp: 22°C (cool)
- Humidity: 75% (high)
- **Decision**: Probably NO - cool weather reduces evaporation
**Example 2: Moderate but Hot**
- Soil: 45% (moderate)
- Temp: 32°C (hot)
- Humidity: 60% (low)
- **Decision**: Probably YES - high evaporation stress
**Example 3: Critical Combination**
- Soil: 20% (very dry)
- Temp: 33°C (very hot)
- Humidity: 60% (low)
- **Decision**: Definitely YES - multiple stress factors
### 📁 File Structure
```
irrigation-system/
│
├── IrrigationModel.h # ML model (auto-generated)
├── ESP32_Irrigation.ino # Main Arduino sketch
├── Test_Model.ino # Test suite
├── README.md # This file
│
├── training_visuals/ # Training visualizations
│ ├── 01_model_comparison.png
│ ├── 02_confusion_matrix.png
│ ├── 03_roc_curve.png
│ ├── 04_precision_recall.png
│ ├── 05_feature_importance.png
│ ├── 06_learning_curves.png
│ ├── 07_correlation_matrix.png
│ ├── 08_feature_distributions.png
│ └── 09_final_summary.png
│
└── python_scripts/ # Training scripts (not needed for deployment)
├── generate_dataset.py
├── train_enhanced_model.py
└── deploy_enhanced.py
```
### 🔒 Safety Features
The system includes multiple safety mechanisms:
1. **Water Level Check**
- Won't irrigate if tank < 10%
- Prevents pump damage
2. **Duration Limit**
- Irrigation limited to 30 seconds
- Prevents over-watering
3. **Sensor Validation**
- Checks for NaN readings
- Uses safe defaults if sensor fails
4. **Visual Feedback**
- LED indicates system status
- Blinks on warnings
### 📝 Serial Monitor Output
Normal operation looks like this:
```
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
SENSOR READING
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Sensor Values:
Soil Moisture: 35.2%
Temperature: 28.5 °C
Humidity: 68.3%
Water Level: 72.1%
ML Prediction:
Decision: IRRIGATE
Confidence: 87.3%
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
🌊 STARTING IRRIGATION
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Duration: 30 seconds
```
### 🎯 Next Steps
1. ✅ **Test**: Upload Test_Model.ino, verify predictions
2. ✅ **Wire**: Connect sensors per wiring diagram
3. ✅ **Calibrate**: Adjust sensor calibration values
4. ✅ **Deploy**: Upload ESP32_Irrigation.ino
5. ✅ **Monitor**: Watch Serial Monitor for 24-48 hours
6. ✅ **Optimize**: Adjust IRRIGATION_DURATION if needed
7. 📊 **Log**: Record performance data
8. 🔄 **Iterate**: Retrain with real data after 1 month
### 🤝 Support
**Issues?** Check the troubleshooting section above.
**Want to contribute?**
- Collect real-world data
- Test in different climates
- Share your results!
### 📄 License
This is an educational project. Feel free to modify and use for your plants! 🌱
---