Last active
April 19, 2024 13:50
-
-
Save ElectricImpSampleCode/1ca881b1bd75f2378ae8 to your computer and use it in GitHub Desktop.
Electric Imp imp API imp.net.info() example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local netData = imp.net.info(); | |
if ("active" in netData) { | |
local type = netData.interface[netData.active].type; | |
// We have an active network connection - what type is it? | |
if (type == "cell") { | |
// The imp is on a cellular connection | |
local imei = netData.interface[netData.active].imei; | |
server.log("The imp has IMEI " + imei + " and is connected via cellular"); | |
} else { | |
// The imp is connected by WiFi or Ethernet | |
if ("address" in netData.ipv4) { | |
local ip = netData.ipv4.address; | |
server.log("The imp has IP address " + ip + " and is connected via " + type); | |
} else { | |
server.error("The imp does not have an IP address"); | |
} | |
} | |
if (netData.interface.len() > 1) { | |
// The imp has more than one possible network interface | |
// so note the second (disconnected) one | |
local altType = netData.active == 0 ? netData.interface[1].type : netData.interface[0].type; | |
server.log("(It can also connect via " + altType + ")"); | |
} | |
} else { | |
server.log("The imp is not connected"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment