#include <EEPROM.h>
#include "thingProperties.h"
#include <TinyGPSPlus.h>
#include <SoftwareSerial.h>
// Define the software serial connection to the GPS module
SoftwareSerial ss(4, 5);
TinyGPSPlus gps;
#define EEPROM_SIZE 64
#define PHONE_NUMBER_ADDRESS 0
String number = "+639615393117"; // Default number
void setup() {
// Start the hardware serial for debugging
Serial.begin(9600);
// Start the software serial connection to the GPS module
ss.begin(9600);
// Initialize EEPROM
EEPROM.begin(EEPROM_SIZE);
// Load the stored phone number from EEPROM
number = readNumberFromEEPROM();
Serial.println("Loaded phone number from EEPROM: " + number);
// Wait for the serial connection to be ready
delay(1500);
Serial.println("GPS Start");
// Defined in thingProperties.h
initProperties();
// Connect to WiFi
Serial.println("Connecting to WiFi...");
WiFi.begin(SSID, PASS);
int timeout = 0;
while (WiFi.status() != WL_CONNECTED && timeout < 20) {
delay(500);
Serial.print(".");
timeout++;
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println("\nConnected to WiFi.");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
} else {
Serial.println("\nFailed to connect to WiFi.");
Serial.print("WiFi status: ");
Serial.println(WiFi.status());
}
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
// Set the debug message level and print debug info
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
}
void loop() {
while (ss.available() > 0)
if (gps.encode(ss.read()))
displayInfo();
if (millis() > 5000 && gps.charsProcessed() < 10)
{
Serial.println(F("No GPS detected: check wiring."));
while(true);
}
float Lat = gps.location.lat() ;
float Long = gps.location.lng() ;
lati = gps.location.lat() ;
longi = gps.location.lng() ;
coordinate = {Lat, Long};
ArduinoCloud.update();
// Debug print for the phone number
Serial.println("Current Phone Number: " + number);
}
void displayInfo(){
//GPS
Serial.print(F("Location: "));
if (gps.location.isValid())
{
Serial.print(gps.location.lat(), 6);
Serial.print(F(","));
Serial.println(gps.location.lng(), 6);
}
else
{
Serial.print(F("INVALID"));
}
}
void onCoordinateChange() {
Location coordinates = coordinate.getValue();
float target_lat = coordinates.lat ;
float target_lon = coordinates.lon ;
Serial.println(coordinates.lat, 6);
Serial.println(coordinates.lon, 6);
Serial.println("Location updated from the cloud.");
}
/*
Since Lati is READ_WRITE variable, onLatiChange() is
executed every time a new value is received from IoT Cloud.
/
void onLatiChange() {
// Add your code here to act upon Lati change
Location coordinates = coordinate.getValue();
float Lat = coordinates.lat;
Serial.println(coordinates.lat, 6);
}
/
Since Longi is READ_WRITE variable, onLongiChange() is
executed every time a new value is received from IoT Cloud.
/
void onLongiChange() {
Location coordinates = coordinate.getValue();
float Long = coordinates.lon ;
Serial.println(coordinates.lon, 6);
}
/
Since PhoneNumber is READ_WRITE variable, onPhoneNumberChange() is
executed every time a new value is received from IoT Cloud.
*/
void onPhoneNumberChange() {
number = phoneNumber; // Update the local number variable
saveNumberToEEPROM(number); // Save the updated number to EEPROM
Serial.println("Phone number updated and saved to EEPROM: " + number);
}
// Save the phone number to EEPROM
void saveNumberToEEPROM(String number) {
for (int i = 0; i < number.length(); i++) {
EEPROM.write(PHONE_NUMBER_ADDRESS + i, number[i]);
}
EEPROM.write(PHONE_NUMBER_ADDRESS + number.length(), '\0'); // Null terminator
EEPROM.commit();
}
// Read the phone number from EEPROM
String readNumberFromEEPROM() {
char buffer[EEPROM_SIZE];
int i = 0;
while (i < EEPROM_SIZE - 1) {
char c = EEPROM.read(PHONE_NUMBER_ADDRESS + i);
if (c == '\0') break;
buffer[i++] = c;
}
buffer[i] = '\0';
return String(buffer);
}
(thingProperties.h)
// Code generated by Arduino IoT Cloud, DO NOT EDIT.
#include <ArduinoIoTCloud.h>
#include <Arduino_ConnectionHandler.h>
const char DEVICE_LOGIN_NAME[] = "8993f15e-2fcf-4395-9dee-b380db6a8376";
const char SSID[] = SECRET_SSID; // Network SSID (name)
const char PASS[] = SECRET_OPTIONAL_PASS; // Network password (use for WPA, or use as key for WEP)
const char DEVICE_KEY[] = SECRET_DEVICE_KEY; // Secret device password
void onPhoneNumberChange();
void onLatiChange();
void onLongiChange();
void onCoordinateChange();
String phoneNumber;
float lati;
float longi;
CloudLocation coordinate;
void initProperties(){
ArduinoCloud.setBoardId(DEVICE_LOGIN_NAME);
ArduinoCloud.setSecretDeviceKey(DEVICE_KEY);
ArduinoCloud.addProperty(phoneNumber, READWRITE, ON_CHANGE, onPhoneNumberChange);
ArduinoCloud.addProperty(lati, READWRITE, ON_CHANGE, onLatiChange);
ArduinoCloud.addProperty(longi, READWRITE, ON_CHANGE, onLongiChange);
ArduinoCloud.addProperty(coordinate, READWRITE, ON_CHANGE, onCoordinateChange);
}
WiFiConnectionHandler ArduinoIoTPreferredConnection(SSID, PASS);
This is the output of the console
Current Phone Number: ���������������������������������������������������������������
Current Phone Number: ���������������������������������������������������������������
Current Phone Number: ���������������������������������������������������������������
Current Phone Number: ���������������������������������������������������������������
Current Phone Number: ���������������������������������������������������������������
Current Phone Number: ���������������������������������������������������������������
Current Phone Number: ���������������������������������������������������������������
Current Phone Number: ���������������������������������������������������������������
Current Phone Number: ���������������������������������������������������������������
Current Phone Number: ���������������������������������������������������������������
Current Phone Number: ���������������������������������������������������������������
Current Phone Number: ���������������������������������������������������������������
Current Phone Number: ���������������������������������������������������������������
Current Phone Number: ���������������������������������������������������������������
Current Phone Number: ���������������������������������������������������������������
Current Phone Number: ���������������������������������������������������������������
Current Phone Number: ���������������������������������������������������������������