-
Notifications
You must be signed in to change notification settings - Fork 18
/
arduino_sample_sketch.ino
173 lines (148 loc) · 3.75 KB
/
arduino_sample_sketch.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
#include <SoftwareSerial.h>
#define SERIAL_RX_PIN 8
#define SERIAL_TX_PIN 9
#define EVENT_PIN 2
#define RESET_PIN 4
#define MY_AWS_IOT_ENDPOINT "<hash>-ats.iot.<region>.amazonaws.com"
typedef enum event_t {
EVENT_NONE = 0,
EVENT_STARTUP = 2,
EVENT_CONLOST,
EVENT_OVERRUN,
EVENT_OTA,
EVENT_SHADOW,
EVENT_CONFMODE,
} event_t;
typedef enum {
STATE_INIT = 0,
STATE_WAIT_EL_READY,
STATE_EL_READY,
STATE_UNPROVISIONED,
STATE_PROVISIONING,
STATE_PROVISIONED,
STATE_CONNECTED,
} state_t;
String execute_command(String command, unsigned long timeout_ms)
{
unsigned long saved_timeout_value_ms = Serial.getTimeout();
Serial.setTimeout(timeout_ms);
Serial.println(command);
String s = Serial.readStringUntil('\n');
s.trim();
Serial.setTimeout(saved_timeout_value_ms);
return s;
}
void expresslink_reset()
{
digitalWrite(RESET_PIN, LOW);
delay(200);
digitalWrite(RESET_PIN, HIGH);
}
event_t process_event()
{
int val = 0;
event_t event_number = EVENT_NONE;
String response;
val = digitalRead(EVENT_PIN);
if(val)
{
response = execute_command("AT+EVENT?", 3000);
if (response.equals("OK"))
{
return EVENT_NONE;
}
else {
char ok_string[3];
int topic_index;
int total_read;
total_read = sscanf(response.c_str(), "%s %d %d %*s" , ok_string, &event_number, &topic_index);
return event_number;
}
}
}
state_t process_ssid(String response)
{
int event_number = 0;
char ok_string[3];
char ssid_string[33] = {0};
int total_read;
total_read = sscanf(response.c_str(), "%s %s" , ok_string, ssid_string);
if(total_read > 1) {
return STATE_PROVISIONED;
}
else {
return STATE_UNPROVISIONED;
}
}
void setup()
{
SoftwareSerial mySerial(SERIAL_RX_PIN, SERIAL_TX_PIN);
Serial.begin(115200);
mySerial.begin(115200);
pinMode(EVENT_PIN, INPUT);
pinMode(RESET_PIN, OUTPUT);
while (!Serial) {
;
}
while (!mySerial) {
;
}
}
void loop()
{
event_t event = EVENT_NONE;
String response;
static state_t state = STATE_INIT;
if (state != STATE_INIT)
{
event = process_event();
}
switch (state) {
case STATE_INIT:
expresslink_reset();
state = STATE_WAIT_EL_READY;
break;
case STATE_WAIT_EL_READY:
if (event == EVENT_STARTUP) {
state = STATE_EL_READY;
}
break;
case STATE_EL_READY:
response = execute_command("AT+CONF Endpoint="MY_AWS_IOT_ENDPOINT"", 3000);
response = execute_command("AT+CONF Topic1=TEST", 3000);
response = execute_command("AT+CONF? SSID", 3000);
state = process_ssid(response);
break;
case STATE_UNPROVISIONED:
response = execute_command("AT+CONFMODE", 5000);
if (response.equals("OK CONFMODE ENABLED"))
{
state = STATE_PROVISIONING;
}
break;
case STATE_PROVISIONING:
if (event == EVENT_CONFMODE) {
state = STATE_PROVISIONED;
}
break;
case STATE_PROVISIONED:
response = execute_command("AT+CONNECT", 30000);
if (response.equals("OK 1 CONNECTED")) {
state = STATE_CONNECTED;
}
break;
case STATE_CONNECTED:
if (event == EVENT_CONLOST)
{
state = STATE_PROVISIONED;
break;
}
static unsigned long last_send_time;
if (millis() - last_send_time >= 10000)
{
response = execute_command("AT+SEND1 Hello World", 5000);
last_send_time = millis();
}
break;
}
}