B4R Question Set an dynamicaly IP address as static IP with Inline C# for ESP8266

Chris160179

Member
Licensed User
Longtime User
Hello everybody,

i would like to pass an IP address direct to the Inline C# "Void SetIP Sub"
Is there a way?

This code works.
B4X:
RunNative( "SetIP" , Null )

#if C
  void SetIP(B4R::Object* o) {

  IPAddress ip(192, 168, 10, 101);  
  IPAddress gateway(192, 168, 1, 1);
  IPAddress subnet(255, 255, 255, 0);
  WiFi.config(ip, gateway, subnet);

  }
  #end if


I passed an array of bytes und used (o->toULong) to convert the object but i don´t get the right IP

B4X:
'Process_Globals
 Public WiFiIP() As Byte = Array As Byte(192, 168, 10, 101)

'Sub
 Private Sub SetClientStation_IP(Byte1() As Byte)  'IP As Object
  'Log("set WLAN_Station_IP: ", Byte1)
  RunNative("SetIP", Byte1)    
 End Sub


    #if C
        void SetIP(B4R::Object* o) {
        uint32_t myInt1;
        myInt1 = (o->toLong());
        IPAddress gateway(192, 168, 0, 1);
        IPAddress subnet(255, 255, 255, 0);
        WiFi.config(myInt1, gateway, subnet);
        }
    #end if

The result IP is: 132.242.254.63

I used bit operations to change from "Big Endian" to "Little Endian".

The result IP is: 63.254.242.132 'hmmm.... not the desired result



Thank You
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Here:
B4X:
Private Sub AppStart
   Serial1.Initialize(115200)
   Log("AppStart")
   Dim b() As Byte = Array As Byte(192, 168, 1, 5)
   RunNative("SetIP", b)
   
End Sub

#if C
  void SetIP(B4R::Object* o) {
   B4R::Array* b = (B4R::Array*)B4R::Object::toPointer(o);
   IPAddress ip(ByteFromArray(b, 0),ByteFromArray(b, 1) , ByteFromArray(b, 2), ByteFromArray(b, 3));  
   IPAddress gateway(192, 168, 1, 1);
   IPAddress subnet(255, 255, 255, 0);
   WiFi.config(ip, gateway, subnet);
  }
#end if
 
Upvote 0
Top