How to display the assigned IP of your device

@L4GSpike I had a look at the BlynkSimpleShieldEsp8266.h library and it seems that the IP address should be obtainable.
I then had a look at the sketch builder examples and realised that WiFi object is referenced as ‘wifi’ (all lower case, like this:

ESP8266 wifi(&EspSerial);

So, for the first time ever, I dug-out an Arduino Mega and an ESP-01 module and had a play around :open_mouth:
It turns-out that you can call wifi.getLocalIP(), but the problem is that it returns both the IP and Mac address, plus some unwanted characters that come from the AT command structure. The format looks like this:

+CIFSR:STAIP,"192.168.1.33"
+CIFSR:STAMAC,"60:01:94:10:3c:32"

As the length of the IP address could vary considerably, because leading zeros aren’t used, the results need to be parsed. I knocked-up this (not very elegant) function to do that…

void Parse_AT_Data()
{
  /*  The function call wifi.getLocalIP() returns a string which contains
   *  the IP address and MAC address in this format:
   *  +CIFSR:STAIP,"192.168.1.33"
   *  +CIFSR:STAMAC,"60:01:94:10:3c:32"
   *
   *  This function extracts the IP and MAC addresses as strings and assignes them to 
   *  globally assigned String variables called IP and MAC
   */
  
  String AT_Data = wifi.getLocalIP();
  int firstQuote = AT_Data.indexOf('"')+1;
  int secondQuote = AT_Data.indexOf('"',firstQuote+1);
  IP = AT_Data.substring(firstQuote, secondQuote);

  int thirdQuote = secondQuote+18;
  int fourthQuote = thirdQuote+17;
  MAC = AT_Data.substring(thirdQuote, fourthQuote);
}

As you’ll see, it needs global variables declaring to hold the IP address and MAC address (I threw the MAC address in there for completeness while I was at it).

They go near the top of your your code (before void setup) so that they are global and can be accessed at any point:

String IP; // Global variable to hold the device IP address, assigned by DHCP
String MAC; // Global variable to hold the device MAC address

The full version of the Blynk Blink sketch with this incorporated looks like this:

/*************************************************************
  Download latest Blynk library here:
    https://github.com/blynkkk/blynk-library/releases/latest

  Blynk is a platform with iOS and Android apps to control
  Arduino, Raspberry Pi and the likes over the Internet.
  You can easily build graphic interfaces for all your
  projects by simply dragging and dropping widgets.

    Downloads, docs, tutorials: http://www.blynk.cc
    Sketch generator:           http://examples.blynk.cc
    Blynk community:            http://community.blynk.cc
    Follow us:                  http://www.fb.com/blynkapp
                                http://twitter.com/blynk_app

  Blynk library is licensed under MIT license
  This example code is in public domain.

 *************************************************************
  WARNING!
    It's very tricky to get it working. Please read this article:
    http://help.blynk.cc/hardware-and-libraries/arduino/esp8266-with-at-firmware

  You’ll need:
   - Blynk App (download from AppStore or Google Play)
   - Arduino Mega 2560 board
   - Decide how to connect to Blynk
     (USB, Ethernet, Wi-Fi, Bluetooth, ...)

  There is a bunch of great example sketches included to show you how to get
  started. Think of them as LEGO bricks  and combine them as you wish.
  For example, take the Ethernet Shield sketch and combine it with the
  Servo example, or choose a USB sketch and add a code from SendData
  example.
 *************************************************************/

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial


#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "****"; // Test_Environment

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "****";
char pass[] = "****";
  
String IP; // Global variable to hold the device IP address, assigned by DHCP
String MAC; // Global variable to hold the device MAC address

// Hardware Serial on Mega, Leonardo, Micro...
#define EspSerial Serial1

// or Software Serial on Uno, Nano...
//#include <SoftwareSerial.h>
//SoftwareSerial EspSerial(2, 3); // RX, TX

// Your ESP8266 baud rate:
#define ESP8266_BAUD 115200

ESP8266 wifi(&EspSerial);

void setup()
{
  // Debug console
  Serial.begin(9600);

  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(10);

  Blynk.begin(auth, wifi, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, wifi, ssid, pass, "blynk-cloud.com", 80);
  //Blynk.begin(auth, wifi, ssid, pass, AT_Dataess(192,168,1,100), 8080);

  Parse_AT_Data();
  Serial.println(); 
  Serial.print("Local IP address = ");
  Serial.println(IP);
  Serial.print("MAC address = ");
  Serial.println(MAC);
}


void Parse_AT_Data()
{
  /*  The function call wifi.getLocalIP() returns a string which contains
   *  the IP address and MAC address in this format:
   *  +CIFSR:STAIP,"192.168.1.33"
   *  +CIFSR:STAMAC,"60:01:94:10:3c:32"
   *
   *  This function extracts the IP and MAC addresses as strings and assignes them to 
   *  globally assigned String variables called IP and MAC
   */
  
  String AT_Data = wifi.getLocalIP();
  int firstQuote = AT_Data.indexOf('"')+1;
  int secondQuote = AT_Data.indexOf('"',firstQuote+1);
  IP = AT_Data.substring(firstQuote, secondQuote);

  int thirdQuote = secondQuote+18;
  int fourthQuote = thirdQuote+17;
  MAC = AT_Data.substring(thirdQuote, fourthQuote);
}

void loop()
{
  Blynk.run();
  // You can inject your own code or combine it with other sketches.
  // Check other examples on how to communicate with Blynk. Remember
  // to avoid delay() function!
}

This produces an output like this:

[9] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.6.1 on Arduino Mega

[599] Connecting to UltraNet
[3648] AT version:1.1.0.0(May 11 2016 18:09:56)
SDK version:1.5.4(baaeaebb)
Ai-Thinker Technology Co. Ltd.
Jun 13 2016 11:29:20
OK
[4732] Failed to enable MUX
[7757] +CIFSR:STAIP,"192.168.1.33"
+CIFSR:STAMAC,"60:01:94:10:3c:32"
[7764] Connected to WiFi
[17971] Ready (ping: 11ms).

Local IP address = 192.168.1.33
MAC address = 60:01:94:10:3c:32

and as I said earlier, those IP and MAC values that are outputted to the serial monitor are available as global variables.

You’ll notice that my ESP-01 is running an old version of the AT firmware. I guess it’s possible that later versions produce a different format of output for IP and MAC address. If so then it should be fairly easy to tweak the code to continue working, but give it a try and see if it works.

I’m off for a lie down to recover from the emotional trauma of working with the Arduino/ESP-01 combination :rofl:

Pete.

7 Likes