include Arduino include Wire include SPI include Adafruit_Sensor inclu

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <Arduino.h>
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <Ticker.h>
#define BMP_SCK 13
#define BMP_MISO 12
#define BMP_MOSI 11
#define BMP_CS 10
#define BME280_ADDRESS 0x76
Adafruit_BMP280 sensor01;
float Sensor01_Temperature, Sensor01_Pressure;
const char* ssid = "sidiys-ap-2ghz"; // Enter SSID here
const char* password = "home_passwd"; //Enter Password here
IPAddress ip(172,16,1,230);
IPAddress dns(1,1,1,1);
IPAddress gateway(172,16,1,1);
IPAddress subnet(255,255,255,0);
String DeviceMAC = "DC:4F:22:0A:40:3A";
ESP8266WebServer server(80);
Ticker timer;
void setup() {
Serial.begin(9600);
if (!sensor01.begin(BME280_ADDRESS)) {
Serial.println("Could not find a valid BME280 sensor.");
delay(100);
while(1);
}
WiFi.config(ip, dns, gateway, subnet);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
delay(100);
server.on("/", handle_OnConnect);
server.on("/json", handle_OnSensor01);
server.onNotFound(handle_NotFound);
server.begin();
Serial.println("HTTP server started");
timer.attach(30, SentDataToNarodmon);
}
void loop() {
server.handleClient();
}
void handle_OnConnect() {
Sensor01_Temperature = sensor01.readTemperature();
Sensor01_Pressure = (sensor01.readPressure())/133,322;
server.send(200, "text/html", SendHTML(Sensor01_Temperature, Sensor01_Pressure));
}
void handle_OnSensor01() {
Sensor01_Temperature = sensor01.readTemperature();
Sensor01_Pressure = (sensor01.readPressure())/133,322;
server.send(200, "text/html", SendJSON_Sensor01(Sensor01_Temperature, Sensor01_Pressure));
}
void handle_NotFound(){
server.send(404, "text/plain", "Not found");
}
void SentDataToNarodmon() {
Sensor01_Temperature = sensor01.readTemperature();
Sensor01_Pressure = (sensor01.readPressure())/133,322;
//String query = "http://narodmon.ru/get?ID=" + DeviceMAC + "&T1=" + String(Sensor01_Temperature) + "&P1=" + String(Sensor01_Pressure);
String query = "http://128.30.52.21/HTTP/connection.html";
Serial.println(query);
WiFiClient client;
HTTPClient http;
if(http.begin(client, query)) {
int httpCode = http.GET();
Serial.println(httpCode);
if(httpCode > 0) {
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
String payload = http.getString();
Serial.println(payload);
}
} else {
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
} else {
Serial.println("Error to begin HTTP-query");
}
}
String SendHTML(float Temperature, float Pressure) {
String ptr = "<!DOCTYPE html><html>\n";
ptr +="<head>\n";
ptr +="<meta charset=\"utf-8\">\n";
ptr +="<title>Sidiys Smart Home Sensor #01</title>\n";
ptr +="</head>\n";
ptr +="<body>\n";
ptr +="<h3>Размещение: улица</h3>\n";
ptr +="<h3>Температура: " + String(Temperature) + " &deg;C </h3>\n";
ptr +="<h3>Давление: " + String(Pressure) + " мм рт.ст </h3>\n";
ptr +="</body>\n";
ptr +="</html>\n";
return ptr;
}
String SendJSON_Sensor01(float Temperature, float Pressure){
String ptr = "{\"temperature\": " + String(Temperature) + ", \"pressure\": " + String(Pressure) + "}";
return ptr;
}