Automating WiFi Connection to a Tasmota Device

Hello everyone,

My current goal is to connect my Tasmota plug to my private WiFi. I would like to automate the connection process by directly sending an HTTP request to the device to set up the WiFi. My main problem is the web configuration page that automatically open when I connect to the Tasmota network. I would like to avoid this page

My application already retrieves the SSID and password of the WiFi network, and I can manually connect to the Tasmota network by entering these details on the web configuration page. However, I would like my application to do this automatically.

Here's my code :

func sendRequest(ssid : String, password : String, ip : String) {
                let urlStr = "http://\(ip)/cm?cmnd=Backlog%20SetOption19%200;%20SetOption54%200;%20WifiConnect%20%22\(ssid)%22,%20%22\(password)%22"
                
                guard let url = URL(string: urlStr) else {
                    print("Invalid URL.")
                    return
                }
                
                let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
                    if let error = error {
                        print("Error: \(error)")
                    } else if let data = data {
                        let str = String(data: data, encoding: .utf8)
                        request_datas = str ?? "No callback"
                    }
                }

                task.resume()
            }
func connectToWifi(ssid: String) {
        let hotspotConfig = NEHotspotConfiguration(ssid: ssid)
        hotspotConfig.joinOnce = true

        NEHotspotConfigurationManager.shared.apply(hotspotConfig) { [self] (error) in
            if let error = error {
                connection_state = "Error: \(error.localizedDescription)"
            }
            else {
            
                    sendRequest(ssid: datas.temp_ssid, password: datas.password, ip: wifi_adress!)
                    isConnected = true
                    connection_state = "Connected"
                   }
            }
        }
    }
Button {
   connectToWifi(ssid: reseau)
              
} label: {
   ...
}
            

I'm retrieving the IP, password and SSID in another part of my code, which I know is correct.

I don't know if there's any ways I can do what I want, but I got a few ideas, which didn't work, like try to fill automatically the web form. I also have tried sending an HTTP request to set up the WiFi, but the configuration page still automatically opens. Does anyone know how I can automate the submission of the configuration form or prevent the web configuration page from displaying?

Any help would be greatly appreciated. Thank you in advance.

Best regards,

Gainder

My current goal is to connect my Tasmota plug to my private Wi-Fi.

Do you control the firmware of this accessory? Or are you just trying to integrate an accessory built by some other third party?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

I can actually change the firmware, but it's not a viable solution : I'm buying the plugs with Tasmota included, and I just cannot change every firmware of every plug. I'm searching a convenient way to do what I want to do with Swift only

I can actually change the firmware, but it's not a viable solut

OK.

Earlier you wrote:

My main problem is the web configuration page that automatically open when I connect to the Tasmota network.

You’re talking about the captive network web interface, right?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Yes, the web page included in the firmware that automatically opens when the device connect to the plug network. Normally, it's supposed to make the configuration easier, but in my case it's really annoying because I can't send a HTTP request to connect the plug to the internet

Right. This brings me back to the accessory’s firmware. The fact that the captive network comes up is the result of the accessory ‘lying’ to iOS about its facilities. The accessory presents a Wi-Fi network that suggests that it might lead to the wider Internet, whereas in reality that’s not true. It’s hard to get around that in general, and especially from your app, where you’re not involved in the captive network negotiation.

I can't send a HTTP request to connect the plug to the internet

You should be able to do that, but with one serious caveat. At the time the captive network web interface comes up, iOS is actually associated with that accessory’s network. The issue is that this network is not the default route, which makes sense because it doesn’t lead to the wider Internet. If you use an API, like NWConnection, that allows you to bind a connection to a specific interface, you should be able to talk to the accessory.

The caveat is that you want to use URLSession, because you want to speak HTTP, and URLSession does not let you force a request to run over a specific interface.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Automating WiFi Connection to a Tasmota Device
 
 
Q