-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsinric_board3.ino
165 lines (136 loc) · 4.75 KB
/
sinric_board3.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
* If you encounter any issues:
* - check the readme.md at https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md
* - ensure all dependent libraries are installed
* - see https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md#arduinoide
* - see https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md#dependencies
* - open serial monitor and check whats happening
* - check full user documentation at https://sinricpro.github.io/esp8266-esp32-sdk
* - visit https://github.com/sinricpro/esp8266-esp32-sdk/issues and check for existing issues or open a new one
*/
#include "DHT.h"
#define DHTPIN 2
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
// Uncomment the following line to enable serial debug output
#define ENABLE_DEBUG
#ifdef ENABLE_DEBUG
#define DEBUG_ESP_PORT Serial
#define NODEBUG_WEBSOCKETS
#define NDEBUG
#endif
#ifdef ESP8266
#include <ESP8266WiFi.h>
#endif
#ifdef ESP32
#include <WiFi.h>
#endif
//#include <Arduino.h>
#include "SinricPro.h"
#include "SinricProLight.h"
#define BLYNK_PRINT Serial
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
char auth[] = "Du0raXAM8d7badnevZeVo1o_OBY2fiD6";
#define WIFI_SSID "TNI_ROBOT_WIFI"
#define WIFI_PASS "tnieng406"
#define APP_KEY_3 "106b1eb0-a3c7-42f8-89d2-7f381b0243d1"
#define APP_SECRET_3 "5fa82026-a64d-4f10-b399-78658a385d4a-a1f86937-43cf-4c00-a9c3-c54cb68cd68b"
#define R_ID "60cddeae8cf8a303b93a0d0e" // Should look like "5dc1564130xxxxxxxxxxxxxx"
#define BD_ID "60cdded48cf8a303b93a0d12" // Should look like "5dc1564130xxxxxxxxxxxxxx"
#define BAUD_RATE 9600 // Change baudrate to your need
//#define BAUD_RATE 115200 // Change baudrate to your need
#define BD_LED 22
#define R_LED 23
DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;
bool onPowerState(const String &deviceId, bool &state) {
if(deviceId==R_ID){
// Serial.printf("Device %s power turned %s \r\n", deviceId.c_str(), state?"on":"off");
if(state){
digitalWrite(R_LED,HIGH);
Serial.println("RED LIGHT TURNED ON");
}
else{
digitalWrite(R_LED,LOW);
}
}
else if(deviceId==BD_ID){
// Serial.printf("Device %s power turned %s \r\n", deviceId.c_str(), state?"on":"off");
if(state){
digitalWrite(BD_LED,HIGH);
}
else{
digitalWrite(BD_LED,LOW);
}
}
return true; // request handled properly
}
void setupWiFi() {
Serial.printf("\r\n[Wifi]: Connecting");
WiFi.begin(WIFI_SSID, WIFI_PASS);
while (WiFi.status() != WL_CONNECTED) {
Serial.printf(".");
delay(250);
}
IPAddress localIP = WiFi.localIP();
// Serial.printf("connected!\r\n[WiFi]: IP-Address is %d.%d.%d.%d\r\n", localIP[0], localIP[1], localIP[2], localIP[3]);
Serial.println("connected!");
Serial.print("[WiFi]: IP-Address is ");
Serial.print(localIP[0]);
Serial.print(".");
Serial.print(localIP[1]);
Serial.print(".");
Serial.print(localIP[2]);
Serial.print(".");
Serial.print(localIP[3]);
}
void setupSinricPro() {
// get a new Light device from SinricPro
SinricProLight &myLight1 = SinricPro[R_ID];
SinricProLight &myLight2 = SinricPro[BD_ID];
// set callback function to RED LIGHT
myLight1.onPowerState(onPowerState);
// set callback function to RED LIGHT
myLight2.onPowerState(onPowerState);
// setup SinricPro
SinricPro.onConnected([](){ Serial.println("Connected to SinricPro"); });
SinricPro.onDisconnected([](){ Serial.println("Disconnected from SinricPro"); });
SinricPro.begin(APP_KEY_3, APP_SECRET_3);
}
void sendSensor()
{
float h = dht.readHumidity();
float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
if (isnan(h) || isnan(t))
{
Serial.println("Failed to read Data from sensors!");
return;
}
else
{
Serial.print(t);
Serial.println("ºC");
Serial.println("----------------------------------");
Serial.print(h);
Serial.println("----------------------------------");
Serial.println("ºC");
}
Blynk.virtualWrite(V0, t); // Virtual Pin V5 for Temprature
Blynk.virtualWrite(V1, h); // Virtual Pin V6 for Humidity
}
// main setup function
void setup() {
Serial.begin(BAUD_RATE); Serial.println("");
dht.begin();
pinMode(BD_LED,OUTPUT);
pinMode(R_LED,OUTPUT);
setupWiFi();
setupSinricPro();
timer.setInterval(1000, sendSensor);
Blynk.begin(auth, WIFI_SSID, WIFI_PASS);
}
void loop() {
SinricPro.handle();
Blynk.run();
timer.run();
}