/*#include <Wire.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <ThingSpeak.h>
// Include the library for your gyroscope sensor (e.g., MPU6050)
#include <MPU6050.h>
// Replace with your network credentials
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// ThingSpeak API settings
const char* apiKey = "3IJLOHAFMEYDCMBL";
const char* server = "api.thingspeak.com";
const int fieldNumber4 = 4; // The field number on ThingSpeak
// Create an instance of the gyroscope sensor
MPU6050 mpu;
void setup() {
Serial.begin(115200);
Wire.begin(12,13);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
// Initialize the gyroscope sensor
mpu.initialize();
if (!mpu.testConnection()) {
Serial.println("MPU6050 connection failed");
while (1);
}
}
void loop() {
// Read gyroscope data
int16_t gyroX, gyroY, gyroZ;
mpu.getRotation(&gyroX, &gyroY, &gyroZ);
Serial.print("Value X:");
Serial.println("gyroX:");
// Send data to ThingSpeak
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String data = "field" + String(fieldNumber4) + "=" + String(gyroX); // Change to the appropriate gyro data
String path = "/update?api_key=" + String(apiKey) + "&" + data;
Serial.println("Sending data to ThingSpeak...");
if (http.begin(server, 80, path)) {
int httpCode = http.GET();
if (httpCode == HTTP_CODE_OK) {
Serial.println("Data sent successfully to ThingSpeak!");
} else {
Serial.println("Data send failed. HTTP error code: " + String(httpCode));
}
http.end();
} else {
Serial.println("Unable to connect to ThingSpeak");
}
}
// Add a delay to control the data send rate
delay(10000); // Adjust as needed
}*/
//second try code
/*#include <WiFi.h>
#include <HTTPClient.h>
#include <Wire.h>
#include <MPU6050.h>
MPU6050 mpu;
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* server = "api.thingspeak.com";
const char* apiKey = "3IJLOHAFMEYDCMBL";
const char* channelID = "your_thingspeak_channel_id";
const int (ax),(ay),az,gx,gy,gz) ;
void setup() {
Serial.begin(115200);
Wire.begin(21,22);
Wire.begin();
mpu.initialize();
// Check if the MPU6050 is connected
if (mpu.testConnection()) {
Serial.println("MPU6050 connection successful");
} else {
Serial.println("MPU6050 connection failed. Please check your connections.");
while (1);
}
}
void loop() {
// Read raw accelerometer and gyroscope values
int16_t ax, ay, az, gx, gy, gz;
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
// Convert raw values to meaningful data
float accelX = ax / 16384.0;
float accelY = ay / 16384.0;
float accelZ = az / 16384.0;
float gyroX = gx / 131.0;
float gyroY = gy / 131.0;
float gyroZ = gz / 131.0;
// Print the sensor data
Serial.print("Accelerometer: ");
Serial.print("X = "); Serial.print(accelX); Serial.print(" | ");
Serial.print("Y = "); Serial.print(accelY); Serial.print(" | ");
Serial.print("Z = "); Serial.println(accelZ);
Serial.print("Gyroscope: ");
Serial.print("X = "); Serial.print(gyroX); Serial.print(" | ");
Serial.print("Y = "); Serial.print(gyroY); Serial.print(" | ");
Serial.print("Z = "); Serial.println(gyroZ);
Serial.println();
sendToThingsSpeak(ax,ay,az,gx,gy,gz);
delay(1000); // Adjust the delay based on your application's requirements
}
void connectWiFi() {
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
}
void sendToThingSpeak(int ax,ay,az,gx,gy,gz) {
String url = "/update?api_key=" + String(apiKey) + "&field4=" + String(value);
WiFiClient client;
HTTPClient http;
if (http.begin(client, server, 80, url)) {
int httpCode = http.GET();
if (httpCode > 0) {
Serial.println("Data sent to ThingSpeak successfully.");
}
http.end();
} else {
Serial.println("Error in connecting to ThingSpeak.");
}
}*/
//THIRD TRY CODE
#include <WiFi.h>
#include <HTTPClient.h>
#include <Wire.h>
#include <MPU6050.h>
MPU6050 mpu;
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* server = "api.thingspeak.com";
const char* apiKey = "3IJLOHAFMEYDCMBL";
const char* channelID = "2262786";
void setup() {
Serial.begin(115200);
Wire.begin(21, 22);
mpu.initialize();
connectWiFi();
// Check if the MPU6050 is connected
if (mpu.testConnection()) {
Serial.println("MPU6050 connection successful");
} else {
Serial.println("MPU6050 connection failed. Please check your connections.");
while (1);
}
}
void loop() {
// Read raw accelerometer and gyroscope values
int16_t ax, ay, az, gx, gy, gz;
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
// Print the sensor data
Serial.print("Accelerometer: ");
Serial.print("X = "); Serial.print(ax); Serial.print(" | ");
Serial.print("Y = "); Serial.print(ay); Serial.print(" | ");
Serial.print("Z = "); Serial.println(az);
Serial.print("Gyroscope: ");
Serial.print("X = "); Serial.print(gx); Serial.print(" | ");
Serial.print("Y = "); Serial.print(gy); Serial.print(" | ");
Serial.print("Z = "); Serial.println(gz);
Serial.println();
sendToThingSpeak(ax, ay, az, gx, gy, gz);
delay(1000); // Adjust the delay based on your application's requirements
}
void connectWiFi() {
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
}
void sendToThingSpeak(int ax, int ay, int az, int gx, int gy, int gz) {
String url = "/update?api_key=" + String(apiKey) +
"&field4=" + String(ax) +
"&field5=" + String(ay) +
"&field6=" + String(az) +
"&field7=" + String(gx) +
"&field8=" + String(gy) +
"&field9=" + String(gz);
WiFiClient client;
HTTPClient http;
if (http.begin(client, server, 80, url)) {
int httpCode = http.GET();
if (httpCode > 0) {
Serial.println("Data sent to ThingSpeak successfully.");
} else {
Serial.println("Error in sending data to ThingSpeak.");
}
http.end();
} else {
Serial.println("Error in connecting to ThingSpeak.");
}
}