Hallo,
ich hab versucht mit einem ESP 8266 Wemos D1 Daten an die OpenSenseMap zu sendnen, hierzu hab ich den Quellcode von hier als Basis genommen. Da ich erstmal die Funktionalität testen wollte habe ich die Sensoren weggelassen und hab erstmal fixe Werte versucht zu senden:
#include <SPI.h>
#include <ESP8266WiFi.h>
// Wifi Configuration
const char* server = "ingress.opensensemap.org";
WiFiClient client;
const char* ssid = "***";
const char* password = "****";
typedef struct sensor {
const uint8_t ID[12];
} sensor;
uint8_t sensorsIndex = 0;
// Number of sensors
static const uint8_t NUM_SENSORS = 1;
// senseBox ID : ***
const uint8_t SENSEBOX_ID[12] = { 0x**, 0x**, 0x**, 0x**, 0x**, 0x**, 0x**, 0x**, 0x**, 0x**, 0x**, 0x**};
// sensor IDs
const sensor sensors[NUM_SENSORS] = {
// Windspeed: 5cb87bc1c9e9ab001a0a6fdf
{0x**, 0x**, 0x**, 0x**, 0x**, 0x**, 0x**, 0x**, 0x**, 0x**, 0x**, 0x**},
};
uint8_t contentLength = 0;
float values[NUM_SENSORS];
const unsigned int postingInterval = 60000;
void setup() {
sleep(2000);
Serial.begin(9600);
// start the Wifi connection:
Serial.println("SenseBox Home software version 2.1");
Serial.println();
Serial.print("Starting wifi connection...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("done!");
sleep(1000);
Serial.print("Initializing sensors...");
//###
Serial.println("done!");
Serial.println("Starting loop.\n");
}
void loop() {
addValue(1.0);
sleep(200);
submitValues();
sleep(postingInterval);
}
void addValue(const float& value) {
values[sensorsIndex] = value;
sensorsIndex = sensorsIndex + 1;
}
int printHexToStream(const uint8_t* data,
uint8_t length,
Print& stream) // prints 8-bit data in hex
{
byte first;
int j = 0;
for (uint8_t i = 0; i < length; i++) {
first = (data[i] >> 4) | 48;
if (first > 57) {
stream.write(first + (byte)39);
} else {
stream.write(first);
}
j++;
first = (data[i] & 0x0F) | 48;
if (first > 57) {
stream.write(first + (byte)39);
} else {
stream.write(first);
}
j++;
}
return j;
}
int printCsvToStream(Print& stream) {
int len = 0;
for (uint8_t i = 0; i < sensorsIndex; i++) {
if (!isnan(values[i])) {
len = len + printHexToStream(sensors[i].ID, 12, stream);
len = len + stream.print(",");
// do not print digits for illuminance und uv-intensity
if (i < 3)
len = len + stream.println(values[i]);
else
len = len + stream.println(values[i], 0);
}
}
return len;
}
// 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;
}
}
void waitForResponse() {
// if there are incoming bytes from the server, read and print them
sleep(100);
String response = "";
char c;
boolean repeat = true;
do {
if (client.available())
c = client.read();
else
repeat = false;
response += c;
if (response == "HTTP/1.1 ")
response = "";
if (c == '\n')
repeat = false;
} while (repeat);
Serial.print("Server Response: ");
Serial.print(response);
client.flush();
client.stop();
}
void submitValues() {
// close any connection before send a new request.
// This will free the socket on the WiFi shield
Serial.println("__________________________\n");
if (client.connected()) {
client.stop();
sleep(1000);
}
// if there's a successful connection:
if (client.connect(server, 80)) {
Serial.println("connecting...");
// send the HTTP POST request:
client.print(F("POST /boxes/"));
printHexToStream(SENSEBOX_ID, 12, client);
client.println(F("/data HTTP/1.1"));
// !!!!! DO NOT REMOVE !!!!!
// !!!!! NICHT LÖSCHEN !!!!!
// print once to Serial to get the content-length
int contentLen = printCsvToStream(Serial);
// !!!!! DO NOT REMOVE !!!!!
// !!!!! NICHT LÖSCHEN !!!!!
// Send the required header parameters
client.print(F("Host: "));
client.println(server);
client.print(
F("Content-Type: text/csv\nConnection: close\nContent-Length: "));
client.println(contentLen);
client.println();
printCsvToStream(client);
client.println();
Serial.println("done!");
waitForResponse();
// reset index
sensorsIndex = 0;
} else {
// if you couldn't make a connection:
Serial.println("connection failed. Restarting System.");
sleep(5000);
ESP.restart();
}
}
Bis zum erzeugen der “Nachricht” hat das auch geklappt, aber beim Aufruf der Methode “waitForResponse()” kam dann folgende ESP Exception:
Server Response:
Soft WDT reset
>>>stack>>>
ctx: cont
sp: 3ffffd50 end: 3fffffc0 offset: 01b0
3fffff00: 00000000 3ffee630 3fffff40 40100a04
3fffff10: 00c41b4e 00418937 00c41b4e 00000000
3fffff20: 00c41b4e 00000000 00418937 00000000
3fffff30: 3ffe88c5 3ffee630 401001a5 4bc6a7f0
3fffff40: 00000000 00000000 00000000 40203bc0
3fffff50: 003a0c2d 3ffee680 3ffee680 0000001f
3fffff60: 3ffe84e4 3ffee680 3ffee630 3ffee78c
3fffff70: 3fffdad0 00002835 0000ea60 40202572
3fffff80: 3fffdad0 00000000 3ffee75c 40202814
3fffff90: 3f800000 3ffe85bd 3ffee680 40202644
3fffffa0: feefeffe 00000000 3ffee75c 40204460
3fffffb0: feefeffe feefeffe 3ffe8514 40100a61
<<<stack<<<
Kennt jemand das Problem und weiß wie man es löst?
Ich würde micht freuen, weil ich bin langsam mit meinen Kenntnissen am Ende.