Advertisement
safwan092

Untitled

Feb 10th, 2023
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.34 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <Adafruit_BMP085.h>
  3. #include <TinyGPSPlus.h>
  4. #include "DHT.h"
  5. #include <HardwareSerial.h>
  6. #include <WiFi.h>
  7. #include <HTTPClient.h>
  8. #include <WiFiClientSecure.h>
  9. #include <UniversalTelegramBot.h>
  10.  
  11. // Replace with your network credentials
  12. const char* ssid = "network";
  13. const char* password = "123456789";
  14. #define BOT_TOKEN "6274904330:AAHFocSs3xDROz6cNMqdMPZXXDvogSR17sE"
  15. WiFiClientSecure secured_client;
  16. UniversalTelegramBot bot(BOT_TOKEN, secured_client);
  17. String chat_id = "473975732";
  18. // Update HOST URL here
  19.  
  20. #define HOST "safetyhelmetiot.000webhostapp.com"
  21.  
  22. String sendhumidity, sendtemp, sendgas, sendpressure, sendhr, sendo_r, sendgpslat, sendgpslon, postData;
  23.  
  24. HardwareSerial SerialPort(1); // use UART1
  25. char c;
  26. String dataIn;
  27. int8_t indexOfA, indexOfB;
  28. String data1, data2;
  29.  
  30. Adafruit_BMP085 bmp;
  31.  
  32. #define DHTPIN 18
  33. #define DHTTYPE DHT11
  34. float h;
  35. float t;
  36. float Pa;
  37. int GAZsensor;
  38. int Buzzer = 19;
  39. int LED = 26 ;
  40. int Gas = 34 ;
  41. String lon;
  42. String latt;
  43. DHT dht(DHTPIN, DHTTYPE);
  44. TinyGPSPlus gps;
  45.  
  46.  
  47. void setup() {
  48. dht.begin();
  49. pinMode (Gas, INPUT);
  50. pinMode(Buzzer, OUTPUT);
  51. pinMode (LED, OUTPUT);
  52. Serial2.begin(9600);
  53. Serial.begin(9600);
  54. SerialPort.begin(9600, SERIAL_8N1, 14, 27);
  55. WiFi.begin(ssid, password);
  56. Serial.println("Connecting");
  57. while (WiFi.status() != WL_CONNECTED) {
  58. delay(500);
  59. Serial.print(".");
  60. }
  61. Serial.println("");
  62. Serial.print("Connected to WiFi network with IP Address: ");
  63. Serial.println(WiFi.localIP());
  64. Serial.print("Retrieving time: ");
  65. configTime(0, 0, "pool.ntp.org"); // get UTC time via NTP
  66. time_t now = time(nullptr);
  67. while (now < 24 * 3600)
  68. {
  69. Serial.print(".");
  70. delay(100);
  71. now = time(nullptr);
  72. }
  73. Serial.println(now);
  74. if (!bmp.begin()) {
  75. Serial.println("Could not find a valid BMP085/BMP180 sensor, check wiring!");
  76. while (1) {}
  77. }
  78. bot.sendMessage(chat_id, "Project Connected to Telegram Server <ONLINE>");
  79. }
  80.  
  81. void loop() {
  82. //::::::::::::::::::::::::::::::::
  83. BMP();
  84. readGPS_Data();
  85. GAS();
  86. TEMP();
  87. Heart();
  88. //bot.sendMessage(chat_id, "Alert Message");
  89. //::::::::::::::::::::::::::::::::
  90. /*
  91. data1;//HR
  92. data2;//OR
  93. Pa ;
  94. GAZsensor ;
  95. h ;
  96. t ;
  97. lon ;
  98. latt;
  99. */
  100. HTTPClient http; // http object of clas HTTPClient
  101.  
  102. // Convert integer variables to string
  103. sendhumidity = String(h);
  104. sendtemp = String(t);
  105. sendgas = String(GAZsensor);
  106. sendpressure = String(Pa);
  107. sendhr = String(data1);
  108. sendo_r = String(data2);
  109. sendgpslat = String(latt);
  110. sendgpslon = String(lon);
  111.  
  112. postData = "sendhumidity=" + sendhumidity + "&sendtemp=" + sendtemp + "&sendgas=" + sendgas + "&sendpressure=" + sendpressure + "&sendhr=" + sendhr + "&sendo_r=" + sendo_r + "&sendgpslat=" + sendgpslat + "&sendgpslon=" + sendgpslon;
  113.  
  114.  
  115. http.begin("http://safetyhelmetiot.000webhostapp.com/dbwrite.php"); // Connect to host where MySQL databse is hosted
  116. http.addHeader("Content-Type", "application/x-www-form-urlencoded"); //Specify content-type header
  117.  
  118. //http://safetyhelmetiot.000webhostapp.com/dbwrite.php?sendhumidity=##&sendtemp=##
  119.  
  120. int httpCode = http.POST(postData); // Send POST request to php file and store server response code in variable named httpCode
  121.  
  122. // if connection eatablished then do this
  123. if (httpCode == 200) {
  124. Serial.println("Values uploaded successfully."); Serial.println(httpCode);
  125. String webpage = http.getString(); // Get html webpage output and store it in a string
  126. Serial.println(webpage + "\n");
  127. }
  128.  
  129. // if failed to connect then return and restart
  130.  
  131. else {
  132. Serial.println(httpCode);
  133. Serial.println("Failed to upload values. \n");
  134. http.end();
  135. return;
  136. }
  137.  
  138. delay(3000);
  139. }
  140. void readGPS_Data() {
  141. while (Serial2.available() > 0)
  142. if (gps.encode(Serial2.read()))
  143. displayInfo();
  144. if (millis() > 5000 && gps.charsProcessed() < 10) {
  145. Serial.println(F("No GPS detected: check wiring."));
  146. while (true);
  147. }
  148. }
  149.  
  150. void displayInfo() {
  151. //Serial.print(F("Location: "));
  152. if (gps.location.isValid()) {
  153. lon = String(gps.location.lng(), 6);
  154. latt = String(gps.location.lat(), 6);
  155. /*
  156. Serial.print("Lat: ");
  157. Serial.print(latt);
  158. Serial.print(F(","));
  159. Serial.print("Lng: ");
  160. Serial.print(lon);
  161. Serial.println();
  162. */
  163. }
  164. else {
  165. // Serial.println(F("INVALID"));
  166. lon = "39.180770";
  167. latt = "21.579805";
  168. }
  169. }
  170. void BMP()
  171. {
  172. Pa = bmp.readPressure();
  173.  
  174. if (bmp.readPressure() > 101432 || bmp.readPressure() < 30000) {
  175. digitalWrite(Buzzer, 1);
  176. digitalWrite(LED, 1);
  177. }
  178. else {
  179. digitalWrite(Buzzer, 0);
  180. digitalWrite(LED, 0);
  181. }
  182. }
  183.  
  184. void GAS()
  185. {
  186.  
  187. GAZsensor = analogRead(Gas);
  188. // Serial.println(F("Air Satuts "));
  189. //Serial.println(GAZsensor);
  190. if (GAZsensor > 1500) {
  191. digitalWrite(Buzzer, HIGH); // turn on
  192. digitalWrite(LED, HIGH);
  193. }
  194. else if (GAZsensor < 1500) {
  195. digitalWrite(Buzzer, LOW); //turn off
  196. digitalWrite(LED, LOW); // turn off
  197. }
  198. }
  199.  
  200. void TEMP()
  201. {
  202. h = dht.readHumidity();
  203. //Serial.println(h);
  204. // Read temperature as Celsius (the default)
  205. t = dht.readTemperature();
  206. //Serial.println(t);
  207.  
  208. if (t > 35 || h > 60) {
  209. digitalWrite(Buzzer, HIGH); // turn on
  210. digitalWrite(LED, HIGH);
  211. } // turn on
  212. else {
  213. digitalWrite(Buzzer, LOW); //turn off
  214. digitalWrite(LED, LOW);
  215. } // turn off
  216. }
  217. void Heart()
  218. {
  219. while (SerialPort.available() > 0) {
  220. c = SerialPort.read();
  221. if (c == '\n') {
  222. break;
  223. }
  224. else {
  225. dataIn += c;
  226. }
  227. }
  228. if (c == '\n') {
  229. parseData();
  230. ShowRxData();
  231. resetData();
  232. }
  233. }
  234.  
  235. void parseData() {
  236. indexOfA = dataIn.indexOf("A");
  237. indexOfB = dataIn.indexOf("B");
  238. data1 = dataIn.substring(0, indexOfA);
  239. data2 = dataIn.substring(indexOfA + 1, indexOfB);
  240. }
  241.  
  242. void ShowRxData() {
  243. Serial.println("HR: " + data1);
  244. Serial.println("O2: " + data2);
  245. Serial.println("Gas: " + String(GAZsensor));
  246. Serial.println("Hum: " + String(h));
  247. Serial.println("Temp: " + String(t));
  248. Serial.println("Pressure: " + String(Pa));
  249. Serial.println("Lat: " + latt);
  250. Serial.println("Lon: " + lon);
  251. Serial.println("####################");
  252. }
  253.  
  254. void resetData() {
  255. c = 0;
  256. dataIn = "";
  257. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement