Hello everyone,
My name is Ahmed, and this is my first topic in the Arduino community. I am trying to build a project to my IoT course, and I am facing difficulty hopefully to get a good tip or guidance since I am still a beginner.
My project is using ESP8266 with GPS Neu-6m (Hardware: GT-u7) with a vibration sensor (SW-420), the idea is to create a device that can help to provide information about accident moments. where I can at a certain vibration It will be considered an accident and the vibration value + car speed will be recorded. so for this prototype, I have used Arduino cloud to get the data into the dashboard, and this is my code:
#include <TinyGPS++.h> // library for GPS module
#include <SoftwareSerial.h>
#include <PubSubClient.h>
#include "thingProperties.h"
TinyGPSPlus gps; // The TinyGPS++ object
SoftwareSerial GPSSerial(D2, D1); // The serial connection to the GPS device
static const uint32_t GPSBaud = 9600;
float latitude, longitude;
int year, month, date, hour, minute, second;
String date_str, time_str, lat_str, lng_str, vib_str, speed_str;
int pm;
int vs = D5; //vibration sensor
boolean connectionled=false;
void setup() {
Serial.begin(115200);
GPSSerial.begin(GPSBaud);
pinMode(D8, OUTPUT);
pinMode(vs, INPUT);
// Initialize serial and wait for port to open:
//Serial.begin(9600);
// This delay gives the chance to wait for a Serial Monitor without blocking if none is found
//delay(1500);
// Defined in thingProperties.h
initProperties();
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
ArduinoCloud.addCallback(ArduinoIoTCloudEvent::CONNECT, doThisOnConnect);
ArduinoCloud.addCallback(ArduinoIoTCloudEvent::SYNC, doThisOnSync);
ArduinoCloud.addCallback(ArduinoIoTCloudEvent::DISCONNECT, doThisOnDisconnect);
/*
The following function allows you to obtain more information
related to the state of network and IoT Cloud connection and errors
the higher number the more granular information you’ll get.
The default is 0 (only errors).
Maximum is 4
*/
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
}
void loop() {
ArduinoCloud.update();
if(connectionled){
digitalWrite(D8, HIGH); // turn the LED on (HIGH is the voltage level)
delay(500); // wait for a second
digitalWrite(D8, LOW); // turn the LED off by making the voltage LOW
delay(500); // wait for a second
digitalWrite(D8, HIGH); // turn the LED on (HIGH is the voltage level)
delay(500); // wait for a second
digitalWrite(D8, LOW); // turn the LED off by making the voltage LOW
delay(500); // wait for a second
digitalWrite(D8, HIGH); // turn the LED on (HIGH is the voltage level)
delay(500); // wait for a second
digitalWrite(D8, LOW); // turn the LED off by making the voltage LOW
delay(500);
}
while (GPSSerial.available() > 0){
if(gps.encode(GPSSerial.read())){
if (gps.location.isValid()){
delay(1000);
speed = gps.speed.kmph();
location = Location(gps.location.lat(), gps.location.lng());
// Latitude in degrees (double)
Serial.print("Latitude= ");
Serial.print(gps.location.lat(), 6);
// Longitude in degrees (double)
Serial.print(" Longitude= ");
Serial.println(gps.location.lng(), 6);
// Raw latitude in whole degrees
Serial.print("Raw latitude = ");
Serial.print(gps.location.rawLat().negative ? "-" : "+");
Serial.println(gps.location.rawLat().deg);
// ... and billionths (u16/u32)
Serial.println(gps.location.rawLat().billionths);
// Raw longitude in whole degrees
Serial.print("Raw longitude = ");
Serial.print(gps.location.rawLng().negative ? "-" : "+");
Serial.println(gps.location.rawLng().deg);
// ... and billionths (u16/u32)
Serial.println(gps.location.rawLng().billionths);
// Raw date in DDMMYY format (u32)
Serial.print("Raw date DDMMYY = ");
Serial.println(gps.date.value());
// Year (2000+) (u16)
Serial.print("Year = ");
Serial.println(gps.date.year());
// Month (1-12) (u8)
Serial.print("Month = ");
Serial.println(gps.date.month());
// Day (1-31) (u8)
Serial.print("Day = ");
Serial.println(gps.date.day());
// Raw time in HHMMSSCC format (u32)
Serial.print("Raw time in HHMMSSCC = ");
Serial.println(gps.time.value());
// Hour (0-23) (u8)
Serial.print("Hour = ");
Serial.println(gps.time.hour());
// Minute (0-59) (u8)
Serial.print("Minute = ");
Serial.println(gps.time.minute());
// Second (0-59) (u8)
Serial.print("Second = ");
Serial.println(gps.time.second());
// 100ths of a second (0-99) (u8)
Serial.print("Centisecond = ");
Serial.println(gps.time.centisecond());
// Speed in miles per hour (double)
Serial.print("Speed in miles/h = ");
Serial.println(gps.speed.mph());
// Speed in meters per second (double)
Serial.print("Speed in m/s = ");
Serial.println(gps.speed.mps());
// Speed in kilometers per hour (double)
Serial.print("Speed in km/h = ");
Serial.println(gps.speed.kmph());
// Raw altitude in centimeters (i32)
Serial.print("Raw altitude in centimeters = ");
Serial.println(gps.altitude.value());
// Altitude in meters (double)
Serial.print("Altitude in meters = ");
Serial.println(gps.altitude.meters());
// Altitude in miles (double)
Serial.print("Altitude in miles = ");
Serial.println(gps.altitude.miles());
// Altitude in kilometers (double)
Serial.print("Altitude in kilometers = ");
Serial.println(gps.altitude.kilometers());
// Altitude in feet (double)
Serial.print("Altitude in feet = ");
Serial.println(gps.altitude.feet());
// Number of satellites in use (u32)
Serial.print("Number os satellites in use = ");
Serial.println(gps.satellites.value());
// Horizontal Dim. of Precision (100ths-i32)
Serial.print("HDOP = ");
Serial.println(gps.hdop.value());
}
}
}
long measurement = vibrationf();
vibration = int(measurement);
}
long vibrationf() {
//long measurement=pulseIn (vs, HIGH); //wait for the pin to get HIGH and returns measurement
long measurement = pulseIn(vs, HIGH);
return measurement;
}
void doThisOnConnect(){
/* add your custom code here */
connectionled=true;
Serial.println("Board successfully connected to Arduino IoT Cloud");
}
void doThisOnSync(){
/* add your custom code here */
Serial.println("Thing Properties synchronised");
}
void doThisOnDisconnect(){
connectionled=false;
/* add your custom code here */
Serial.println("Board disconnected from Arduino IoT Cloud");
}
my problem is: I am not getting live data on GPS. it happens to get a moment, where speed and location is appearing on Arduino cloud dashboard, so wiring should be fine.
What I am thinking is that there is a problem in syncing the data, or a difference in reading speeds.. I am not sure, and hopefully someone can guide me why I am getting only moments.
PS: I am noticing that Arduino connectivity to the cloud is not stable, it disconnects for some reasons.