forked from easytarget/esp32-cam-webserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvsc.cpp
171 lines (144 loc) · 4.47 KB
/
vsc.cpp
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
#include <HTTPClient.h>
#ifdef NO_OTA
#include <ESP32httpUpdate.h>
#define FIRMWARE_FOLDER "http://192.168.108.43:8080/win/winweb/espfw/esp32-cam/"
#define FIRMWARE_FILE "esp32_cam_last.bin"
bool need_update = false;
#endif
#define SWITCH_PIN 2
#define SWITCH_WAIT 300
#define RELAY_PIN 15
int relay_on = 0;
bool switcher_revert = false;
int switcher_wait = 300;
#include <DHT.h>
#define DHT_PIN 13
DHT dht11(DHT_PIN, DHT11);
DHT dht21(DHT_PIN, DHT21);
int8_t dht_type = 0; // 0 - None, 1 - dht 11, 2 - dht 21
int dht_interval = 0;
bool is_dht_inited = false;
float humidity, temp; // Values read from sensor
unsigned long dht_prevMs = 0;
unsigned long dht_curMs = 0;
#define TB_SERVER "192.168.108.43:8081"
/* Auto rename if last part of IP address is in the list */
/* Instead of ___ at the end of CAM_NAME will appear index 1,2,3...15... from this list */
#include "vsc.h"
int camera_data_index = -1;
#ifdef NO_OTA
void update_fw(void) {
Serial.println("Updating with " FIRMWARE_FILE "...");
//
stop_httpds();
t_httpUpdate_return ret = ESPhttpUpdate.update(FIRMWARE_FOLDER FIRMWARE_FILE);
switch (ret) {
case HTTP_UPDATE_FAILED:
Serial.printf("HTTP_UPDATE_FAILED Error (%d): %s\n", ESPhttpUpdate.getLastError(), ESPhttpUpdate.getLastErrorString().c_str());
need_update = false;
break;
case HTTP_UPDATE_NO_UPDATES:
Serial.println("HTTP_UPDATE_NO_UPDATES");
break;
case HTTP_UPDATE_OK:
Serial.println("HTTP_UPDATE_OK");
break;
}
}
#endif
void relay(int8_t value) {
pinMode(RELAY_PIN, OUTPUT);
if(value == -1)
relay_on = abs(relay_on - 1);
else
relay_on = value;
if (relay_on > 0)
relay_on = 1;
else if (relay_on < 0)
relay_on = 0;
digitalWrite(RELAY_PIN, relay_on);
}
void switcher(void) {
int L0 = 0;
int L1 = 1;
if (switcher_revert) {
L0 = 1;
L1 = 0;
}
pinMode(SWITCH_PIN, OUTPUT);
digitalWrite(SWITCH_PIN, L1);
if (switcher_wait > 0) {
delay(switcher_wait);
digitalWrite(SWITCH_PIN, L0);
}
}
unsigned long previousMillis = 0; // will store last temp was read
const long interval = 2000; // interval at which to read sensor
void gettemperature(void) {
if (dht_type) {
if (!is_dht_inited){
switch (dht_type) {
case 1: dht11.begin();
break;
case 2: dht21.begin();
break;
}
is_dht_inited = true;
delay(1000);
}
// Wait at least 2 seconds seconds between measurements.
// if the difference between the current time and last time you read
// the sensor is bigger than the interval you set, read the sensor
// Works better than delay for things happening elsewhere also
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you read the sensor
previousMillis = currentMillis;
// Reading temperature for humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (it's a very slow sensor)
// Check if any reads failed and exit early (to try again).
for (int i = 0; i < 10; i++) {
switch (dht_type) {
case 1: humidity = dht11.readHumidity();
temp = dht11.readTemperature(false);
break;
case 2: humidity = dht21.readHumidity();
temp = dht21.readTemperature(false);
break;
}
if (isnan(humidity) || isnan(temp) || temp > 1000 || humidity > 1000) {
delay(500);
} else {
return;
}
}
}
humidity = 0;
temp = 0;
}
}
char tb_url[128] = "";
void handleThingsBoard(void) {
if (dht_type && camera_data_index >= 0 && dht_interval) {
dht_curMs = millis();
if (dht_prevMs == 0 || dht_curMs - dht_prevMs >= dht_interval * 1000) {
dht_prevMs = dht_curMs;
gettemperature();
char buf[128];
HTTPClient http;
if (strlen(tb_url) == 0)
sprintf(tb_url, "http://" TB_SERVER "/api/v1/%s/telemetry", camera_datas[camera_data_index].token);
http.begin(tb_url);
http.addHeader("Content-Type", "application/json");
sprintf(buf, "{\"temperature\": %.1f, \"humidity\": %.0f}", temp, humidity);
int httpCode = http.POST(buf);
if (httpCode != 200) {
Serial.println(tb_url);
Serial.println(buf);
Serial.print("HTTP Response code is: ");
Serial.println(httpCode);
}
http.end();
}
}
}