Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By vinicivs
#82176 In my code I am receiving a "Wifi is not declared in this scope" error when compiling to a NodeMCU board. The code has some customization, but regarding the WiFi and lines where it calls Wifi functions, it has the same structure to the source code. The source code compiles flawless, what let me think that there is no issue with libraries or any kind of updates. I already reviewed my code many times and don't get the error.

Here follows the complete compiling error:

C:\Users\Administrator\Documents\Arduino\teste_watsoniot\teste_watsoniot.ino: In function 'void setup()':

teste_watsoniot:65:14: error: 'Wifi' was not declared in this scope

if (strcmp(Wifi.SSID().c_str(), ssid) != 0) {

^
teste_watsoniot:73:59: error: 'Wifi' was not declared in this scope

Serial.print("Connected, IP address: "); Serial.println(Wifi.localIP()); ^

exit status 1 'Wifi' was not declared in this scope


Here follows the code:

Code: Select all#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <SimpleTimer.h>
#include "DHT.h"

/* Wi-Fi Information */
const char* ssid = "xxx";
const char* password = "xxx";

/* Watson Configurations */
#define DEVICE_TYPE "xxx"
#define DEVICE_ID "xxx"

#define ORG "xxx"
#define TOKEN "xxx" // this authentication token given with API key

char server[] = ORG ".messaging.internetofthings.ibmcloud.com";
char topic[] = "iot-2/evt/status/fmt/json"; //"iot-2/type/xxx/id/xxx/evt/1-anl/fmt/json";     // customize type and ID
char authMethod[] = "use-token-auth";
//char authMeth[] = "xxx";                                // here a API key
char token[] = TOKEN;                                                   
char clientID[] = "d:" ORG ":" DEVICE_TYPE ":" DEVICE_ID;

/* String to send data */
String Str1 = "hum";
String Str2 = "temp";
String Str3 = "ldrValue1";
String Str4 = "soilValue2";

/* Start Wi-Fi */
WiFiClientSecure wifiClient;
PubSubClient client(server, 1833, wifiClient);

/* TIMER */
SimpleTimer timer;

/* DHT22*/
#define DHTPIN D3 
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
float hum = 0;
float temp = 0;

/* Soil Moister and LDR */
int sensorPin = A0;    // analog input for both sensors
int enable1 = D1;      // enable reading Sensor 1
int enable2 = D2;      // enable reading Sensor 2

int ldrValue1 = 0; 
int soilValue2 = 0; 


void setup()
{
  Serial.begin(115200); Serial.println();

  Serial.print("Conectando na rede ");  Serial.print(ssid);
  if (strcmp(Wifi.SSID().c_str(), ssid) != 0) {
     WiFi.begin(ssid, password);
  }   
  while (WiFi.status() != WL_CONNECTED) {
    delay (500);
    Serial.print (".");
      }
  Serial.println("");
  Serial.print("Connected, IP address: "); Serial.println(Wifi.localIP());

  timer.setInterval(1000L, getDhtData);         
  pinMode(enable1, OUTPUT);
  pinMode(enable2, OUTPUT);
  dht.begin();
}

/* Send to cloud */
void enviaDado(float dado1,float dado2, float dado3, float dado4){
String payload = "{\"d\":{\"" + Str1 + "\":";
 payload += dado1;
 payload += ", \"" + Str2 + "\":";
 payload += dado2;
 payload += ", \"" + Str3 + "\":";
 payload += dado3;
 payload += ", \"" + Str4 + "\":";
 payload += dado4;
 payload += "}}";

 Serial.print("Sending payload: ");
 Serial.println(payload);

 //__ Envia o dado

 if (client.publish(topic, (char*) payload.c_str())) {
    Serial.println("Publish ok");
  } else {
    Serial.println("Publish failed");
  }
 }


void loop()
{
  // Sensor DHT22
  getDhtData();

  // Sensor 1 LDR
  digitalWrite(enable1, HIGH);
  ldrValue1 = analogRead(sensorPin);
  ldrValue1 = constrain(ldrValue1, 300, 850);
  ldrValue1 = map(ldrValue1, 300, 850, 0, 1023);
  Serial.print("Light intensity:  ");
  Serial.println(ldrValue1);
  digitalWrite(enable1, LOW);
  delay(500);

  // Sensor 2 SOIL MOISTURE
  digitalWrite(enable2, HIGH);
  delay(500);
  soilValue2 = analogRead(sensorPin);
  soilValue2 = constrain(soilValue2, 300, 0);
  soilValue2 = map(soilValue2, 300, 0, 0, 100);

  Serial.print("Soil moisture:  ");
  Serial.println(soilValue2);
  Serial.println();
  delay(500);
  digitalWrite(enable2, LOW);

  displayData();
  delay(2000); // delay for getting DHT22 data
  timer.run(); // Initiates SimpleTimer
}


/* Get DHT data */
void getDhtData(void)
{
  float tempIni = temp;
  float humIni = hum;
  temp = dht.readTemperature();
  hum = dht.readHumidity();
  if (isnan(hum) || isnan(temp))   // Check if any reads failed and exit early (to try again).
  {
    Serial.println("Failed to read from DHT sensor!");
    temp = tempIni;
    hum = humIni;
    return;
  }
}

/* display DHT data */

void displayData(void)
{
  Serial.print(" Temperature: ");
  Serial.print(temp);
  Serial.print("oC   Humidity: ");
  Serial.print(hum);
  Serial.println("%");

}


Here is the source code: https://github.com/ibm-watson-iot/devic ... Secure.ino