B4R Question ESP8266 set host name

BertI

Member
Licensed User
Longtime User
I have seen an old thread with this inline C suggestion for setting the wifi.hostname (what appears in the router connections list):
B4X:
#if C
void SetHostName(B4R::Object* o) {
   WiFi.hostname(b4r_main::_hostname->data);
}
#end if
however I'd like to pass the hostname via the call to the inline C rather than use a fixed global variable. So I tried this:
B4X:
#if C
  void SetHostName(B4R::Object* o) {
   B4R::Array* b = (B4R::Array*)B4R::Object::toPointer(o);
   char* c = (char*)b->data;
   WiFi.hostname(c);
  }
#end if
but this results in a watchdog reset when I try running the code. I am guessing that char* c is perhaps not seen as a string for the WiFi.hostname function. Can someone enlighten me?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
This code seems to work fine:
B4X:
Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    Dim bb() As Byte = "abc"
    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;
   WiFi.hostname(c);
  }
#end if
I guess that the problem is in the way you are building the strings. It is possible that WiFi doesn't make a copy of the string.
 
Upvote 0

BertI

Member
Licensed User
Longtime User
Thanks, that does indeed work. The difference was that I was stupidly trying RunNative("SetHostName","abc"). Getting my head around when a string is automatically considered an array of bytes or not is proving difficult at present. In fact as you can see from my other posts, strings are getting me twisted into a variety of knots .. :)
 
Upvote 0
Top