ESP8266, when connecting to AP, requesting sepcific IP or discoverable host name? - microcontroller

Is there any way in the ESP8266 code to ask the router for a specific IP? I mean, in the following example (which I copied from the web), it gets "192.168.1.3". The part "3" is automatically assigned, and may change next time. I want this number to be a specific number. I know I can modify the router setting to add a static IP, but at least for my router, adding a static IP is slow and inconvenient. And I may swap the ESP8266 board and run the same code. The router is mine, and used only by me, so if I need to change some settings of the router to grant this kind of request from the client, I can do that.
If there is no such feature, can I make the ESP8266 discoverable by a specific name (again, without creating a translation entry in the router settings, but within the ESP code)? For example, if the ESP8266 runs a web server, can I make the web server accessible by something like "http://myserver1" instead of "http://192.168.1.3"?
#include <ESP8266WiFi.h> // Include the Wi-Fi library
const char* ssid = "SSID"; // The SSID (name) of the Wi-Fi network you want to connect to
const char* password = "PASSWORD"; // The password of the Wi-Fi network
void setup() {
Serial.begin(115200); // Start the Serial communication to send messages to the computer
delay(10);
Serial.println('\n');
WiFi.begin(ssid, password); // Connect to the network
Serial.print("Connecting to ");
Serial.print(ssid); Serial.println(" ...");
int i = 0;
while (WiFi.status() != WL_CONNECTED) { // Wait for the Wi-Fi to connect
delay(1000);
Serial.print(++i); Serial.print(' ');
}
Serial.println('\n');
Serial.println("Connection established!");
Serial.print("IP address:\t");
Serial.println(WiFi.localIP()); // Send the IP address of the ESP8266 to the computer
}

Almost all routers have DHCP Server. What you can do is create a DHCP Reservation for your ESP8266s MAC Address.
For domain name what you can do is crate host entry in your local PC. Something like this,
192.168.1.3 myserver1
Update
I have found this tutorial which can answer to static IP configuration.
The resolution of the domain name is done by the DNS Server. Since you don't have a public IP or actual domain, your IP will not resolved by any DNS Servers out there. So what you can do is crate a DHCP host entry in your router (creating this host entry is also done in above tutorial) or create a host entry inside your local PC.
Note: I currently don't have a NodeMCU module. Otherwise I could have try this myself.

Related

Send Sensor Data Over Wifi From One Arduino To Another Arduino

I want to send the data obtained by one arduino from the flex sensor to another arduino which take actions on the basis of the data recieved and I want to do this data transfer process over wifi. Can you help me how can I do this.
Do I want to configure a server on any one of these Arduinos or anything else ?
If I want to configure a server then how can I do that?
WiFiClient object wraps a TCP socket. A normal TCP socket is connected to IP address and port. WiFiServer starts a listening socket on a port. If server on listening socket is contacted by a remote client socket, it creates a local socket connected with the remote client socket on a free port and returns a WiFiClient object wrapping the socket. Everything you write or print to a WiFiClient is send to that one remote socket.
If one of your client boards creates a WiFiClient and connects it to IP address and port of the WiFiServer on your 'server' board, then you get there a WiFiClient from server.available() and this two WiFiClient objects are connected. What you write/print on one side you read only from the WiFiClient object on the other side.
client socket
if (client.connect(serverIP, PORT)) {
client.print("request\n");
String response = client.readStringUntil('\n');
Serial.println(response);
client.stop();
}
server side
WiFiClient client = server.available();
if (client && client.connected()) {
String request = client.readStringUntil('\n');
Serial.println(request);
client.print("response\n");
client.stop();
}
see the ChatServer example for a WiFiServer example

In QT Trying to receive data from specific IP address using UDP

