Bir yapılandırma sayfasına metin olarak girilen broker adresi: string'i ESP32'nin kullanabileceği bir şeye nasıl çevirebilirim?
Yapay zekâ tarafından English dilinden çevrildi ·
Yine ben. ESP32 kartlarıma küçük bir kurulum sayfası ekledim (WiFiManager ile özel bir parametre), böylece MQTT broker adresini koda sabitlemek yerine tarayıcıdan yazabiliyorum. Parametre String olarak geri geliyor, örneğin test Mosquitto’m için Pi üzerindeki “192.168.1.40”.Kopyaladığım örnekte PubSubClient, setServer() içinde bir IPAddress bekliyor; “192.168.1.40” değerinden IPAddress’e nasıl geçeceğimi çözemiyorum. IPAddress ip = brokerStr; denedim ama derlenmiyor. Bu çalışınca aynı kartları Pi yerine Hermetry’ye yönlendirmek istiyorum.
Me again. I’ve added a little setup page to my ESP32 boards (WiFiManager with a custom parameter) so I can type the MQTT broker address in the browser instead of hardcoding it. The parameter comes back as a String, e.g. “192.168.1.40” for my test Mosquitto on the Pi.
PubSubClient wants an IPAddress in setServer() in the example I copied, and I can’t figure out how to get from “192.168.1.40” to IPAddress. I tried IPAddress ip = brokerStr; and it doesn’t compile. Once this works I want to point the same boards at Hermetry instead of the Pi.
Yapay zekâ tarafından English dilinden çevrildi ·
Noktalardan bölüp her parçayı atoi ile çevirebilir, sonra IPAddress(a, b, c, d) kullanabilirsiniz. Ben bir keresinde böyle yapmıştım.
You could split it on the dots and atoi each part, then IPAddress(a, b, c, d). That’s what I did once.
Yapay zekâ tarafından English dilinden çevrildi ·
Bu işe yarıyor ama bunun için yerleşik bir yöntem var. Ayrıca Hermetry kısmı soruyu biraz değiştiriyor.
That works but there’s a built-in way. Also, the Hermetry part changes the question a bit.
Yapay zekâ tarafından English dilinden çevrildi ·
IPAddress sınıfında fromString() var, bu yüzden bölmeye gerek yok:
IPAddress ip;
if (ip.fromString(brokerStr)) {
client.setServer(ip, 1883);
}
Ama Ciaran’ın dediği gibi Hermetry için hiç IP kullanmak istemezsiniz. Broker’ı 8883 portunda TLS ile çalışır ve SNI gerektirir, bu yüzden host adıyla bağlanmanız gerekir. setServer() fonksiyonunun host adı alan bir sürümü var; Pi’niz için de adını veya IP’sini metin olarak yazarsanız çalışır:
char brokerHost[64]; // global, lives as long as the program
void applyConfig(const String &s) {
s.toCharArray(brokerHost, sizeof(brokerHost));
client.setServer(brokerHost, 8883);
}
Önemli olan global tampon. PubSubClient, verdiğiniz işaretçiyi tutar, metni kopyalamaz. Eğer setup() sonunda kapsam dışına çıkan bir String’den brokerStr.c_str() verirseniz, işaretçi sarkar ve daha sonra çöp bir adrese bağlanmaya çalışır. Ben bunu bitirme projemde yaşadım; kart ilk açılışta bağlandı, ama yeniden bağlanma sonrası bir daha hiç bağlanamadı.Parametreyi, cihazınızın Bağlantı ayrıntıları kartındaki Hermetry host’unu alacak kadar uzun yapın; 64 karakter yeterli.
IPAddress has fromString(), so no splitting needed:
IPAddress ip;
if (ip.fromString(brokerStr)) {
client.setServer(ip, 1883);
}
But Ciaran’s point: for Hermetry you don’t want an IP at all. Its broker is TLS on 8883 and needs SNI, so you must connect by host name. setServer() has a version that takes a host name, which works for your Pi too if you type its name or IP as text:
char brokerHost[64]; // global, lives as long as the program
void applyConfig(const String &s) {
s.toCharArray(brokerHost, sizeof(brokerHost));
client.setServer(brokerHost, 8883);
}
The global buffer is the important bit. PubSubClient keeps the pointer you pass, it doesn’t copy the text. If you pass brokerStr.c_str() from a String that goes out of scope at the end of setup(), the pointer dangles and it’ll try to connect to garbage later. I lost an afternoon to that in my final year project, the board connected on first boot and then never again after a reconnect.
Make the parameter long enough for the Hermetry host on your device’s Connection details card, 64 characters is plenty.
Yapay zekâ tarafından English dilinden çevrildi ·
fromString Pi’de işe yaradı, sonra char buffer sürümüne geçtim. Aoife, işaretçi konusunda haklıydın: c_str() ile ilk denemem bir kez bağlandı, sonra her yeniden bağlanmada rc=-2 ile başarısız oldu. Global buffer ile sorun yok ve shed kartı artık kurulum sayfasına yazılan host ile Hermetry’ye bağlanıyor.
fromString worked for the Pi, and then I switched to the char buffer version. Aoife you were right about the pointer: my first try with c_str() connected once and then failed every reconnect with rc=-2. With the global buffer it’s fine, and the shed board is now talking to Hermetry with the host typed into the setup page.
- Bu konuyu yanıtlamak için giriş yapmış olmalısınız.
Sohbete katılın
Soru sormak ve yanıtlamak, yararlı gönderileri beğenmek ve sorununuzu çözen yanıtı işaretlemek için oturum açın. Her katkı puan ve seviye kazandırır.

