neuer Versuch - komplett neu gebauter Code in Blockly, nach Arduino IDE geholt und dort noch den Watchdog hinzugefügt… läuft auf 2 CO2-Ampeln mit WLAN-Bee ab sofort - stay tuned - ich werde berichten! Wer auch spielen / testen möchte:
#include "SenseBoxMCU.h"
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "SparkFun_SCD30_Arduino_Library.h"
#include <Adafruit_SleepyDog.h>
int co2;
float temp;
float hum;
const long interval = 30000;
long time_start = 0;
long time_actual = 0;
unsigned long myTime;
Bee* b = new Bee();
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
SCD30 airSensor;
#include <Adafruit_NeoPixel.h>
Adafruit_NeoPixel rgb_led_1= Adafruit_NeoPixel(3, 1,NEO_GRB + NEO_KHZ800);
const char SENSOR_ID7DE[] PROGMEM = "SENSOR ID VOM CO2 SENSOR"; //bei opensensemap.org auslesen
const char SENSOR_ID7DD[] PROGMEM = "SENSOR ID VOM Temp. SENSOR"; //bei opensensemap.org auslesen
const char SENSOR_ID7DC[] PROGMEM = "SENSOR ID VOM Humi. SENSOR"; //bei opensensemap.org auslesen
static const uint8_t NUM_SENSORS = 3;
const char SENSEBOX_ID [] PROGMEM = "SENSEBOX ID"; //bei opensensemap.org auslesen
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;
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 printOnDisplay(String title1, String measurement1, String unit1, String title2, String measurement2, String unit2) {
display.setCursor(0, 0);
display.setTextSize(1);
display.setTextColor(WHITE, BLACK);
display.println(title1);
display.setCursor(0, 9);
display.setTextSize(2);
display.print(measurement1);
display.print(" ");
display.setTextSize(1);
display.println(unit1);
display.setCursor(0, 28);
display.setTextSize(1);
display.println(title2);
display.setCursor(0, 37);
display.setTextSize(2);
display.print(measurement2);
display.print(" ");
display.setTextSize(1);
display.println(unit2);
display.setCursor(0, 56);
myTime = millis() / 1000 / 60; // in minutes
display.print("Uptime: " + String(myTime / 60) + " h " + String(myTime % 60) + " min");
}
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\nAuthorization: \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;
}
if (connected == false) {
// Reset durchführen
delay(5000);
noInterrupts();
NVIC_SystemReset();
while (1)
;
}
}
}
void setup() {
b->connectToWifi("WLAN SSID hier eintragen","WLAN-Schlüssel hier eintragen");
delay(1000);
senseBoxIO.powerI2C(true);
delay(2000);
display.begin(SSD1306_SWITCHCAPVCC, 0x3D);
display.display();
delay(100);
display.clearDisplay();
Wire.begin();
if (airSensor.begin() == false)
{
while (1)
;
}
rgb_led_1.begin();
rgb_led_1.setBrightness(255);
Watchdog.enable(10000);
}
void loop() {
Watchdog.reset();
co2 = airSensor.getCO2();
temp = airSensor.getTemperature();
hum = airSensor.getHumidity();
if (co2 < 1000) {
rgb_led_1.setPixelColor(0,rgb_led_1.Color(0,255,0)); //GRÜN
rgb_led_1.show();
} else if (co2 >= 1000 && co2 < 1500) {
rgb_led_1.setPixelColor(0,rgb_led_1.Color(255,100,0)); //GELB
rgb_led_1.show();
} else if (co2 >= 1500) {
rgb_led_1.setPixelColor(0,rgb_led_1.Color(255,0,0)); //ROT
rgb_led_1.show();
}
display.clearDisplay();
printOnDisplay("CO2", String(co2), "ppm", "Temperatur", String(temp), "*C");
display.display();
time_start = millis();
if (time_start > time_actual + interval) {
time_actual = millis();
addMeasurement(SENSOR_ID7DE,co2);
addMeasurement(SENSOR_ID7DD,temp);
addMeasurement(SENSOR_ID7DC,hum);
submitValues();
}
}