Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <DFRobot_sim808.h>
- #include <SoftwareSerial.h>
- #define MESSAGE_LENGTH 160
- char message[MESSAGE_LENGTH];
- int messageIndex = 0;
- char MESSAGE[300];
- char lat[12];
- char lon[12];
- char wspeed[12];
- char phone[16] = {'6', '3', '9', '2', '7', '0', '3', '4', '8', '2', '8', '9'};
- char datetime[24];
- //
- #define PIN_TX 50
- #define PIN_RX 52
- SoftwareSerial mySerial(PIN_TX, PIN_RX);
- DFRobot_SIM808 sim808(&mySerial);
- const int switchPin1 = 2;
- const int switchPin2 = 3;
- const int switchPin3 = 4;
- const int switchPin4 = 5;
- const int switchPin5 = 6;
- const int switchPin6 = 7;
- const int switchPin7 = 8;
- int switchState1 = 1;
- int switchState2 = 0;
- int switchState3 = 0;
- int switchState4 = 0;
- int switchState5 = 0;
- int switchState6 = 0;
- int switchState7 = 0;
- const int ledPin1 = 30; // LEFT
- int ledState1 = LOW;
- unsigned long previousMillis1 = 0;
- const int ledPin2 = 32; //RIGHT
- int ledState2 = LOW;
- unsigned long previousMillis2 = 0;
- const long interval = 200; // interval at which to blink (milliseconds)
- //true or false condition
- int counterLeft = 0;
- int counterRight = 0;
- int counterHazard = 0;
- int counter4 = 0;
- bool leftSelected = false;
- bool rightSelected = false;
- bool hazardSelected = false;
- bool brakeSelected = false;
- //buttons
- unsigned long debounceDelay = 10;
- #define buttonPinLeft 22 //BUTTON PIN 1
- int buttonStateLeft;
- int lastButtonStateLeft = LOW;
- unsigned long lastDebounceTimeLeft = 0;
- #define buttonPinRight 24 //BUTTON PIN 2
- int buttonStateRight;
- int lastButtonStateRight = LOW;
- unsigned long lastDebounceTimeRight = 0;
- #define buttonPinHazard 26 //BUTTON PIN 3
- int buttonStateHazard;
- int lastButtonStateHazard = LOW;
- unsigned long lastDebounceTimeHazard = 0;
- #define buttonPinClear 28 //BUTTON PIN 4
- int buttonStateClear;
- int lastButtonStateClear = LOW;
- unsigned long lastDebounceTimeClear = 0;
- const int buttonPin = 34; // BRAKE
- int buttonState = 0; // variable for reading the pushbutton status
- void setup()
- {
- Serial.begin(9600);
- mySerial.begin(9600);
- pinMode(switchPin1, INPUT_PULLUP);
- // pinMode(switchPin2, INPUT_PULLUP);
- // pinMode(switchPin3, INPUT_PULLUP);
- // pinMode(switchPin4, INPUT_PULLUP);
- // pinMode(switchPin5, INPUT);
- // pinMode(switchPin6, INPUT);
- // pinMode(switchPin7, INPUT);
- pinMode(ledPin1, OUTPUT);
- pinMode(ledPin2, OUTPUT);
- pinMode(buttonPinClear, INPUT_PULLUP);
- pinMode(buttonPinLeft, INPUT_PULLUP);
- pinMode(buttonPinHazard, INPUT_PULLUP);
- pinMode(buttonPin, INPUT_PULLUP);
- pinMode(ledPin1, OUTPUT);
- // Serial3.begin(9600);
- //******** Initialize sim808 module *************
- while (!sim808.init())
- {
- Serial.print("Sim808 init error\r\n");
- delay(1000);
- }
- delay(3000);
- if ( sim808.attachGPS())
- Serial.println("Open the GPS power success");
- else
- Serial.println("Open the GPS power failure");
- Serial.println("Sim 808 Init Success, you can now Send SMS to sim808");
- }
- void loop()
- {
- switches();
- control();
- readButtonClear();
- readButtonLeft();
- readButtonRight();
- readButtonHazard();
- readButtonBrake();
- }
- void control()
- {
- if (leftSelected)
- {
- Serial.println("BLINK LEFT TURN");
- led1();
- digitalWrite(ledPin2, LOW);
- counterRight = 0;
- counterHazard = 0;
- //blink left turn
- if (counterLeft == 2)
- {
- digitalWrite(ledPin1, !ledState1);
- digitalWrite(ledPin1, LOW);
- ledState2 = LOW;
- ledState1 = LOW;
- hazardSelected = false;
- leftSelected = false;
- rightSelected = false;
- counterLeft = 0;
- counterRight = 0;
- counterHazard = 0;
- }
- }
- if (rightSelected)
- {
- counterLeft = 0;
- counterHazard = 0;
- Serial.println("BLINK RIGHT TURN");
- led2();
- digitalWrite(ledPin1, LOW);
- if (counterRight == 2)
- {
- digitalWrite(ledPin2, !ledState2);
- digitalWrite(ledPin2, LOW);
- ledState2 = LOW;
- ledState1 = LOW;
- hazardSelected = false;
- leftSelected = false;
- rightSelected = false;
- counterLeft = 0;
- counterRight = 0;
- counterHazard = 0;
- }
- }
- else if (hazardSelected)
- {
- led2();
- led1();
- Serial.println("BLINK hazard ");
- //blink left turn
- if (counterHazard == 2)
- {
- digitalWrite(ledPin1, !ledState1);
- digitalWrite(ledPin2, !ledState2);
- digitalWrite(ledPin1, LOW);
- digitalWrite(ledPin2, LOW);
- ledState2 = LOW;
- ledState1 = LOW;
- hazardSelected = false;
- leftSelected = false;
- rightSelected = false;
- counterLeft = 0;
- counterRight = 0;
- counterHazard = 0;
- }
- }
- else if (brakeSelected)
- {
- led2();
- led1();
- }
- }
- void readButtonClear()
- {
- int reading = digitalRead(buttonPinClear);
- if (reading != lastButtonStateClear)
- {
- lastDebounceTimeClear = millis();
- }
- if ((millis() - lastDebounceTimeClear) > debounceDelay) {
- if (reading != buttonStateClear)
- {
- buttonStateClear = reading;
- if (buttonStateClear == LOW)
- {
- rightSelected = false;
- leftSelected = false;
- hazardSelected = false; digitalWrite(ledPin1, LOW);
- digitalWrite(ledPin2, LOW);
- counterLeft = 0;
- counterRight = 0;
- counterHazard = 0;
- //TURN OFF ALL
- }
- }
- }
- lastButtonStateClear = reading;
- }
- void readButtonLeft()
- {
- int reading = digitalRead(buttonPinLeft);
- if (reading != lastButtonStateLeft)
- {
- lastDebounceTimeLeft = millis();
- }
- if ((millis() - lastDebounceTimeLeft) > debounceDelay)
- {
- if (reading != buttonStateLeft)
- {
- buttonStateLeft = reading;
- if (buttonStateLeft == LOW)
- {
- rightSelected = false;
- leftSelected = true;
- hazardSelected = false;
- counterLeft++;
- }
- }
- }
- lastButtonStateLeft = reading;
- }
- void readButtonRight()
- {
- int reading = digitalRead(buttonPinRight);
- if (reading != lastButtonStateRight)
- {
- lastDebounceTimeRight = millis();
- }
- if ((millis() - lastDebounceTimeRight) > debounceDelay) {
- if (reading != buttonStateRight)
- {
- buttonStateRight = reading;
- if (buttonStateRight == LOW)
- {
- rightSelected = true;
- leftSelected = false;
- hazardSelected = false;
- counterRight++;
- }
- }
- }
- lastButtonStateRight = reading;
- }
- void readButtonHazard()
- {
- int reading = digitalRead(buttonPinHazard);
- if (reading != lastButtonStateHazard)
- {
- lastDebounceTimeHazard = millis();
- }
- if ((millis() - lastDebounceTimeHazard) > debounceDelay)
- {
- if (reading != buttonStateHazard)
- {
- buttonStateHazard = reading;
- if (buttonStateHazard == LOW)
- {
- rightSelected = false;
- leftSelected = false;
- hazardSelected = true;
- counterHazard++;
- }
- }
- }
- lastButtonStateHazard = reading;
- }
- void readButtonBrake()
- {
- buttonState = digitalRead(buttonPin);
- if (buttonState == LOW)
- {
- brakeSelected = true;
- Serial.println("TRUE");
- }
- else
- {
- brakeSelected = false;
- Serial.println("FALSE");
- digitalWrite(ledPin1, LOW);
- digitalWrite(ledPin2, LOW);
- ledState2 = LOW;
- ledState1 = LOW;
- }
- }
- void led1()
- {
- unsigned long currentMillis = millis();
- if (currentMillis - previousMillis1 >= interval) {
- previousMillis1 = currentMillis;
- if (ledState1 == LOW) {
- ledState1 = HIGH;
- } else {
- ledState1 = LOW;
- }
- // set the LED with the ledState of the variable:
- digitalWrite(ledPin1, ledState1);
- }
- }
- void led2()
- {
- unsigned long currentMillis = millis();
- if (currentMillis - previousMillis2 >= interval) {
- previousMillis2 = currentMillis;
- if (ledState2 == LOW) {
- ledState2 = HIGH;
- Serial.println("GG");
- } else {
- ledState2 = LOW;
- }
- // set the LED with the ledState of the variable:
- digitalWrite(ledPin2, ledState2);
- }
- }
- void switches()
- {
- switchState1 = digitalRead(switchPin1);
- switchState2 = digitalRead(switchPin2);
- switchState3 = digitalRead(switchPin3);
- switchState4 = digitalRead(switchPin4);
- switchState5 = digitalRead(switchPin5);
- switchState6 = digitalRead(switchPin6);
- switchState7 = digitalRead(switchPin7);
- if (switchState1 == LOW)
- //if (switchState1 == LOW || switchState2 == HIGH || switchState3 == HIGH || switchState4 == HIGH || switchState5 == HIGH || switchState6 == HIGH || switchState7 == HIGH)
- {
- Serial.println("High");
- getGPS();
- hazardSelected = true;
- }
- }
- void getGPS()
- {
- while (!sim808.getGPS())
- {
- }
- Serial.print(sim808.GPSdata.year);
- Serial.print("/");
- Serial.print(sim808.GPSdata.month);
- Serial.print("/");
- Serial.print(sim808.GPSdata.day);
- Serial.print(" ");
- Serial.print(sim808.GPSdata.hour);
- Serial.print(":");
- Serial.print(sim808.GPSdata.minute);
- Serial.print(":");
- Serial.print(sim808.GPSdata.second);
- Serial.print(":");
- Serial.println(sim808.GPSdata.centisecond);
- Serial.print("latitude :");
- Serial.println(sim808.GPSdata.lat);
- Serial.print("longitude :");
- Serial.println(sim808.GPSdata.lon);
- Serial.print("speed_kph :");
- Serial.println(sim808.GPSdata.speed_kph);
- Serial.print("heading :");
- Serial.println(sim808.GPSdata.heading);
- Serial.println();
- float la = sim808.GPSdata.lat;
- float lo = sim808.GPSdata.lon;
- float ws = sim808.GPSdata.speed_kph;
- dtostrf(la, 6, 2, lat);
- dtostrf(lo, 6, 2, lon);
- dtostrf(ws, 6, 2, wspeed);
- //sim808.detachGPS();
- sendSMS();
- }
- void sendSMS()
- {
- sprintf(MESSAGE, "Latitude : %s\nLongitude : %s\nWind Speed : %s ", lat, lon, wspeed);
- Serial.println("Start to send message ...");
- Serial.println(MESSAGE);
- Serial.println(phone);
- sim808.sendSMS(phone, MESSAGE);
- }
Advertisement
Add Comment
Please, Sign In to add comment