Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

plz help me to make me understand this code

fhtechiiuc

New Coder
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <Hash.h>
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <FS.h>
#include <String.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_SSD1306.h>
#include <SoftwareSerial.h>
SoftwareSerial sim808(D6,D7); //Rx, Tx

#define gsm sim808 // gsm as sim module
#define vibPin D5 // Vibration Sensor
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display = Adafruit_SSD1306(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire);

/* Assign a unique ID to this sensor at the same time */
Adafruit_MPU6050 mpu;
/* Variables for holding axis data*/
float x_axis, y_axis, z_axis; //0, 0 ,~9.8 ms^-2
int pos;
String collision = "No";
long t_time = millis();
long c_time = millis();
String posi;
String data[5];
#define DEBUG true
String state,timegps,latitude,longitude;
String sms, cellno = "+8801869441097"; // USE INTERNATIONAL FORMAT CODE FOR MOBILE NUMBERS
int sms_token = 0;
// Replace with your network credentials
const char* ssid = "fahimajmal";
const char* password = "fahim360@";

// Create AsyncWebServer object on port 80
AsyncWebServer server(80);

void read_adxl(float* x_axis, float* y_axis, float* z_axis, int* pos){
/* Get a new sensor event */
sensors_event_t event, g, temp;
mpu.getEvent(&event, &g, &temp);
*x_axis = event.acceleration.x;
*y_axis = event.acceleration.y;
*z_axis = event.acceleration.z;
/* Display the results (acceleration is measured in m/s^2) */
Serial.print("X: "); Serial.print(*x_axis); Serial.print(" ");
Serial.print("Y: "); Serial.print(*y_axis); Serial.print(" ");
Serial.print("Z: "); Serial.print(*z_axis); Serial.print(" ");Serial.println("m/s^2 ");
if(*x_axis > 7.0){
*pos = 1;
}
else if (*x_axis < -7.0){
*pos = 2;
}
else if (*y_axis > 7.0){
*pos = 3;
}
else if (*y_axis < -7.0){
*pos = 4;
}
else if (*z_axis < -7.0){
*pos = 5;
}
else{
*pos = 0;
}
delay(500);
}

void get_location(){
sendTabData("AT+CGNSINF",1000,DEBUG);
if (state !=0) {
Serial.println("State :"+state);
Serial.println("Time :"+timegps);
Serial.println("Latitude :"+latitude);
Serial.println("Longitude :"+longitude);
sim808.flush();
} else {
Serial.println("GPS Initialising...");
}
}

void accident_alert(){
// //wait for the pin to get HIGH and returns measurement
long measurement=pulseIn (vibPin, HIGH);
Serial.println(measurement); // Print the measurement.
if(collision.equals("Heavy") || collision.equals("Medium")){
digitalWrite(D3, HIGH);
if((c_time+60000) < millis()){
collision = "No";
}
if(sms_token){
sendMessage(collision, posi);
sms_token = 0;
}
}
else{
digitalWrite(D3, LOW);
if (measurement > 90000){
collision = "Heavy";
c_time = millis();
sms_token = 1;
}
else if (measurement > 10000){
collision = "Medium";
c_time = millis();
sms_token = 1;
}
else if (measurement > 30){
collision = "Mild";
}
else{
collision = "No";
}
}

}

void setup() {
pinMode(D5, INPUT);
pinMode(D3, OUTPUT);
digitalWrite(D3, LOW);
sim808.begin(9600);
Serial.begin(115200);
/* Initialise the sensor */
if (!mpu.begin()) {
Serial.println("Sensor init failed");
while (1)
yield();
}
Serial.println("Found a MPU-6050 sensor");

if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x32
Serial.println(F("SSD1306 allocation failed"));
for(;😉; // Don't proceed, loop forever
}
display.display();
delay(500); // Pause for 2 seconds
display.setTextSize(1);
display.setTextColor(WHITE);
display.setRotation(0);
display.clearDisplay();
display.setCursor(1, 0);
display.println(F("Starting File System..."));
display.display();
delay(100);
// Initialize SPIFFS
if(!SPIFFS.begin()){
Serial.println("An Error has occurred while mounting SPIFFS");
display.println(F("Error Loading File System..."));
return;
}
// Clear the buffer
display.clearDisplay();
display.setTextSize(1); // Draw 2X-scale text
display.setTextColor(WHITE);
display.setCursor(1, 0);
display.println(F("Connecting to WiFi.."));
display.display();
delay(100);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi..");
}

