What's the ip Kenneth or An easy way to find your DIY IoT or WiFi device
Sep 05th, 2021 | by: J.S.Bee 7649 | Views 224
Views 224

Being able to create networkable DIY devices is a great thing! Finding them on your network when you need to know the IP address can be a real pain sometimes though.  You can have it printed on the serial monitor of the device or use your WiFi router's connected devices page to look it up, but that's not convenient sometimes, and takes extra time.

In this tutorial, I will show you a simple way to get the IP that you can integrate into your own projects. 

What's the magic?

To do this, I wanted an easy-to-access method to find devices on my local network that did not require a lot of work, special libraries, paid-for or access-keyed services, or special hardware.  In the past, I have used static IP's or the bonjour service to find my devices, but many times this is not supported (in the case of bonjour) or wanted as with the static IPs.  


Enter Dweet.io

Dweet.io or just Dweet as we will call it is a free non-signup service to send and receive messages with. Here we will use it for our IoT type needs, but it's not restricted to that use. As stated, there is no registration or signup and no extra external modules are needed to use it.

For information on Dweet.io and using it see: http://dweet.io/.  Dweets are public and can be seen by others, so no personal info, please! I have used it to send out GPS info, local IPs of IoT devices, and of course, sensor data.  The messages being public should be no problem if you use the service with caution in mind.  For our use here, we will send out a local IP and not one that lives in the wilds of the Internet. This IP will only be locally used as well, meaning we will need to be connected to that local network to access the device that it belongs to. 

Sending a "Dweet"

Sending a dweet message is quite simple.  

All you need to do is send a "GET" HTTP request like:


This will create a response like: 


("this":"succeeded","by":"dweeting","the":"dweet", "with":{"thing":"my-silly-thing-name", "created":"2021-09-06T13:20:22.059Z","content":{"greeting":"Hello World"}, "transaction":"86358edc-8111-4d00-a171-45dda3146868"})


The data returned is Json formatted and easy to parse from the ESP, Arduino and RPI worlds. It contains the dweet related info like the success of the call, trans id, etc. The part we care about is the "content": field as it has the data we dweeted.  Here we see {"greeting":"Hello World"}, and this matches the data we sent in via the GET request. 

This response data can be parsed by a Json library or methods/functions for whatever coding system you are using for the device.

To retrieve the last sent data sent from the device you do - https://dweet.io/get/latest/dweet/for/my/my-silly-thing-name and it should return -


("this":"succeeded","by":"getting","the":"dweets","with":[{"thing":"my-silly-thing-name","created":"2021-09-06T13:20:22.059Z","content":{"greeting":"Hello World"}}])
Putting it in action

In this example, I give some ESP Micropython code to connect to your local WiFi, send the dweet with the thing name and the IP given back from the WiFi system.

#   Send locale device IP via Dweet.IO.
#   Written By - Scott Beasley 2021
#   Public domain. Free to use or change. Enjoy :)

import network
import urequests
from time import sleep

# Globals

# Add your wifi info here...
SSID = str('YOUR-SSID', 'utf8')
PASSWD = str('YOUR-PASSWD', 'utf8')

# Add your Dweet thing name (See http://dweet.io for more info)
thing_name = str('YOUR-THING-NAME', 'utf8')
ip_address = str('', 'utf8')

def wifi_connect ():
    global SSID, PASSWD
    sta_if = network.WLAN(network.STA_IF)
    if not sta_if.isconnected():
        print('connecting to network...')
        sta_if.connect(SSID, PASSWD)
        while not sta_if.isconnected():
           pass  # Dwell until we get connected or an error.
    settings = sta_if.ifconfig()
    print('network settings:', settings)
    ip_address = settings[0]
# Let's get it all started up...
wifi_connect( )

# Send the GET request with the IP. 
r = urequests.get("http://dweet.io/dweet/for/" + thing_name + "?pip=" + ip_address)

# If the the send request failed, you could add retry logic.
if r.status_code == 200:
    print('Send IP was good')
    print('Send IP was bad')
    # Add retry here or place in loop until good or you decide to stop trying.

r.close( )

# The rest of your code...

The first part of the code includes the needed libraries. In this case from "network", "urequests", and "time".  Afterward, we set up the WiFi connection info, and then the name that we wish to use for this particular device.   For the "thing name" you want to try and make it unique so as not to bump heads with other dweet users. This is actually easy to achieve with some thought. You could go the whimsical route and have something like: mydeviceisagooddevice_gualt_2028 or you could use a GUID or other unique sequence.  Just make it your own!

Next we need some code to snag the dweet sent by the device. You could do a dweet request from code to do this, via Python, C, C++, javascript, etc. Here I do some simple HTML and javascript to fetch and display it. There is also the ability to redirect to the IP via an HTTP redirect if the device takes web requests. 

<h2>Getting last ip</h2>
// CHANGE THIS to your DWEET "thing" name!!!
// See https://dweet.io for more info.
const url = 'https://dweet.io/get/latest/dweet/for/' + THINGY;

// Do a GET request for the URL
const Http = new XMLHttpRequest();
Http.open("GET", url);

// Handle the response sent back from the GET request.
Http.onreadystatechange = (e) => {
    var dweetdata = JSON.parse(Http.responseText)

    // Get the 'pip' field we sent in the dweet from the device
    var ip = dweetdata["with"][0].content.pip;

    // Hit F12 in the browser to see any console info

    // Build the URL to the device (if needed).
    const url = 'http://' + ip + '/';

    // Set the lable with the ip text.
    document.getElementById('IP').innerHTML = ip;

    // Redirect the browser to the real page.
    // This is if your device takes HTTP requests
    // Uncomment if needed.
    //window.location.href = url;
<label id = "IP" name="IP"> </label>



After trying this out, you should be able to integrate this technique into your own code and projects. I will be using this method in a future tutorial I will post here soon.

I hope you found this tutorial informative and helpful. 




Login or Sign Up to post comments on this tutorial.