-->
Page 1 of 4

Connecting ESP8266 to my wifi network

PostPosted: Sun Apr 24, 2016 5:37 pm
by Zero_PD
Hi guys,

I'm very familiar with low level microcomputer control and protocol but very unfamiliar with http and internet protocol, so please bear with me.

My professor has tasked me with getting a whole bunch of adafruit huzzah esp8266 boards connected to the internet, and talking with a sever. I have been furiously reading up on HTTP, how the protocol works, POST/GET, etc. With limited success.

So now I'm trying to get this esp8266 connected to my lab's wifi. I wrote a simple code that just tries to connect. I put in the internet name and password correctly but never get into the "connected" loop. The problem is I don't really know how to go about debugging. I haven't been able to find a readme or some documentation of the ESP8266Wifi library, and I struggle to figure it out by reading through the library code. For instance, what exactly is status, and where can I find what it is returning?

Feeling stuck - any advice/suggestions appreciated!

Code: Select all#include <ESP8266WiFi.h>
#include <ESP8266WiFiAP.h>
#include <ESP8266WiFiGeneric.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266WiFiScan.h>
#include <ESP8266WiFiSTA.h>
#include <ESP8266WiFiType.h>
#include <WiFiClient.h>
#include <WiFiClientSecure.h>
#include <WiFiServer.h>
#include <WiFiUdp.h>


char ssid[] = "LOFT";     // the name of your network
char pass[] = "pass";
int status = WL_IDLE_STATUS;     // the Wifi radio's status
byte mac[6];                     // the MAC address of your Wifi shield


void setup()
{
 Serial.begin(9600);
 Serial.println("serial begin");
 delay(1000);
 status = WiFi.begin(ssid, pass);
 delay(1000);
 Serial.println("past wifi begin");

 if ( status != WL_CONNECTED) {
    Serial.println("Couldn't get a wifi connection");
    //while(true);
 }
  else{
  Serial.println("Successfully connected");
  }
}

void loop () {
  Serial.println(getMacAddress());
  delay(1000);
  }

Re: Connecting ESP8266 to my wifi network

PostPosted: Mon Apr 25, 2016 6:22 am
by bbx10node
I suggest starting with an example such as WiFiClientBasic which just requires adding in your SSID and password. The source is included below but it is available via the Arduino IDE. See File | Examples | ESP8266* for more examples.

Code: Select all/*
 *  This sketch sends a message to a TCP server
 *
 */

#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>

ESP8266WiFiMulti WiFiMulti;

void setup() {
    Serial.begin(115200);
    delay(10);

    // We start by connecting to a WiFi network
    WiFiMulti.addAP("SSID", "passpasspass");

    Serial.println();
    Serial.println();
    Serial.print("Wait for WiFi... ");

    while(WiFiMulti.run() != WL_CONNECTED) {
        Serial.print(".");
        delay(500);
    }

    Serial.println("");
    Serial.println("WiFi connected");
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());

    delay(500);
}


void loop() {
    const uint16_t port = 80;
    const char * host = "192.168.1.1"; // ip or dns

   
   
    Serial.print("connecting to ");
    Serial.println(host);

    // Use WiFiClient class to create TCP connections
    WiFiClient client;

    if (!client.connect(host, port)) {
        Serial.println("connection failed");
        Serial.println("wait 5 sec...");
        delay(5000);
        return;
    }

    // This will send the request to the server
    client.print("Send this data to server");

    //read back one line from server
    String line = client.readStringUntil('\r');
    client.println(line);

    Serial.println("closing connection");
    client.stop();
   
    Serial.println("wait 5 sec...");
    delay(5000);
}

Re: Connecting ESP8266 to my wifi network

PostPosted: Mon Apr 25, 2016 7:20 am
by IOT@urremote.com
You only wait 1 second and then test once. It takes longer than that to get connected when using DHCP to get an an IP number.

Try this sketch to configure your network settings through a web browser https://github.com/kentaylor/WiFiManage ... Switch.ino .

Re: Connecting ESP8266 to my wifi network

PostPosted: Mon Apr 25, 2016 8:03 am
by martinayotte
Your current code doesn't wait for the connection to occur, you should add a loop with timeout such as :

Code: Select all 
  int timeout = 60;
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    timeout--;
    if (timeout <= 0) {
      Serial.printf("\r\nWiFi connect aborted !\r\n");
      return false;
    }
  }
  Serial.printf("\r\nWiFi connected !\r\n");
  return true;