J'essaye de stocker l'adresse IP dans une chaîne externe. La valeur de mon adresse IP est en .cpp, mais je souhaite la stocker dans mon fichier .h. Je le stocke sous forme de chaîne car je veux en faire un lien. (http: // "adresse IP" /)
Mon fichier .h
if (connectWifi("", "") == WL_CONNECTED) { DEBUG_WM(F("IP Address:")); DEBUG_WM(WiFi.localIP()); ipadd1 = String(WiFi.localIP()); //connected return true; }
Mon fichier .cpp
extern std::string ipadd1 = "";
3 Réponses :
Une recherche de 5 minutes me donne la description de la fonction WiFi.localIp () , à partir de là, je savais qu'il renvoyait l'objet IPAddress
. Suite à forum.arduino.cc Rubrique: Comment manipuler les variables IPAddress / convertir en chaîne a> vous pouvez utiliser la fonction suivante pour le convertir en chaîne: // author apicquot from https://forum.arduino.cc/index.php?topic=228884.0
String IpAddress2String(const IPAddress& ipAddress)
{
return String(ipAddress[0]) + String(".") +\
String(ipAddress[1]) + String(".") +\
String(ipAddress[2]) + String(".") +\
String(ipAddress[3]) ;
}
IPAddress
peut être traité comme un tableau de 4 int
s .
Convertissez IPAddress
en String
, puis récupérez le const char *
et convertissez-le en std :: string .
ipadd1 = WiFi.localIP().toString().c_str();
Merci de m'avoir aidé, mais j'ai déjà essayé et cela n'a pas fonctionné.
ipadd1 = std :: string (WiFi.localIP (). ToString (). C_str ())
fonctionne-t-il? Notez qu'il existe une différence entre std :: string
et String
.
@KamilCuk pouvez-vous mettre à jour votre réponse ci-dessus pour refléter cela? Cela peut valoir la peine de fournir les deux exemples, un pour char * et un pour std :: string.
Si nous voulons écrire Wifi.localIP ()
dans oled (SSD1306)
ou serial, écrivez simplement WiFi.localIP (). toString () code>. Comme ceci:
Serial.print("Connected, IP address: ");
Serial.print(WiFi.localIP().toString());
display.clear();
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.setFont(ArialMT_Plain_10);
display.drawString(0, 0, " WiFi is Connected." );
display.drawString(0, 10, " IP address: " + WiFi.localIP().toString() );
Alors quelle est la question?
comment le convertissez-vous?
Vous devez marquer
WiFi.localIP (). ToString (). C_str ();
comme réponse acceptée.