// Print ESP8266 Local IP Address
// Clear the buffer
display.clearDisplay();
display.setTextSize(1); // Draw 2X-scale text
display.setTextColor(WHITE);
display.setCursor(1, 0);
display.print(F("IP:"));
display.println(WiFi.localIP());
display.display(); // Show initial text
delay(100);
Serial.println(WiFi.localIP());

delay(50);

/*Initiate the GSM Module*/
sendData("AT",1000,DEBUG);
delay(50);
sendData("AT+CSQ",1000,DEBUG); //Signal Quality Report
delay(150);
sendData("AT+CREG?",1000,DEBUG); //Attach or Detach from Gprs Service
delay(150);
/*Initiate the GPS Module*/
sendData("AT+CGNSPWR=1",1000,DEBUG);
delay(50);
sendData("AT+CGNSSEQ=RMC",1000,DEBUG);
delay(150);

// Route for root / web page
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
request->send(SPIFFS, "/index.html");
});
server.on("/position", HTTP_GET, [](AsyncWebServerRequest *request){
//read_adxl(&x_axis, &y_axis, &z_axis);
request->send_P(200, "text/plain", (String(x_axis)+","+String(y_axis)+","+String(z_axis)).c_str());
});
server.on("/location", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/plain", (latitude+","+longitude).c_str());
});
server.on("/alert", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/plain", (collision+","+posi).c_str());
});

// Start server
server.begin();
}

void loop() {
read_adxl(&x_axis, &y_axis, &z_axis, &pos);
accident_alert();
if(pos>0){
if (pos == 1){
posi = "Flipped Left";
}
else if (pos == 2){
posi = "Flipped Right";
}
else if (pos == 3){
posi = "Flipped Backward";
}
else {
posi = "Flipped Forward";
}
}
else{
posi = "Normal";
}
if((t_time+4000) < millis()){
get_location();
t_time = millis();
}
display.clearDisplay();
display.setCursor(1, 0);
display.print(F("IP:"));
display.println(WiFi.localIP());
display.print("X:");
display.print(x_axis, 1);
display.print(",Y:");
display.print(y_axis, 1);
display.print(",Z:");
display.print(z_axis, 1);
display.println("");
display.println("Postion: "+ posi);
display.println(collision + " Collision Detected");
display.display();
delay(100);
}

void sendTabData(String command , const int timeout , boolean debug){

sim808.println(command);
long int time = millis();
int i = 0;

while((time+timeout) > millis()){
while(sim808.available()){
char c = sim808.read();
if (c != ',') {
data +=c;
delay(100);
} else {
i++;
}
if (i == 5) {
delay(100);
goto exitL;
}
}
}exitL:
if (debug) {
state = data[1];
timegps = data[2];
latitude = data[3];
longitude = data[4];
}
}

String sendData (String command , const int timeout ,boolean debug){
String response = "";
sim808.println(command);
long int time = millis();
int i = 0;

while ( (time+timeout ) > millis()){
while (sim808.available()){
char c = sim808.read();
response +=c;
}
}
if (debug) {
Serial.print(response);
}
return response;
}

void sendMessage(String coll, String posi){
display.clearDisplay();
display.setCursor(1, 0);
display.println("Sending SOS Alert...");
gsm.println("AT+CMGF=1"); //Sets the GSM Module in Text Mode
ShowSerialData();
gsm.print("AT+CMGS=\"");
gsm.print(cellno);
gsm.println("\"\r");
ShowSerialData();

sms = coll + " Collision Detect!!!";
sms += "Position of vehicle is "+ posi;
sms += ". Vehicle can be found at:";

if(latitude.toDouble()!= 0.0 || longitude.toDouble()!= 0.0 ){
sms += "https://maps.google.com/maps?q="+latitude+","+longitude;
}else{
sms += "https://maps.google.com/maps?q=22.355318,91.838569";
}

gsm.println(sms);// The SMS text you want to send
delay(100);
gsm.println((char)26);// ASCII code of CTRL+Z
ShowSerialData();
gsm.println("AT+CMGF=0"); //Sets the GSM Module in PDU Mode
ShowSerialData();
display.println("SMS sent");
display.display();
Serial.println("SMS sent");
delay(5000);
}

void ShowSerialData() //Show serial data
{ delay(1000);
while(sim808.available()!=0)
Serial.write(sim808.read());
Serial.println();
}
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom