#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include <WiFi.h>
#define AIO_SERVIDOR "io.adafruit.com"
#define AIO_PUERTOSERVIDOR 1883
#define AIO_USUARIO "oscar_martinez"
#define AIO_KEY "aio_tswB68ItKVGHr4c9c3l2OXC7z1HW"
Adafruit_MPU6050 mpu;
WiFiClient client;
Adafruit_MQTT_Client mqtt(&client, AIO_SERVIDOR, AIO_PUERTOSERVIDOR, AIO_USUARIO, AIO_KEY);
Adafruit_MQTT_Publish Tempertaura = Adafruit_MQTT_Publish(&mqtt, AIO_USUARIO"/feeds/temperatura");
Adafruit_MQTT_Publish X = Adafruit_MQTT_Publish(&mqtt, AIO_USUARIO"/feeds/x");
Adafruit_MQTT_Publish Y = Adafruit_MQTT_Publish(&mqtt, AIO_USUARIO"/feeds/y");
Adafruit_MQTT_Publish Z = Adafruit_MQTT_Publish(&mqtt, AIO_USUARIO"/feeds/z");
void setup() {
Serial.begin(115200);
//MPU6050 Connection
while (!mpu.begin()) {
Serial.println("MPU6050 not connected!");
delay(1000);
}
Serial.println("MPU6050 ready!");
//WiFi Connection
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println(" Connected!");
//MQTT Connection
mqtt.connect();
if (mqtt.connect()) {
Serial.println("Conectado a MQTT");
} else {
Serial.println("No conectado a MQTT");
}
}
sensors_event_t event1;
sensors_event_t event2;
void loop() {
mpu.getAccelerometerSensor()->getEvent(&event1);
mpu.getTemperatureSensor()->getEvent(&event2);
delay(3000);
Tempertaura.publish(event2.temperature);
Serial.print("Temperatura publicada");
X.publish(event1.acceleration.x);
Serial.print("Aceleracion X publicada");
Y.publish(event1.acceleration.y);
Serial.print("Aceleracion Y publicada");
Z.publish(event1.acceleration.z);
Serial.print("Aceleracion Z publicada");
}