#include <WiFi.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
#include "ThingSpeak.h"
#include <HTTPClient.h>
#include <base64.h>
const char* WIFI_NAME="Wokwi-GUEST";
const char* WIFI_PASSWORD = "";
unsigned long myChannelNumber =2550050; // Replace with your ThingSpeak channel number
const char* myWriteAPIKey = "S9NKN4LZ3K8VXHML";
const char* serverName = "http://api.thingspeak.com/channels/2550050/fields/1/last.json";
const char* apiKey = "ELGKR7BI7YQW87SO"; // Read API Key
const char* TWILIO_ACCOUNT_SID = "AC1a080b7a50c50d64fba9dba2d7b63ad6"; // Replace with your Twilio Account SID
const char* TWILIO_AUTH_TOKEN = "620acede0cb36cafe1789bc2facb70ed"; // Replace with your Twilio Auth Token
const char* TWILIO_PHONE_NUMBER = "+12077212066"; // Replace with your Twilio phone number
const char* RECIPIENT_PHONE_NUMBER = "+91 9061340905";
Adafruit_MPU6050 mpu;
WiFiClient client;
void setup() {
Serial1.begin(115200);
Serial1.println("Hello, Raspberry Pi Pico W!");
WiFi.begin(WIFI_NAME,WIFI_PASSWORD);
while (WiFi.status()!=WL_CONNECTED){
delay(1000);
Serial1.print("Wifi not connected...\n");
}
Serial1.println("\nWifi connected");
Serial1.println("IP Address: "+(WiFi.localIP()));
WiFi.mode(WIFI_STA);
Serial1.println("Adafruit MPU6050 test!");
if (!mpu.begin()) {
Serial1.println("Failed to find MPU6050 chip");
while (1) {
delay(10);
}
}
Serial1.println("MPU6050 Found!");
ThingSpeak.begin(client);
}
void loop() {
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
Serial1.print("Acceleration X: ");
Serial1.print(a.acceleration.x);
Serial1.print(", Y: ");
Serial1.print(a.acceleration.y);
Serial1.print(", Z: ");
Serial1.print(a.acceleration.z);
Serial1.println(" m/s^2");
ThingSpeak.setField(1, a.acceleration.x);
ThingSpeak.setField(2, a.acceleration.y);
ThingSpeak.setField(3, a.acceleration.z);
int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if (x == 200) {
Serial1.println("Channel update successful.");
} else {
Serial1.println("Problem updating channel. HTTP error code " + String(x));
}
Serial1.println("");
delay(500);
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
// Make the request
String serverPath = serverName + String("?api_key=") + apiKey;
http.begin(serverPath.c_str());
// Send HTTP GET request
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
String payload = http.getString();
Serial1.println("HTTP Response code: " + String(httpResponseCode));
Serial1.println("Received payload: " + payload);
// Process JSON payload to extract the value
int start = payload.indexOf("field1\":");
int end = payload.indexOf("}", start);
String fieldValue = payload.substring(start + 8, end);
//Serial1.println("Field1 value: " + fieldValue);
if (fieldValue.toInt() == 1) {
sendTwilioSMS();
}
}
else {
Serial1.println("Error in HTTP request");
}
// Free resources
http.end();
}
else {
Serial1.println("WiFi Disconnected");
}
delay(60);
}
void sendTwilioSMS() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
// Prepare the Twilio API URL
String url = "https://api.twilio.com/2010-04-01/Accounts/" + String(TWILIO_ACCOUNT_SID) + "/Messages.json";
// Create the message body
String messageBody = "You forgot your helmet";
// Create the POST data
String postData = "To=" + String(RECIPIENT_PHONE_NUMBER) + "&From=" + String(TWILIO_PHONE_NUMBER) + "&Body=" + messageBody;
// Encode the credentials for HTTP basic authentication
String credentials = String(TWILIO_ACCOUNT_SID) + ":" + String(TWILIO_AUTH_TOKEN);
String encodedCredentials = base64::encode(credentials);
// Make the request
http.begin(url);
http.addHeader("Authorization", "Basic " + encodedCredentials);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
// Send the POST request
int httpResponseCode = http.POST(postData);
if (httpResponseCode > 0) {
String response = http.getString();
Serial1.println("SMS sent successfully. Response: " + response);
} else {
Serial1.println("Error sending SMS. HTTP error code: " + String(httpResponseCode));
}
// Free resources
http.end();
} else {
Serial1.println("WiFi Disconnected");
}
}Loading
pi-pico-w
pi-pico-w