-->
Page 1 of 2

Call Wifi.begin() before setup() function.

PostPosted: Sun Dec 04, 2016 9:44 am
by mickey
Hi
Can I Call Wifi.begin() before setup() function?

Because when I do I get :
error: 'WiFi' does not name a type

Re: Call Wifi.begin() before setup() function.

PostPosted: Sun Dec 04, 2016 9:50 am
by rudy
You didn't post your code so I don't know what you are doing wrong. Load one of the sample programs. They work. Play with them. Modify them and if it breaks then back up a step.

Re: Call Wifi.begin() before setup() function.

PostPosted: Sun Dec 04, 2016 9:57 am
by AcmeUK
Good advice from Rudy!

Re: Call Wifi.begin() before setup() function.

PostPosted: Sun Dec 04, 2016 10:20 am
by mickey
Hi
Thanks for answers.
I only post my question after I checked one of the example sketches and verified the result so I want to be sure and also to get an explanation what in the setup() function so I can take Wifi.begin() outside setup().
this is the code:
Code: Select all#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>

const char* ssid = "........";
const char* password = "........";
WiFi.begin(ssid, password);
ESP8266WebServer server(80);

const int led = 13;

void handleRoot() {
  digitalWrite(led, 1);
  server.send(200, "text/plain", "hello from esp8266!");
  digitalWrite(led, 0);
}

void handleNotFound(){
  digitalWrite(led, 1);
  String message = "File Not Found\n\n";
  message += "URI: ";
  message += server.uri();
  message += "\nMethod: ";
  message += (server.method() == HTTP_GET)?"GET":"POST";
  message += "\nArguments: ";
  message += server.args();
  message += "\n";
  for (uint8_t i=0; i<server.args(); i++){
    message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  }
  server.send(404, "text/plain", message);
  digitalWrite(led, 0);
}

void setup(void){
  pinMode(led, OUTPUT);
  digitalWrite(led, 0);
  Serial.begin(115200);
  //WiFi.begin(ssid, password);
  Serial.println("");

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  if (MDNS.begin("esp8266")) {
    Serial.println("MDNS responder started");
  }

  server.on("/", handleRoot);

  server.on("/inline", [](){
    server.send(200, "text/plain", "this works as well");
  });

  server.onNotFound(handleNotFound);

  server.begin();
  Serial.println("HTTP server started");
}

void loop(void){
  server.handleClient();
}

and I get this:
HelloServer:8: error: 'WiFi' does not name a type

if I get the WiFi.begin(ssid, password) back to the setup() its all OK.