Hallo zusammen,
ich habe folgendes Problem:
Ich habe die senseBox mit Blockly “programmiert”.
Wenn ich lediglich das hochladen der Sensordaten auf die openSenseMap programmiere, klappt alles.
Wenn ich lediglich die Ausgabe auf das LED-Display programmiere, klappt alles.
Wenn ich beides jedoch hintereinanderhänge, lädt die SenseBox lediglich die Daten des ersten Sensors hoch, die restlichen Sensordaten fehlen. Die Ausgabe auf dem Display funtioniert einwandfrei.
Was mache ich falsch?
Vielen Dank für eure Hilfe und Viele Grüße
Sebastian
Hier der Code, den ich zusammengepuzzelt habe:
#include "SenseBoxMCU.h"
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
float Temperatur;
float Luftfeuchtigkeit;
float Bodentemperatur;
float Bodenfeuchte;
const long interval = 10000;
long time_start = 0;
long time_actual = 0;
Bee* b = new Bee();
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
HDC1080 hdc;
const char SENSOR_ID967[] PROGMEM = "###bee4967";
static const uint8_t NUM_SENSORS = 1;
const char SENSEBOX_ID [] PROGMEM = "###bee4963";
const char server [] PROGMEM ="ingress.opensensemap.org";
WiFiClient client;
typedef struct measurement {
const char *sensorId;
float value;
} measurement;
char buffer[750];
measurement measurements[NUM_SENSORS];
uint8_t num_measurements = 0;
const int lengthMultiplikator = 35;
const char SENSOR_ID966[] PROGMEM = "###bee4966";
const char SENSOR_ID964[] PROGMEM = "###bee4964";
const char SENSOR_ID965[] PROGMEM = "###bee4965";
void addMeasurement(const char *sensorId, float value) {
measurements[num_measurements].sensorId = sensorId;
measurements[num_measurements].value = value;
num_measurements++;
}
void writeMeasurementsToClient() {
// iterate throug the measurements array
for (uint8_t i = 0; i < num_measurements; i++) {
sprintf_P(buffer, PSTR("%s,%9.2f\n"), measurements[i].sensorId,
measurements[i].value);
// transmit buffer to client
client.print(buffer);
}
// reset num_measurements
num_measurements = 0;
}
void submitValues() {
if (client.connected()) {
client.stop();
delay(10);
}
bool connected = false;
char _server[strlen_P(server)];
strcpy_P(_server, server);
for (uint8_t timeout = 2; timeout != 0; timeout--) {
Serial.println(F("connecting..."));
connected = client.connect(_server, 80);
if (connected == true) {
// construct the HTTP POST request:
sprintf_P(buffer,
PSTR("POST /boxes/%s/data HTTP/1.1\nHost: %s\nContent-Type: "
"text/csv\nConnection: close\nContent-Length: %i\n\n"),
SENSEBOX_ID, server, num_measurements * lengthMultiplikator);
// send the HTTP POST request:
client.print(buffer);
// send measurements
writeMeasurementsToClient();
// send empty line to end the request
client.println();
uint16_t timeout = 0;
// allow the response to be computed
while (timeout <= 5000) {
delay(10);
timeout = timeout + 10;
if (client.available()) {
break;
}
}
num_measurements = 0;
break;
}
}
}
float getSMT50Temperature(int analogPin){
int sensorValue = analogRead(analogPin);
float voltage = sensorValue * (3.3 / 1024.0);
return (voltage - 0.5) * 100;
}
float getSMT50Moisture(int analogPin){
int sensorValue = analogRead(analogPin);
float voltage = sensorValue * (3.3 / 1024.0);
return (voltage * 50) / 3;
}
void setup() {
b->connectToWifi("SSID","Passwort");
delay(1000);
senseBoxIO.powerI2C(true);
delay(2000);
display.begin(SSD1306_SWITCHCAPVCC, 0x3D);
display.display();
delay(100);
display.clearDisplay();
hdc.begin();
}
void loop() {
Temperatur = (float)(hdc.getTemperature());
time_start = millis();
if (time_start > time_actual + interval) {
time_actual = millis();
addMeasurement(SENSOR_ID967,Temperatur);
submitValues();
}
Luftfeuchtigkeit = (float)(hdc.getHumidity());
time_start = millis();
if (time_start > time_actual + interval) {
time_actual = millis();
addMeasurement(SENSOR_ID966,Luftfeuchtigkeit);
submitValues();
}
Bodentemperatur = (float)(getSMT50Temperature(1));
time_start = millis();
if (time_start > time_actual + interval) {
time_actual = millis();
addMeasurement(SENSOR_ID964,Bodentemperatur);
submitValues();
}
Bodenfeuchte = (float)(getSMT50Moisture(2));
time_start = millis();
if (time_start > time_actual + interval) {
time_actual = millis();
addMeasurement(SENSOR_ID965,Bodenfeuchte);
submitValues();
}
display.setCursor(0,0);
display.setTextSize(1);
display.setTextColor(WHITE,BLACK);
display.println("Luftwerte");
display.display();
display.setCursor(0,15);
display.setTextSize(1);
display.setTextColor(WHITE,BLACK);
display.println(hdc.getTemperature());
display.display();
display.setCursor(40,15);
display.setTextSize(1);
display.setTextColor(WHITE,BLACK);
display.println("grad C");
display.display();
display.setCursor(0,30);
display.setTextSize(1);
display.setTextColor(WHITE,BLACK);
display.println(hdc.getHumidity());
display.display();
display.setCursor(40,30);
display.setTextSize(1);
display.setTextColor(WHITE,BLACK);
display.println("rel. F in %");
display.display();
delay(10000);
display.clearDisplay();
display.setCursor(0,0);
display.setTextSize(1);
display.setTextColor(WHITE,BLACK);
display.println("Bodenwerte");
display.display();
display.setCursor(0,15);
display.setTextSize(1);
display.setTextColor(WHITE,BLACK);
display.println(getSMT50Temperature(1));
display.display();
display.setCursor(40,15);
display.setTextSize(1);
display.setTextColor(WHITE,BLACK);
display.println("grad C");
display.display();
display.setCursor(0,30);
display.setTextSize(1);
display.setTextColor(WHITE,BLACK);
display.println(getSMT50Moisture(2));
display.display();
display.setCursor(40,30);
display.setTextSize(1);
display.setTextColor(WHITE,BLACK);
display.println("rel F in %");
display.display();
display.clearDisplay();
delay(10000);
}