/
opensensemap.h
89 lines (82 loc) · 3.46 KB
/
opensensemap.h
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
/************************************************************************
* Slightly modified code from opensensemap.org. *
* Some variable names and Parameters have been changed to make it *
* work with the OpenNOX Station. *
* Putting the code into a cpp file causes some problems with the *
* Arduino IDE (it apears that the IDE has a Problem with the long *
* string used for storing the webpage) *
* OpenSenseMap code: https://github.com/sensebox/openSenseMap *
************************************************************************/
#include <WiFi.h>
#include <WiFiAP.h>
#include <EEPROM.h>
#include <HTTPClient.h>
//Variables for OpenSense Map
WiFiClient clientsense;
char serveradd[] = "ingress.opensensemap.org";
/************************************************************************
* Open sense map function definitions for the compiler *
************************************************************************/
void postFloatValue (float measurement, int digits, String sensorId, String senseboxId);
void waitForServerResponse ();
void sleep(unsigned long ms);
void postFloatValue (float measurement, int digits, String sensorId, String senseboxId) {
//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 (clientsense.connect(serveradd, 80)) {
Serial.println("connected!");
Serial.println("-------------------------------------");
//HTTP Header aufbauen
clientsense.print("POST /boxes/"); clientsense.print(senseboxId); clientsense.print("/"); clientsense.print(sensorId); clientsense.println(" HTTP/1.1");
clientsense.print("Host:");
clientsense.println(serveradd);
clientsense.println("Content-Type: application/json");
clientsense.println("Connection: close");
clientsense.print("Content-Length: "); clientsense.println(jsonValue.length());
clientsense.println();
//Daten senden
clientsense.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 (clientsense.available()) {
char c = clientsense.read();
Serial.print(c);
}
//Verbindung beenden
if (!clientsense.connected()) {
Serial.println();
Serial.println("--------------");
Serial.println("Disconnecting.");
Serial.println("--------------");
clientsense.stop();
repeat = false;
}
} while (repeat);
}
// millis() rollover fix - http://arduino.stackexchange.com/questions/12587/how-can-i-handle-the-millis-rollover
void sleep(unsigned long ms) { // ms: duration
unsigned long start = millis(); // start: timestamp
for (;;) {
unsigned long now = millis(); // now: timestamp
unsigned long elapsed = now - start; // elapsed: duration
if (elapsed >= ms) // comparing durations: OK
return;
}
}