Internet of Things : ESP module Wifi Ip address - java

as you can read in title I'm making a super simple IOT (Not really)
and
using a esp8266 and want to make it connect to my home wifi network and make a communication between a android app and the module (android app connects to home wifi)
Note : Iam Using CODE VISION AVR
you may say :
Why not directly connect to ESP when its on Server Mode?
that's fine cause when ESP is server "he" can set ip for him self so
ip will be always same BUT I want it to connect to home wifi
-
Why you wont connect with IP ?
you cant find the module ip easily because its given by Wifi
-
Find ESP ip using IP scanner
Its not possible cause it changes every time and user have to change
IP every time in android app AND my app cant scann every time it want
to connect to a single device its so dumb...
-
Do a Static Ip for ESP !
well that's not possible too because maybe that IP was taken by
someone before ESP send static IP AT command ! + if IP wasent taken it may taken after Home Wifi Restarts !
Sorry for bad English :X

You can use software serial to configure esp8266 from arduino. You need to know the setup circuit perfectly. Keep it in mind that esp8266 operates on 3.3V where arduino gives 5V output. The most important thing is to send AT commands to esp8266. You can use the following command to connect with your wifi.
AT+CWJAP="Your_WiFi","password"
If you need to see the ip address of esp8266, you can use this command.
AT+CIFSR
If you want to set static ip to esp8266 , then you can try this one.
AT+CIPSTA="ip module","ip gateway","subnet mask"

Use mDNS responder:
https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266mDNS/examples/mDNS_Web_Server/mDNS_Web_Server.ino
This way you would be able to connect to ESP8266 via hardcoded url like http://esp8266.local.

Related

How to find IP address of OTHER WiFi Devices on network programmatically in android

I have an ESP8266 that connects to my WiFi network after being configured (via WiFi Manager library) and runs a HTTP web server that displays certain sensor data. It connects to my specific network with a 192.168.0.XX IP and it works well.
My problem comes with the android app, and how to actually programmatically retrieve the IP that the webserver is running on? My routers DHCP only reserves IP's for a week - so hardcoding the IP into the app is not a good option, and having less tech-savvy people find the IP and configure it themselves is also not what I'm looking to do.
I also can't hardcode a static IP as friends & family will be using this with different networks and routers (so 192.168.0.X would not work on a router with an IP of 10.0.0.1 for example).
So, how do I go about programmatically getting the IP of my ESP8266 that is connected to my local network?
There is a solution that maybe isn't very optimal and professional, but will do for hobby project. You can make your android app scan your local network using http GET on all addresses - e.g. trying to GET something like this: http://192.168.0.X/sensor_status. You can configure your esp8266 device to respond with code 200 (OK) and save this address in your android app's persistent storage. Next time you can simply check if there is ip saved in persistent storage and try to connect to it. If esp8266 is not available under remembered ip, you can re-scan your network.

Communicate over network without knowing device IP address

