How to ping or get MAC Address to check if alive?

Hello fellow Spark-adians!

I would like to simply ping both some local IPs and global IPs to check if they are alive (+/- returning the ping time). I am still new to Arduino and was wondering if there would be an easier way than attempting to establish a TCP connection?

Is it possible to obtain the MAC address of a device that is connected to the local network (given I have assigned the device a static local IP)?

Hi @timolol,

Hmm, you could always grab the mac address from your router / dhcp table if you wanted!

I think the CC3000 driver has a ping function built in that you could use (from netapp.h in core-common-lib)
https://github.com/spark/core-common-lib/blob/master/CC3000_Host_Driver/netapp.c

//*****************************************************************************
//
//!  netapp_ping_send
//!
//!  @param  ip              destination IP address
//!  @param  pingAttempts    number of echo requests to send
//!  @param  pingSize        send buffer size which may be up to 1400 bytes 
//!  @param  pingTimeout     Time to wait for a response,in milliseconds.
//!
//!  @return       return on success 0, otherwise error.
//!
//!  @brief       send ICMP ECHO_REQUEST to network hosts 
//!	 
//! @note         If an operation finished successfully asynchronous ping report 
//!               event will be generated. The report structure is as defined
//!               by structure netapp_pingreport_args_t.
//!
//! @warning      Calling this function while a previous Ping Requests are in 
//!               progress will stop the previous ping request.
//*****************************************************************************

 #ifndef CC3000_TINY_DRIVER
extern long netapp_ping_send(unsigned long *ip, unsigned long ulPingAttempts, unsigned long ulPingSize, unsigned long ulPingTimeout);
#endif

//*****************************************************************************
//
//!  netapp_ping_stop
//!
//!  @param  none
//!
//!  @return  On success, zero is returned. On error, -1 is returned.      
//!
//!  @brief   Stop any ping request.
//!	 
//!
//*****************************************************************************

#ifndef CC3000_TINY_DRIVER
extern long netapp_ping_stop();
#endif
//*****************************************************************************
//
//!  netapp_ping_report
//!
//!  @param  none
//!
//!  @return  none
//!
//!  @brief   Request for ping status. This API triggers the CC3000 to send 
//!           asynchronous events: HCI_EVNT_WLAN_ASYNC_PING_REPORT.
//!           This event will carry  the report structure:
//!           netapp_pingreport_args_t. This structure is filled in with ping
//!           results up till point of triggering API.
//!           netapp_pingreport_args_t:\n packets_sent - echo sent,
//!           packets_received - echo reply, min_round_time - minimum
//!           round time, max_round_time - max round time,
//!           avg_round_time - average round time
//!	 
//! @note     When a ping operation is not active, the returned structure 
//!           fields are 0.
//!
//*****************************************************************************
#ifndef CC3000_TINY_DRIVER
extern void netapp_ping_report();
#endif

Thanks!
David

Thanks for the heads up, @Dave!

I was hoping to get Spark core to obtain connected deviceā€™s MAC address on the private network to match up a predefined list, so it can tell who is present in the house (on the network).

I have spent some time reading up on CC3000 netapp (which is not much), but find it a bit complex to simply enquire whether a local IP address exists. In order to get a report from the ping, I will have to use a callback function and the process is asynchronous. Any chance you can provide a short sample code based on the function? :stuck_out_tongue:

Hmm, sounds like an interesting project, Iā€™m not sure how to build this offhand, I would have to dig around in the firmware a bit, but Iā€™m guessing there should be a way to accomplish this. Maybe someone more familiar can jump in and help write up an example. Otherwise Iā€™ll circle back when I get a chance and see what would work. :slight_smile:

Thanks,
David

1 Like

Tried figuring out how to use it.....

void setup() {
int IP[4];
IP[0] = 192;
IP[1] = 168;
IP[2] = 1;
IP[3] = 1;

pinMode(D7,OUTPUT);
digitalWrite(D7,HIGH);
//netapp_ping_send(unsigned long *ip, unsigned long ulPingAttempts, unsigned long ulPingSize, unsigned long ulPingTimeout)
netapp_ping_send((unsigned long *)IP,4,4,100);

}

void loop() {
digitalWrite(D7,HIGH);
delay(200);
digitalWrite(D7,LOW);
delay(200);
}

I posted about this over here:

2 Likes

@bko awesome!

