/
Ivypots V1.4.0.ino
151 lines (93 loc) · 4.04 KB
/
Ivypots V1.4.0.ino
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
#define BLYNK_TEMPLATE_ID "TMPLx_bWw8Rh"
#define BLYNK_TEMPLATE_NAME "Ivypots"
#define BLYNK_AUTH_TOKEN "kPV_t8FGysQvF8stVPMp0UQpFDyGGX9h"
#define BLYNK_PRINT Serial
#include "DFRobot_EnvironmentalSensor.h"
#if defined(ARDUINO_AVR_UNO)||defined(ESP8266)
#include <SoftwareSerial.h>
#endif
#define MODESWITCH /*UART:*/0 /*I2C: 1*/
#if MODESWITCH
#if defined(ARDUINO_AVR_UNO)||defined(ESP8266)
SoftwareSerial mySerial(/*rx =*/4, /*tx =*/5);
DFRobot_EnvironmentalSensor environment(/*addr =*/SEN050X_DEFAULT_DEVICE_ADDRESS, /*s =*/&mySerial);//Create an object for broadcast address, which can configure all devices on the bus in batches
#else
DFRobot_EnvironmentalSensor environment(/*addr =*/SEN050X_DEFAULT_DEVICE_ADDRESS, /*s =*/&Serial1); //Create an object for broadcast address, which can configure all devices on the bus in batches
#endif
#else
DFRobot_EnvironmentalSensor environment(/*addr = */SEN050X_DEFAULT_DEVICE_ADDRESS, /*pWire = */&Wire);
#endif
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "SFR_1B80";
char pass[] = "dciz2qxg94rzgp5vbc85";
const int RelayPin = 2;
// Declaring a global variable for sensor data
int sensorData;
int sensorData2;
int sensorData3;
// This function creates the timer object. It's part of Blynk library
BlynkTimer timer;
void myTimer()
{
Blynk.virtualWrite(V5, sensorData);
Blynk.virtualWrite(V6, sensorData2);
Blynk.virtualWrite(V7, sensorData3);
Blynk.virtualWrite(V1, environment.getUltravioletIntensity());
Blynk.virtualWrite(V2, environment.getTemperature(TEMP_C));
Blynk.virtualWrite(V3, environment.getHumidity());
Blynk.virtualWrite(V4, environment.getLuminousIntensity());
Blynk.virtualWrite(V8, environment.getElevation());
}
void setup()
{
//Connecting to Blynk Cloud.
Blynk.begin(auth, ssid, pass);
// Setting interval to send data to Blynk Cloud to 1000ms.
// It means that data will be sent every second.
timer.setInterval(1000L, myTimer);
Serial.begin(115200);
Blynk.begin(auth, ssid, pass);
pinMode(RelayPin, OUTPUT);
#if defined(ARDUINO_AVR_UNO)||defined(ESP8266)
mySerial.begin(9600);
#elif defined(ESP32)
Serial1.begin(9600, SERIAL_8N1, /*rx =*/D3, /*tx =*/D2);
#else
Serial1.begin(9600);
#endif
}
void loop() {
Blynk.run();
timer.run();
sensorData2 = analogRead(A1); // this is an example of reading sensor data.
sensorData3 = analogRead(A2); // this is an example of reading sensor data.
sensorData = analogRead(A0); // this is an example of reading sensor data.
/*if (currentLine.endsWith("/GET")) {
//Read environmental data and print them on the web
client.print("Temp: "); client.print(environment.getTemperature(TEMP_C));client.println(" ℃");
client.print("Humidity: "); client.print(environment.getHumidity());client.println(" %");
client.print("Ultraviolet intensity: "); client.print(environment.getUltravioletIntensity());client.println(" mw/cm2");
client.print("LuminousIntensity: "); client.print(environment.getLuminousIntensity());client.println(" lx");
client.print("Atmospheric pressure: "); client.print(environment.getAtmospherePressure(HPA));client.println(" hpa");
client.print("Elevation: "); client.print(environment.getElevation());client.println(" m");*/
}
BLYNK_CONNECTED()
{
Blynk.syncVirtual(V0); // will cause BLYNK_WRITE(V0) to be executed
}
BLYNK_WRITE(V0) // Executes when the value of virtual pin 0 changes
{
if(param.asInt() == 1)
{
// execute this code if the switch widget is now ON
digitalWrite(RelayPin,HIGH); // Set digital RelayPin to HIGH
}
else
{
// execute this code if the switch widget is now OFF
digitalWrite(RelayPin,LOW); // Set digital RelayPin to LOW
}
}