Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problems with v2 #70

Open
joelzavala opened this issue Mar 4, 2021 · 1 comment
Open

Problems with v2 #70

joelzavala opened this issue Mar 4, 2021 · 1 comment

Comments

@joelzavala
Copy link

joelzavala commented Mar 4, 2021

Hi, I used this library and was excited because it solved my problem to connect an esp8266 to the WiFi without ap mode.
I am testing v1 and it works fine, but v2 does not transmit the keys (SSID and PASSWORD) or my esp8266 does not receive the transmission.

this is my code:

// Within a fragment

package com.example.kiinbalam.ui.slideshow

import android.content.Context
import android.content.Context.WIFI_SERVICE
import android.net.wifi.WifiManager
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.example.esp_touch.*
import com.example.kiinbalam.EspTouchApp
import com.example.kiinbalam.R
import java.util.*
import kotlin.collections.ArrayList

class SlideshowFragment : Fragment() {

    private lateinit var slideshowViewModel: SlideshowViewModel

    private val TAG: String = SlideshowFragment::class.java.simpleName

    val KEY_PROVISION = "provision"
    val KEY_PROVISION_REQUEST = "provision_request"

    private val mStations: List<EspProvisioningResult>? = ArrayList()

    private lateinit var mProvisioner: EspProvisioner

    private lateinit var mWifiManager: WifiManager

    private lateinit var mBroadcastObserver: Observer<String>

    private var mWifiFailed = false

    private var mTime: Long = 0

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        val request = EspProvisioningRequest.Builder(requireContext())
                .setSSID("INFINITUM559F_2.4".toByteArray())
                .setBSSID(TouchNetUtil.convertBssid2Bytes("44:a1:91:c3:e1:6c"))
                .setPassword("hsadkfljasdñlkfjsdfklas".toByteArray())
                .build()

        mProvisioner = EspProvisioner(requireContext())

        mWifiManager = context?.getSystemService(WIFI_SERVICE) as WifiManager
        mBroadcastObserver = Observer {
            val connected = TouchNetUtil.isWifiConnected(mWifiManager)

            if(!connected && mProvisioner.isProvisioning()) {
                mWifiFailed  = true
                mProvisioner.stopProvisioning()
            }
        }

        EspTouchApp().getInstance()?.observeBroadcastForever(mBroadcastObserver)

        mTime = System.currentTimeMillis()

//        mProvisioner.startSync(mListener)


        slideshowViewModel = ViewModelProvider(this).get(SlideshowViewModel::class.java)
        val root = inflater.inflate(R.layout.fragment_slideshow, container, false)
        val textView: TextView = root.findViewById(R.id.text_slideshow)
        val btnScan = root.findViewById<Button>(R.id.btn_scan_devices)

        btnScan.setOnClickListener {
            mProvisioner.startProvisioning(request, ProvisionListener())

            val result = mWifiManager.scanResults

            result.forEach {
                println(it.SSID)
                println(it.BSSID)
                println("...........................")
            }
        }

        slideshowViewModel.text.observe(viewLifecycleOwner, Observer {
            textView.text = it
        })

        return root
    }

    inner class ProvisionListener : EspProvisioningListener {
        override fun onStart() {
            Log.d(TAG, "ProvisionListener onStart: ")
        }

        override fun onResponse(result: EspProvisioningResult) {
            val mac = result.bssid
            val host = result.address!!.hostAddress
            Log.d(TAG, "ProvisionListener onResponse: $mac $host")
            /*runOnUiThread(Runnable {
                mStations.add(result)
                mStationAdapter.notifyItemInserted(mStations!!.size - 1)
            })*/
        }

        override fun onStop() {
            Log.d(TAG, "ProvisionListener onStop: ")
            /*runOnUiThread(Runnable {
                if (!mWifiFailed && mStations!!.isEmpty()) {
                    mBinding.messageView.setText(R.string.esptouch2_provisioning_result_none)
                }
                mBinding.stopBtn.setEnabled(false)
                mBinding.progressView.setVisibility(View.GONE)
            })*/
            mTime = System.currentTimeMillis() - mTime
            Log.e(TAG, "Provisioning task cost $mTime")
        }

        override fun onError(e: Exception) {
            Log.w(TAG, "ProvisionListener onError: ", e)
            mProvisioner.stopProvisioning()
            /*activity?.runOnUiThread(Runnable {
                val message = getString(
                    R.string.esptouch2_provisioning_result_exception,
                    e.localizedMessage
                )
                mBinding.messageView.setText(message)
            })*/
        }
    }
}

I would like to know what is i´m doing wrong, thanks in advance

@XuXiangJun
Copy link
Collaborator

EspTouchV2 is not compatible with EspTouch
The device require smart-config v2 firmware install

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants