ChipChop Support Forum
Log in
Log out
Join the forum
My Details
REPLIES: 4
VIEWS: 229
BACK
REPLY TO THIS POST
jnogues
17 Feb 2024
Doubt with ChipChop.connected
Hi! I have an existential doubt.

Which of these codes is better for send a notification?


if(WiFi.status() == WL_CONNECTED) ChipChop.triggerEvent("notifier","ALARM!! FIRE!!"); //sends the status immediately


or


if(ChipChop.connected == true) ChipChop.triggerEvent("notifier","ALARM!! FIRE!!"); //sends the status immediately


or only


ChipChop.triggerEvent("notifier","ALARM!! FIRE!!"); //sends the status immediately



Regards
Aubrey44
17 Feb 2024

Based on Gizmo’s last reply to my post, i would think checking against “(ChipChop.connected)” would be the best … cause you know almost forsure the notification isi going to go through …

I would think that “(ChipChop.connected)” would even be a better argument then (Wifi.status) before any “ChipChop.updateStatus” is sent, only because Gizmo has already coded quite a bit of checks into the bool “ChipChop.connected”

Gizmo
17 Feb 2024

New library is out 1.40, there's a lot of improvements on all that to make life even easier but I am soooo tired I'll give a full explanation tomorrow. 💤

Aubrey is right and here's a very short example, exactly like this:




    if(ChipChop.connected){ //or connected == 1 or connected == true
    
         ChipChop.triggerEvent("notification","ALERT"); // if word "ALERT" triggers the notification in the action
         //and then immediately change it to something that does not trigger the action
         // so the future heartbeats will keep sending it
         ChipChop.updateStatus("notification","stop");

    }
    

jnogues
18 Feb 2024

In ChipChop.connected, is implicit wifi connection? Or we need add it?
Gizmo
18 Feb 2024

No, you don't really need anything special.

The way it works is by checking the state of the websockets. The websockets don't care if there is a WiFi connection, they try to establish a connection and if they can't connect or the connection is lost the low level libraries that deal with http, sockets, streams etc will return a failure and the library will know that.

If you purely rely on WL_CONNECTED you will only know if there is WiFi...but...that doesn't mean you actually have an internet connection!

Simple example is if you connect to a public hotspot where you have a "captive portal" (airports, hotels....) and need to enter some personal info before you get access. An ESP can't submit forms but the hotspot will allow it to connect and it will just hang there in a dead-end. So, the WL_CONNECTED will be true but actually you are going nowhere.


So, yeah, you don't need to waste an extra processor tick to check for WL_CONNECTED unless you need it for something else like a local web server on the ESP.

If you use the little WiFiPortal library I've sent you that will take care of the actual WiFi connection and ChipChop will handle the internet, they are designed to work together and the new "keepAlive" is an extra addition so everything is made to fight to death to keep that connection going :-)