WiFiEsp Example - ConnectWPA

This is one of the examples to the WiFiEsp library.

This example shows you how to connect to a WPA2 Personal encrypted 802.11b/g network with an ESP8266 module. Your Arduino's serial monitor will provide information about the connection once it has connected.

It is similar to the Connect With WPA example of the standard Arduino WiFi Shield. If you compare them you will notice very few differences.


Hardware Required

  • Shield-compatible Arduino board.
  • The ESP WiFi shield is highly recommended but any reliable connection of an ESP8266 module should work.


Code

#include "WiFiEsp.h"

// Emulate Serial1 on pins 6/7 if not present
#ifndef HAVE_HWSERIAL1
#include "SoftwareSerial.h"
SoftwareSerial Serial1(6, 7); // RX, TX
#endif

char ssid[] = "Twim";            // your network SSID (name)
char pass[] = "12345678";        // your network password
int status = WL_IDLE_STATUS;     // the Wifi radio's status

void setup() {
  // initialize serial for debugging
  Serial.begin(115200);
  // initialize serial for ESP module
  Serial1.begin(9600);
  // initialize ESP module
  WiFi.init(&Serial1);

  // check for the presence of the shield
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue
    while (true);
  }

  // attempt to connect to WiFi network
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network
    status = WiFi.begin(ssid, pass);
  }

  // you're connected now, so print out the data
  Serial.println("You're connected to the network");
}

void loop()
{
  // check the network connection once every 10 seconds
  Serial.println();
  printCurrentNet();
  printWifiData();
  
  delay(10000);
}

void printWifiData()
{
  // print your WiFi shield's IP address
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print your MAC address
  byte mac[6];
  WiFi.macAddress(mac);
  char buf[20];
  sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", mac[5], mac[4], mac[3], mac[2], mac[1], mac[0]);
  Serial.print("MAC address: ");
  Serial.println(buf);
}

void printCurrentNet()
{
  // print the SSID of the network you're attached to
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print the MAC address of the router you're attached to
  byte bssid[6];
  WiFi.BSSID(bssid);
  char buf[20];
  sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", bssid[5], bssid[4], bssid[3], bssid[2], bssid[1], bssid[0]);
  Serial.print("BSSID: ");
  Serial.println(buf);

  // print the received signal strength
  long rssi = WiFi.RSSI();
  Serial.print("Signal strength (RSSI): ");
  Serial.println(rssi);
}

7 comments:

  1. This blog awesome and i learn a lot about programming from here.The best thing about this blog is that you doing from beginning to experts level.

    Love from Pprogramming

    ReplyDelete
  2. will it work with arduino mega ???

    ReplyDelete
    Replies
    1. Yes it work better with Mega because you can use the hardware serial.

      Delete
    2. I cannot get this board to initialise on the Mega. Is there something special i need to do? I keep getting
      [WiFiEsp] Initializing ESP module
      [WiFiEsp] >>> TIMEOUT >>>
      [WiFiEsp] >>> TIMEOUT >>>
      [WiFiEsp] >>> TIMEOUT >>>
      [WiFiEsp] >>> TIMEOUT >>>
      [WiFiEsp] >>> TIMEOUT >>>
      [WiFiEsp] Cannot initialize ESP module
      [WiFiEsp] >>> TIMEOUT >>>
      [WiFiEsp] No tag found
      WiFi shield not present

      Delete
  3. can you help me? i am planning to have a controller via wifi. i have 3 arduino board and 3 wifi shield. i want one for the access point and the two as clients. how will i do this using the wifiesp library? do you have resources so i can read and learn this things. Big thanks sir!

    ReplyDelete
  4. Hi,
    I am using this example and I am getting message >>> TIMEOUT >>>,[WiFiEsp] Cannot initialize ESP module,[WiFiEsp] >>> TIMEOUT >>>,[WiFiEsp] No tag found,WiFi shield not present

    I use ESP8266-01 connected via ESP-01 adapter. (http://www.dx.com/cs/p/esp-01-esp8266-serial-wi-fi-wireless-module-adapter-module-3-3v-5v-compatible-for-arduino-404644#.WE1jHIWcHIU)

    I connected RX on ESP adapter to 6 and TX to 7. (GND to GND and VCC to 5V). This is why did update --> SoftwareSerial Serial1(6, 7); // RX, TX

    I would really appreciate if you can help/. Thanks a lot.
    Josef

    ReplyDelete
  5. What firmware are you using in your esp wifi shield ? AT or doit firmware. Thanks

    ReplyDelete