naivxnaivet

JMSJ

Mar 12th, 2020
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.96 KB | None | 0 0
  1. #include <DFRobot_sim808.h>
  2. #include <SoftwareSerial.h>
  3. #define MESSAGE_LENGTH 160
  4. char message[MESSAGE_LENGTH];
  5. int messageIndex = 0;
  6. char MESSAGE[300];
  7. char lat[12];
  8. char lon[12];
  9. char wspeed[12];
  10.  
  11. char phone[16] = {'6', '3', '9', '2', '7', '0', '3', '4', '8', '2', '8', '9'};
  12. char datetime[24];
  13. //
  14. #define PIN_TX 50
  15. #define PIN_RX 52
  16. SoftwareSerial mySerial(PIN_TX, PIN_RX);
  17. DFRobot_SIM808 sim808(&mySerial);
  18.  
  19.  
  20.  
  21. const int switchPin1 = 2;
  22. const int switchPin2 = 3;
  23. const int switchPin3 = 4;
  24. const int switchPin4 = 5;
  25. const int switchPin5 = 6;
  26. const int switchPin6 = 7;
  27. const int switchPin7 = 8;
  28.  
  29. int switchState1 = 1;
  30. int switchState2 = 0;
  31. int switchState3 = 0;
  32. int switchState4 = 0;
  33. int switchState5 = 0;
  34. int switchState6 = 0;
  35. int switchState7 = 0;
  36.  
  37.  
  38.  
  39.  
  40. const int ledPin1 = 30; // LEFT
  41. int ledState1 = LOW;
  42. unsigned long previousMillis1 = 0;
  43.  
  44. const int ledPin2 = 32; //RIGHT
  45. int ledState2 = LOW;
  46. unsigned long previousMillis2 = 0;
  47.  
  48.  
  49. const long interval = 200; // interval at which to blink (milliseconds)
  50.  
  51.  
  52. //true or false condition
  53. int counterLeft = 0;
  54. int counterRight = 0;
  55. int counterHazard = 0;
  56. int counter4 = 0;
  57.  
  58. bool leftSelected = false;
  59. bool rightSelected = false;
  60. bool hazardSelected = false;
  61. bool brakeSelected = false;
  62.  
  63.  
  64.  
  65.  
  66. //buttons
  67. unsigned long debounceDelay = 10;
  68.  
  69.  
  70. #define buttonPinLeft 22 //BUTTON PIN 1
  71. int buttonStateLeft;
  72. int lastButtonStateLeft = LOW;
  73. unsigned long lastDebounceTimeLeft = 0;
  74.  
  75. #define buttonPinRight 24 //BUTTON PIN 2
  76. int buttonStateRight;
  77. int lastButtonStateRight = LOW;
  78. unsigned long lastDebounceTimeRight = 0;
  79.  
  80. #define buttonPinHazard 26 //BUTTON PIN 3
  81. int buttonStateHazard;
  82. int lastButtonStateHazard = LOW;
  83. unsigned long lastDebounceTimeHazard = 0;
  84.  
  85.  
  86. #define buttonPinClear 28 //BUTTON PIN 4
  87. int buttonStateClear;
  88. int lastButtonStateClear = LOW;
  89. unsigned long lastDebounceTimeClear = 0;
  90.  
  91.  
  92. const int buttonPin = 34; // BRAKE
  93. int buttonState = 0; // variable for reading the pushbutton status
  94.  
  95.  
  96. void setup()
  97. {
  98. Serial.begin(9600);
  99. mySerial.begin(9600);
  100. pinMode(switchPin1, INPUT_PULLUP);
  101. // pinMode(switchPin2, INPUT_PULLUP);
  102. // pinMode(switchPin3, INPUT_PULLUP);
  103. // pinMode(switchPin4, INPUT_PULLUP);
  104. // pinMode(switchPin5, INPUT);
  105. // pinMode(switchPin6, INPUT);
  106. // pinMode(switchPin7, INPUT);
  107.  
  108.  
  109. pinMode(ledPin1, OUTPUT);
  110. pinMode(ledPin2, OUTPUT);
  111.  
  112. pinMode(buttonPinClear, INPUT_PULLUP);
  113. pinMode(buttonPinLeft, INPUT_PULLUP);
  114. pinMode(buttonPinHazard, INPUT_PULLUP);
  115. pinMode(buttonPin, INPUT_PULLUP);
  116. pinMode(ledPin1, OUTPUT);
  117.  
  118. // Serial3.begin(9600);
  119. //******** Initialize sim808 module *************
  120. while (!sim808.init())
  121. {
  122. Serial.print("Sim808 init error\r\n");
  123. delay(1000);
  124. }
  125. delay(3000);
  126.  
  127. if ( sim808.attachGPS())
  128. Serial.println("Open the GPS power success");
  129. else
  130. Serial.println("Open the GPS power failure");
  131.  
  132. Serial.println("Sim 808 Init Success, you can now Send SMS to sim808");
  133.  
  134. }
  135. void loop()
  136. {
  137. switches();
  138. control();
  139. readButtonClear();
  140. readButtonLeft();
  141. readButtonRight();
  142. readButtonHazard();
  143. readButtonBrake();
  144. }
  145.  
  146. void control()
  147. {
  148. if (leftSelected)
  149. {
  150.  
  151. Serial.println("BLINK LEFT TURN");
  152. led1();
  153. digitalWrite(ledPin2, LOW);
  154. counterRight = 0;
  155. counterHazard = 0;
  156. //blink left turn
  157. if (counterLeft == 2)
  158. {
  159. digitalWrite(ledPin1, !ledState1);
  160. digitalWrite(ledPin1, LOW);
  161.  
  162. ledState2 = LOW;
  163. ledState1 = LOW;
  164. hazardSelected = false;
  165. leftSelected = false;
  166. rightSelected = false;
  167. counterLeft = 0;
  168. counterRight = 0;
  169. counterHazard = 0;
  170. }
  171. }
  172. if (rightSelected)
  173. {
  174. counterLeft = 0;
  175. counterHazard = 0;
  176. Serial.println("BLINK RIGHT TURN");
  177. led2();
  178. digitalWrite(ledPin1, LOW);
  179.  
  180. if (counterRight == 2)
  181. {
  182. digitalWrite(ledPin2, !ledState2);
  183. digitalWrite(ledPin2, LOW);
  184.  
  185. ledState2 = LOW;
  186. ledState1 = LOW;
  187. hazardSelected = false;
  188. leftSelected = false;
  189. rightSelected = false;
  190. counterLeft = 0;
  191. counterRight = 0;
  192. counterHazard = 0;
  193. }
  194. }
  195.  
  196. else if (hazardSelected)
  197. {
  198.  
  199. led2();
  200. led1();
  201.  
  202. Serial.println("BLINK hazard ");
  203. //blink left turn
  204. if (counterHazard == 2)
  205. {
  206. digitalWrite(ledPin1, !ledState1);
  207. digitalWrite(ledPin2, !ledState2);
  208. digitalWrite(ledPin1, LOW);
  209. digitalWrite(ledPin2, LOW);
  210. ledState2 = LOW;
  211. ledState1 = LOW;
  212. hazardSelected = false;
  213. leftSelected = false;
  214. rightSelected = false;
  215. counterLeft = 0;
  216. counterRight = 0;
  217. counterHazard = 0;
  218. }
  219. }
  220. else if (brakeSelected)
  221. {
  222. led2();
  223. led1();
  224.  
  225. }
  226.  
  227.  
  228.  
  229. }
  230.  
  231. void readButtonClear()
  232. {
  233. int reading = digitalRead(buttonPinClear);
  234. if (reading != lastButtonStateClear)
  235. {
  236. lastDebounceTimeClear = millis();
  237. }
  238. if ((millis() - lastDebounceTimeClear) > debounceDelay) {
  239. if (reading != buttonStateClear)
  240. {
  241. buttonStateClear = reading;
  242. if (buttonStateClear == LOW)
  243. {
  244. rightSelected = false;
  245. leftSelected = false;
  246. hazardSelected = false; digitalWrite(ledPin1, LOW);
  247. digitalWrite(ledPin2, LOW);
  248.  
  249. counterLeft = 0;
  250. counterRight = 0;
  251. counterHazard = 0;
  252.  
  253. //TURN OFF ALL
  254. }
  255. }
  256. }
  257. lastButtonStateClear = reading;
  258. }
  259.  
  260. void readButtonLeft()
  261. {
  262. int reading = digitalRead(buttonPinLeft);
  263. if (reading != lastButtonStateLeft)
  264. {
  265. lastDebounceTimeLeft = millis();
  266. }
  267. if ((millis() - lastDebounceTimeLeft) > debounceDelay)
  268. {
  269. if (reading != buttonStateLeft)
  270. {
  271. buttonStateLeft = reading;
  272. if (buttonStateLeft == LOW)
  273. {
  274. rightSelected = false;
  275. leftSelected = true;
  276. hazardSelected = false;
  277. counterLeft++;
  278. }
  279. }
  280. }
  281. lastButtonStateLeft = reading;
  282. }
  283.  
  284. void readButtonRight()
  285. {
  286. int reading = digitalRead(buttonPinRight);
  287. if (reading != lastButtonStateRight)
  288. {
  289. lastDebounceTimeRight = millis();
  290. }
  291. if ((millis() - lastDebounceTimeRight) > debounceDelay) {
  292. if (reading != buttonStateRight)
  293. {
  294. buttonStateRight = reading;
  295. if (buttonStateRight == LOW)
  296. {
  297. rightSelected = true;
  298. leftSelected = false;
  299. hazardSelected = false;
  300. counterRight++;
  301. }
  302. }
  303. }
  304. lastButtonStateRight = reading;
  305. }
  306.  
  307. void readButtonHazard()
  308. {
  309. int reading = digitalRead(buttonPinHazard);
  310. if (reading != lastButtonStateHazard)
  311. {
  312. lastDebounceTimeHazard = millis();
  313. }
  314. if ((millis() - lastDebounceTimeHazard) > debounceDelay)
  315. {
  316. if (reading != buttonStateHazard)
  317. {
  318. buttonStateHazard = reading;
  319. if (buttonStateHazard == LOW)
  320. {
  321. rightSelected = false;
  322. leftSelected = false;
  323. hazardSelected = true;
  324. counterHazard++;
  325. }
  326. }
  327. }
  328. lastButtonStateHazard = reading;
  329. }
  330.  
  331. void readButtonBrake()
  332. {
  333. buttonState = digitalRead(buttonPin);
  334. if (buttonState == LOW)
  335. {
  336. brakeSelected = true;
  337. Serial.println("TRUE");
  338. }
  339. else
  340. {
  341. brakeSelected = false;
  342. Serial.println("FALSE");
  343.  
  344. digitalWrite(ledPin1, LOW);
  345. digitalWrite(ledPin2, LOW);
  346. ledState2 = LOW;
  347. ledState1 = LOW;
  348.  
  349.  
  350. }
  351. }
  352.  
  353. void led1()
  354. {
  355. unsigned long currentMillis = millis();
  356.  
  357. if (currentMillis - previousMillis1 >= interval) {
  358. previousMillis1 = currentMillis;
  359.  
  360. if (ledState1 == LOW) {
  361. ledState1 = HIGH;
  362. } else {
  363. ledState1 = LOW;
  364. }
  365.  
  366. // set the LED with the ledState of the variable:
  367. digitalWrite(ledPin1, ledState1);
  368. }
  369. }
  370.  
  371. void led2()
  372. {
  373. unsigned long currentMillis = millis();
  374.  
  375. if (currentMillis - previousMillis2 >= interval) {
  376. previousMillis2 = currentMillis;
  377.  
  378. if (ledState2 == LOW) {
  379. ledState2 = HIGH;
  380. Serial.println("GG");
  381. } else {
  382. ledState2 = LOW;
  383. }
  384.  
  385. // set the LED with the ledState of the variable:
  386. digitalWrite(ledPin2, ledState2);
  387. }
  388. }
  389.  
  390. void switches()
  391. {
  392.  
  393. switchState1 = digitalRead(switchPin1);
  394. switchState2 = digitalRead(switchPin2);
  395. switchState3 = digitalRead(switchPin3);
  396. switchState4 = digitalRead(switchPin4);
  397. switchState5 = digitalRead(switchPin5);
  398. switchState6 = digitalRead(switchPin6);
  399. switchState7 = digitalRead(switchPin7);
  400. if (switchState1 == LOW)
  401.  
  402. //if (switchState1 == LOW || switchState2 == HIGH || switchState3 == HIGH || switchState4 == HIGH || switchState5 == HIGH || switchState6 == HIGH || switchState7 == HIGH)
  403. {
  404. Serial.println("High");
  405.  
  406. getGPS();
  407. hazardSelected = true;
  408. }
  409.  
  410. }
  411.  
  412.  
  413.  
  414.  
  415. void getGPS()
  416. {
  417. while (!sim808.getGPS())
  418. {
  419.  
  420. }
  421.  
  422.  
  423. Serial.print(sim808.GPSdata.year);
  424. Serial.print("/");
  425. Serial.print(sim808.GPSdata.month);
  426. Serial.print("/");
  427. Serial.print(sim808.GPSdata.day);
  428. Serial.print(" ");
  429. Serial.print(sim808.GPSdata.hour);
  430. Serial.print(":");
  431. Serial.print(sim808.GPSdata.minute);
  432. Serial.print(":");
  433. Serial.print(sim808.GPSdata.second);
  434. Serial.print(":");
  435. Serial.println(sim808.GPSdata.centisecond);
  436. Serial.print("latitude :");
  437. Serial.println(sim808.GPSdata.lat);
  438. Serial.print("longitude :");
  439. Serial.println(sim808.GPSdata.lon);
  440. Serial.print("speed_kph :");
  441. Serial.println(sim808.GPSdata.speed_kph);
  442. Serial.print("heading :");
  443. Serial.println(sim808.GPSdata.heading);
  444. Serial.println();
  445.  
  446. float la = sim808.GPSdata.lat;
  447. float lo = sim808.GPSdata.lon;
  448. float ws = sim808.GPSdata.speed_kph;
  449.  
  450. dtostrf(la, 6, 2, lat);
  451. dtostrf(lo, 6, 2, lon);
  452. dtostrf(ws, 6, 2, wspeed);
  453.  
  454. //sim808.detachGPS();
  455. sendSMS();
  456. }
  457.  
  458. void sendSMS()
  459. {
  460. sprintf(MESSAGE, "Latitude : %s\nLongitude : %s\nWind Speed : %s ", lat, lon, wspeed);
  461. Serial.println("Start to send message ...");
  462. Serial.println(MESSAGE);
  463. Serial.println(phone);
  464. sim808.sendSMS(phone, MESSAGE);
  465.  
  466.  
  467. }
Advertisement
Add Comment
Please, Sign In to add comment