diff --git a/Software/Sensor_Node/Sensor_Node.ino b/Software/Sensor_Node/Sensor_Node.ino index 28ae758..692e1aa 100644 --- a/Software/Sensor_Node/Sensor_Node.ino +++ b/Software/Sensor_Node/Sensor_Node.ino @@ -10,32 +10,103 @@ #define ETH_CLK_MODE ETH_CLOCK_GPIO17_OUT #define ETH_PHY_POWER 12 -const char* wifi_ssid = "SSID"; -const char* wifi_password = "PASSWORD"; +const char* wifi_ssid = "usrspace"; +const char* wifi_password = "1qay2wsx"; + +float temperature = 0; +float pressure = 0; +float humidity = 0; + +String website = ""; Ticker Ticker_ReadSensors; Ticker Ticker_Service; -WebServer WebServer_Server1(80); +WebServer WebServer_Server(80); -Adafruit_BME280 BME280_Sensor1; +Adafruit_BME280 BME280_Sensor; void setup() { - Serial.begin(115200); - + Serial.begin(115200); // RS232 + Wire.begin (13, 16); // I²C Init + pinMode (34, INPUT); // User Button Ticker_ReadSensors.attach(15, ReadSensors); Ticker_Service.attach(1, Service); + + Serial.println("Starting..."); + + WiFi.begin(wifi_ssid, wifi_password); + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + Serial.println("WiFi connected"); + Serial.println("IP address: "); + Serial.println(WiFi.localIP()); + + WebServer_Server.on("/", handleRoot); + WebServer_Server.on("/metrics", handleMetrics); + WebServer_Server.onNotFound(handleNotFound); + WebServer_Server.begin(); + Serial.println("HTTP server started"); + + if (! BME280_Sensor.begin(0x76)) { + Serial.println("Could not find a valid BME280 sensor, check wiring!"); + while (1); + } + + BME280_Sensor.setSampling(Adafruit_BME280::MODE_FORCED, + Adafruit_BME280::SAMPLING_X1, // temperature + Adafruit_BME280::SAMPLING_X1, // pressure + Adafruit_BME280::SAMPLING_X1, // humidity + Adafruit_BME280::FILTER_OFF); } void loop() { - + WebServer_Server.handleClient(); } void Service() { - + Serial.print("."); } - void ReadSensors() { - + BME280_Sensor.takeForcedMeasurement(); + temperature = BME280_Sensor.readTemperature(); + pressure = (BME280_Sensor.readPressure() / 100.0F); + humidity = BME280_Sensor.readHumidity(); + Serial.print("\nTemperature: "); + Serial.print(temperature); + Serial.print(", Pressure: "); + Serial.print(pressure); + Serial.print(", Humidity: "); + Serial.println(humidity); +} + +void handleRoot() { + website = ""; + website += "

Temperatur: "; + website += temperature; + website += " °C

Luftdruck: "; + website += pressure; + website += " hPa

rel. Luftfeuchte: "; + website += humidity; + website += " %

"; + WebServer_Server.send(200, "text/html", website); +} + +void handleNotFound() { + WebServer_Server.send(404, "text/plain", "Not found!"); +} + +void handleMetrics() { + website = ""; + website += "# HELP temperature The temperature measured in degree Celcius by the specified sensor.\n# TYPE temperature gauge\ntemperature{sensor=\"BME280\",address=\"0x76\"} "; + website += temperature; + website += "\n\n# HELP humidity The relative humidity measured in percent by the specified sensor.\n# TYPE humidity gauge\nhumidity{sensor=\"BME280\",address=\"0x76\"} "; + website += humidity; + website += "\n\n# HELP pressure The air pressure measured in hPa by the specified sensor.\n# TYPE pressure gauge\npressure{sensor=\"BME280\",address=\"0x76\"} "; + website += pressure; + website += "\n"; + WebServer_Server.send(200, "text/plain", website); }