· 4 years ago · Jun 18, 2021, 10:04 AM
1/*
2 Rui Santos
3 Complete project details at https://RandomNerdTutorials.com/esp32-esp8266-mysql-database-php/
4
5 Permission is hereby granted, free of charge, to any person obtaining a copy
6 of this software and associated documentation files.
7
8 The above copyright notice and this permission notice shall be included in all
9 copies or substantial portions of the Software.
10
11*/
12//#include <SoftwareSerial.h>
13#ifdef ESP32
14 #include <WiFi.h>
15 #include <HTTPClient.h>
16#else
17 #include <ESP8266WiFi.h>
18 #include <ESP8266HTTPClient.h>
19 #include <WiFiClient.h>
20#endif
21
22#include <Wire.h>
23
24
25
26int red_led_pin = 5; //D1
27int green_led_pin = 4; // D2
28int buzzer_pin = 13; // D7
29int smoke_sensor_pin = 0; //A0
30
31// Replace with your network credentials
32const char* ssid = "";
33const char* password = "";
34
35// REPLACE with your Domain name and URL path or IP address with path
36const char* serverName = "http://192.168.132.193/InnovationProject/SMS/sms.php";
37
38// Keep this API Key value to be compatible with the PHP code provided in the project page.
39// If you change the apiKeyValue value, the PHP file /post-esp-data.php also needs to have the same key
40String apiKeyValue = "tPmAT5Ab3j7F9";
41
42String sensorName = "BME280";
43String sensorLocation = "Office";
44
45int statussms = 0;
46
47void setup() {
48 Serial.begin(115200);
49 pinMode(red_led_pin, OUTPUT);
50 pinMode(green_led_pin, OUTPUT);
51 pinMode(buzzer_pin, OUTPUT);
52 pinMode(smoke_sensor_pin, INPUT);
53 int statussms = 0;
54 WiFi.begin(ssid, password);
55 Serial.println("Connecting");
56 while(WiFi.status() != WL_CONNECTED) {
57 delay(500);
58 Serial.print(".");
59 }
60 Serial.println("");
61 Serial.print("Connected to WiFi network with IP Address: ");
62 Serial.println(WiFi.localIP());
63
64 // (you can also pass in a Wire library object like &Wire2)
65//// bool status = bme.begin(0x76);
66// if (!status) {
67// Serial.println("Could not find a valid BME280 sensor, check wiring or change I2C address!");
68// while (1);
69// }
70}
71
72void loop() {
73 //Check WiFi connection status
74 if(WiFi.status()== WL_CONNECTED){
75
76 int analogSensor = analogRead(smoke_sensor_pin);
77
78 if(analogSensor > 561){
79 digitalWrite(red_led_pin, HIGH);
80 digitalWrite(green_led_pin,LOW);
81 tone(buzzer_pin, 1000, 200);
82 if(statussms == 0){
83 HTTPClient http;
84
85
86 // Your Domain name with URL path or IP address with path
87 http.begin(serverName);
88
89 // Specify content-type header
90 http.addHeader("Content-Type", "application/x-www-form-urlencoded");
91
92
93 // You can comment the httpRequestData variable above
94 // then, use the httpRequestData variable below (for testing purposes without the BME280 sensor)
95 String httpRequestData = "api_key=tPmAT5Ab3j7F9&location=Office&value1=" + String(analogSensor) + "";
96
97 // Send HTTP POST request
98 int httpResponseCode = http.POST(httpRequestData);
99
100 // If you need an HTTP request with a content type: text/plain
101 //http.addHeader("Content-Type", "text/plain");
102 //int httpResponseCode = http.POST("Hello, World!");
103
104 // If you need an HTTP request with a content type: application/json, use the following:
105 //http.addHeader("Content-Type", "application/json");
106 //int httpResponseCode = http.POST("{\"value1\":\"19\",\"value2\":\"67\",\"value3\":\"78\"}");
107
108 if (httpResponseCode>0) {
109 Serial.print("HTTP Response code: ");
110 Serial.println(httpResponseCode);
111 }
112 else {
113 Serial.print("Error code: ");
114 Serial.println(httpResponseCode);
115 }
116 // Free resources
117 http.end();
118 }}
119 else{
120 digitalWrite(red_led_pin, LOW);
121 digitalWrite(green_led_pin, HIGH);
122 }
123 }
124 else {
125 Serial.println("WiFi Disconnected");
126 }
127 //Send an HTTP POST request every 30 seconds
128 delay(30000);
129}