How to transform a String into an IPaddress ?

Brains,

I have this String:

String("192,168,16,33"); 

How to pass this value to IPadress server to look like this:

IPAddress server(192,168,16,33);

Thanks();

1 Like

That is not a valid String. Do you mean you have a string like this "192.168.16.33" ?

True. I forgot to type the quotation marks. Correct it there. Grateful.

Try this:

void setup() {

  Serial.begin(115200);

//String("192,168,16,33"); 

  String s = "192.168.16.33";
IPAddress ip;

ip.fromString(s);
Serial.println(ip);

}

void loop() {
  // put your main code here, to run repeatedly:
}
1 Like

Thank you very much for trying to help. But if the String were made up of points, this code solves it 100%.

But the String needs to be formed by a comma, and being formed by a comma shows (IP unset) on the serial monitor.

Why can't you just use replace to swap them out to points?

1 Like

In this case, it would be the other way around, replacing periods with commas because IPAddress requires the use of commas and not periods. But thanks for the suggestion. Here in BR it's a bit late, I'm going for it, tomorrow I'll try some more ideas.

By the way, the IPAdress server is used here:
client.connect(server, 80);

I thought you said that if the String had points the other solution would work. If you use replace then you can change the commas to points and use that solution.

Before continuing my request for help with the code I'm testing, I would like to ask for some information:

I'm testing communication between an esp01s and a nodemcu via WIFI through my home router. Communication occurs through WifiClient(server,80), I put the same IP on both esp's and send any data and I actually receive this same data on the serial monitor of the other esp. So far so good.

But I asked how easy it is for a kid to play that disrupts the IP and disrupt this communication ? and what test could I do here, with an Android app, to disrupt communication between the two esp's ?

What can I do here to really verify that communication between the two esp's via port 80 is very easy to be interrupted on purpose by someone with bad intentions, even if it's a joke ?

Thanks

a proper IP network does not have devices with the same IP address. Each device needs its own address... So, so far, not so good....

the kid would need to join your IP network to start messing around with it at IP level. So if joining the network is protected, then you minimised the risks. (there is a much longer answer to network and security postures but it's probably way beyond your needs)

So, this is an XY Problem having to nothing to do with the title of your thread?

Well, to even see the devices, the kid would have already already gained access to your router and gotten an IP address on your local network ... Is that possible? If so, you have bigger problems than the security of your little ESP project.

The only way to gain access to an ESP from the outside internet would be if you opened up Port Forwarding to it.

Thank You. So changing my question a little: What can I try here to disrupt communication between the two esp's using an Android app ? I have the FING app that shows all active IPs. There must be hundreds of apps that show the IP of a network. Is there something easy that I can do, "from a distance", to confuse the communication between my two esp's ?

Answer this question first:

How far away is "from a distance"?
Is the disrupter already on your WiFi network?

OK, this is my code on ESP01S. Every 10 seconds I send a number to nodemcu via WIFI. See loop().

First create a file called numeroserie.txt. In it I recorded two Strings that I typed into the WiFiManager Portal.

The two data I enter there (in addition to configuring the SSID and password) are: number and ip.

What I'm not able to do is consider an IPAddres server the contents of the ip String

It's just that there can't be points 190,160,16,31
it has to be commas: 190,160,16,31
but if I use commas I see the IP(unset) error message on the serial monitor.

Does anyone have any...try this ?

#include <SPI.h>
#include <ESP8266WiFi.h>
#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager
#include <FS.h>
#include <LittleFS.h>

WiFiManager WM;

File myFile;
#define FORMAT_LittleFS_IF_FAILED true

//Variáveis Globais:
int recNum;
char nnumero[10] = "";
char nip[17] = "";
String numeroserie = "98765";
String dados,wifissid,wifisenha,numero,ip;

IPAddress server(190,160,16,31);      //Mesmo IP FIXO do SERVIDOR
WiFiClient client;

void setup() {
  
  delay(2000);
  Serial.begin(115200);
 
  if(!LittleFS.begin()) {
     Serial.println("LittleFS FAIL");
     return;
  } else {
      Serial.println("LittleFS OK");
    }

  if(!LittleFS.exists(numeroserie+".txt")) {
    myFile = LittleFS.open(numeroserie+".txt", "w");    
    myFile.close();
  }     

  if(LittleFS.exists(numeroserie+".txt")) {
     myFile = LittleFS.open(numeroserie+".txt", "r");    
      if (myFile) {  
        while (myFile.available()) {
         dados = myFile.readStringUntil('\n');
         recNum++;
         if(recNum == 1) {numero = dados;}
         if(recNum == 2) {ip = dados;}        }
      }
       myFile.close();
       dados = "";
     }    


    //WM.resetSettings();
    //WM.setConfigPortalBlocking(false);
    //WM.setConfigPortalTimeout(180);
   WM.setConnectRetries(100);

   WiFiManagerParameter custom_nnumero("numero", "Número, até 9 digitos", nnumero, 10);
   WiFiManagerParameter custom_nip("IP", "IP", nip, 17);
   
   //add all your parameters here
   WM.addParameter(&custom_nnumero);
   WM.addParameter(&custom_nip);
      
   if(WM.getWiFiIsSaved())
       WM.setEnableConfigPortal(false);
       WM.autoConnect("ControlOne","5001133");
      
   if(!WM.autoConnect("ControlOne","5001133")) {
       Serial.println("Wi-Fi FAIL");
   } 
    else {    
          Serial.println("OK");

          strcpy(nnumero, custom_nnumero.getValue());
          strcpy(nip, custom_nip.getValue());

          if(String(nnumero) != 0) {
            numero = String(nnumero);
          }
          if(String(nip) != 0) {
            ip = String(nip);
          }

          numero.trim();
          ip.trim();

       }          
        
   wifissid = WiFi.SSID();
   wifisenha = WiFi.psk();
   
}

void loop () {
  client.connect(server, 80);
  client.println(numero+"\r");
  Serial.println(numero+"\r");
  client.flush();
  delay(10000);  //send 10 seconds
}

Any app messing around at the IP level means you joined the network. If you let the fox into the henhouse then you are asking for trouble

If the attacker is not within the network then it’s a different story.

See your IP network as some sort of walled garden. If you don’t let someone enter in the garden and keep all access locked then it’s safer than if you open the door and let anyone in.

Thank You

Already answered for you if you had bothered to read Post 11 or Post 14.

Thank You

There are commas in both of those. No "points".

gvalvo, I sincerely thank you for your participation here, but the level of your knowledge is too high for my silly questions, my posting errors, etc. Have a happy 2024 with good health and peace.