I am working on design of a device that are installed on clients networks. Currently there is about 180 devices installed arround the world. One major concern is to access them for debugging purpose later.
Context :
All devices work the same.
OS is Linux
Default mode is DHCP but sometimes Static IP
Currently the method to get addresses is :
Log on a computer of the client with any remote desktop software available and search with :
DHCP address in router's leases table (most of the time clients doesn't know their credentials)
Angry IP scanner, scan for port 22 (slow)
ping the name of the device (doesn't always work)
Expecting :
Actually I know it is possible to communicate with devices on network without knowing its IP address by broadcasting on the network but not found any example yet or start point. I would like to write a small Java program that will be able to broadcast on a network to :
List every devices with their IP
Assign IP address to device and/or other functions
Remember : I would like to find devices that have no keyboard, screen or other UI. If I receive a unit from a client (for debug purposes) that was configurated for example with 10.1.1.100 and my computer is on 192.168.1.110 (on the same switch), I want to be able first to know the IP address of this device, second, be able to send a SET IP command (I'll manage how to handle it once I'm able to send data).
For the server part of the device, I don't mind, it could be done in C++ or script... This will probably needed to answer to broadcast and receive requests.
So far, I've been able to create a small UDP client and server.
Client in Java using DatagramSocket, server in C using recvfrom().
The problem with this solution is that my computer need to have the same network mask as the device, example :
Device has 192.168.2.217
Computer has 192.168.1.110
My computer will need 255.255.0.0 as netmask, ortherwise I will not be able to broadcast 192.168.255.255.
Seaching more, the need to change the local computer netmask to meed broadcasting range seems to be a limitation of Java...
new DatagramPacket(sendData, sendData.length, InetAddress.getByName("255.255.255.255"), PORT); // not working
new DatagramPacket(sendData, sendData.length, InetAddress.getByName("192.168.255.255"), PORT); // working only if netmask is 255.255.0.0
I think I will need to go deeper in network protocols to do it the way I want and that I know it is possible.

Connecting android device to WiFi hotspot, created on another device

I have two android devices - one is a server, second is a client. I run WiFi hotspot on server (using some external app, like QuickSettings), and then connect to this hotspot on second device. I have an application which transfers some data between these devices, so I need to get an IP address of the server to be able to create a socket on client. So my question is how can I do that inside my application (not justing by typing the proper IP manually)?
Did you try assuming the hotspot is the first IP in the valid range?
I mean: The hotspot gives you device an IP and a mask (and it should give even a gateway). The IP of the hotspot is the IP gateway, but if the hostspot did not tell your device such IP, the gateway is usually the first IP in the range allowed by the mask.

Communicating between Android phone and PC using TCP in java

I'm working on a project where I want my Droid 2 to be able to send and receive data over 3G, to a device connected to an ethernet port (not necessarily a PC, but I'm using this for testing the communication). I believe that eventually the end to end communication will be accomplished over an SSL tunnel, but for right now, I just need to establish basic communication to show that the devices can communicate.
I've written some client/server applications that worked between two computers on the same network, but I'm not sure if the communication in this case will work because of the different networks that the phone and PC are on.
I've been trying to establish if this can be done by pinging some of the PCs at my University and house using a pinging program I got off the market, but I've had no success so far. However, my friend has rooted his phone, and was able to do so.
I suppose this question really has two parts: 1) Is this possible? and 2) Do I need to root my phone to accomplish this, or should I be able to do it without rooting?
Thanks for any responses.
I've never worked with Android phones but I think:
You do not need to have a root access to use sockets;
You said that you want to use 3G to connect to your home PC and I suppose you have some kind of router at home and you need to configure port mapping from your router to your desktop.
I've written some client/server applications that worked between two computers on the same network, but I'm not sure if the communication in this case will work because of the different networks that the phone and PC are on.
One potential problem I can see is that, depending on your 3G provider, there may be a restriction on port numbers and protocols that can be used. For instance, HTTP to any port number may be allowed but unknown packet data (i.e., an unidentifiable protocol) to the same port numbers that work for HTTP, may be dropped.
I've been trying to establish if this can be done by pinging some of the PCs at my University and house using a pinging program I got off the market, but I've had no success so far. However, my friend has rooted his phone, and was able to do so.
Again, another potential problem - ping (ICMP) is not a good test of accessing devices on other networks as most perimeter routers/firewalls will by default refuse ICMP traffic (usually silently) - even my home DSL router/firewall silently blocks ICMP by default. As for needing a rooted phone to be allowed to use a ping app - my phone isn't rooted and I have a ping app from the market which works fine.
I suppose this question really has two parts: 1) Is this possible? and 2) Do I need to root my phone to accomplish this, or should I be able to do it without rooting?
There is no reason I know of that would require a phone to be rooted to use socket-based comms, I'm just wondering if trying to ping across a router/firewall isn't the best test for what you are trying to achieve.
I'd recommend starting with HTTP - I don't know what you use to program for the PC but a simple HTTP 'listener' should be easy enough to put together. Start by listening on port 80 and try from the Android browser - if that works, change the listening port to something like 6789 (remember to open the ports on your home router/firewall). Assuming you've got that working, try using the socket-based stuff you've developed using wi-fi on your LAN, if that works, then try over 3G.

Access dev machine localhost via usb

My activity needs to communicate with a java server located on my dev machine. Actually, using the emulator, I can access the server socket via the special 10.0.2.2 address. Now I'd like to test my app on my phone connected via usb, but it seems like I can't use 10.0.2.2 to access my dev machine localhost anymore.
Naturally using a wifi router and a proper lan addressing does the trick, but I need to use usb connection instead. Any hint ?
edit:
Just added different scenarios to let everyone understand better:
1. Emulator-based scenario:
standard java server running on my pc #localhost:15000
android java client running on my emulator.
When the android app needs to contact the server it uses 10.0.2.2:15000 like specified here.
Everything works fine.
2. Proper wifi scenario with physical connected device:
standard java server running on my pc #192.168.1.10:15000.
android java client running on my device with 192.168.1.11 address assigned.
When the android app needs to contact the server it uses 192.168.1.10:15000 and everything works fine too.
3. Desired scenario:
standard java server running on my pc #localhost:15000.
android java client running on my device connected via usb cable. No network is active.
When the android app needs to contact the server it tries to connect to 10.0.2.2:15000 but instead an exception is thrown (Network unreachable). It seems like special address 10.0.2.2 works only for emulator instances.
I just wonder if in the adb/adt suite there's a way to access pc address from the device connected via usb cable.
I hope it's a bit more clear now.
Thanks.
It seems like there's no way to let scenario #3 work properly, because usb connection is not a real network connection and there's no ip associated to it by the phone.
Actually my solution is to use an ad-hoc wifi connection between my phone and my pc. This scenario is similar to #2, but without router or dhcp. Unfortunately my Hero can't handle ad-hoc connections natively because of this issue. So I had to root my device and to change manually some config script. I hope functionality like this can be available in next releases.
You can setup a connect to your phone over USB from your dev pc, but you have to adb forward. However, I don't think you can route the otherway around over usb: setting up a connection from the phone to the pc.
You could have your pc connect to the phone and check which IP it has.
While thinking about it, I realized, using adb forward you have to use localhost as your host, so maybe you could also do this on your phone: adb forward the port you want to use and on your phone, connect to that port on localhost.

Categories