Unable to get SHT35 working with web server.

T0T4LN00B
Posts: 1
Joined: Thu May 12, 2022 3:58 pm

Unable to get SHT35 working with web server.

Postby T0T4LN00B » Thu May 12, 2022 4:08 pm

I use ESP32-Wrover-B and SHT35. I am trying to get it to display it's readings on web server. I get "Seeed_SHT35 does not name a type" error when I try to compile the code. What am I doing wrong?
  1. #include "WiFi.h"
  2. #include "ESPAsyncWebServer.h"
  3. #include <Arduino.h>
  4. #include <Wire.h>
  5. #include <Seeed_SHT35.h>
  6.  
  7. // Replace with your network credentials
  8. const char* ssid = "MySSID";
  9. const char* password = "MyPassword";
  10.  
  11. SHT35 sht35 = SHT35();
  12.  
  13. // Create AsyncWebServer object on port 80
  14. AsyncWebServer server(80);
  15.  
  16.   String readSHT35Temperature(){
  17.  
  18.   float t = sht35.readTemperature();
  19.  
  20.   if (! sht35.begin(0x44))        // Set to 0x45 for alternate i2c addr
  21. {
  22. Serial.println("SHT35 test");
  23. while (1) delay(1);
  24. }
  25.  
  26.   if (isnan(t)) {    
  27.     Serial.println("Failed to read temperature!");
  28.     return "--";
  29.   }
  30.   else {
  31.     Serial.print("");
  32.     Serial.print("Temperature: ");
  33.     Serial.println(t);
  34.     return String(t);
  35.   }
  36. }
  37.  
  38. String readSHT35Humidity() {
  39.  
  40.   float h = sht35.readHumidity();
  41.   if (isnan(h)) {
  42.     Serial.println("Failed to read Humidity!");
  43.     return "--";
  44.   }
  45.   else {
  46.     Serial.print("Humidity: ");
  47.     Serial.println(h);
  48.     Serial.println("");
  49.     return String(h);
  50.   }
  51. }
  52.  
  53. const char index_html[] PROGMEM = R"rawliteral(
  54. <!DOCTYPE HTML><html>
  55. <head>
  56.   <meta name="viewport" content="width=device-width, initial-scale=1">
  57.   <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
  58.   <style>
  59.     html {
  60.      font-family: Arial;
  61.      display: inline-block;
  62.      margin: 0px auto;
  63.      text-align: center;
  64.     }
  65.     h2 { font-size: 3.0rem; }
  66.     p { font-size: 3.0rem; }
  67.     .units { font-size: 1.2rem; }
  68.     .sht35-labels{
  69.       font-size: 1.5rem;
  70.       vertical-align:middle;
  71.       padding-bottom: 15px;
  72.     }
  73.   </style>
  74. </head>
  75. <body>
  76.   <h2>ESP32 SHT35 Web Server</h2>
  77.   <p>
  78.     <i class="fas fa-thermometer-half" style="color:#059e8a;"></i>
  79.     <span class="sht35-labels">Temperature</span>
  80.     <span id="temperature">%TEMPERATURE%</span>
  81.     <sup class="units">&deg;C</sup>
  82.   </p>
  83.   <p>
  84.     <i class="fas fa-tint" style="color:#00add6;"></i>
  85.     <span class="sht35-labels">Humidity</span>
  86.     <span id="humidity">%HUMIDITY%</span>
  87.     <sup class="units">%</sup>
  88.   </p>
  89. </body>
  90. <script>
  91. setInterval(function ( ) {
  92.   var xhttp = new XMLHttpRequest();
  93.   xhttp.onreadystatechange = function() {
  94.     if (this.readyState == 4 && this.status == 200) {
  95.       document.getElementById("temperature").innerHTML = this.responseText;
  96.     }
  97.   };
  98.   xhttp.open("GET", "/temperature", true);
  99.   xhttp.send();
  100. }, 10000 ) ;
  101.  
  102. setInterval(function ( ) {
  103.   var xhttp = new XMLHttpRequest();
  104.   xhttp.onreadystatechange = function() {
  105.     if (this.readyState == 4 && this.status == 200) {
  106.       document.getElementById("humidity").innerHTML = this.responseText;
  107.     }
  108.   };
  109.   xhttp.open("GET", "/humidity", true);
  110.   xhttp.send();
  111. }, 10000 ) ;
  112. </script>
  113. </html>)rawliteral";
  114.  
  115. // Replaces placeholder with sht35 values
  116. String processor(const String& var){
  117.   //Serial.println(var);
  118.   if(var == "TEMPERATURE"){
  119.     return readSHT35Temperature();
  120.   }
  121.   else if(var == "HUMIDITY"){
  122.     return readSHT35Humidity();
  123.   }
  124.   return String();
  125. }
  126.  
  127. void setup(){
  128.   // Serial port for debugging purposes
  129.   Serial.begin(115200);
  130.  
  131.   // Connect to Wi-Fi
  132.   WiFi.begin(ssid, password);
  133.   while (WiFi.status() != WL_CONNECTED) {
  134.     delay(1000);
  135.     Serial.println("Connecting to WiFi..");
  136.   }
  137.  
  138.   // Print ESP32 Local IP Address
  139.   Serial.println(WiFi.localIP());
  140.  
  141.   // Route for root / web page
  142.   server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
  143.     request->send_P(200, "text/html", index_html, processor);
  144.   });
  145.   server.on("/temperature", HTTP_GET, [](AsyncWebServerRequest *request){
  146.     request->send_P(200, "text/plain", readSHT35Temperature().c_str());
  147.   });
  148.   server.on("/humidity", HTTP_GET, [](AsyncWebServerRequest *request){
  149.     request->send_P(200, "text/plain", readSHT35Humidity().c_str());
  150.   });
  151.  
  152.   // Start server
  153.   server.begin();
  154. }
  155.  
  156. void loop(){
  157.  
  158. }

Who is online

Users browsing this forum: No registered users and 19 guests