As the title says... Chat on...

User avatar
By Timmiej93
#78658 For some reason, I can't get net.socket:send() to work reliably. The code below should do the following:
    - Set the wifi.sta config with the data from the browser
    - Connect
    - On connect, callback
    - Set a repeating timer that checks if an IP has been obtained every 500ms
    - If so, send the IP and SSID back to the browser
    - After sending, close, and move from station+AP to just station mode

It works 100% of the time, up to the repeating timer that checks if an IP has been obtained. Sending that data only works 5-10% of the time, and I can't figure out why. ("data" + the JSON data prints, "SENT" doesn't)
I already added the 5 second delay before switching over to station mode to prevent errors regarding the connection I was getting, and commented out the unregistering of the timer, which seemed to help initially, but still no luck.

Does anyone have a clue what could be preventing my socket from sending?

PS. Other instances of socket:send() work just fine, although those don't use as many callbacks.

Code: Select allsetWiFi_sta(jsonData, true, function()
   print("CONNECTED1")

   local hold = false
   local timer = tmr.create()
   timer:alarm(500, tmr.ALARM_AUTO, function()
      if (wifi.sta.status() == wifi.STA_GOTIP and not hold) then
         hold = true

         print("ip", wifi.sta.getip())
         print("ssid", wifi.sta.getconfig(true).ssid)

         local data = {}
         data.ip = wifi.sta.getip()
         data.ssid = wifi.sta.getconfig(true).ssid

         print("data", sjson.encode(data))

         localSocket:send(sjson.encode(data), function()
            print("SENT")
            localSocket:close()
            tmr.create():alarm(5000, tmr.ALARM_SINGLE, function()
               print("CLOSING SOCKET AND GOING TO STATION MODE")
               wifi.setmode(wifi.STATION)
               -- timer:unregister()
            end)
         end)

      end
   end)
end)

function setWiFi_sta(data, doConnectedCheck, connectedCallback)
   
   print("setWiFi_sta")

   if (data.ssid == "") then
      setWiFi_ap()
      return
   end

   wifi.sta.clearconfig()
   print("config", wifi.sta.config({
      ssid = data.ssid,
      pwd = data.pass,
      auto = false,
      save = false
   }))

   if (data.ip ~= "") then
      print("setip", wifi.sta.setip({
         ip = data.ip,
         netmask = data.netmask,
         gateway = data.gateway
      }))
   end

   if (doConnectedCheck) then
      tmr.alarm(0, 60*1000, tmr.ALARM_SINGLE, function(timer)
         -- If this code runs, the module hasn't connected to the network after 60 seconds,
         --   so we should probably return to AP mode to fix this

         setWiFi_ap()

      end)      
   end

   wifi.sta.connect(function()
      print("CONNECTED2")
      tmr.unregister(0)
      if (connectedCallback ~= nil) then
         connectedCallback()
      end
   end)
end
User avatar
By Timmiej93
#78965 A follow up:

I kinda gave up on this for a while, went on to do some other stuff, because I simply couldn't get past this issue. Today I decided to give it a go again. I didn't change a single thing, and whaddayaknow: it works fine. I'd be really curious to see if anyone has an explanation for this weird behavior.

Edit:
As always, I spoke too soon. It worked for a while, then started to work intermittendly, and then stopped working outright. If I remove the programming USB thingy for a while, let the ESP cool down (it gets quite warm in this specific USB thingy), and then try again, it all works fine. Could it really be a temperature issue?

Edit2:
As it turns out, I was completely missing the fact that the connection between the browser and the ESP module gets lost when configuring the wifi STA mode. Somewhat strange, somewhat makes sense. I'll have to look into it more to see if I can actually fix this, or if it's a limitation of NodeMCU.