BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

New API request

api
#1

Because the server is registering the hardware’s IP when connected (external ip for most devices if not all), can an API be added to read said IP i.e. http://blynk-cloud.com/token/ip.

0 Likes

#2

you could just add it to a Virtual Pin value. that way you’re not hiding behind a nat address.

None the less, if you still are for the api method, you could submit it to the roadmap. Im always in favor of more api options.

0 Likes

#3

Thank you @lvennard for the suggestion.

I’ve been retriving my external ip with code and displaying it flawlessly for over a year on a Yun board’s virtual pin. However, when I moved the code to a rpi board and using the blynk-python library, the poking for the external ip is cousing the ‘broken pipe error’. Typically, retriving the ip doesn’t exeed 1.5 seconds but, evidently, it’s too much for blynk’s heartbeat.

0 Likes

#4

Googling this error seems to indicate a Python issue, not Blynk one. Check out some of the articles as there are many references and solutions that might help you.

https://www.google.ca/search?source=hp&ei=q4lcXMSpL-Hl9AOp1ba4Ag&q=broken+pipe+error+python

0 Likes

#5

Thanks @Gunner I’m aware that the error is caused by Python but, for some reason, the function I’m using to read my external ip is causing communication interruptions to the blynk.cloud server. Will keep troubleshooting…

0 Likes

#6

I suspect the error in a python script will stop it from running and thus by nature, end the Blynk Link.

0 Likes

#7

Yes @Gunner, I guess we’re saying the same thing. Here’s the culpit code if you want to play with it :neutral_face:
Without it, the project works flawlessly.

The function is called every 10 minutes:

# set virtual pins
_ip = 18

@blynk.VIRTUAL_READ(_ip)
    def get_external_ip():
        import re
        import requests

        url = "http://checkip.dyndns.org"
        request = requests.get(url)
        clean = request.text.split(': ', 1)[1]
        ext_ip = clean.split('</body></html>', 1)[0]
        blynk.virtual_write(_ip, ext_ip)
0 Likes

#8

ugh… i love you @Emilio, but not a fan of that method.

dns is awesome, never stop using dns names. however, in this case, we have to fight time… the ip address for ipv4bot.whatismyipaddress.com is 66.171.248.178.
anyway you could go straight there and get rid of (maybe) 100ms of time?

just curious, what board do you have?

0 Likes

#9

ps… look what i found in the new server.properties file :slight_smile:

#when enabled server will also store hardware and app IP
allow.store.ip=false 

Dmitry was way ahead of us i see.

1 Like

#10

@lvennard, thanks for following up. I’m aware of it, in fact, the API

http://blynk-cloud.com/token/project

shows the ip address among other information. Will try to extract the ip from said data but, having an API which returns the ip, saves communication and processing time.

FYI, I’m using a rpi board and I used the following code with an Arduino Yun without any issues…

void getExtIP() {
  Process p;
  char c = ' ';

  //get external ip via python file
  p.runShellCommand("/usr/bin/python -U " + path + "getip.py");
  /*
    or
     p.begin("/mnt/sda1/getip.py");
     p.run();
  */

  // read and display the output
  while (p.available() > 0)  {
    for (char k = 0; k < 16; k++)
    {
      c = p.read();
      if ((c >= '0' && c <= '9') || c == '.') {
        externalIP[k] = c;
      }
    }
  }

  for (char k = 0; k < 16; k++)
  { if (lastExternalIP[k] != externalIP[k]) //update ext ip variable
    {
      for (char j = 0; j < 16; j++)
      { //SerialUSB.println(externalIP[j]);
        lastExternalIP[j] = externalIP[j]; //assign current extIP to last extIP
        extIP += lastExternalIP[j];
      }
      break;
    }
  }

  Blynk.virtualWrite(V_EXT_IP, extIP);
}
0 Likes

#11

@lvennard Here’s a quick and dirty code to read the hardware’s ip using the following API till the new API is available :wink:

http://blynk-cloud.com/token/project

#!/usr/bin/env python

import requests as req
import json

url = 'http://139.59.206.133/token/project'

def get_ext_ip():
    r = req.get(url)
    if(r.status_code == req.codes.ok):
      t = r.elapsed # returns datetime.timedelta(0, 1, 666890)
      print('elapsed time: {}'.format(t))
      print()
      data = r.json()
      #txt = json.dumps(data)
      #txt = json.loads(json.dumps(data))
      #txt = data['widgets'][1]
      txt = data['widgets'][1]['value']
      print(txt)
1 Like