Sending IP Address trough I2C

Hi all,

I have an ESP32-CAM with a code based on the CameraWebServer example and a PCB with a code based on Arduino.
I'm trying to send the IP Address that the ESP32 is connected to, to my PCB through I2C.
I obtain the IP address with WiFi.localIP().
I really don't know how to do it.

This API?

Yes, so if I print to serial "WiFi.localIP()" I will see for example "192.168.1.27" What I want is to send this to my other board (ideally in that same format).

I see, thanks.

If you look at the IPAddress class, you can see it's implemented as an array of 4 uint8_t values (or the corresponding 32 bit value):

class IPAddress : public Printable {    
private:    
    union {    
    uint8_t bytes[4];  // IPv4 address    
    uint32_t dword;    
    } _address;

These correspond to the 4 octets in your address '192.168.1.27'. You can access the individual octets using the [] operator.

So you have two options really. If you literally want to send a string to the other board, I think you'd need to format the address yourself. Possibly the libc inet_ntop function might be of use, unless IPAddress itself offers a means of rendering to a string or char array (it has a printTo method but I'm not sure whether that's easily useful for this or not).

Alternatively you could send the individual octets.

It really boils down to what you want the other board to do with the address. If you want to use it as an IP address for communication the octet variant is probably more useful since it avoids the other board having to parse the address all over again.

Yeah, I saw this.

IPAddress localIp = WiFi.localIP();
    uint8_t firstOctet = localIp[0];
    uint8_t secondOctet = localIp[1];
    uint8_t thirdOctet = localIp[2];
    uint8_t fourthOctet = localIp[3];

This will give you each of the octets.

So to explain a bit, the esp32 connects to an IP that can change as it will connect to different WIFIs, I want the IP to be sent to my board and this will be sent with BLE to an app that will check the once the IP (ideally every time the device is connected).

So is your question more about how to send the data using I2C?

I know more or less how to send data using I2C, but I'm struggling to send the IP.
For example, I have 192 168 1 27. Every time I try to send the IP as Byte,int ,uint32_t... I receive 192 255 255 255. I think the problem is the way I receive the data:

Wire.requestFrom(106, 4);    // request 4 bytes from slave device #106
  //int Buffer[4];
  while (Wire.available()) {
    uint8_t c = Wire.read(); 
    Serial.println(c); 
  }

What does the sending code look like?

So I'm changing it a lot but basically, I send data with Wire.write().

void sendIP() {

  if (WiFi.status() == WL_CONNECTED) {
    //_______OPTION 1_______
    //String IP = WiFi.localIP().toString().c_str();
    //Wire.write(IP);

    //_______OPTION 2_______
    uint32_t localIp = WiFi.localIP(); // I've tried byte, int, IPAddress ...
    uint8_t firstOctet = localIp[0];
    uint8_t secondOctet = localIp[1];
    uint8_t thirdOctet = localIp[2];
    uint8_t fourthOctet = localIp[3];
    uint8_t Buffer[4];
    Buffer[0] = firstOctet;
    Buffer[1] = secondOctet;
    Buffer[2] = thirdOctet;
    Buffer[3] = fourthOctet;
    //Serial.println(localIp); // Works if I send one of the octets

    Wire.write(localIp);
    delay(500);
  }

}

From my reading of the Wire docs, I would be trying something like:

uint8_t ipaddr[4] = { localIP[0], localIP[1], localIP[2], localIP[3] };
Write.write(ipaddr, 4);

Additionally, I think there should be some brokering between the two boards (one should be master, one slave) -- I'm assuming you have that all set up OK?

Okay, I see my problem now! It works!! Thank you so much, I think I was starting to overthink...

1 Like

Huzzah! :slight_smile:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.