Skip to content

Instantly share code, notes, and snippets.

@wmontg5988
Created February 16, 2017 20:41
Show Gist options
  • Save wmontg5988/654337350fd69630b9d4748bcc65b23b to your computer and use it in GitHub Desktop.
Save wmontg5988/654337350fd69630b9d4748bcc65b23b to your computer and use it in GitHub Desktop.
connect esp wpa2
#include "WiFiEsp.h"
// Emulate Serial1 on pins 6/7 if not present
#ifndef HAVE_HWSERIAL2
#include "SoftwareSerial.h"
SoftwareSerial Serial2(6, 7); // RX, TX
#endif
char ssid[] = "free"; // your network SSID (name)
char pass[] = ""; // 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
Serial2.begin(9600);
// initialize ESP module
WiFi.init(&Serial2);
// 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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment