#include <WiFi.h>
#include "ThingSpeak.h" // always include thingspeak header file after other header files and custom macros
#include <MPU6050_light.h>
MPU6050 mpu(Wire);
#define SECRET_SSID "Wokwi-GUEST" // replace MySSID with your WiFi network name
#define SECRET_PASS "" // replace MyPassword with your WiFi password
#define SECRET_CH_ID 2499159 // replace 0000000 with your channel number
#define SECRET_WRITE_APIKEY "07GW09X9P31GJTR7" // replace XYZ with your channel write API Key
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password
WiFiClient client;
unsigned long myChannelNumber = SECRET_CH_ID;
const char * myWriteAPIKey = SECRET_WRITE_APIKEY;
unsigned long timer = 0;
void setup() {
pinMode(2, OUTPUT);
Serial.begin(115200); //Initialize serial
Wire.begin();
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client); // Initialize ThingSpeak
if (WiFi.status() != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(SECRET_SSID);
while (WiFi.status() != WL_CONNECTED) {
WiFi.begin(ssid, pass); // Connect to WPA/WPA2 network. Change this line if using open or WEP network
Serial.print(".");
delay(5000);
}
Serial.println("\nConnected.");
}
byte status = mpu.begin();
mpu.calcOffsets();
mpu.setFilterGyroCoef(0.98);
mpu.setFilterAccCoef(0.02);
}
void loop() {
mpu6050result();
}
void mpu6050result() {
mpu.update();
if ((millis() - timer) > 100) { // print data every 0.1s
ThingSpeak.setField(1, mpu.getAccX());
ThingSpeak.setField(2, mpu.getAccY());
ThingSpeak.setField(3, mpu.getAccZ());
ThingSpeak.setField(4, mpu.getGyroX());
ThingSpeak.setField(5, mpu.getGyroY());
ThingSpeak.setField(6, mpu.getGyroZ());
ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
timer = millis();
}
}