#include <Wire.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <math.h>
// ===============================
// MPU6050 CONFIGURATION
// ===============================
Adafruit_MPU6050 mpu;
#define SDA_PIN 21
#define SCL_PIN 19
// Movement threshold
#define MOVEMENT_THRESHOLD 2.0
// ===============================
// WIFI CONFIGURATION
// ===============================
const char* WIFI_SSID = "Wokwi-GUEST";
const char* WIFI_PASSWORD = "";
// ===============================
// THINGSPEAK CONFIGURATION
// ===============================
// Use a NEW ThingSpeak Write API Key
const char* THINGSPEAK_API_KEY = "OA8MD2Q44FM9VB2Z";
// ===============================
// SETUP
// ===============================
void setup() {
Serial.begin(115200);
Serial.println();
Serial.println("================================");
Serial.println(" COLLAR MONITORING STARTED");
Serial.println("================================");
// -------------------------------
// Start I2C
// -------------------------------
Wire.begin(SDA_PIN, SCL_PIN);
// -------------------------------
// Start MPU6050
// -------------------------------
if (!mpu.begin()) {
Serial.println("ERROR: MPU6050 NOT FOUND!");
while (1) {
delay(10);
}
}
Serial.println("MPU6050 INITIALIZED");
// -------------------------------
// Configure MPU6050
// -------------------------------
mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
// -------------------------------
// Connect to WiFi
// -------------------------------
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("WiFi Connected!");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
Serial.println("--------------------------------");
}
// ===============================
// MAIN LOOP
// ===============================
void loop() {
// =================================
// 1. READ MPU6050
// =================================
sensors_event_t acceleration;
sensors_event_t gyro;
sensors_event_t temperature;
mpu.getEvent(
&acceleration,
&gyro,
&temperature
);
// =================================
// 2. CALCULATE MOVEMENT
// =================================
float magnitude = sqrt(
acceleration.acceleration.x *
acceleration.acceleration.x +
acceleration.acceleration.y *
acceleration.acceleration.y +
acceleration.acceleration.z *
acceleration.acceleration.z
);
// Remove approximately 1g of gravity
float movement = fabs(magnitude - 9.81);
// =================================
// 3. DETERMINE MOVEMENT STATUS
// =================================
int movementStatus;
String status;
if (movement > MOVEMENT_THRESHOLD) {
movementStatus = 1;
status = "MOVEMENT_DETECTED";
}
else {
movementStatus = 0;
status = "MOVEMENT_NOT_DETECTED";
}
// =================================
// 4. DISPLAY COLLAR DATA
// =================================
Serial.println();
Serial.println("COLLAR DATA");
Serial.print("Animal ID: ");
Serial.println("COW001");
Serial.print("Movement Value: ");
Serial.print(movement, 2);
Serial.println(" m/s^2");
Serial.print("Movement Status: ");
Serial.println(status);
// =================================
// 5. DISPLAY ANIMAL STATUS
// =================================
if (movementStatus == 1) {
Serial.println("STATUS: ANIMAL MOVING");
}
else {
Serial.println("STATUS: NO MOVEMENT");
}
// =================================
// 6. CREATE DATA PACKET
// =================================
Serial.print("DATA PACKET: ");
Serial.print("COW001");
Serial.print(",MOVEMENT:");
Serial.print(movement, 2);
Serial.print(",STATUS:");
Serial.println(status);
// =================================
// 7. SEND DATA TO THINGSPEAK
// =================================
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
// CORRECT THINGSPEAK URL
String url ="https://api.thingspeak.com/update?api_key=";
// Add API key
url += THINGSPEAK_API_KEY;
// Field 3 = Movement
url += "&field3=";
url += String(movement, 2);
// Field 4 = Movement Status
// 0 = NOT DETECTED
// 1 = DETECTED
url += "&field4=";
url += String(movementStatus);
// -------------------------------
// Display request information
// -------------------------------
Serial.println();
Serial.println("Sending data to ThingSpeak...");
// Do NOT print the complete URL
// because it contains your API key.
// -------------------------------
// Send HTTP request
// -------------------------------
http.begin(url);
int responseCode = http.GET();
// -------------------------------
// Display HTTP response
// -------------------------------
Serial.print("Cloud Response: ");
Serial.println(responseCode);
// -------------------------------
// Read ThingSpeak response
// -------------------------------
String response = http.getString();
Serial.print("ThingSpeak Response: ");
Serial.println(response);
// -------------------------------
// Check whether successful
// -------------------------------
if (responseCode == 200) {
Serial.println("COLLAR DATA SENT SUCCESSFULLY");
}
else {
Serial.println("COLLAR DATA NOT SENT");
Serial.print("HTTP ERROR CODE: ");
Serial.println(responseCode);
}
// Close HTTP connection
http.end();
}
else {
Serial.println("WIFI DISCONNECTED");
}
// =================================
// 8. WAIT BEFORE NEXT UPDATE
// =================================
Serial.println("--------------------------------");
Serial.println("Next update in 2 seconds...");
delay(2000);
}Loading
esp32-s2-devkitm-1
esp32-s2-devkitm-1