-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaws_iot.c~
312 lines (269 loc) · 9.07 KB
/
aws_iot.c~
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
/*
* Derived from examples/mqtt_client/mqtt_client.c - added TLS1.2 support and some minor modifications.
*/
#include "espressif/esp_common.h"
#include "esp/uart.h"
#include <string.h>
#include <FreeRTOS.h>
#include <task.h>
#include <queue.h>
#include <ssid_config.h>
#include <espressif/esp_sta.h>
#include <espressif/esp_wifi.h>
#include <paho_mqtt_c/MQTTESP8266.h>
#include <paho_mqtt_c/MQTTClient.h>
#include "task.h"
#include "i2c/i2c.h"
#include "bmp280/bmp280.h"
#include "ssl_connection.h"
#define MQTT_PUB_TOPIC "esp8266/status"
#define MQTT_SUB_TOPIC "esp8266/control"
#define GPIO_LED 2
#define MQTT_PORT 8883
//bmp
#define PCF_ADDRESS 0x38
#define MPU_ADDRESS 0x68
#define BUS_I2C 0
#define SCL 14
#define SDA 12
typedef enum {
BMP280_TEMPERATURE, BMP280_PRESSURE
} bmp280_quantity;
bmp280_t bmp280_dev;
/* certs, key, and endpoint */
extern char *ca_cert, *client_endpoint, *client_cert, *client_key;
static int wifi_alive = 0;
static int ssl_reset;
static SSLConnection *ssl_conn;
static QueueHandle_t publish_queue;
float read_bmp280(bmp280_quantity quantity) {
float temperature, pressure;
bmp280_force_measurement(&bmp280_dev);
// wait for measurement to complete
while (bmp280_is_measuring(&bmp280_dev)) {
};
bmp280_read_float(&bmp280_dev, &temperature, &pressure, NULL);
if (quantity == BMP280_TEMPERATURE) {
return temperature;
} else if (quantity == BMP280_PRESSURE) {
return pressure;
}
return 0;
}
static void beat_task(void *pvParameters) {
char msg[64];
while (1) {
if (!wifi_alive) {
vTaskDelay(1000 / portTICK_PERIOD_MS);
continue;
}
printf("Schedule to publish\r\n");
float temp = read_bmp280(BMP280_TEMPERATURE);
snprintf(msg, sizeof(msg), "%.2f", temp);
if (xQueueSend(publish_queue, (void *) msg, 0) == pdFALSE) {
printf("Publish queue overflow\r\n");
}
vTaskDelay(pdMS_TO_TICKS(5000));
}
}
static void topic_received(mqtt_message_data_t *md) {
mqtt_message_t *message = md->message;
int i;
printf("Received: ");
for (i = 0; i < md->topic->lenstring.len; ++i)
printf("%c", md->topic->lenstring.data[i]);
printf(" = ");
for (i = 0; i < (int) message->payloadlen; ++i)
printf("%c", ((char *) (message->payload))[i]);
printf("\r\n");
if (!strncmp(message->payload, "on", 2)) {
printf("Turning on LED\r\n");
gpio_write(GPIO_LED, 0);
} else if (!strncmp(message->payload, "off", 3)) {
printf("Turning off LED\r\n");
gpio_write(GPIO_LED, 1);
}
}
static const char *get_my_id(void) {
// Use MAC address for Station as unique ID
static char my_id[13];
static bool my_id_done = false;
int8_t i;
uint8_t x;
if (my_id_done)
return my_id;
if (!sdk_wifi_get_macaddr(STATION_IF, (uint8_t *) my_id))
return NULL;
for (i = 5; i >= 0; --i) {
x = my_id[i] & 0x0F;
if (x > 9)
x += 7;
my_id[i * 2 + 1] = x + '0';
x = my_id[i] >> 4;
if (x > 9)
x += 7;
my_id[i * 2] = x + '0';
}
my_id[12] = '\0';
my_id_done = true;
return my_id;
}
static int mqtt_ssl_read(mqtt_network_t * n, unsigned char* buffer, int len,
int timeout_ms) {
int r = ssl_read(ssl_conn, buffer, len, timeout_ms);
if (r <= 0
&& (r != MBEDTLS_ERR_SSL_WANT_READ
&& r != MBEDTLS_ERR_SSL_WANT_WRITE
&& r != MBEDTLS_ERR_SSL_TIMEOUT)) {
printf("%s: TLS read error (%d), resetting\n\r", __func__, r);
ssl_reset = 1;
};
return r;
}
static int mqtt_ssl_write(mqtt_network_t* n, unsigned char* buffer, int len,
int timeout_ms) {
int r = ssl_write(ssl_conn, buffer, len, timeout_ms);
if (r <= 0
&& (r != MBEDTLS_ERR_SSL_WANT_READ
&& r != MBEDTLS_ERR_SSL_WANT_WRITE)) {
printf("%s: TLS write error (%d), resetting\n\r", __func__, r);
ssl_reset = 1;
}
return r;
}
static void mqtt_task(void *pvParameters) {
int ret = 0;
struct mqtt_network network;
mqtt_client_t client = mqtt_client_default;
char mqtt_client_id[20];
uint8_t mqtt_buf[100];
uint8_t mqtt_readbuf[100];
mqtt_packet_connect_data_t data = mqtt_packet_connect_data_initializer;
memset(mqtt_client_id, 0, sizeof(mqtt_client_id));
strcpy(mqtt_client_id, "ESP-");
strcat(mqtt_client_id, get_my_id());
ssl_conn = (SSLConnection *) malloc(sizeof(SSLConnection));
while (1) {
if (!wifi_alive) {
vTaskDelay(1000 / portTICK_PERIOD_MS);
continue;
}
printf("%s: started\n\r", __func__);
ssl_reset = 0;
ssl_init(ssl_conn);
ssl_conn->ca_cert_str = ca_cert;
ssl_conn->client_cert_str = client_cert;
ssl_conn->client_key_str = client_key;
mqtt_network_new(&network);
network.mqttread = mqtt_ssl_read;
network.mqttwrite = mqtt_ssl_write;
printf("%s: connecting to MQTT server %s ... ", __func__,
client_endpoint);
ret = ssl_connect(ssl_conn, client_endpoint, MQTT_PORT);
if (ret) {
printf("error: %d\n\r", ret);
ssl_destroy(ssl_conn);
continue;
}
printf("done\n\r");
mqtt_client_new(&client, &network, 5000, mqtt_buf, 100, mqtt_readbuf,
100);
data.willFlag = 0;
data.MQTTVersion = 4;
data.cleansession = 1;
data.clientID.cstring = mqtt_client_id;
data.username.cstring = NULL;
data.password.cstring = NULL;
data.keepAliveInterval = 1000;
printf("Send MQTT connect ... ");
ret = mqtt_connect(&client, &data);
if (ret) {
printf("error: %d\n\r", ret);
ssl_destroy(ssl_conn);
continue;
}
printf("done\r\n");
mqtt_subscribe(&client, MQTT_SUB_TOPIC, MQTT_QOS1, topic_received);
xQueueReset(publish_queue);
while (wifi_alive && !ssl_reset) {
char msg[64] = "\0";
while (xQueueReceive(publish_queue, (void *) msg, 0) == pdTRUE) {
TickType_t task_tick = xTaskGetTickCount();
uint32_t free_heap = xPortGetFreeHeapSize();
uint32_t free_stack = uxTaskGetStackHighWaterMark(NULL);
printf("Publishing: %s\r\n", msg);
mqtt_message_t message;
message.payload = msg;
message.payloadlen = strlen(msg);
message.dup = 0;
message.qos = MQTT_QOS1;
message.retained = 0;
ret = mqtt_publish(&client, MQTT_PUB_TOPIC, &message);
if (ret != MQTT_SUCCESS) {
printf("error while publishing message: %d\n", ret);
break;
}
}
ret = mqtt_yield(&client, 1000);
if (ret == MQTT_DISCONNECTED)
break;
}
printf("Connection dropped, request restart\n\r");
ssl_destroy(ssl_conn);
}
}
static void wifi_task(void *pvParameters) {
uint8_t status = 0;
uint8_t retries = 30;
struct sdk_station_config config = { .ssid = WIFI_SSID, .password =
WIFI_PASS, };
printf("%s: Connecting to WiFi\n\r", __func__);
sdk_wifi_set_opmode (STATION_MODE);
sdk_wifi_station_set_config(&config);
while (1) {
wifi_alive = 0;
while ((status != STATION_GOT_IP) && (retries)) {
status = sdk_wifi_station_get_connect_status();
printf("%s: status = %d\n\r", __func__, status);
if (status == STATION_WRONG_PASSWORD) {
printf("WiFi: wrong password\n\r");
break;
} else if (status == STATION_NO_AP_FOUND) {
printf("WiFi: AP not found\n\r");
break;
} else if (status == STATION_CONNECT_FAIL) {
printf("WiFi: connection failed\r\n");
break;
}
vTaskDelay(1000 / portTICK_PERIOD_MS);
--retries;
}
while ((status = sdk_wifi_station_get_connect_status())
== STATION_GOT_IP) {
if (wifi_alive == 0) {
printf("WiFi: Connected\n\r");
wifi_alive = 1;
}
vTaskDelay(500 / portTICK_PERIOD_MS);
}
wifi_alive = 0;
printf("WiFi: disconnected\n\r");
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
void user_init(void) {
uart_set_baud(0, 115200);
i2c_init(BUS_I2C, SCL, SDA, I2C_FREQ_100K);
bmp280_params_t params;
bmp280_init_default_params(¶ms);
params.mode = BMP280_MODE_FORCED;
bmp280_dev.i2c_dev.bus = BUS_I2C;
bmp280_dev.i2c_dev.addr = BMP280_I2C_ADDRESS_0;
bmp280_init(&bmp280_dev, ¶ms);
gpio_enable(GPIO_LED, GPIO_OUTPUT);
gpio_write(GPIO_LED, 1);
publish_queue = xQueueCreate(3, 16);
xTaskCreate(&wifi_task, "wifi_task", 256, NULL, 2, NULL);
xTaskCreate(&beat_task, "beat_task", 256, NULL, 2, NULL);
xTaskCreate(&mqtt_task, "mqtt_task", 2048, NULL, 2, NULL);
}