#include <WiFi.h>
#include <TinyGPS++.h>
#include <HardwareSerial.h>
#include <math.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include "Audio.h" // Include the Audio library for I2S audio playback
// GPS module connected to RX2 (16) and TX2 (17)
static const int RXPin = 16, TXPin = 17;
static const uint32_t GPSBaud = 9600;
// Create a TinyGPS++ object
TinyGPSPlus gps;
// Create a hardware serial object
HardwareSerial ss(2);
// Replace with your network credentials
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// Server addresses
const String gpsServerName = "http://192.168.147.29:5000/update-gps";
const String reminderServerName = "http://192.168.147.29:5000/reminders";
// Interval in milliseconds between sending GPS data
const unsigned long gpsInterval = 10000; // 10 seconds
unsigned long previousGpsMillis = 0;
// Interval in milliseconds between fetching reminders
const unsigned long reminderInterval = 3000; // 3 seconds
unsigned long previousReminderMillis = 0;
// Initialize the audio object
Audio audio;
struct HttpResponse {
int status;
String body;
String location;
};
HttpResponse HttpPost(const String& url, const String& payload, bool followRedirects) {
HttpResponse response;
WiFiClient client; // Use WiFiClient for HTTP requests
HTTPClient http;
http.setFollowRedirects(followRedirects ? HTTPC_FORCE_FOLLOW_REDIRECTS : HTTPC_DISABLE_FOLLOW_REDIRECTS);
http.addHeader("Content-Length", String(payload.length()));
http.addHeader("Content-Type", "application/json");
if (http.begin(client, url)) {
response.status = http.POST(payload);
response.body = http.getString();
response.location = http.getLocation();
http.end();
} else {
Serial.println("Failed to connect to server");
response.status = -1;
}
return response;
}
HttpResponse HttpGet(const String& url) {
HttpResponse response;
WiFiClient client;
HTTPClient http;
if (http.begin(client, url)) {
response.status = http.GET();
response.body = http.getString();
http.end();
} else {
Serial.println("Failed to connect to server");
response.status = -1;
}
return response;
}
String parseReminder(String payload) {
StaticJsonDocument<200> doc;
DeserializationError error = deserializeJson(doc, payload);
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.f_str());
return "";
}
JsonObject reminder = doc[0];
String name = reminder["name"];
return name;
}
void playReminder(String reminder) {
if (reminder == "Take your medicine") {
audio.connecttoFS(SPIFFS, "/reminder_take_medicine.wav");
} else if (reminder == "Drink water") {
audio.connecttoFS(SPIFFS, "/reminder_drink_water.wav");
} else {
Serial.println("Unknown reminder: " + reminder);
}
}
void setup() {
Serial.begin(115200);
// Initialize GPS module
ss.begin(GPSBaud, SERIAL_8N1, RXPin, TXPin);
// 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 audio library and speaker
audio.setPinout(26, 25, 22); // Set the correct I2S pins for your setup
//Audio.begin();
}
void loop() {
// Update GPS data
while (ss.available() > 0) {
gps.encode(ss.read());
}
// Check if it's time to send GPS data
unsigned long currentMillis = millis();
if (currentMillis - previousGpsMillis >= gpsInterval) {
previousGpsMillis = currentMillis;
if (true) { // Replace with gps.location.isValid() if using real GPS
float latitude = 36.778259;
float longitude = -119.417931;
Serial.print("Latitude= ");
Serial.print(latitude, 6);
Serial.print(" Longitude= ");
Serial.println(longitude, 6);
// Create JSON string with GPS data
String jsonPayload = String("{\"lat\":") + String(latitude, 6) +
String(", \"lng\":") + String(longitude, 6) + "}";
// Send GPS data to external server
if (WiFi.status() == WL_CONNECTED) {
HttpResponse httpResponse = HttpPost(gpsServerName, jsonPayload, true);
if (httpResponse.status > 0) {
Serial.println(httpResponse.status);
Serial.println(httpResponse.body);
} else {
Serial.print("Error on sending POST: ");
Serial.println(httpResponse.status);
}
}
} else {
Serial.println("Waiting for valid GPS signal...");
}
}
// Check if it's time to fetch reminders
if (currentMillis - previousReminderMillis >= reminderInterval) {
previousReminderMillis = currentMillis;
if (WiFi.status() == WL_CONNECTED) {
HttpResponse httpResponse = HttpGet(reminderServerName);
if (httpResponse.status > 0) {
Serial.println(httpResponse.status);
Serial.println(httpResponse.body);
String reminder = parseReminder(httpResponse.body);
if (reminder.length() > 0) {
playReminder(reminder);
}
} else {
Serial.print("Error on fetching reminders: ");
Serial.println(httpResponse.status);
}
}
}
}