Advertisement
safwan092

Untitled

Jan 22nd, 2024
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.47 KB | None | 0 0
  1. #include <TinyGPSPlus.h>
  2. #include "DHT.h"
  3.  
  4. int DHTPIN = 21;
  5. #define DHTTYPE DHT11
  6. String s = "www.google.com/maps/dir/";
  7. String Lat = "1.23";
  8. String Lng = "1.23";
  9.  
  10. TinyGPSPlus gps;
  11. DHT dht(DHTPIN, DHTTYPE);
  12.  
  13. void setup() {
  14. Serial.begin(115200);
  15. Serial.println(F("DHTxx test!"));
  16. dht.begin();
  17. setupGPS_GSM();
  18. }
  19.  
  20. void loop() {
  21. readDHT_Sensor();
  22. readGPSDATA_and_show_to_Serial();
  23. }//end of LOOP
  24.  
  25. void setupGPS_GSM() {
  26. Serial2.begin(9600);
  27. Serial2.print("Serial2");
  28. Serial2.println("\r");
  29. Serial2.println("AT\r");
  30. delay(10);
  31. Serial.println(".");
  32. Serial2.println("\r");
  33. Serial2.println("AT+GPS=1\r");
  34. delay(100);
  35. Serial.println("|");
  36. Serial2.println("AT+CREG=2\r");
  37. delay(6000);
  38. Serial.println("|");
  39. //Serial2.print("AT+CREG?\r");
  40. Serial2.println("AT+CGATT=1\r");
  41. delay(6000);
  42. Serial.println("|");
  43. Serial2.println("AT+CGDCONT=1,\"IP\",\"WWW\"\r");
  44. delay(6000);
  45. Serial.println("|");
  46. // Serial2.println("AT+LOCATION=1\r");
  47. Serial2.println("AT+CGACT=1,1\r");
  48. delay(6000);
  49. Serial.println(".");
  50. //Initialize ends
  51. //Initialize GPS
  52. Serial2.println("\r");
  53. Serial2.println("AT+GPS=1\r");
  54. delay(1000);
  55. Serial.println(".");
  56. //Serial2.println("AT+GPSMD=1\r"); // Change to only GPS mode from GPS+BDS, set to 2 to revert to default.
  57. Serial2.println("AT+GPSRD=10\r");
  58. delay(100);
  59. Serial.println(".");
  60. // set SMS mode to text mode
  61. Serial2.println("AT+CMGF=1\r");
  62. delay(1000);
  63. Serial.println("D");
  64. //Serial2.println("AT+LOCATION=2\r");
  65. Serial.println("Setup Executed");
  66. Serial.println(F("FullExample.ino"));
  67. Serial.println(F("An extensive example of many interesting TinyGPSPlus features"));
  68. Serial.print(F("Testing TinyGPSPlus library v. ")); Serial.println(TinyGPSPlus::libraryVersion());
  69. Serial.println(F("by Mikal Hart"));
  70. Serial.println();
  71. Serial.println(F("Sats HDOP Latitude Longitude Fix Date Time Date Alt Course Speed Card Distance Course Card Chars Sentences Checksum"));
  72. Serial.println(F(" (deg) (deg) Age Age (m) --- from GPS ---- ---- to London ---- RX RX Fail"));
  73. Serial.println(F("----------------------------------------------------------------------------------------------------------------------------------------"));
  74. }
  75.  
  76. void readGPSDATA_and_show_to_Serial() {
  77. if (gps.location.isValid()) {
  78. Lat = String(gps.location.lat(), 6);
  79. Lng = String(gps.location.lng(), 6);
  80. Serial.print(Lat);
  81. Serial.print(",");
  82. Serial.println(Lng);
  83. }
  84. static const double LONDON_LAT = 51.508131, LONDON_LON = -0.128002;
  85.  
  86. printInt(gps.satellites.value(), gps.satellites.isValid(), 5);
  87. printFloat(gps.hdop.hdop(), gps.hdop.isValid(), 6, 1);
  88. printFloat(gps.location.lat(), gps.location.isValid(), 11, 6);
  89. printFloat(gps.location.lng(), gps.location.isValid(), 12, 6);
  90. printInt(gps.location.age(), gps.location.isValid(), 5);
  91. printDateTime(gps.date, gps.time);
  92. printFloat(gps.altitude.meters(), gps.altitude.isValid(), 7, 2);
  93. printFloat(gps.course.deg(), gps.course.isValid(), 7, 2);
  94. printFloat(gps.speed.kmph(), gps.speed.isValid(), 6, 2);
  95. printStr(gps.course.isValid() ? TinyGPSPlus::cardinal(gps.course.deg()) : "*** ", 6);
  96.  
  97. unsigned long distanceKmToLondon =
  98. (unsigned long)TinyGPSPlus::distanceBetween(
  99. gps.location.lat(),
  100. gps.location.lng(),
  101. LONDON_LAT,
  102. LONDON_LON) / 1000;
  103. printInt(distanceKmToLondon, gps.location.isValid(), 9);
  104.  
  105. double courseToLondon =
  106. TinyGPSPlus::courseTo(
  107. gps.location.lat(),
  108. gps.location.lng(),
  109. LONDON_LAT,
  110. LONDON_LON);
  111.  
  112. printFloat(courseToLondon, gps.location.isValid(), 7, 2);
  113.  
  114. const char *cardinalToLondon = TinyGPSPlus::cardinal(courseToLondon);
  115.  
  116. printStr(gps.location.isValid() ? cardinalToLondon : "*** ", 6);
  117.  
  118. printInt(gps.charsProcessed(), true, 6);
  119. printInt(gps.sentencesWithFix(), true, 10);
  120. printInt(gps.failedChecksum(), true, 9);
  121. Serial.println();
  122.  
  123. smartDelay(1000);
  124.  
  125. if (millis() > 5000 && gps.charsProcessed() < 10)
  126. Serial.println(F("No GPS data received: check wiring"));
  127.  
  128. }
  129. // This custom version of delay() ensures that the gps object
  130. // is being "fed".
  131. static void smartDelay(unsigned long ms)
  132. {
  133. unsigned long start = millis();
  134. do
  135. {
  136. while (Serial2.available())
  137. gps.encode(Serial2.read());
  138. } while (millis() - start < ms);
  139. }
  140.  
  141. static void printFloat(float val, bool valid, int len, int prec)
  142. {
  143. if (!valid)
  144. {
  145. while (len-- > 1)
  146. Serial.print('*');
  147. Serial.print(' ');
  148. }
  149. else
  150. {
  151. Serial.print(val, prec);
  152. int vi = abs((int)val);
  153. int flen = prec + (val < 0.0 ? 2 : 1); // . and -
  154. flen += vi >= 1000 ? 4 : vi >= 100 ? 3 : vi >= 10 ? 2 : 1;
  155. for (int i = flen; i < len; ++i)
  156. Serial.print(' ');
  157. }
  158. smartDelay(0);
  159. }
  160.  
  161. static void printInt(unsigned long val, bool valid, int len)
  162. {
  163. char sz[32] = "*****************";
  164. if (valid)
  165. sprintf(sz, "%ld", val);
  166. sz[len] = 0;
  167. for (int i = strlen(sz); i < len; ++i)
  168. sz[i] = ' ';
  169. if (len > 0)
  170. sz[len - 1] = ' ';
  171. Serial.print(sz);
  172. smartDelay(0);
  173. }
  174.  
  175. static void printDateTime(TinyGPSDate &d, TinyGPSTime &t)
  176. {
  177. if (!d.isValid())
  178. {
  179. Serial.print(F("********** "));
  180. }
  181. else
  182. {
  183. char sz[32];
  184. sprintf(sz, "%02d/%02d/%02d ", d.month(), d.day(), d.year());
  185. Serial.print(sz);
  186. }
  187.  
  188. if (!t.isValid())
  189. {
  190. Serial.print(F("******** "));
  191. }
  192. else
  193. {
  194. char sz[32];
  195. sprintf(sz, "%02d:%02d:%02d ", t.hour(), t.minute(), t.second());
  196. Serial.print(sz);
  197. }
  198.  
  199. printInt(d.age(), d.isValid(), 5);
  200. smartDelay(0);
  201. }
  202.  
  203. static void printStr(const char *str, int len)
  204. {
  205. int slen = strlen(str);
  206. for (int i = 0; i < len; ++i)
  207. Serial.print(i < slen ? str[i] : ' ');
  208. smartDelay(0);
  209. }
  210.  
  211.  
  212. void readDHT_Sensor() {
  213. float h = dht.readHumidity();
  214. float t = dht.readTemperature();
  215. float f = dht.readTemperature(true);
  216. if (isnan(h) || isnan(t) || isnan(f)) {
  217. Serial.println(F("Failed to read from DHT sensor!"));
  218. return;
  219. }
  220. float hif = dht.computeHeatIndex(f, h);
  221. float hic = dht.computeHeatIndex(t, h, false);
  222. Serial.print(F("Humidity: "));
  223. Serial.print(h);
  224. Serial.print(F("% Temperature: "));
  225. Serial.print(t);
  226. Serial.print(F("°C "));
  227. Serial.print(f);
  228. Serial.print(F("°F Heat index: "));
  229. Serial.print(hic);
  230. Serial.print(F("°C "));
  231. Serial.print(hif);
  232. Serial.println(F("°F"));
  233. }
  234.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement