Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added ability to change mac address #149

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ It is a cheap version of the Arduino WiFi shield that uses an ESP-01 module to p
- [WebServerAP](https://github.com/bportaluri/WiFiEsp/blob/master/examples/WebServerAP/WebServerAP.ino) - Serve a webpage from the WiFi shield starting a local Access Point
- [WebServerLed](https://github.com/bportaluri/WiFiEsp/blob/master/examples/WebServerLed/WebServerLed.ino) - Turn on and off a led from a webpage
- [UdpNTPClient](https://github.com/bportaluri/WiFiEsp/blob/master/examples/UdpNTPClient/UdpNTPClient.ino) - Query a Network Time Protocol (NTP) server using UDP

- [ChangeMac](https://github.com/habi3000/WiFiEsp/blob/master/examples/ChangeMac/ChangeMac.ino) - Change MAC address temporary

## Supported APIs

Expand Down
46 changes: 46 additions & 0 deletions examples/ChangeMac/ChangeMac.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include "WiFiEsp.h"
#include "SoftwareSerial.h"
int status = WL_IDLE_STATUS; // the Wifi radio's status

void setup() {
// initialize serial for debugging
Serial.begin(115200);
// initialize serial for ESP module "for boards that like uno with only one serial interface, use software serial"
Serial1.begin(115200);
// initialize ESP module
WiFi.init(&Serial1);
delay(1000);
WiFi.setSTAmacAddress("18:fe:35:98:d3:7a",true);
printWifiData();
WiFi.setSTAmacAddress("18:fe:35:98:d3:7b",true);
printWifiData();
WiFi.setSTAmacAddress("18:fe:35:98:d3:7c",true);
printWifiData();
WiFi.setSTAmacAddress("18:fe:35:98:d3:7d",true);
printWifiData();
WiFi.setSTAmacAddress("18:fe:35:98:d3:7e",true);
printWifiData();
WiFi.setSTAmacAddress("18:fe:35:98:d3:7f",true);
printWifiData();
WiFi.setSTAmacAddress("18:fe:35:98:d3:70",true);
printWifiData();
}

void loop() {}

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

2 changes: 1 addition & 1 deletion keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ endPacket KEYWORD2
parsePacket KEYWORD2
remoteIP KEYWORD2
remotePort KEYWORD2

setSTAmacAddress KEYWORD2

#######################################
# Constants (LITERAL1)
Expand Down
12 changes: 12 additions & 0 deletions src/WiFiEsp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,18 @@ uint8_t* WiFiEspClass::macAddress(uint8_t* mac)
return mac;
}

static bool WiFiEspClass::setSTAmacAddress(const char* mac,bool temp){
espMode=1;
if(temp)
if(EspDrv::setMacAddressSTA_TMP(mac))
return true;
else
return false;
if(EspDrv::setMacAddressSTA_PER(mac))
return true;
return false;
}

IPAddress WiFiEspClass::localIP()
{
IPAddress ret;
Expand Down
14 changes: 11 additions & 3 deletions src/WiFiEsp.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,16 @@ class WiFiEspClass
* return: pointer to uint8_t array with length WL_MAC_ADDR_LENGTH
*/
uint8_t* macAddress(uint8_t* mac);

/**

/**
* Set the station MAC address configration saved in flash.
*
* param mac: Pointer to the mac string.
* param temp: Boolean value indicates if the change should be permanent or not
*/
static bool setSTAmacAddress(const char* mac, bool temp);

/**
* Get the interface IP address.
*
* return: Ip address value
Expand All @@ -132,7 +140,7 @@ class WiFiEspClass
*
* return: gateway ip address value
*/
IPAddress gatewayIP();
IPAddress gatewayIP();

/**
* Return the current SSID associated with the network
Expand Down
90 changes: 66 additions & 24 deletions src/utility/EspDrv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ void EspDrv::wifiDriverInit(Stream *espSerial)
EspDrv::espSerial = espSerial;

bool initOK = false;

for(int i=0; i<5; i++)
{
if (sendCmd(F("AT")) == TAG_OK)
Expand Down Expand Up @@ -131,7 +131,7 @@ void EspDrv::reset()

// Show remote IP and port with "+IPD"
sendCmd(F("AT+CIPDINFO=1"));

// Disable autoconnect
// Automatic connection can create problems during initialization phase at next boot
sendCmd(F("AT+CWAUTOCONN=0"));
Expand Down Expand Up @@ -194,7 +194,7 @@ bool EspDrv::wifiStartAP(const char* ssid, const char* pwd, uint8_t channel, uin
LOGWARN1(F("Failed to start AP"), ssid);
return false;
}

if (espMode==2)
sendCmd(F("AT+CWDHCP_CUR=0,1")); // enable DHCP for AP mode
if (espMode==3)
Expand Down Expand Up @@ -225,10 +225,10 @@ void EspDrv::config(IPAddress ip)

// disable station DHCP
sendCmd(F("AT+CWDHCP_CUR=1,0"));

// it seems we need to wait here...
delay(500);

char buf[16];
sprintf_P(buf, PSTR("%d.%d.%d.%d"), ip[0], ip[1], ip[2], ip[3]);

Expand All @@ -244,15 +244,15 @@ void EspDrv::config(IPAddress ip)
void EspDrv::configAP(IPAddress ip)
{
LOGDEBUG(F("> config"));

sendCmd(F("AT+CWMODE_CUR=2"));

// disable station DHCP
sendCmd(F("AT+CWDHCP_CUR=2,0"));

// it seems we need to wait here...
delay(500);

char buf[16];
sprintf_P(buf, PSTR("%d.%d.%d.%d"), ip[0], ip[1], ip[2], ip[3]);

Expand Down Expand Up @@ -353,6 +353,48 @@ uint8_t* EspDrv::getMacAddress()
return _mac;
}

static bool EspDrv::setMacAddressSTA_PER(const char* mac)
{

LOGDEBUG(F("> setMacAddressSTA_PER"));

int ret = sendCmd(F("AT+CIPSTAMAC_DEF=\"%s\""), 20000, mac);

if (ret==TAG_OK)
{
LOGINFO1(F("Changed mac to "), mac);
return true;
}

LOGWARN1(F("Failed changing mac to"), mac);

// clean additional messages logged after the FAIL tag
delay(1000);
espEmptyBuf(false);

return false;
}

static bool EspDrv::setMacAddressSTA_TMP(const char* mac)
{
LOGDEBUG(F("> SetMacAddressSTA_TMP"));

int ret = sendCmd(F("AT+CIPSTAMAC_CUR=\"%s\""), 20000, mac);

if (ret==TAG_OK)
{
LOGINFO1(F("Changed mac to "), mac);
return true;
}

LOGWARN1(F("Failed changing mac to"), mac);

// clean additional messages logged after the FAIL tag
delay(1000);
espEmptyBuf(false);

return false;
}

void EspDrv::getIpAddress(IPAddress& ip)
{
Expand Down Expand Up @@ -459,23 +501,23 @@ uint8_t EspDrv::getScanNetworks()
uint8_t ssidListNum = 0;
int idx;
bool ret = false;


espEmptyBuf();

LOGDEBUG(F("----------------------------------------------"));
LOGDEBUG(F(">> AT+CWLAP"));

espSerial->println(F("AT+CWLAP"));

char buf[100];

idx = readUntil(10000, "+CWLAP:(");

while (idx == NUMESPTAGS)
{
_networkEncr[ssidListNum] = espSerial->parseInt();

// discard , and " characters
readUntil(1000, "\"");

Expand All @@ -485,20 +527,20 @@ uint8_t EspDrv::getScanNetworks()
memset(_networkSsid[ssidListNum], 0, WL_SSID_MAX_LENGTH );
ringBuf.getStrN(_networkSsid[ssidListNum], 1, WL_SSID_MAX_LENGTH-1);
}

// discard , character
readUntil(1000, ",");

_networkRssi[ssidListNum] = espSerial->parseInt();

idx = readUntil(1000, "+CWLAP:(");

if(ssidListNum==WL_NETWORKS_LIST_MAXNUM-1)
break;

ssidListNum++;
}

if (idx==-1)
return -1;

Expand Down Expand Up @@ -576,7 +618,7 @@ bool EspDrv::ping(const char *host)
LOGDEBUG(F("> ping"));

int ret = sendCmd(F("AT+PING=\"%s\""), 8000, host);

if (ret==TAG_OK)
return true;

Expand All @@ -599,7 +641,7 @@ bool EspDrv::startServer(uint16_t port, uint8_t sock)
bool EspDrv::startClient(const char* host, uint16_t port, uint8_t sock, uint8_t protMode)
{
LOGDEBUG2(F("> startClient"), host, port);

// TCP
// AT+CIPSTART=<link ID>,"TCP",<remote IP>,<remote port>

Expand Down Expand Up @@ -685,7 +727,7 @@ uint16_t EspDrv::availData(uint8_t connId)
espSerial->read(); // "
espSerial->read(); // ,
_remotePort = espSerial->parseInt(); // <remote port>

espSerial->read(); // :

LOGDEBUG();
Expand Down Expand Up @@ -762,7 +804,7 @@ bool EspDrv::getData(uint8_t connId, uint8_t *data, bool peek, bool* connClose)
_bufPos = 0;
_connId = 0;
*data = 0;

return false;
}

Expand All @@ -778,14 +820,14 @@ int EspDrv::getDataBuf(uint8_t connId, uint8_t *buf, uint16_t bufSize)

if(_bufPos<bufSize)
bufSize = _bufPos;

for(int i=0; i<bufSize; i++)
{
int c = timedRead();
//LOGDEBUG(c);
if(c==-1)
return -1;

buf[i] = (char)c;
_bufPos--;
}
Expand Down
14 changes: 14 additions & 0 deletions src/utility/EspDrv.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,20 @@ class EspDrv
*/
static uint8_t* getMacAddress();

/*
* Set the station MAC address configration saved in flash.
*
* param mac: Pointer to the mac string.
*/
static bool setMacAddressSTA_PER(const char* mac);

/*
* Set the station MAC address configration not saved in flash.
*
* param mac: Pointer to the mac string.
*/
static bool setMacAddressSTA_TMP(const char* mac);

/*
* Get the interface IP address.
*
Expand Down
Binary file added src/utility/EspDrv.h.gch
Binary file not shown.