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);
}

33 comments:

  1. 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. same problem here with Arduino UNO

      Delete
    4. Same problem here using a Mega. I am using the TX1/RX1 pins (18/19) but no such luck...

      Delete
    5. They can be your ports for TX and RX, they could be upside down.

      Delete
    6. They can be your ports for TX and RX, connect TX with RX and RX with TX.

      Delete
    7. Tbm estou com esse problema, ja troquei o TX e RX e continua a mesma coisa

      Delete
    8. For all the person who have this problem,, finally I solved it!!!

      first, check your serial tx/rx
      then change the baud rate at serial1 to 115200

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

      then at serial monitor, make sure you go at 115200 baudrate

      sorry for bad english

      Delete
    9. Or, you can change the baud rate in the sketch to 9600. You may also want to pre-configure the ESP8266 card.

      Delete
  2. 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
  3. 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
  4. What firmware are you using in your esp wifi shield ? AT or doit firmware. Thanks

    ReplyDelete
  5. i'm using AT and my wifi shield is ESP8266-01 and i have a same problem "Time out". my ssid and pass are good. I'm not use ESP-01 for my case

    ReplyDelete
  6. Check the firmware and baudrate of your ESP, it has to be at least 1.1.1 and newer versions are @115200 bps . Send AT command AT+GMR to see the version.

    ReplyDelete
  7. In my case the problem was that I swapped the rx/tx pins. Now it works.

    ReplyDelete
  8. [WiFiEsp] Initializing ESP module
    [WiFiEsp] Initilization successful - 2.0.0
    Attempting to connect to WPA SSID: TROJAN
    [WiFiEsp] Connected to TROJAN
    You're connected to the network
    SSID: TROJAN
    IP Address: 192.168.8.102
    Signal strength (RSSI):-622 dBm

    Starting connection to server...
    [WiFiEsp] Connecting to 192.168.8.101
    Connected to server
    [WiFiEsp] Data packet send error (2)
    [WiFiEsp] Failed to write to socket 3
    [WiFiEsp] Disconnecting 3

    Disconnecting from server...


    Could you please help me to overcome this error?

    ReplyDelete
  9. can you guys provide ur schematic diagram?

    ReplyDelete
  10. This comment has been removed by the author.

    ReplyDelete
  11. On Arduino mega and esp8266 are working, but i changed the "Serial1.begin(9600);" to "Serial1.begin(115200);"

    ReplyDelete
  12. While I am here, I have made a pool automation system working flawlessly for 2 years.
    At that time I was trying to use blynk app to connect my system through local wifi and be able to send data other the internet.
    By the time blynk has turned as a paying system.
    Therfore I am looking for the simplest way to connect my system to the internet? Any suggestions?

    Thanks,

    Antoine

    ReplyDelete
  13. [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

    I getting the same problem. if I switch RX to TX and TX to RX then it connects for a sec and again disconnects
    I new to IOT so Please help me if any one can. thank you.

    ReplyDelete
  14. This comment has been removed by the author.

    ReplyDelete
  15. This comment has been removed by the author.

    ReplyDelete

Note: Only a member of this blog may post a comment.