G

aaaa

public
Guest May 24, 2024 Never 14
Clone
Plaintext suhuayaq 112 lines (86 loc) | 3.01 KB
1
/*********
2
Rui Santos
3
Complete project details at https://randomnerdtutorials.com
4
Based on the Dallas Temperature Library example
5
*********/
6
#define BLYNK_TEMPLATE_ID "TMPL67b1saj85"
7
#define BLYNK_TEMPLATE_NAME "Quickstart Template"
8
#define BLYNK_AUTH_TOKEN "E3hllXhUcQB5UZ_73det2waSt526ZMaV"
9
10
#include <OneWire.h>
11
#include <DallasTemperature.h>
12
#include <WiFi.h>
13
#include <BlynkSimpleEsp32.h>
14
#include <WiFiClient.h>
15
#include <Wire.h>
16
#include <LiquidCrystal_I2C.h>
17
18
// Data wire is connected to the Arduino digital pin 4
19
#define ONE_WIRE_BUS 13
20
int trigPin = 4; // TRIG pin
21
int echoPin = 2; // ECHO pin
22
23
// You should get Auth Token in the Blynk App.
24
// Go to the Project Settings (nut icon).
25
26
char auth[] = BLYNK_AUTH_TOKEN;
27
28
// Your WiFi credentials.
29
char ssid[] = "Yong 2-4-2";
30
char pass[] = "242Yong!!!";
31
32
float duration_us, distance_cm;
33
34
// Setup a oneWire instance to communicate with any OneWire devices
35
OneWire oneWire(ONE_WIRE_BUS);
36
37
// Pass our oneWire reference to Dallas Temperature sensor
38
DallasTemperature sensors(&oneWire);
39
40
// I2C LCD setup
41
LiquidCrystal_I2C lcd(0x27, 20, 4); // Address 0x27, 20 chars, 4 lines
42
43
void setup(void) {
44
// Start serial communication for debugging purposes
45
Serial.begin(115200);
46
47
// Start up the library
48
sensors.begin();
49
50
// Initialize Blynk
51
Blynk.begin(auth, ssid, pass);
52
53
// Initialize I2C LCD
54
lcd.init(); // Initialize the LCD
55
lcd.backlight();
56
57
// Configure the trigger pin to output mode
58
pinMode(trigPin, OUTPUT);
59
// Configure the echo pin to input mode
60
pinMode(echoPin, INPUT);
61
}
62
63
void loop(void) {
64
Blynk.run();
65
66
// Call sensors.requestTemperatures() to issue a global temperature request to all devices on the bus
67
sensors.requestTemperatures();
68
69
float tempC = sensors.getTempCByIndex(0);
70
float tempF = sensors.getTempFByIndex(0);
71
72
Serial.print("Celsius temperature: ");
73
Serial.print(tempC);
74
Serial.print(" - Fahrenheit temperature: ");
75
Serial.println(tempF);
76
77
// Generate 10-microsecond pulse to TRIG pin
78
digitalWrite(trigPin, HIGH);
79
delayMicroseconds(10);
80
digitalWrite(trigPin, LOW);
81
82
// Measure duration of pulse from ECHO pin
83
duration_us = pulseIn(echoPin, HIGH);
84
85
// Calculate the distance
86
distance_cm = 0.017 * duration_us;
87
88
// Print the value to Serial Monitor
89
Serial.print("Distance: ");
90
Serial.print(distance_cm);
91
Serial.println(" cm");
92
93
// Map sensor value to percentage (adjust according to your sensor)
94
int waterLevelPercent = map(distance_cm, 0, 400, 0, 100); // Adjust the maximum value according to your setup
95
96
// Send water level percentage to Blynk
97
Blynk.virtualWrite(V0, waterLevelPercent);
98
Blynk.virtualWrite(V1, tempC);
99
100
// Display water level and temperature on LCD
101
lcd.clear();
102
lcd.setCursor(0, 0);
103
lcd.print("Water Level: ");
104
lcd.print(waterLevelPercent);
105
lcd.print("%");
106
lcd.setCursor(0, 1);
107
lcd.print("Temp: ");
108
lcd.print(tempC);
109
lcd.print(" C");
110
111
delay(1000);
112
}