/
Example3_IO_Control.ino
230 lines (173 loc) · 5.82 KB
/
Example3_IO_Control.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
| Label | GPIO | Input | Output | Notes |
|-------|-------|--------------|------------|---------------------------------------|
| D0 | GPIO16| no interrupt | no PWM | HIGH at boot, used to wake from deep sleep
| D1 | GPIO5 | OK | OK | Often used as SCL (I2C)
| D2 | GPIO4 | OK | OK | Often used as SDA (I2C)
| D3 | GPIO0 | pulled up | OK | Connected to FLASH button, boot fails if pulled LOW |
| D4 | GPIO2 | pulled up | OK | HIGH at boot, connected to on-board LED, boot fails if pulled LOW
| D5 | GPIO14| OK | OK | SPI (SCLK)
| D6 | GPIO12| OK | OK | SPI (MISO)
| D7 | GPIO13| OK | OK | SPI (MOSI)
| D8 | GPIO15| pulled to GND| OK | SPI (CS), Boot fails if pulled HIGH
| RX | GPIO3 | OK | RX pin | HIGH at boot
| TX | GPIO1 | TX pin | OK | HIGH at boot, debug output at boot, boot fails if pulled LOW
| A0 | ADC0 | Analog Input | X | Analog input
*/
#include <ArduinoJson.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>
#include <detaBaseESP8266.h>
#include "certs.h"
const char* detaKey = "a0uhqqxxxx_xxxxxxxxxxxxxxxxxxxxxxxx";
const char* detaID = "a0uhqqxxxxx";
const char* detaBaseName = "LOG";
const char* ssid = "SSID";
const char* password = "PASSWORD";
unsigned long previousMillis = 0;
const long interval = 4000; // 4 seconds
detaBaseESP8266 detabase(detaKey, detaID, detaBaseName);
X509List cert(cert_DigiCert_Global_Root_CA);
bool onboardled;
int integer = 0;
String entryKey2 = "Esp8266 Output";
//Outputs
bool D1Value = LOW;
bool D2Value = LOW;
bool D3Value = LOW;
bool LEDValue = LOW;
bool D8Value = LOW;
//esp8266 restart
bool reset = false;
String entryKey1 = "Esp8266 Input";
//Inputs
bool D5Value = LOW;
bool D6Value = LOW;
bool D7Value = LOW;
int A0Value = 0;
//PUT request
void sendDetaSpaceInput() {
detabase.addData("key", entryKey1);
detabase.addData("D5", D5Value);
detabase.addData("D6", D6Value);
detabase.addData("D7", D7Value);
detabase.addData("A0", A0Value);
detabase.addData("0RESET", "RESTART");
bool success = detabase.sendData(); // send
if (success) {
Serial.println("Output Data added successfully!");
} else {
Serial.println("Failed to add Output data!");
}
}
void sendDetaSpaceOutput() {
detabase.addData("key", entryKey2);
detabase.addData("D1", D1Value);
detabase.addData("D2", D2Value);
detabase.addData("D3", D3Value);
detabase.addData("D8", D8Value);
detabase.addData("onLED", LEDValue);
detabase.addData("0RESET", reset);
bool success = detabase.sendData(); // send
if (success) {
Serial.println("Input Data added successfully!");
} else {
Serial.println("Failed to add Input data!");
}
}
void setup() {
Serial.begin(115200);
pinMode(2, OUTPUT);
pinMode(5 ,OUTPUT); // D1
pinMode(4 ,OUTPUT); // D2
pinMode(0 ,OUTPUT); // D3
pinMode(15,OUTPUT); // D8
pinMode(A0,INPUT); // A0
pinMode(14,INPUT); // D5
pinMode(12,INPUT); // D6
pinMode(13,INPUT); // D7
//RX
pinMode(3,INPUT); // RX
Serial.println("RX ="+String(digitalRead(3)));
configTime(3*3600, 0, "pool.ntp.org"); // For SSL, UTC+3 Turkey
WiFiClientSecure client;
client.setTrustAnchors(&cert);
delay(100);
digitalWrite(LED_BUILTIN, LOW); // Onboard LED ON
WiFi.begin(ssid, password);
Serial.print("Connecting to ");
Serial.print(ssid);
Serial.println("...");
int i = 0;
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(++i);
Serial.print(' ');
}
Serial.println("Connection Done");
digitalWrite(LED_BUILTIN, HIGH); // Onboard LED OFF
//Send First Customizations
sendDetaSpaceInput();
delay(1000);
sendDetaSpaceOutput();
}
void loop() {
unsigned long currentMillis = millis();
// Call the functions every 3 seconds
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
GetDetaSpace();
UpdateDetaSpace();
};
delay(1000);
}
//Void Get Output Pins
void GetDetaSpace() {
detabase.getItem(entryKey2);
reset = detabase.getBoolData("0RESET");
Serial.print("Reset = ");
Serial.println(reset);
if (reset) {
Serial.println("Resetting...");
ESP.restart();
}
D1Value = detabase.getBoolData("D1");
D2Value = detabase.getBoolData("D2");
D3Value = detabase.getBoolData("D3");
D8Value = detabase.getBoolData("D8");
LEDValue = detabase.getBoolData("onLED");
Serial.println("Output Pins...");
Serial.println("D1 = " + String(D1Value));
digitalWrite(5, D1Value);
Serial.println("D2 = " + String(D2Value));
digitalWrite(4, D2Value);
Serial.println("D3 = " + String(D3Value));
digitalWrite(0, D3Value);
Serial.println("D8 = " + String(D8Value));
digitalWrite(15, D8Value);
Serial.println("onLED = " + String(LEDValue));
digitalWrite(2, LEDValue);
}
//Void Update Input Pins
void UpdateDetaSpace() {
Serial.println("\nInput Pins...");
// print the readings in the Serial Monitor
D5Value = digitalRead(14);
Serial.println("D5 = " + String(D5Value));
D6Value = digitalRead(12);
Serial.println("D6 = " + String(D6Value));
D7Value = digitalRead(13);
Serial.println("D7 = " + String(D7Value));
A0Value = analogRead(A0);
Serial.println("A0 = " + String(A0Value));
detabase.UpdateSet("D5", D5Value);
detabase.UpdateSet("D6", D6Value);
detabase.UpdateSet("D7", D7Value);
detabase.UpdateSet("A0", A0Value);
bool success = detabase.sendUpdate(entryKey1); // send
if (success) {
Serial.println("Data Updated successfully!");
} else {
Serial.println("Failed to add data!");
}
}