SparkCore to watch for other Wi-Fi device on network

For my first project, I was thinking about having the SparkCore turn on lights when I arrive to my home. This would be automated in that the SparkCore would recognize when my mobile phone joins the Wi-Fi network in my home.

Is there a way to for the SparkCore to scan the network and see if new devices have joined? Maybe I need to set my mobile phone to a fixed IP and then have the SparkCore ping this address periodically.

Thank you in advance for pointing me in the right direction. I love how easy it was to get this little microcontroller on the network, internet and programmed.

I spent just a bit today looking more into this project.

First, it doesn’t look like there is a PING function in the Spark device. So I took the route of trying to connect to an open port on my mobile device. I’m using an iPhone and after some Googling I found that port 62078 is open for iPhone syncing.

The following code uses TCPClient to connect to my iPhone’s fixed IP address and port 62078. you will need to fill in the IP address before running this code.

int LED = D7;                               // LED is connected to D7

TCPClient client;

void setup() {
    pinMode(LED, OUTPUT);                   // sets pin as output
}

void loop() {
    
    byte iPhoneAddress[] = {xxx, xxx, xxx, xxx};
    
    if (client.connect(iPhoneAddress, 62078)) 
    {
        digitalWrite(LED, HIGH);            // sets the LED on
        client.stop();
    } 
    else 
    {
        digitalWrite(LED, LOW);             // sets the LED off
        client.stop();
    }
    
    delay(1000);
}

Now, I think there is some bug or issue with TCPClient or with my code, because the Spark device runs for just a bit, then I see the RGB LED flash green, then cyan, and then back to breathing Cyan. This keeps happening every couple minutes (maybe more often).

Any other ideas for detecting if a specific mobile device is on the network or not? I was trying to use wi-fi as my “geofence” so to speak. Maybe there is some web service that is updated periodically from my phone and can then trigger a REST API call.

I think you are running into the current TCPClient-is-not-stable problem. I know that the spark team is working on this, since lots of people are having this problem.

One thing to try that worked better for me is to NOT call client.stop(). In my code, which connects to a web server, I send “connection: close” to the HTTP server and never call client.stop() right now. That improved my stability to the point where my code would stay running for many hours at a time and when it failed, it would restart gracefully without a reset.

I don’t know if this would work for you since you can’t ask the iPhone to tear down the connection for you, but it is worth a try.