I am trying to receive data from specific IP address (like 166.0.0.245) using UDP protocol in QT. I am having multiple connections in my network which are udp.
But the problem is i am receiving from any ip address rather than specific IP address.
Below i am attaching my code where i am setting the ip address of my receiver i.e 166.0.0.34 and my senders IP address is 166.0.0.245 -> where do i specifically set this ip address so that i can receive only that ip address and discard rest of the ip address in my network.
Plz can somebody suggest me where do i specifically set my ip address at the receivers side which accepts only one sender with specific ip address and port.
Thanks in advance
NetBroadcasterDlg::NetBroadcasterDlg(QWidget *parent) : //Main Function
QWidget(parent),
ui(new Ui::NetBroadcasterDlg)
{
// ethernet initialisation and binding wini
udpSocket = new QUdpSocket(this);
udpSocket_send = new QUdpSocket(this);
udpSocket->bind(QHostAddress("166.0.0.34"), 1100, QUdpSocket::ShareAddress);
connect(udpSocket, SIGNAL(readyRead()),
this, SLOT(processPendingDatagrams()));
...
}
void NetBroadcasterDlg::processPendingDatagrams()
{
// Read data from ethernet
while (udpSocket->hasPendingDatagrams()) {
datagram.resize(int(udpSocket->pendingDatagramSize()));
udpSocket->readDatagram(datagram.data(), datagram.size());
// Sending data to the Target
udpSocket_send->writeDatagram(datagram.data(), datagram.size(),
QHostAddress("166.168.1.20"), 2500); //target address and port
...
}
Actually, an open UDP socket is intended to receive anything from any IP address which is able to open that UDP socket in your network.
You can either block other IP addresses to connect to this particular interface or
QHostAddress fromAddress;
udpSocket->readDatagram(datagram.data(), datagram.size(),&fromAddress);
if(fromAddress.isEqual(QHostAddress("166.0.0.245")){
// do whatever with your filtered data
}

UDP Client server: Usage over a WAN

I have an application that sends data (server) to a UDP client. It runs perfectly on the same computer, and over the LAN as long as I know as the destination address of the client. However, the moment I do this over the internet, it no longer works because it does not have the address of the destination computer. Here is the server code:
public ServerSender(String address, Int32 port, int TTL)
{
m_Address = address;
m_Port = port;
m_TTL = TTL;
Init();
}
private Socket m_Socket;
private IPEndPoint m_EndPoint;
private String m_Address;
private Int32 m_Port;
private Int32 m_TTL;
private void Init()
{
IPAddress destAddr = IPAddress.Parse(m_Address);
m_Socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
m_EndPoint = new IPEndPoint(destAddr, m_Port);
}
public void SendBytes(Byte[] bytes)
{
m_Socket.SendTo(bytes, 0, bytes.Length, SocketFlags.None, m_EndPoint);
}
Here is the client:
public void Connect(string strAddress, int port)
{
m_Address = IPAddress.Parse(strAddress);
m_Port = port;
m_Socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); // Multicast Socket
m_Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
m_EndPoint = new IPEndPoint(m_Address, m_Port);
m_Socket.Bind(m_EndPoint);
m_Socket.Connect(m_EndPoint);
IsConnected = true;
this.DoRead();
}
private void DoRead()
{
try
{
m_Socket.BeginReceive(bytes, 0, bytes.Length, SocketFlags.None, new AsyncCallback(OnDataReceived), m_Socket);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
Obviously the server will need to listen for the remote connection and the client will need to connect to the server. I would think that I could then get the remote address and then in the SendBytes method I would sent to a remote endpoint. I have tried many different examples of async servers and they all lead to various errors. Is there any simple way to change this code to allow it to connect to a remote host over the internet?
Thanks
To summarize / elaborate on the comments a bit:
You got an host with a public IP, i.e. directly connected to the inet (your 'server')
You got an host without a public IP, i.e. connected to the inet over some gateway/firewall ... (your 'client' )
Since I don't know how much you know about networking:
TCP / UDP packets are wrapped in IP packets. IP deals with IP addresses, TCP and UDP know about ports, but: TCP port 1 and UDP port 1 are different things!
Your network setup probably look like , with some sample IPs:
|--------| 47.46.43.42 |---------|192.168.0.1 |--------|
| Server |======== The INET =========| Gateway |==================| Client |
|--------| 81.82.83.84 |---------| 192.168.0.100|--------|
The gateway hererin is your 'router'.
Your client will be configured to send any traffic not belonging to the local LAN to the Gateway (if you are able to browse websites from your client, it is configured the right way).
Note again: The network devices will deliver IP packets to the proper recipients, if the destination IP belongs to 'their' network. They won't deliver to anyone if the destination IP is not known.
If your client tries to send an UDP packet to your server port 1 , it will send an IP packet that contains the destination IP 47.46.43.42 and the source IP 192.168.0.100 to the gateway, along with the destination port 1 and the port it uses to send the packet (lets assume port 2). The gateway receives the packet, sees the destination IP and sends the content of the IP packet as a new IP packet with (destination 47.46.43.42:1, source 81.82.83.84) further along. It has to use another UDP port for sending, lets say 15. The key thing here is: It also remembers that it sent a packet from 192.168.0.100 port 2 to 47.46.43.42 port 1 its own port 15. Whenever an UDP packet arrives on port 15 from 47.46.43.42, it can assume that it is some reply that should be forwarded to 192.168.0.100 port 2.
Then if the server receives the packet, all it sees is (dest IP 47.46.43.42:1, source IP 81.82.83.84:83:15). if it wants to send back an answer, it will send an IP packet with (dest 81.82.83.84:15, source 47.46.43.42:1). NOTE that the server only 'sees' the gateway, not your client behind it! The gateway receives the packet, recalls that it just sent an IP packet (dest 47.46.43.42:1, source 192.168.0.100:2), assumes that the IP packet is some kind of answer, and sends a new IP packet (dest 192.168.0.100:2, source 47.46.43.42:1) with the content received from the server to your client.
NOTE ESPECIALLY that this only works because the gateway received a packet from the client and learned from this that 192.168.0.100 and 81.82.83.84 'speak to each other'. Only because of that it knows what to do with packets arriving from your server!
Now lets try the other way round. There are two possible cases:
The server knows the IP of your client (192.168.0.100) . Recall that this IP is not known to any device in the internet, thus if it sends out an IP packet (dest 192.168.0.100, source 81.82.83.84), it will not reach the gateway because the only packets reaching the gateway are those with a destination IP of 47.46.43.42 . This option will never work.
The server knows the IP of the gateway and sends out an IP packet (destination 47.46.43.42, source 81.82.83.84). This packet will reach the gateway. But what should the gateway do with it? It never memorized that packets from IP 81.82.83.84 should probably be forwarded to your client.
The only way for the gateway to learn would be if the client sent out a packet to your server first. This option will not work either.
Thus, it does not suffice* for the server to know the IP of the client, but the gateway needs to learn about the 'conversation' between your server and client. And the way your gateway works requires that the client sends out a packet first, using precisely the port for sending that you want to use further on. If your server should send to your client UDP port 2, the client must have sent a packet to the server first from UDP port 2.
What your gateway does is NATTING, and you better read a bit about it.
I assume, however, some standard setup of your network. This is likely, but technically, it could be entirely different, perhaps your gateway acts as a firewall, partially blocking outgoing traffic as well, perhaps your server is configured to use your gateway as a gateway as well (weird, but technically possible).
Thus troubleshooting offline is hard, and I am sorry if I cannot give you the solution (TM) for your concrete problem here.
PS: You might wonder why your client can send packets to the gateway with a wrong destination IP, while your server can't. This relates to the fact, that on the ethernet layer, your LAN is a single network, while the internet is not...

Communicate through a VPN link using Arduino UART WiFi Module

The following describes the configuration of the system and other details. The problem is mentioned after that. If the initial description is too much or not needed, please skip it.
I have a VPN (between two SIMs). One SIM is in a USB dongle, the other one is in a WiFi router. The dongle is connected to computer 'A', and another computer ('B') is wirelessly connected to the WiFi router which can access the dongle. The VPN is between the router and the computer 'A' (dongle). As the WiFi router leases 'B' a private IP, 'A' can't ping 'B'. But 'B' can ping 'A'. There is no issue up to now.
I have created a socket-based simple Java network application (client - server pair). The client is running on 'B', the server is running on 'A. Only the client can send data to the server, the other way around is not possible. This part also is OK. Now I need to replace the computer 'B' with an Arduino. I need to send some data from the Arduino to computer 'A' through the same network.
For this, the Arduino needs a WiFi module to access the router. I've this USART WiFi module with the shield. It can successfully connect to the router, (I can ping it from a computer, which is also connected to the router).
THE PROBLEM
Now the configuration is as below.
There's the (server) application running on computer 'A', which is listening on a given port (port 8090). How can I send some data (eg. some text "hello!"), from the Arduino to the computer 'A'?
Please note that for the WiFi module I'm using, I found some examples for applications based on the Arduino WiFi shield. But I can't apply them to my configuration (can I?).

RTOS connects via TCP to Local Server, but not to Remote Server

I'm using an RTOS device and when trying to connect over TCP to a server I am running into some unusual results.
When connecting to a server on my local network, the connection is fine and I can see the packets flowing on Wire shark.
However, when attempting to connect to a remote server, one outside of my domain, the connection fails with an error code of:
TCP_ERR_NOSUCH_SOCKET (-4) --- Indicates that you have attempted to allocate a socket that
does not exist.
This occurs during connect() function:
bool CTCP::Connect( const char * ipaddr, unsigned short port ) {
IPADDR ip = AsciiToIp( ipaddr ) ;
this->m_fdnet = connect( ip, INADDR_ANY, port, TICKS_PER_SECOND * 3);
if( this->m_fdnet < 0 ) {
CLogging::Debuglogf("[TCP] Error: Connection failed, error=[%d]", getsocketerror( this->m_fdnet ) );
return false;
}
return true ;
I tried with two different remote servers, one with Google, and another one.
As a note: I am able to ping both, and both of the ports are open.
I was wondering why this is occurring.
I am a colleage of #alexfontaine and we found that this problem is actually caused by the network setup of our office and has nothing to do with the connect code above.
See this question
DHCP IP address have access to the internet but static IP address don't behind a router
Instead of deleting this question I am answering it with another question for the next person that might have this problem as well.

Resources