# Mittelwert umformen ```javascript msg.topic = "mittelwert"; msg.payload = msg.payload[0].mittelwert; return msg; ``` # Mittelwert berechnen ```sql let wertid = 1; let timestamp = Date.now()-60000*5; let select = "SELECT AVG(wert) AS mittelwert FROM sensorwerte WHERE timestamp >"+timestamp+" AND wertid="+wertid; msg.topic = select; return msg; ``` # SQL Insert ```javascript let temperatur = msg.payload.sht.t; let sensorid = 1; let wertid = 1; let timestamp = Date.now(); let insert = "INSERT INTO sensorwerte (timestamp,sensorid,wertid,wert) VALUES ("+timestamp+","+sensorid+","+wertid+","+temperatur+")"; msg.topic = insert; return msg; ``` # SQL Statement ```sql CREATE TABLE sensorwerte(id INTEGER primary key AUTOINCREMENT, timestamp int, sensorid int, wertid int, wert float) ``` ```cpp #include <ArduinoMqttClient.h> #include <WiFiNINA.h> #include <DHT22.h> //define pin data #define pinDATA 0 // SDA, or almost any other I/O pin DHT22 dht22(pinDATA); ///////please enter your sensitive data in the Secret tab/arduino_secrets.h char ssid[] = "#####"; // your network SSID (name) char pass[] = "#####"; // your network password (use for WPA, or use as key for WEP) WiFiClient wifiClient; MqttClient mqttClient(wifiClient); const char broker[] = "digilab.htk.local"; int port = 1883; #include <Wire.h> #include "rgb_lcd.h" rgb_lcd lcd; const int colorR = 255; const int colorG = 0; const int colorB = 255; void setup() { Serial.begin(115200); Wire.begin(); // set up the LCD's number of columns and rows: lcd.begin(16, 2); lcd.setRGB(colorR, colorG, colorB); // Print a message to the LCD. lcd.print("hello, world!"); delay(1000); // attempt to connect to Wifi network: Serial.print("Attempting to connect to WPA SSID: "); Serial.println(ssid); while (WiFi.begin(ssid, pass) != WL_CONNECTED) { // failed, retry Serial.print("."); delay(5000); } Serial.println("You're connected to the network"); Serial.println(); Serial.print("Attempting to connect to the MQTT broker: "); Serial.println(broker); mqttClient.setUsernamePassword("digilab","htklabor"); if (!mqttClient.connect(broker, port)) { Serial.print("MQTT connection failed! Error code = "); Serial.println(mqttClient.connectError()); while (1); } Serial.println("You're connected to the MQTT broker!"); Serial.println(); } void loop() { // set the cursor to column 0, line 1 // (note: line 1 is the second row, since counting begins with 0): float t = dht22.getTemperature(); float h = dht22.getHumidity(); lcd.clear(); lcd.setCursor(0, 0); lcd.print("T: "); lcd.print(t); lcd.print("H: "); lcd.print(h); mqttClient.poll(); mqttClient.beginMessage("Amerhauser/t"); mqttClient.print(t); mqttClient.endMessage(); mqttClient.beginMessage("Amerhauser/h"); mqttClient.print(h); mqttClient.endMessage(); Serial.println("in"); delay(3000); }