-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSender-ESP
99 lines (78 loc) · 2.83 KB
/
Sender-ESP
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
#include <ArduinoIoTCloud.h>
#include <Arduino_ConnectionHandler.h>
#include <WiFiClientSecure.h>
#include <ESP8266WiFi.h>
const char DEVICE_LOGIN_NAME[] = "8d0e5e21-90d2-474d-b875-5b2f7d15b96a";
//Change the SSID PASS DEVICE_KEY with your credentials.
const char SSID[] = SECRET_SSID; // Network SSID (name)
const char PASS[] = SECRET_OPTIONAL_PASS; // Network password (use for WPA, or use as key for WEP)
const char DEVICE_KEY[] = SECRET_DEVICE_KEY; // Secret device password
void onWaterLevelChange();
void onLightChange();
int waterLevel;
bool light;
void initProperties(){
ArduinoCloud.setBoardId(DEVICE_LOGIN_NAME);
ArduinoCloud.setSecretDeviceKey(DEVICE_KEY);
ArduinoCloud.addProperty(waterLevel, READWRITE, ON_CHANGE, onWaterLevelChange);
ArduinoCloud.addProperty(light, READWRITE, ON_CHANGE, onLightChange);
}
WiFiConnectionHandler ArduinoIoTPreferredConnection(SSID, PASS);
#define trigger_pin D5
#define Echo_pin D6
long duration;
int distance;
void setup() {
pinMode(trigger_pin, OUTPUT);
pinMode(Echo_pin, INPUT);
Serial.begin(115200);
// This delay gives the chance to wait for a Serial Monitor without blocking if none is found
delay(1500);
// Defined in thingProperties.h
initProperties();
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
/*
The following function allows you to obtain more information
related to the state of network and IoT Cloud connection and errors
the higher number the more granular information you’ll get.
The default is 0 (only errors).
Maximum is 4
*/
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
}
void loop() {
ArduinoCloud.update();
// Your code here
digitalWrite(trigger_pin, LOW); //set trigger signal low for 2us
delayMicroseconds(2);
digitalWrite(trigger_pin, HIGH); // make trigger pin active high
delayMicroseconds(10); // wait for 10 microseconds
digitalWrite(trigger_pin, LOW); // make trigger pin active low
duration = pulseIn(Echo_pin, HIGH); // save time duration value in "duration variable
distance= duration*0.034/2; //Convert pulse duration into distance
Serial.println("Distance: ");
Serial.print(distance);
Serial.println(" cm");
//int dist = (distance/86) * 100;
waterLevel = 100 - ((distance* 100)/100);
delay(1000);
}
/*
Since WaterLevel is READ_WRITE variable, onWaterLevelChange() is
executed every time a new value is received from IoT Cloud.
*/
void onWaterLevelChange() {
// Add your code here to act upon WaterLevel change
}
/*
Since LIGHT is READ_WRITE variable, onLIGHTChange() is
executed every time a new value is received from IoT Cloud.
*/
void onLIGHTChange() {
// Add your code here to act upon LIGHT change
}
void onLightChange() {
// Add your code here to act upon Light change
}