Advertisement
mayerm001

Get Sensor from DB

Jun 18th, 2023
975
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.01 KB | Software | 0 0
  1. float getActiveSensorTemperature(String serverName)
  2. {
  3.     WiFiClient wclient;
  4.     HTTPClient http;
  5.     String json, timestamp, uname = "";
  6.     float temp = -1;
  7.     char result[64];
  8.     DynamicJsonDocument document(1024);  
  9.     http.begin(wclient, serverName);
  10.     http.addHeader("Content-Type", "application/text");                                  
  11.     int httpResponseCode = http.GET();
  12.     if (httpResponseCode > 0) {
  13.         json = http.getString();
  14.         deserializeJson(document, json);
  15.         JsonArray array = document.as<JsonArray>();
  16.         temp = document[0]["temperature"].as<float>();
  17.         timestamp = document[0]["timestamp"].as<String>();
  18.         uname = document[0]["uname"].as<String>();
  19.         Serial.print(getTimestamp() + " : Room temperature (from database): "); Serial.print(temp); Serial.println(" °C - (" + uname + " - " + timestamp + ")");        
  20.     } else
  21.         Serial.println(getTimestamp() + " : No response " + httpResponseCode);
  22.     http.end();
  23.     return temp;
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement