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
  Serial.begin(115200);
  // initialize serial for ESP module
  Serial1.begin(115200);
  // initialize ESP module
  WiFi.init(&Serial1);

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

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

void loop()
{
  // print the network connection information 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);

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

}

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

Thanks

DArduino86:
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

DArduino86,

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

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


 printWiFiData();

}

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

 // 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();
   Serial.write(c);
 }
 // if ten seconds have passed since your last connection,
 // then connect again and send data:
 if (millis() - lastConnectionTime > postingInterval) {
   httpRequest();
   inputpin();
 
 }
}
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
 client.stop();
 
 // if there's a successful connection:
 if (client.connect(server, 80)) {
   Serial.println("connected");
   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=";
   s.concat(rssi);
   s.concat("&quality=");
   s.concat((float)quality);
   s.concat("&ip=");
   s.concat(ip);
   s.concat("&GreenLED=");
   s.concat(buttonState1);
   s.concat("&YellowLED=");
   s.concat(buttonState2);
   s.concat("&RedLED=");
   s.concat(buttonState3);
   s.concat("&Status=");
   s.concat(WL_CONNECTED);
   
   Serial.println(s);
   client.println(s);
   
    // wait 10 seconds for connection:
   delay(10000);
   
   client.println("Host: www.dweet.io");
   client.println("Connection: close");
   client.println();

   // 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: ");
 Serial.println(broadCast);

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

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

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