-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
70 lines (59 loc) · 1.92 KB
/
main.cpp
File metadata and controls
70 lines (59 loc) · 1.92 KB
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
#include <Arduino.h>
#include <WiFi.h>
#include <PubSubClient.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
#include <Adafruit_NeoPixel.h>
// --- CONFIG ---
const char* ssid = "bla bla"; //The values here are changed for privacy reasons
const char* password = "090900//hahaha";
const char* mqtt_server = "12.uuccuu";
#define PIN_RGB 48
Adafruit_NeoPixel pixels(1, PIN_RGB, NEO_GRB + NEO_KHZ800);
Adafruit_MPU6050 mpu;
WiFiClient espClient;
PubSubClient client(espClient);
void setup() {
Serial.begin(115200);
pixels.begin();
pixels.setBrightness(30);
// Status: Purple (Initializing)
pixels.setPixelColor(0, pixels.Color(150, 0, 255));
pixels.show();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); }
Serial.println("\nWiFi Connected!");
client.setServer(mqtt_server, 1883);
Wire.begin(8,9); // SDA, SCL pins for MPU6050
if (!mpu.begin()) {
Serial.println("Failed to find MPU6050 chip");
pixels.setPixelColor(0, pixels.Color(255, 0, 0)); // Red (Error)
pixels.show();
while (1) { delay(10); }
}
// Status: Green (All Systems Go)
pixels.setPixelColor(0, pixels.Color(0, 255, 0));
pixels.show();
}
void reconnect() {
while (!client.connected()) {
if (client.connect("ESP32_Vibration_Client")) {
Serial.println("MQTT Connected!");
} else {
delay(5000);
}
}
}
void loop() {
if (!client.connected()) reconnect();
client.loop();
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
String payload = "{\"x\":" + String(a.acceleration.x) +
",\"y\":" + String(a.acceleration.y) +
",\"z\":" + String(a.acceleration.z) + "}";
client.publish("motor/vibration", payload.c_str());
Serial.println("Sent: " + payload);
delay(100);
}