I was trying hard to figure out how to get the netapp_ping_report()ā€¦

possible?

@timolol,

@bko has an example code working!

2 Likes

I just tested this with a known working and known not working address and the ā€œsuccessā€ is not if the host answers or notā€“just if the ping was sent (which I see going by). Id see the pings going out on my router.

The netapp_ping_report looks like it is declared wrong since it does not return the value of the struct with the data.

1 Like

Iā€™m guessing what everyone here really nts is some kind of reduced functionality nmap (find devices and mac addresses).

I just wanted to let everyone know that I am trying to get the ping report working but it is not working yet.

Even with the ping report, you will not get the MAC address of the remote host, you get packets sent, packets received, minimum round trip time, maximum round trip time, and average round trip time.

I report back here when I get some better news.

3 Likes

Thank you for your help and expertise, @bko! :slight_smile:

OK, I have something that works and will get my pull request together and merge against the upcoming RSSI feature. It currently looks like this:

  unsigned long packetsReceived = Network.ping(IPAddress(10,0,0,2));
  //or try 10 times instead of the default 5 
  unsigned long packetsReceived2 = Network.ping(IPAddress(10,0,0,2), 10);

There are some interesting trade-offs to be made here since the local subnet is very fast (3-5ms round-trip typically) but from where I am to spark.io is 140ms round-trip, so that the timeout value has to be fairly large (500ms). There may be more work to do here to avoid cloud disconnect.

I was able to ping my iOS gizmo on the local subnet and see quick results and then put it in airplane mode and see the zero packetsReceived. I was also able to ping some well-known hosts on the broader internet.

Special thanks to @david_s5 for a boost on this one!

3 Likes

awesome! initial help changed into code improvement for all the :spark: owners :wink:

For those who are interested and can build locally:

Test program:

#include "application.h"

IPAddress google(74,125,225,52);
IPAddress yahoo(98,139,180,149);
IPAddress spark(62,116,130,8);

void setup() {
    Serial.begin(9600);
    delay(2000);
}

void loop() {
  Serial.print("G: ");
  Serial.println(Network.ping(google));
  Serial.print(" Y: ");
  Serial.println(Network.ping(yahoo));
  Serial.print(" S: ");
  Serial.println(Network.ping(spark));    
  Serial.print(" Fail: ");
  Serial.println(Network.ping(IPAddress(10,0,0,200)));    
  delay(10000);
}

Printing ā€˜5ā€™ for the first three meaning 5 successful pings out of 5 and 0 for the last one is correct. You may need to change the IPAddress of the fail test to something not in use on your network.

2 Likes

Iā€™ve been reading the forums for anything related to MAC address, and this is the closest thread I could find, so here goes:

I need to get the MAC address of my core in order to claim it. The Wifi network I am on is MAC-address filtered. Itā€™s a school network and I am not the administrator of it, so I canā€™t check the router logs to get the MAC addresses. And it seems I canā€™t upload sketches in any way to the core until I claim it. So at the moment, itā€™s a brick until I get on another network or get the MAC address so I can register it on the school network.

Any solutions? Is there something I am missing?

@tigoe, look at the documentation for the function Network.macAddress().

http://docs.spark.io/firmware/#connection-management-network

Besides OTA, you can also flash your Core over USB using Spark CLI. :smile:

1 Like

Do you have spark-cli installed?

We can upload a code which disables Wifi and prints out the Mac address via serial for you to record down. :wink:

@peekay123, why did you reply so fast?! :stuck_out_tongue:

2 Likes

What about connecting it to your mobile phone AP, androids will tell you mac address of connected devices (not sure if iPhones do or not). you could use a serial terminal to input the SSID, security and password. Just put the core in listening mode and start the terminal and type ā€˜wā€™ and follow the prompts. full instructions for connecting with the terminal can be found on the docs siteā€¦

Am i right in thinking you need to get the mac address to give to the school so they can add it to the allowed list?

1 Like

@Hootie81 you are correct in thinking that, I need the MAC address so I can register it with the school, then itā€™ll be allowed to connect to the network.

@peekay123 I looked at the Network API, and tried to upload via the CLI, but it wouldnā€™t flash the core. I assumed it was because this core is not yet registered with my account, which I canā€™t do until it can get on the network, which it canā€™t do until I get and MAC address. Itā€™s a chicken-egg problem.