Post topics, source code that relate to the Arduino Platform

User avatar
By ViniciusKruz
#54918 Hello , my ESP8266 receives the IP by the POST method (String ), then converts to IP address and then writes to the EEPROM , the problem is that command write 192.168.0.39 and the system records 184.143.254.63 . I guess it's conversion error , but have not found the solution yet.

Data received by the POST method :

Code: Select allif (server.hasArg("SSID") && server.hasArg("SENHA")&& server.hasArg("IPESTACAO")){//enviou configurações
   
    String _qip = server.arg("IPESTACAO");
   
    IPAddress qip=ipFromString(_qip.c_str());
User avatar
By martinayotte
#54960 Where this ipFromString() function comes from ?
If you means the fromString() method of IPAddress, you're not using it right, this one convert strings such "10.111.111.25" into an IPAddress, it is not resolving a hostname ... :ugeek:

BTW, on ArduinoESP framework, you can get client IP using server.client().remoteIP()
User avatar
By ViniciusKruz
#54961 I was unable to use the IPAddress::fromString function, so I adapted (ipFromString) it and was as follows:

Code: Select allIPAddress ipFromString(const char *address) {
    // TODO: add support for "a", "a.b", "a.b.c" formats
    IPAddress _IP;
    uint16_t acc = 0; // Accumulator
    uint8_t dots = 0;
    while (*address)
    {
        char c = *address++;
        if (c >= '0' && c <= '9')
        {
            acc = acc * 10 + (c - '0');
            //acc = uint8_t(acc);
            if (acc > 255) {
                // Value out of [0..255] range
                //return false;
                _IP = IPAddress(0,0,0,0);
                return _IP;
            }
        }
        else if (c == '.')
        {
            if (dots == 3) {
                // Too much dots (there must be 3 dots)
                //return false;
                _IP = IPAddress(0,0,0,0);
                return _IP;
            }
            _IP[dots++] = acc;
            acc = 0;
        }
        else
        {
            // Invalid char
            //return false;
            _IP = IPAddress(0,0,0,0);
            return _IP;
        }
    }

    if (dots != 3) {
        // Too few dots (there must be 3 dots)
        //return false;
        _IP = IPAddress(0,0,0,0);
        return _IP;
    }
    _IP[3] = acc;
    return _IP;
}


This way I can get the IP (POST = String) and convert into IPAddress

See the image with the return of the serial port monitor:

Image
User avatar
By martinayotte
#54963 We don't see any image ...

You probably need to debug your ipFromString() function by adding some prints.

BTW, why are you saying that the default IPAddress.fromString() doesn't work ?