0

Io.run 'io' was not declared in this scope
Moderators: adafruit_support_bill, adafruit

Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.
Please be positive and constructive with your questions and comments.

Io.run 'io' was not declared in this scope

by diego1971 on Wed Jan 15, 2020 4:28 am

Good morning I can't understand why the compiler signals the error in question, since 'I' has been defined. I also send the code of my sketch.

Diego

Code: Select all | TOGGLE FULL SIZE
#include "ConfigManager.h"
#include <time.h>
#include "AdafruitIO_WiFi.h"

// Costanti per pagina configurazione Esp8266
const char *settingsHTML = (char *)"/settings.html";
const char *stylesCSS = (char *)"/styles.css";
const char *mainJS = (char *)"/main.js";

// Costanti per tempo
const long utcOffsetInSeconds = 3600;
// Define NTP Client to get time
#include <NTPClient.h>
#include <WiFiUdp.h>
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org", utcOffsetInSeconds);
//Stringhe per tempo
String formattedDate;
String dayStamp;
String timeStamp;

union passaggioDatiSeriale {
  byte by[22];
  struct  {
    byte ind;
    byte ora;
    byte minuti;
    byte secondi;
    byte giorno;
    byte mese;
    byte annoPrimeDueCifre;
    byte annoSecondeDueCifre;
    byte intTemp;
    byte virTemp;
    byte accesoSpento;
  } orarioInternetEAccensione;
} t_passaggioDatiSeriale;


struct Config {
  char name[255];
  bool enabled;
  char mqtt_host[255];
  char mqtt_port[10];
  char mqtt_username[255];
  char mqtt_password[255];
} config;

struct Metadata {
  int8_t version;
} meta;

ConfigManager configManager;

void APCallback(WebServer *server) {
  server->on("/styles.css", HTTPMethod::HTTP_GET, [server]() {
    configManager.streamFile(stylesCSS, mimeCSS);
  });

  DebugPrintln(F("AP Mode Enabled. You can call other functions that should run after a mode is enabled ... "));
}


void APICallback(WebServer *server) {
  server->on("/disconnect", HTTPMethod::HTTP_GET, [server]() {
    configManager.clearWifiSettings(false);
  });

  server->on("/settings.html", HTTPMethod::HTTP_GET, [server]() {
    configManager.streamFile(settingsHTML, mimeHTML);
  });

  // NOTE: css/js can be embedded in a single page HTML
  server->on("/styles.css", HTTPMethod::HTTP_GET, [server]() {
    configManager.streamFile(stylesCSS, mimeCSS);
  });

  server->on("/main.js", HTTPMethod::HTTP_GET, [server]() {
    configManager.streamFile(mainJS, mimeJS);
  });

}

void setup() {
  Serial.begin(115200);
  DebugPrintln(F(""));
  DEBUG_MODE = true;

  meta.version = 3;

  // Setup config manager
  configManager.setAPName("Termostato");
  configManager.setAPFilename("/index.html");

  // Settings variables
  configManager.addParameter("name", config.name, 255);
  configManager.addParameter("mqtt_host", config.mqtt_host, 255);
  configManager.addParameter("mqtt_port", config.mqtt_port, 10);
  configManager.addParameter("mqtt_username", config.mqtt_username, 255);
  configManager.addParameter("mqtt_password", config.mqtt_password, 255, set);

  // Meta Settings
  configManager.addParameter("version", &meta.version, get);

  // Init Callbacks
  configManager.setAPCallback(APCallback);
  configManager.setAPICallback(APICallback);

  configManager.begin(config);

  timeClient.begin();

  char charSSID[64];
  char charPWD[64];

  WiFi.SSID().toCharArray(charSSID, 64);
  WiFi.psk().toCharArray(charPWD, 64);


  AdafruitIO_WiFi io =  AdafruitIO_WiFi(config.mqtt_username, config.mqtt_password, charSSID, charPWD);


  // set up the 'counter' feed
  AdafruitIO_Feed *termostato = io.feed("termostato");
  AdafruitIO_Feed *temperatura = io.feed("temperatura");

  io.connect();
  termostato->onMessage(handleMessage);
  while (io.mqttStatus() < AIO_CONNECTED) {
    Serial.print(".");
    delay(500);
  }
  Serial.println("");
  Serial.print("Mac-Address = ");
  Serial.println(WiFi.macAddress());
  // Because Adafruit IO doesn't support the MQTT retain flag, we can use the
  // get() function to ask IO to resend the last value for this feed to just
  // this MQTT client after the io client is connected.
  termostato->get();

  // we are connected
  Serial.println();
  Serial.println(io.statusText());
  Serial.print("SSID : ");
  Serial.println(WiFi.SSID());
  Serial.print("Password : ");
  Serial.println(WiFi.psk());
}


void loop() {
    io.run();
  configManager.loop();

  delay(5000);
  orarioInternet();
}

void orarioInternet() {
  timeClient.update();
  formattedDate = timeClient.getFormattedDate();

  // Extract date
  int splitT = formattedDate.indexOf("T");
  dayStamp = formattedDate.substring(0, splitT);

  // Preparo l'ora attuale da inviare via Seriale
  t_passaggioDatiSeriale.orarioInternetEAccensione.ind = 1;
  t_passaggioDatiSeriale.orarioInternetEAccensione.ora = timeClient.getHours();
  t_passaggioDatiSeriale.orarioInternetEAccensione.minuti = timeClient.getMinutes();
  t_passaggioDatiSeriale.orarioInternetEAccensione.secondi = timeClient.getSeconds();
  t_passaggioDatiSeriale.orarioInternetEAccensione.giorno = timeClient.getDay();
  // Preparo la data attuale da inviare via Seriale
  t_passaggioDatiSeriale.orarioInternetEAccensione.giorno = dayStamp.substring(8, 10).toInt();
  t_passaggioDatiSeriale.orarioInternetEAccensione.mese = dayStamp.substring(5, 7).toInt();
  t_passaggioDatiSeriale.orarioInternetEAccensione.annoPrimeDueCifre = dayStamp.substring(0, 4).substring(0, 2).toInt();
  t_passaggioDatiSeriale.orarioInternetEAccensione.annoSecondeDueCifre = dayStamp.substring(0, 4).substring(1, 4).toInt();
  Serial.println("Ora attuale = " + String(t_passaggioDatiSeriale.orarioInternetEAccensione.ora) + ":" + String(t_passaggioDatiSeriale.orarioInternetEAccensione.minuti) + ":" + String(t_passaggioDatiSeriale.orarioInternetEAccensione.secondi));
  Serial.println("Data attuale = " + String(t_passaggioDatiSeriale.orarioInternetEAccensione.giorno) + "-" + String(t_passaggioDatiSeriale.orarioInternetEAccensione.mese) + "-" + String(t_passaggioDatiSeriale.orarioInternetEAccensione.annoPrimeDueCifre) + String(t_passaggioDatiSeriale.orarioInternetEAccensione.annoSecondeDueCifre));
  delay(1000);
}

void handleMessage(AdafruitIO_Data *data) {
  Serial.print("received <- ");
  Serial.println(data->value());
  // accesoSpento = data->value();
}

diego1971
 
Posts: 7
Joined: Mon Mar 04, 2019 7:51 am

Re: Io.run 'io' was not declared in this scope

by dastels on Wed Jan 15, 2020 11:15 am

The variable io is declared in the setup function, and you're using it in the loop function. It doesn't exist outside the setup function. Hence the error about it not being declared in the loop function.

Just add:
Code: Select all | TOGGLE FULL SIZE
AdafruitIO_WiFi io;

to just above the setup function.

Then change the line in setup from
Code: Select all | TOGGLE FULL SIZE
AdafruitIO_WiFi io =  AdafruitIO_WiFi(config.mqtt_username, config.mqtt_password, charSSID, charPWD);

to
Code: Select all | TOGGLE FULL SIZE
io =  AdafruitIO_WiFi(config.mqtt_username, config.mqtt_password, charSSID, charPWD);


That should do it.

Dave

dastels
 
Posts: 1643
Joined: Tue Oct 20, 2015 3:22 pm

Re: Io.run 'io' was not declared in this scope

by diego1971 on Thu Jan 16, 2020 4:10 am

Hi Dave, thank you for your reply. I too understood that I had to initialize the IO variable, but I didn't know how. However nonj works it gives me the following error: no matching function for call to

Code: Select all | TOGGLE FULL SIZE
 'AdafruitIO_ESP8266 :: AdafruitIO_ESP8266 ()'


referred to the following instruction:

Code: Select all | TOGGLE FULL SIZE
AdafruitIO_WiFi io;


Which I inserted after the library declaration

Tell me where am I wrong please.
Thank you as of now for your collaboration.

Diego

diego1971
 
Posts: 7
Joined: Mon Mar 04, 2019 7:51 am

Re: Io.run 'io' was not declared in this scope

by dastels on Thu Jan 16, 2020 9:55 am

Gotta love C++. OK, I suggest having a look at the library examples to see how this is done. E.g. https://github.com/adafruit/Adafruit_IO_Arduino/tree/master/examples/adafruitio_02_pubsub.

You'll have to do the entire construction/assignment above setup. That example show how you can do that. In this case, io is declare and assigned in config.h and is then available for use everywhere in the sketch.

Dave

dastels
 
Posts: 1643
Joined: Tue Oct 20, 2015 3:22 pm

Please be positive and constructive with your questions and comments.