-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprj_ESP.ino
331 lines (278 loc) · 9.02 KB
/
prj_ESP.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#include <EEPROM.h>
#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <MQTTClient.h>
ESP8266WebServer AP_server(80);
WiFiClient wificlient;
MQTTClient mqtt_client;
#include <Wire.h>
#define i2c_SDA 14 //D5 I2C data
#define i2c_SDL 12 //D6 I2C clock
#include <Servo.h>
Servo myservo;
//#define GPIO_I2C_ADDRESS 0x21 //100
#define MPU6050_I2C_ADDRESS 0x68 //AN0=0
//#define MINI_I2C_ADDRESS 0x08 //AN0=0
int EEEPROM_length = 512;
char WIFI_ssid[32];
char WIFI_password[32];
int wifi_mode = 1;
char CLASSROOM_code[32];
char Sensor_list[64];
String classroom_id; //for changing the topic of mqtt
String mqtt_topic;
char mqtt_id[30];
bool AP_flag = false; //if true, go into AP mode
bool AP_psw_updated = false; //after user updated the WIFI password from AP, change this flag;
bool mqtt_flag = false; //if true, in the normal mode, mqtt client keeps listenting
int mqtt_try_set = 10;
int WIFI_try_set = 30;
int temp_led_count = 1;
int Motor_status = 0;
int analog_buffer[4] = {0, 0, 0, 0};
int sensor_status[4] = {0, 0, 0, 0};
#define interrupt_pin 13 //D7
#define Echo_trig 5 //D1
#define Echo_receive 16 //D0
#define ext1_pin 4 //D2
#define ext2_pin 0 //D3
#include "wifi_hotspot.h"
#include "LED_ring.h"
#include "buzzer.h"
#include "OLED.h"
#include "mqtt_received.h"
#include "mqtt_autosend.h"
//#include "RGB.h"
//#include "compass.h"
//___________________________________________________________________________________________________________
void setup() {
//ESP.wdtDisable();
pinMode(interrupt_pin, OUTPUT);//D7 as interrupt trigger
digitalWrite(interrupt_pin, LOW);
Serial.begin(115200);
Wire.begin(i2c_SDA, i2c_SDL); //begin(sda,scl) D5/D6
Wire.setClock(400000L);//test result is 400K
//Wire.setClock(100000);//test result is 100K
oled_init();
led_ring_init();
buzzer_init();
char EEPROM_string[512];
EEPROM.begin(512);
for (int temp_j = 0; temp_j < 512; temp_j++) {
EEPROM_string[temp_j] = EEPROM.read(temp_j);
}
Serial.println(EEPROM_string);
StaticJsonBuffer<512> json_EEPROM;
JsonObject& root_EEPROM = json_EEPROM.parseObject(EEPROM_string);
if (root_EEPROM.success()) {
if (!root_EEPROM.containsKey("ws") || !root_EEPROM.containsKey("wp") || !root_EEPROM.containsKey("wm") || !root_EEPROM.containsKey("ss")) {
reset_EEPROM();
}
else {
const char* eeprom_ssid = root_EEPROM["ws"];
const char* eeprom_psw = root_EEPROM["wp"];
wifi_mode = root_EEPROM["wm"];
sensor_status[0] = root_EEPROM["ss"][0];
sensor_status[1] = root_EEPROM["ss"][1];
sensor_status[2] = root_EEPROM["ss"][2];
sensor_status[3] = root_EEPROM["ss"][3];
strncpy(WIFI_ssid, eeprom_ssid, strlen(eeprom_ssid));
strncpy(WIFI_password, eeprom_psw, strlen(eeprom_psw));
}
}
else reset_EEPROM();
if (wifi_mode == 2) {
WiFi.mode(WIFI_AP_STA);
Serial.println("mode 2");
//WIFI_search(); //generate a list of avaliable WIFI SSID (do not try to connect)
delay(1000);
WIFI_AP_init();
}
if (wifi_mode == 1) {
Serial.println("mode 1");
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_ssid, WIFI_password);
int wificonnect_count = 0;
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
wificonnect_count++;
if (wificonnect_count >= WIFI_try_set) {
eeprom_default = "{\"ws\":\"" + String(WIFI_ssid) + "\",\"wp\":\"" + String(WIFI_password) + "\",\"wm\":2,\"ss\":[1,1,1,1]}";
reset_EEPROM();
Serial.println("ESP will reset");
ESP.restart();//soft restart and enter AP mode next time "wm"=2
//break;
}
}
mqtt_connect();
}
/*
//___________________________________________________________________________________________________________
if (sensor_status[0] == 1) {
pinMode(Echo_trig, OUTPUT);
pinMode(Echo_receive, INPUT);
}
if (sensor_status[2] == 1) {
pinMode(ext1_pin, OUTPUT);//D2
digitalWrite(ext1_pin, LOW);
}
if (sensor_status[3] == 1) {
}
*/
/*
//___________________________________________________________________________________________________________
WiFi.mode(WIFI_STA);
String mqtt_id_str = "robot/" + WiFi.macAddress();
mqtt_id_str.toCharArray(mqtt_id, 30);
//___________________________________________________________________________________________________________
WiFi.begin(WIFI_ssid, WIFI_password); //use the ssid stored in the EEPROM
int wificonnect_count = 0;
while (WiFi.status() != WL_CONNECTED) {
temp_led_count++;
Serial.print("x");
delay(500);
if (wificonnect_count == WIFI_try_set) {
// very important, about the timeout length, may have some problems in some worse condition
AP_flag = true; //flag to turn on AP mode in LOOP;
WIFI_search(); //generate a list of avaliable WIFI SSID (do not try to connect)
delay(1000);
WiFi.mode(WIFI_AP_STA);
delay(1000);
break;
}
wificonnect_count++;
AP_flag = false; //continue to skip AP mode in LOOP;
}
if (WiFi.status() == WL_CONNECTED) {
mqtt_connect();
}
//___________________________________________________________________________________________________________
if (AP_flag) {
WIFI_AP_init();
}
*/
}
void loop() {
if (!mqtt_flag) {
if (wifi_mode == 2) {
AP_server.handleClient();
}
}
else {
if (wifi_mode == 1) {
mqtt_client.loop();
if (!wificlient.connect("139.59.233.107", 9001)) {
Serial.println("Connection failed.");
}
}
}
}
/*
if (wificlient.connect("mqtt.3dprintingsystems.com", 9003)) {
Serial.println("Connected");
Serial.println(WIFI_ssid);
Serial.println(WIFI_password);
} else {
Serial.println("Connection failed.");
}
*/
/*
if (Serial.available()) process_line();
//___________________________________________________________________________________________________________
if (AP_flag) {
AP_server.handleClient();
//one problem is after upload, AP_flag will become to be false, can the updated webpage be displayed?
delay(10);
AP_server.handleClient();
delay(10);
AP_server.handleClient();
delay(10);
if (AP_psw_updated) {
//after user updated the WIFI password from AP, change this flag;
AP_flag = false;
mqtt_flag = false;
}
}
//___________________________________________________________________________________________________________
if (!AP_flag && WiFi.status() != WL_CONNECTED) {
WiFi.mode(WIFI_STA);
delay(1000);
WiFi.begin(WIFI_ssid, WIFI_password);
int wificonnect_count = 0;
temp_led_count = 1;
while (WiFi.status() != WL_CONNECTED) {
temp_led_count++;
delay(1000);
wificonnect_count++;
if (wificonnect_count == WIFI_try_set) {
delay(500);
ESP.restart();//soft restart
break;
}
}
mqtt_connect();
}
if (mqtt_flag) {
mqtt_client.loop(); //in the normal mode, mqtt always keeps working;
mqtt_autosend();
}
*/
}
void mqtt_connect() {
mqtt_client.begin("139.59.233.107", 1886, wificlient);
int mqttconnect_count = 0;
while (!mqtt_client.connected()) {
mqtt_client.connect(mqtt_id, "try", "try");
//Serial.print("*");
delay(500);
if (mqttconnect_count == mqtt_try_set) {
//Serial.println("MQTT timeout! restart ROBOT!");
break;
}
mqttconnect_count++;
}
String mac_string = "robot/" + WiFi.macAddress();
if (mqtt_client.connected()) {
mqtt_client.subscribe(String("to/robot/" + WiFi.macAddress())); //**********************************************************subscribe 2
//mqtt_topic = "classroom/" + String(CLASSROOM_code) + "/robot/" + WiFi.macAddress();
mqtt_topic = "robot/" + WiFi.macAddress();
mqtt_client.publish(mqtt_topic, "{\"message\":\"classroom_request\"}");
delay(1000);
mqtt_flag = true;
}
}
void process_line() {
String t = Serial.readString();
//analog_buffer=[0,0,0,0];
char json_serial[128];
t.toCharArray(json_serial, 128);
StaticJsonBuffer<128> json_serial_Buffer;
JsonObject& root_serial = json_serial_Buffer.parseObject(json_serial);
if (root_serial.success()) {
//Serial.println("parseObject() OK!");
if (root_serial.containsKey("mt") ) {
//{"mt":2}
int motor_finish = root_serial["mt"];
if (motor_finish == 2) {
if (Motor_status == 1) {
mqtt_client.publish(mqtt_topic, "{\"step\":1}");
Motor_status = 0;
}
if (Motor_status == 2) {
mqtt_client.publish(mqtt_topic, "{\"step\":2}");
Motor_status = 0;
}
}
}
if (root_serial.containsKey("ag1") && root_serial.containsKey("ag2")) {
//{"ag1":2,"ag2":222}
analog_buffer[1] = root_serial["ag1"];
analog_buffer[2] = root_serial["ag2"];
analog_buffer[0] = analogRead(A0);
//mqtt_client.publish(mqtt_topic, "{\"step\":2}");
mqtt_client.publish(mqtt_topic, "{\"analog\":[" + String(analog_buffer[0]) + "," + String(analog_buffer[1]) + "," + String(analog_buffer[2]) + "," + String(analog_buffer[3]) + "]}");
}
}
}