Cannot resolve: expected unqualified-id before 'if'

Hello,

Because I have only an ESP32 board and a DHT11 sensor, but not a ready-made code, I tried to join two existing code samples together, one for displaying DHT sensor values (ESP32 with DHT11/DHT22 Temperature and Humidity Sensor using Arduino IDE | Random Nerd Tutorials) and another one made for sending BME280 sensor values to Thingspeak (ESP32 Publish Sensor Readings to ThingSpeak (easiest way) | Random Nerd Tutorials). After joining the two somewhat together, this is what I have in the editor of Arduino IDE (v. 1.8.16):

#include "DHT.h"
#include <WiFi.h>
#include "ThingSpeak.h"


#define DHTPIN 14    

#define DHTTYPE DHT11

const char* ssid = "123459";   // your network SSID (name) 
const char* password = "48GSMKJREGGYE";   // your network password

WiFiClient  client;

unsigned long myChannelNumber = 2;
const char * myWriteAPIKey = "NW3MMODKJS1NGRL2";

// Timer variables 
unsigned long lastTime = 0;
unsigned long timerDelay = 10000;


DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(115200);
  Serial.println(F("DHT11 test!"));

  dht.begin();

  WiFi.mode(WIFI_STA);   
 
 ThingSpeak.begin(client);
}

void loop() {
  
  delay(20000);

 
  float h = dht.readHumidity();
  
  float t = dht.readTemperature();
  
  float f = dht.readTemperature(true);


  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println(F("Failed to read from DHT sensor!"));
    return;
  }

  if(WiFi.status() != WL_CONNECTED){     
    Serial.print("Attempting to connect");
    while(WiFi.status() != WL_CONNECTED){
      WiFi.begin(ssid, password); 
      delay(5000);     
    } 
    Serial.println("\nConnected.");
  }



  float hif = dht.computeHeatIndex(f, h);

  float hic = dht.computeHeatIndex(t, h, false);

  Serial.print(F("Humidity: "));
  Serial.print(h);
  Serial.print(F("%  Temperature: "));
  Serial.print(t);
  Serial.print(F("°C "));
  Serial.print(f);
  Serial.print(F("°F  Heat index: "));
  Serial.print(hic);
  Serial.print(F("°C "));
  Serial.print(hif);
  Serial.println(F("°F"));
}

int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);

if(x == 200){
  Serial.println("Channel update successful.");
}
else{
  Serial.println("Problem updating channel. HTTP error code " + String(x));
}

And then, this is the output:

sketch_dht11esp32:83:1: error: expected unqualified-id before 'if'
 if(x == 200){
 ^~
sketch_dht11esp32:86:1: error: expected unqualified-id before 'else'
 else{
 ^~~~
Multiple libraries were found for "WiFi.h"
 Used: /home/lauri/.arduino15/packages/esp32/hardware/esp32/2.0.0/libraries/WiFi
 Not used: /home/lauri/Arduino/libraries/WiFi
exit status 1
expected unqualified-id before 'if'

I cannot come up with a solution for the if-error. I looked through a similar error post , but my if-statements don't seem to be outside of any bigger function. The notification about the Wifi library I guess is irrelevant for resolving the errors.

Thanks for any advice!

  Serial.print(hif);
  Serial.println(F("°F"));
}

That } closes loop(). But you still have more code following that. That's at least part of the problem and maybe the whole problem.

1 Like

Great. That resolved it, so grateful.