B4R Code Snippet Setting the hostname of an ESP32

Important is this line:

WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE);

which seems to reset the settings. My ESP32 showed up as "Espressif"

B4X:
Private Sub SetHostName
  
    Dim bb() As Byte = "TheHostname"
    RunNative("SetHostName", bb)
End Sub

#if C
  void SetHostName(B4R::Object* o) {
   B4R::Array* b = (B4R::Array*)B4R::Object::toPointer(o);
   char* c = (char*)b->data;
   [B]WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE);
   WiFi.setHostname( c );[/B]
  }
#end if
 

Johan Hormaza

Well-Known Member
Licensed User
Longtime User
Something like that in B4R


B4X:
Sub Process_Globals
    Private Astream As AsyncStreams   
    Private server As WiFiServerSocket
End Sub

Public Sub Start
    Log("StartAP: ", Main.WiFi.StartAccessPoint("TheHostname"))
    Log("My AP ip: ", Main.WiFi.AccessPointIp)
    Log("My ip local: ", Main.WiFi.LocalIp)
    server.Initialize(51042, "server_NewConnection")
    server.Listen
End Sub

Private Sub Server_NewConnection (NewSocket As WiFiSocket)
    Log("Nueva conexión")
    Astream.InitializePrefix(NewSocket.Stream, False, "astream_NewData", "astream_Error")
    Dim StoredLength As Byte = Main.GetStoredDataLength
    If StoredLength > 0 Then
        Astream.Write(Array As Byte(1))
        Astream.Write(Main.eeprom.ReadBytes(2, StoredLength))
    Else
        Astream.Write(Array As Byte(0))
    End If
End Sub

Private Sub Astream_NewData (Buffer() As Byte)
    Main.SaveNetworkDetails(Buffer)
    
End Sub


Private Sub AStream_Error
    Log("Disconnected WiFiServer")
    server.Socket.Close
    server.Listen
End Sub
 

Cableguy

Expert
Licensed User
Longtime User
Important is this line:

WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE);

which seems to reset the settings. My ESP32 showed up as "Espressif"

B4X:
Private Sub SetHostName
 
    Dim bb() As Byte = "TheHostname"
    RunNative("SetHostName", bb)
End Sub

#if C
  void SetHostName(B4R::Object* o) {
   B4R::Array* b = (B4R::Array*)B4R::Object::toPointer(o);
   char* c = (char*)b->data;
   [B]WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE);
   WiFi.setHostname( c );[/B]
  }
#end if
@KMatle, you should correct the in-line c code and take out the tags as they do nothing inside a code block and may cause confusion.
Also, it's becoming too common to not even do a simple introduction to the thread, or even to explain a bit what we are looking at... Just my too cents
 
Top