-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
56 lines (43 loc) · 1.22 KB
/
main.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
#include <MPU9250_asukiaaa.h>
#include <WiFiClientSecure.h>
#include <PubSubClient.h>
#include <Wire.h>
#include <ArduinoJson.h>
#include "secrets.h"
MPU9250_asukiaaa mySensor;
WiFiClientSecure net;
PubSubClient client(net);
char payloadBuffer[65];
void connectAWS() {
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
// Configure WiFiClientSecure to use the AWS IoT device credentials
net.setCACert(AWS_CERT_CA);
net.setCertificate(AWS_CERT_CRT);
net.setPrivateKey(AWS_CERT_PRIVATE);
// Connect to the MQTT broker on the AWS endpoint we defined earlier
client.setServer(AWS_IOT_ENDPOINT, 8883);
while (!client.connect(THINGNAME)) {
delay(100);
}
}
void setup() {
digitalWrite(LED_BUILTIN,HIGH);
Wire.begin();
mySensor.setWire(&Wire);
mySensor.beginAccel();
connectAWS();
}
void loop() {
mySensor.accelUpdate();
StaticJsonDocument<65> doc;
doc.add(String(mySensor.accelX()));
doc.add(String(mySensor.accelY()));
doc.add(String(mySensor.accelZ()));
serializeJson(doc, payloadBuffer, sizeof(payloadBuffer));
client.publish("ur topic", payloadBuffer);
delay(1); // Shorter delay to publish data more frequently
}