WiFiEsp String IPAddress

Hi all!

I'm working with arduino Zero and the WifiEsp library; I need to get the IP address of the ESP and

concatenate it to a String. So the WiFi.localIP() does the job, and I'm able to print it on the Serial

monitor, but if i try to "String" it the result is different.

looking for how the library works, but help is appreciated :) :) :) :)

 WiFiEsp example: ConnectWPA

 This example connects to an encrypted WiFi network using an ESP8266 module.
 Then it prints the  MAC address of the WiFi shield, the IP address obtained
 and other network details.

 For more details see: http://yaab-arduino.blogspot.com/p/wifiesp-example-connect.html

#include "WiFiEsp.h"

char ssid[] = "";            // 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
  // initialize serial for ESP module
  // initialize ESP module

  // 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: ");
    // Connect to WPA/WPA2 network
    status = WiFi.begin(ssid, pass);

  Serial.println("You're connected to the network");

void loop()
  // print the network connection information every 10 seconds

void printWifiData()
  // print your WiFi shield's IP address
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");

  Serial.print("String IP Address: ");


void printCurrentNet()
  // print the SSID of the network you're attached to
  Serial.print("SSID: ");

  // print the MAC address of the router you're attached to
  byte bssid[6];
  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: ");

  // print the received signal strength
 /* long rssi = WiFi.RSSI();
  Serial.print("Signal strength (RSSI): ");


I need to get the IP address of the ESP and concatenate it to a String.

Why do you want to do that?

I never use String so there maybe a better way to do it but this will turn an IPAddress to a String:

String ipString = String(ip[0]) + '.' + String(ip[1]) + '.' + String(ip[2]) + '.' + String(ip[3]);

or a little more lightweight (but maybe more fragmentary?):

 String ipString = String(ip[0]);
  for (byte octet = 1; octet < 4; ++octet) {
    ipString += '.' + String(ip[octet]);

Hi Pert,

thank you for your reply;

I need to put the IP address in a String and send an HTTP Post with JSON. :)

Code works, thank you.

You might consider using a string (char array) instead of String. This will save a good bit of memory and may make your code more reliable:

  char ipCstring[16]; //size the char array large enough to hold 4 x 3 digit numbers + 3 x dot + null terminator
  utoa(ip[0], ipCstring, 10); //put the first octet in the array
  for (byte octetCounter = 1; octetCounter < 4; ++octetCounter) {
    strcat(ipCstring, ".");
    char octetCstring[4]; //size the array for 3 digit number + null terminator
    utoa(ip[octetCounter], octetCstring, 10);  //convert the octet to a string
    strcat(ipCstring, octetCstring);

strings are pretty much just as easy to use as Strings as long as you remember the null terminator and they are much more generally useful.

1 Like


I am attempting to send my IP address via dweet.io.
You said this worked.

String ipString = String(ip[0]);
 for (byte octet = 1; octet < 4; ++octet) {
   ipString += '.' + String(ip[octet]);

How did you set up the JSON to send this. I have everything working except the IP address. It dweets with 10 values.

Below is the code I am working on. Please note I am not a code writer,I am learning. Could you let me know how to add this to my code to successfully dweet my IP. Thanks???

#include <SPI.h>
#include <WiFi101.h>

///////Enter Sensitive Data
char ssid[] = "xxxxx";     //  your network SSID (name) 
char pass[] = "xxxxxx";    // your network password

int status = WL_IDLE_STATUS;     // the WiFi radio's status
int rssi = 0;
int quality;
int ip;
int broadCast;
int WifiStatus;
int absoluteCon;

int GreenLED = 2;  // Button 1
int YellowLED = 3;  // Button 2
int RedLED = 4;  // Button 3

// initialize the library instance:
WiFiClient client;

char server[] = "www.dweet.io";

unsigned long lastConnectionTime = 0;              // last time you connected to the server, in milliseconds
const unsigned long postingInterval = 10L * 1000L; // delay between updates, in milliseconds
                                                  // the "L" is needed to use long type numbers
unsigned char mac[6];

void setup() {
 //PinButtons assignment
 pinMode(GreenLED, INPUT); // Button 1 as Input
 pinMode(YellowLED, INPUT); // Button 2 as Input
 pinMode(RedLED, INPUT); // Button 3 as Input
 //Initialize serial and wait for port to open:
 while (!Serial) {
   ; // wait for serial port to connect. Needed for native USB port only

 // 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: ");
   // Connect to WPA/WPA2 network:
   status = WiFi.begin(ssid, pass);
   // wait 10 seconds for connection:

 // you're connected now, so print out the data:
 Serial.print("You're connected to the network");



void loop() {
 // check the network connection once every 10 seconds:

 // if there's incoming data from the net connection.
 // send it out the serial port.  This is for debugging
 // purposes only:
 if (client.available()) {
   char c = client.read();
 // if ten seconds have passed since your last connection,
 // then connect again and send data:
 if (millis() - lastConnectionTime > postingInterval) {
void inputpin () {
 //If statements for inputpins
 if(digitalRead(GreenLED) == HIGH)      // If button 1 pressed
 // print out the state of the button:
   Serial.println("GreenLED is on!");        
 if(digitalRead(YellowLED) == HIGH)      // If button 2 pressed
   Serial.println("YellowLED is on!"); 

 if(digitalRead(RedLED) == HIGH)      // If button 3 pressed
   Serial.println("RedLED is on!"); 
// this method makes a HTTP connection to the server:
void httpRequest() {
 // close any connection before send a new request.
 // This will free the socket on the WiFi shield
 // if there's a successful connection:
 if (client.connect(server, 80)) {
   IPAddress ip = WiFi.localIP();
   long rssi = WiFi.RSSI();
   float absoluteCon = abs(rssi);
   quality = 150 - ((1.66666667)*absoluteCon);
   int buttonState1 = digitalRead(GreenLED);
   int buttonState2 = digitalRead(YellowLED);
   int buttonState3 = digitalRead(RedLED);

   int status;
   // Make a HTTP request:
   String s = "GET /dweet/for/RSSI?RSSI=";
    // wait 10 seconds for connection:
   client.println("Host: www.dweet.io");
   client.println("Connection: close");

   // note the time that the connection was made:
   lastConnectionTime = millis();
 else {
   // if you couldn't make a connection:
   Serial.println("connection failed");
void printWiFiData() {
 // print your WiFi shield's IP address:
 IPAddress broadCast = WiFi.localIP();
 broadCast[3] = 255;
 Serial.print("IP Address: ");

 // print your MAC address:
 byte mac[6];
 Serial.print("MAC address: ");
 Serial.print(mac[5], HEX);
 Serial.print(mac[4], HEX);
 Serial.print(mac[3], HEX);
 Serial.print(mac[2], HEX);
 Serial.print(mac[1], HEX);
 Serial.println(mac[0], HEX);

 // print the SSID of the network you're attached to:
 Serial.print("SSID: ");

  // print the received signal strength:
 long rssi = WiFi.RSSI();
 Serial.print("Signal Strength (RSSI):");