· 6 years ago · Nov 03, 2019, 10:12 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
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#include <Adafruit_Sensor.h>
24#include <Adafruit_BMP280.h>
25
26// Replace with your network credentials
27const char* ssid = "Klonowa_3/1";
28const char* password = "baborow123";
29
30// REPLACE with your Domain name and URL path or IP address with path
31const char* serverName = "http://espstation.epizy.com/public_html/post-esp-data.php";
32
33// Keep this API Key value to be compatible with the PHP code provided in the project page.
34// If you change the apiKeyValue value, the PHP file /post-esp-data.php also needs to have the same key
35String apiKeyValue = "tPmAT5Ab3j7F9";
36
37String sensorName = "BMP280";
38String sensorLocation = "Office";
39
40/*#include <SPI.h>
41#define BME_SCK 18
42#define BME_MISO 19
43#define BME_MOSI 23
44#define BME_CS 5*/
45
46#define SEALEVELPRESSURE_HPA (1013.25)
47
48Adafruit_BMP280 bmp; // I2C
49//Adafruit_BME280 bme(BME_CS); // hardware SPI
50//Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI
51
52void setup() {
53 Serial.begin(115200);
54
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
65 // (you can also pass in a Wire library object like &Wire2)
66 bool status = bmp.begin(0x76);
67 if (!status) {
68 Serial.println("Could not find a valid BME280 sensor, check wiring or change I2C address!");
69 while (1);
70 }
71}
72
73void loop() {
74 //Check WiFi connection status
75 if(WiFi.status()== WL_CONNECTED){
76 HTTPClient http;
77
78 // Your Domain name with URL path or IP address with path
79 http.begin(serverName);
80
81 // Specify content-type header
82 http.addHeader("Content-Type", "application/x-www-form-urlencoded");
83
84 // Prepare your HTTP POST request data
85 String httpRequestData = "api_key=" + apiKeyValue + "&sensor=" + sensorName
86 + "&location=" + sensorLocation + "&value1=" + String(bmp.readTemperature())
87 + "&value2=" + String(99.1) + "&value3=" + String(bmp.readPressure()/100.0F) + "";
88 Serial.print("httpRequestData: ");
89 Serial.println(httpRequestData);
90
91 // You can comment the httpRequestData variable above
92 // then, use the httpRequestData variable below (for testing purposes without the BME280 sensor)
93 //String httpRequestData = "api_key=tPmAT5Ab3j7F9&sensor=BME280&location=Office&value1=24.75&value2=49.54&value3=1005.14";
94
95 // Send HTTP POST request
96 int httpResponseCode = http.POST(httpRequestData);
97
98 // If you need an HTTP request with a content type: text/plain
99 //http.addHeader("Content-Type", "text/plain");
100 //int httpResponseCode = http.POST("Hello, World!");
101
102 // If you need an HTTP request with a content type: application/json, use the following:
103 //http.addHeader("Content-Type", "application/json");
104 //int httpResponseCode = http.POST("{\"value1\":\"19\",\"value2\":\"67\",\"value3\":\"78\"}");
105
106 if (httpResponseCode>0) {
107 Serial.print("HTTP Response code: ");
108 Serial.println(httpResponseCode);
109 }
110 else {
111 Serial.print("Error code: ");
112 Serial.println(httpResponseCode);
113 }
114 // Free resources
115 http.end();
116 }
117 else {
118 Serial.println("WiFi Disconnected");
119 }
120 //Send an HTTP POST request every 30 seconds
121 delay(30000);
122}