/*#include <WiFi.h>
#include <HTTPClient.h>
#include <DHT.h>
#include <ThingSpeak.h>
#include <Wire.h>
#include <MPU6050.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* server = "api.thingspeak.com";
const char* apiKey = "3IJLOHAFMEYDCMBL";
const char* channelID = "2262786";
#define DHTPIN 15 // Digital pin connected to the DHT11 sensor
#define DHTTYPE DHT22 // DHT sensor type
DHT dht(DHTPIN, DHTTYPE);
const int photoresistorPin = 35; // Analog pin to which the photoresistor is connected
MPU6050 mpu;
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(float temp, float hum) {
String url = "/update?api_key=" + String(apiKey) + "&field1=" + String(temp) + "&field2=" + String(hum);
WiFiClientSecure client;
HTTPClient http;
if (http.begin(client, server, 443, 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.");
}
}
void sendToThingSpeak(int value) {
String url = "/update?api_key=" + String(apiKey) + "&field1=" + 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.");
}
}
void setup() {
Serial.begin(115200);
connectWiFi();
dht.begin();
Wire.begin(21, 22);
Wire.begin();
mpu.initialize();
if (mpu.testConnection()) {
Serial.println("MPU6050 connection successful");
} else {
Serial.println("MPU6050 connection failed. Please check your connections.");
while (1);
}
}
void loop() {
// Read DHT11 sensor data and send to ThingSpeak
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
if (!isnan(temperature) && !isnan(humidity)) {
Serial.print("Temperature (°C): ");
Serial.println(temperature);
Serial.print("Humidity (%): ");
Serial.println(humidity);
sendToThingSpeak(temperature, humidity);
} else {
Serial.println("Failed to read from DHT sensor!");
}
// Read photoresistor data and send to ThingSpeak
int sensorValue = analogRead(photoresistorPin);
Serial.print("Analog Value: ");
Serial.println(sensorValue);
sendToThingSpeak(sensorValue);
// Read MPU6050 accelerometer and gyroscope data
int16_t ax, ay, az, gx, gy, gz;
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
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;
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();
delay(15000); // Adjust the delay based on your application's requirements
}*/
#include <WiFi.h>
#include <HTTPClient.h>
#include <DHT.h>
#include <ThingSpeak.h>
#include <Wire.h>
#include <MPU6050.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* server = "api.thingspeak.com";
const char* apiKey = "3IJLOHAFMEYDCMBL";
const char* channelID = "2262786";
#define DHTPIN 15 // Digital pin connected to the DHT11 sensor
#define DHTTYPE DHT22 // DHT sensor type
DHT dht(DHTPIN, DHTTYPE);
const int photoresistorPin = 35; // Analog pin to which the photoresistor is connected
MPU6050 mpu;
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(float temperature, float humidity, int photoresistorValue, float accelX, float accelY, float accelZ, float gyroX, float gyroY, float gyroZ) {
String url = "/update?api_key=" + String(apiKey) + "&field1=" + String(temperature) + "&field2=" + String(humidity) +
"&field3=" + String(photoresistorValue) + "&field4=" + String(accelX) + "&field5=" + String(accelY) +
"&field6=" + String(accelZ) + "&field7=" + String(gyroX) + "&field8=" + String(gyroY) + "&field9=" + String(gyroZ);
WiFiClientSecure client;
HTTPClient http;
if (http.begin(client, server, 443, 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.");
}
}
void setup() {
Serial.begin(115200);
connectWiFi();
dht.begin();
Wire.begin(21, 22);
Wire.begin();
mpu.initialize();
if (mpu.testConnection()) {
Serial.println("MPU6050 connection successful");
} else {
Serial.println("MPU6050 connection failed. Please check your connections.");
while (1);
}
}
void loop() {
// Read DHT22 sensor data and send to ThingSpeak
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
if (!isnan(temperature) && !isnan(humidity)) {
Serial.print("Temperature (°C): ");
Serial.println(temperature);
Serial.print("Humidity (%): ");
Serial.println(humidity);
} else {
Serial.println("Failed to read from DHT sensor!");
}
// Read photoresistor data
int photoresistorValue = analogRead(photoresistorPin);
Serial.print("Photoresistor Value: ");
Serial.println(photoresistorValue);
// Read MPU6050 accelerometer and gyroscope data
int16_t ax, ay, az, gx, gy, gz;
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
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;
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);
// Send data to ThingSpeak
sendToThingSpeak(temperature, humidity, photoresistorValue, accelX, accelY, accelZ, gyroX, gyroY, gyroZ);
delay(15000); // Adjust the delay based on your application's requirements
}