Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h>
- #include <Adafruit_BMP085.h>
- #include <TinyGPSPlus.h>
- #include "DHT.h"
- #include <HardwareSerial.h>
- #include <WiFi.h>
- #include <HTTPClient.h>
- #include <WiFiClientSecure.h>
- #include <UniversalTelegramBot.h>
- // Replace with your network credentials
- const char* ssid = "network";
- const char* password = "123456789";
- #define BOT_TOKEN "6274904330:AAHFocSs3xDROz6cNMqdMPZXXDvogSR17sE"
- WiFiClientSecure secured_client;
- UniversalTelegramBot bot(BOT_TOKEN, secured_client);
- String chat_id = "473975732";
- // Update HOST URL here
- #define HOST "safetyhelmetiot.000webhostapp.com"
- String sendhumidity, sendtemp, sendgas, sendpressure, sendhr, sendo_r, sendgpslat, sendgpslon, postData;
- HardwareSerial SerialPort(1); // use UART1
- char c;
- String dataIn;
- int8_t indexOfA, indexOfB;
- String data1, data2;
- Adafruit_BMP085 bmp;
- #define DHTPIN 18
- #define DHTTYPE DHT11
- float h;
- float t;
- float Pa;
- int GAZsensor;
- int Buzzer = 19;
- int LED = 26 ;
- int Gas = 34 ;
- String lon;
- String latt;
- DHT dht(DHTPIN, DHTTYPE);
- TinyGPSPlus gps;
- void setup() {
- dht.begin();
- pinMode (Gas, INPUT);
- pinMode(Buzzer, OUTPUT);
- pinMode (LED, OUTPUT);
- Serial2.begin(9600);
- Serial.begin(9600);
- SerialPort.begin(9600, SERIAL_8N1, 14, 27);
- WiFi.begin(ssid, password);
- Serial.println("Connecting");
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.print("Connected to WiFi network with IP Address: ");
- Serial.println(WiFi.localIP());
- Serial.print("Retrieving time: ");
- configTime(0, 0, "pool.ntp.org"); // get UTC time via NTP
- time_t now = time(nullptr);
- while (now < 24 * 3600)
- {
- Serial.print(".");
- delay(100);
- now = time(nullptr);
- }
- Serial.println(now);
- if (!bmp.begin()) {
- Serial.println("Could not find a valid BMP085/BMP180 sensor, check wiring!");
- while (1) {}
- }
- bot.sendMessage(chat_id, "Project Connected to Telegram Server <ONLINE>");
- }
- void loop() {
- //::::::::::::::::::::::::::::::::
- BMP();
- readGPS_Data();
- GAS();
- TEMP();
- Heart();
- //bot.sendMessage(chat_id, "Alert Message");
- //::::::::::::::::::::::::::::::::
- /*
- data1;//HR
- data2;//OR
- Pa ;
- GAZsensor ;
- h ;
- t ;
- lon ;
- latt;
- */
- HTTPClient http; // http object of clas HTTPClient
- // Convert integer variables to string
- sendhumidity = String(h);
- sendtemp = String(t);
- sendgas = String(GAZsensor);
- sendpressure = String(Pa);
- sendhr = String(data1);
- sendo_r = String(data2);
- sendgpslat = String(latt);
- sendgpslon = String(lon);
- postData = "sendhumidity=" + sendhumidity + "&sendtemp=" + sendtemp + "&sendgas=" + sendgas + "&sendpressure=" + sendpressure + "&sendhr=" + sendhr + "&sendo_r=" + sendo_r + "&sendgpslat=" + sendgpslat + "&sendgpslon=" + sendgpslon;
- http.begin("http://safetyhelmetiot.000webhostapp.com/dbwrite.php"); // Connect to host where MySQL databse is hosted
- http.addHeader("Content-Type", "application/x-www-form-urlencoded"); //Specify content-type header
- //http://safetyhelmetiot.000webhostapp.com/dbwrite.php?sendhumidity=##&sendtemp=##
- int httpCode = http.POST(postData); // Send POST request to php file and store server response code in variable named httpCode
- // if connection eatablished then do this
- if (httpCode == 200) {
- Serial.println("Values uploaded successfully."); Serial.println(httpCode);
- String webpage = http.getString(); // Get html webpage output and store it in a string
- Serial.println(webpage + "\n");
- }
- // if failed to connect then return and restart
- else {
- Serial.println(httpCode);
- Serial.println("Failed to upload values. \n");
- http.end();
- return;
- }
- delay(3000);
- }
- void readGPS_Data() {
- while (Serial2.available() > 0)
- if (gps.encode(Serial2.read()))
- displayInfo();
- if (millis() > 5000 && gps.charsProcessed() < 10) {
- Serial.println(F("No GPS detected: check wiring."));
- while (true);
- }
- }
- void displayInfo() {
- //Serial.print(F("Location: "));
- if (gps.location.isValid()) {
- lon = String(gps.location.lng(), 6);
- latt = String(gps.location.lat(), 6);
- /*
- Serial.print("Lat: ");
- Serial.print(latt);
- Serial.print(F(","));
- Serial.print("Lng: ");
- Serial.print(lon);
- Serial.println();
- */
- }
- else {
- // Serial.println(F("INVALID"));
- lon = "39.180770";
- latt = "21.579805";
- }
- }
- void BMP()
- {
- Pa = bmp.readPressure();
- if (bmp.readPressure() > 101432 || bmp.readPressure() < 30000) {
- digitalWrite(Buzzer, 1);
- digitalWrite(LED, 1);
- }
- else {
- digitalWrite(Buzzer, 0);
- digitalWrite(LED, 0);
- }
- }
- void GAS()
- {
- GAZsensor = analogRead(Gas);
- // Serial.println(F("Air Satuts "));
- //Serial.println(GAZsensor);
- if (GAZsensor > 1500) {
- digitalWrite(Buzzer, HIGH); // turn on
- digitalWrite(LED, HIGH);
- }
- else if (GAZsensor < 1500) {
- digitalWrite(Buzzer, LOW); //turn off
- digitalWrite(LED, LOW); // turn off
- }
- }
- void TEMP()
- {
- h = dht.readHumidity();
- //Serial.println(h);
- // Read temperature as Celsius (the default)
- t = dht.readTemperature();
- //Serial.println(t);
- if (t > 35 || h > 60) {
- digitalWrite(Buzzer, HIGH); // turn on
- digitalWrite(LED, HIGH);
- } // turn on
- else {
- digitalWrite(Buzzer, LOW); //turn off
- digitalWrite(LED, LOW);
- } // turn off
- }
- void Heart()
- {
- while (SerialPort.available() > 0) {
- c = SerialPort.read();
- if (c == '\n') {
- break;
- }
- else {
- dataIn += c;
- }
- }
- if (c == '\n') {
- parseData();
- ShowRxData();
- resetData();
- }
- }
- void parseData() {
- indexOfA = dataIn.indexOf("A");
- indexOfB = dataIn.indexOf("B");
- data1 = dataIn.substring(0, indexOfA);
- data2 = dataIn.substring(indexOfA + 1, indexOfB);
- }
- void ShowRxData() {
- Serial.println("HR: " + data1);
- Serial.println("O2: " + data2);
- Serial.println("Gas: " + String(GAZsensor));
- Serial.println("Hum: " + String(h));
- Serial.println("Temp: " + String(t));
- Serial.println("Pressure: " + String(Pa));
- Serial.println("Lat: " + latt);
- Serial.println("Lon: " + lon);
- Serial.println("####################");
- }
- void resetData() {
- c = 0;
- dataIn = "";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement