How to display the assigned IP of your device

Good afternoon Blynk Community, I have what seems like a really simple question, How do i get my IP address which has been assigned to my device? I have been looking and looking for a technique, I have only found ways of doing this with a standalone nodeMCU. BUT i am using an Arduino Mega 2560 with an ESP8266 Breakout Module.

I have also found techniques such as WiFi.localIP(); but atlas this just comes up with a ’localIP’ was not declared in this scope

I just need to get the IP so i can display it on my OLED display.

As stated this shouldn’t be this hard. But i am new with the whole ESP8266 and Blynk.

Heres hoping you guys can help me…

What i have done - I have searched google, and the blynk forums for well over 5 to 6 hours now - Couldnt find anything :confused:

I am lazy enough that I would just sign into my router and check the ip. Or I believe you can assign an ip in your sketch if you need a specific ip.

yeah that i know, and i am that lazy lol, but i want to display it aswell on my OLED screen, I dont want to assign it a static IP because i plan on moving it around to different locations all of which have different services :slight_smile:

Thank you for your reply!

Try

WiFi.localIP().toString() 

when i use that i get this error

error: ‘WiFi’ was not declared in this scope
IP = WiFi.localIP().toString();
^
exit status 1
‘WiFi’ was not declared in this scope

Add the following to setup right above Blynk.begin().

WiFi.mode(WIFI_STA);

I hope it has nothing to do with the libraries you’re using. I have the following.

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

hmm, i followed the blynk sketch builder and it had the libraries

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

and when i take out my libraries and place yours i get the following

Arduino/libraries/ESP8266WiFi/src/ESP8266WiFiType.h:26:19: fatal error: queue.h: No such file or directory
 #include <queue.h>

                   ^
compilation terminated.
exit status 1
Error compiling for board Arduino/Genuino Mega or Mega 2560.

Perhaps i need to reinstall the libraries?

hmm downloaded the libraries again and still seem to get the same error.

It seems that in Board, you have specified MEGA and not ESP8266, check it

The code is running on the Arduino Mega, the ESP is running AT firmware to act as a Wi-Fi modem.

I assume that sending AT+CIFSR to the ESP-01 will return the IP address, but I’ve never tried it. The result would probably need to be parsed to extract the IP address.

Pete.

Ahh, ok now I read the first post, sorry my mistake. Still I can’t understand why the user is declaring/including:

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

but I have zero experience with the combination of mega+esp8266…

He wasn’t originally…

If only everyone would see the light and do it our way, the Blynk world would be so much easier, and shares in Espressif would be worth so much more :grinning:

Pete.

1 Like

Not sure what ESP chip you are using, but because it is in transceiver mode, and Blynk communicates over that link all the time, it is not easy (if even possible) to simply run AT commands and parse out the needed results while connected.

I suppose if you disconnect the Blynk link, send AT commands, grab the needed info into a variable, reconnect to Blynk and work from there… but having never tried that myself on my Mega & ESP-01, I am unsure if even that will work :thinking:

Unless you need lots of I/O perhaps just try the ESP in standalone mode (if possible with your breakout board), then it should do the job nicely.

1 Like

Hey all sorry I should have sent a picture of my ESP8266 chip as i know there is a lot of different types out there. I will be trying to send AT-Commands to the chip as i havent tried this yet. Yes I am using the arduino mega because i do need the IO pins, and my chip only has GPIO1 and GPIO2.

Here is a pictures.

Also thank you to everyone responding, i do appreciate all your help!

@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

Good afternoon PeteKnight Thank you SO much for your help! This works beautifully. Like i said in my first post, one would think this was a super simple task lol. sorry for your emotional trauma hahaha. But in all seriousness thank you!

3 Likes

Good, glad it works for your ESP-01 firmware version as well.

Pete.