-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwater-temperature-esp8266.ino
168 lines (143 loc) · 4.15 KB
/
water-temperature-esp8266.ino
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
* Send temperature and power to opensensemap
* connect
* esp8266 and DS18B20 at GPIO 2
*
* Sources
* - https://webnist.de/ds18b20-temperatursensor-am-esp8266-mit-2-aa-batterien-verwenden/
* - https://opensensemap.org/
*
* copy&paste work: Louis Kniefs
* April 2020
*/
//senseBox ID
#define SENSEBOX_ID "***"
#include <ESP8266WiFi.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 2 // GPIO des ESP
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire); // Pass our oneWire reference to Dallas Temperature.
ADC_MODE(ADC_VCC); // ADC an zur Auslesung der Spannung
EspClass ESPm;
// Konstantendefinition
const char* ssid = "***";
const char* pass = "***";
const char* serverIp = "ingress.opensensemap.org";
const int intervall = 1800000000; //Mikrosekunden für Sleep.Mode = 30 Minuten
int conn_time;
WiFiClient client;
void setup()
{
delay(1000);
Serial.begin(115200);
Serial.println("Starte Temperatur Messprogramm");
delay(1000);
sensors.begin(); /* Inizialisieren der Dallas Temperature library */
WiFi.begin(ssid, pass);
Serial.println("Verbindungsaufbau");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
conn_time++;
if (conn_time > 20) {
break;
}
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println("");
Serial.println("Verbunden mit WiFi");
Serial.println("meine IP Addresse: ");
Serial.println(WiFi.localIP());
}
else {
Serial.println("");
Serial.println("keine WiFi Verbindung - kurze Pause");
ESP.deepSleep(100000);
Serial.println("Ende des kurzen Schlafmodus");
}
if (!client.connect(serverIp, 80)) {
Serial.println("Verbindung zu Server fehlgeschlagen");
}
else {
Serial.println("Verbindung zu Server vorhanden");
}
// Abfrage Temperatur
double temperatur;
sensors.requestTemperatures();
temperatur = sensors.getTempCByIndex(0);
//send to opensensemap
const String sensorid_temp = "***";
postFloatValue(temperatur, 1, sensorid_temp);
Serial.println();
Serial.print(temperatur);
Serial.print(" Grad Celsius");
// Batteriespannung messen
uint ADCValue = 0;
String batterie;
ADCValue = ESPm.getVcc() + 144; // hier Korrektur Wert eintragen
float ADCfloat = float(ADCValue);
//float batterie = String(ADCfloat, 2);
//batterie = String(ADCfloat / 1000, 2);
const String sensorid_bat = "***";
postFloatValue(ADCfloat, 1, sensorid_bat);
Serial.println();
Serial.print(batterie);
Serial.print(" V");
// ESP8266 schlafen schicken
Serial.println("ESP geht in Ruhemodus");
ESP.deepSleep(intervall); // Pause
}
void loop()
{
}
void postFloatValue (float measurement, int digits, String sensorId) {
//Float zu String konvertieren
char obs[10];
dtostrf(measurement, 5, digits, obs);
//Json erstellen
String jsonValue = "{\" value\":";
jsonValue += obs;
jsonValue += "}";
//Mit OSeM Server verbinden und POST Operation durchführen
Serial.println("-------------------------------------");
Serial.print("Connectingto OSeM Server...");
if (client.connect(serverIp, 80)) {
Serial.println("connected!");
Serial.println("-------------------------------------");
//HTTP Header aufbauen
client.print("POST /boxes/"); client.print(SENSEBOX_ID); client.print("/"); client.print(sensorId); client.println(" HTTP/1.1");
client.print("Host:");
client.println(serverIp);
client.println("Content-Type: application/json");
client.println("Connection: close");
client.print("Content-Length: "); client.println(jsonValue.length());
client.println();
//Daten senden
client.println(jsonValue);
} else {
Serial.println("failed!");
Serial.println("-------------------------------------");
}
//Antwort von Server im seriellen Monitor anzeigen
waitForServerResponse();
}
void waitForServerResponse () {
//Ankommende Bytes ausgeben
boolean repeat = true;
do {
if (client.available()) {
char c = client.read();
Serial.print(c);
}
//Verbindung beenden
if (!client.connected()) {
Serial.println();
Serial.println("--------------");
Serial.println("Disconnecting.");
Serial.println("--------------");
client.stop();
repeat = false;
}
} while (repeat);
}