A tcp server is implemented on the arduino using EthernetServer(port), a connection is accepted using
client = server.available();
if (client) {
...
}
However, as far as I understand the source of available correctly, it returns a valid client only when there is something to read from this client object.
But I want to send a kind of greeting and a prompt as soon as the connection is established, so, before something has been sent to the connection from the remote side.
Who can I achieve that?
Related
I have to send a file to the local server so I use the httpClient and try to use the post with the host: http://127.0.0.1:5000/show-version, In the serial monitor, but it doesn't work, again the ESP is in access point mode
void SendVersion (){
client.begin(HOST);
client.addHeader("Content-Type", "text/plain");
int response = client.POST(version);
if(response>0){
String response = client.getString(); //Get the response to the request
Serial.println(response); //Print return code
Serial.println(response); //Print request answer
}else{
Serial.print("Error on sending POST: ");
Serial.println(response);
}
client.end();
}
127.0.0.1 is a special IP address which means "this computer or device". When you use it on the ESP32 it means the ESP32, not the server you're trying to connect to. It's also known as localhost - again, shorthand for the computer or device the software is running on. It does not identify an external computer.
You need to use the actual IP address of the server you're trying to connect to. How you'll find that depends on the OS the server is running - if you don't know how to do it, use Google to find out.
And of course, if the ESP32 is in AP mode then the server it's trying to talk to needs to be connected to the ESP32's wifi network in order for the ESP32 to be able to talk to it.
My goal is to retrieve the IP address of client connected to an esp32 in soft Access Point mode WITHOUT client sending a request.
So far calling "remoteIP()" method from "client" object seems to be the only way I can successfully retrieve connected client IP is when an HTTP request is made.
If I call "client.remoteIP()" before the remote client sends an HTTP request I get
All zeros, yikes.
Currently I'm polling the "softAPgetStationNum()" method from "WiFi" object and I'm alerted as soon as a device is connected or disconnected.
I would like to get the client IP along with the new connection alert.
My advanced apologies for not including my code hopefully my description has enough clarity to properly relay my intended objective. Thanks
Ps- if I could get guidance on retrieving MAC of remote client too that would be dope!
I don't know what interface Arduino has built on top of Espressif's framework, but the underlying ESP IDF v4.3 has functions esp_wifi_ap_get_sta_list() which gives you a list of all connected stations (including MACs) and esp_netif_get_sta_list() which maps this list of clients to their IP addresses.
I am trying to use gprs for remote monitoring using quectel m95 and pic controllers.
I know a little bit of http commands used for this purpose and we can send data and receive data from server using http POST and GET commands.
Now my application is such that my app request some data from the server.
The server has to get this data from the pic controller using gprs.
How can I send a request from the server to the pic controller which is interfaced using quectel modem.
Also I know that the server has to keep track of the IP address assigned to the modem when initialising a gprs context.
Then only it can send request to the modem.
I want to know is this feasible, if yes how can one achieve this.
Please do throw some light upon this.
Regards
Sanket
The Quectel GPRS module (modem) features a TCP/IP stack, and you treat it as the client in the server-client model. Refer to the Quectel reference material for specific AT commands required to achieve a certain task, but here's the general process:
The PIC initiates the GPRS context in the modem (comms between the PIC and the modem is via a UART).
The PIC initiates the opening of a TCP/IP connection to your server on the GPRS context. The public IP address of the server must be known to the PIC, so keep it static.
The server accepts the client connection request and assigns a socket for data transfer.
Data is sent through the open TCP/IP connection from either end. If originating at the PIC, the data must first be sent over the UART to the modem to forward to the server. If originating in your app on the server, the data must be placed in a TCP/IP packet and directed through the socket to the client. Note that once the connection is open, the socket is bound to the client - this is what you need to keep track of (should you have multiple clients).
I have a ssh tunnel setup using libssh. However, this does not bind it to a local port, so what I need essentially is to forward all data received on a port to the ssh channel. However, when I do this:
QTcpSocket* socket = new QTcpSocket();
socket->bind(27017);
QObject::connect(socket, &QTcpSocket::readyRead, this, &Forwarder::newData);
The newData slot is never invoked i.e. the readyRead on QTcpSocket is never invoked. Whenever I try to connect to the port via an external script, I get an Operation timed out. I am sure that atleast the port is opened because when the Qt application is not running, the error I get is Connection refused.
I have considered using QTcpServer, however, how do I even handle this case? I will probably get newConnection signals, but what data do I write to the ssh channel? How would I handle multiple incoming connections for a QTcpServer? Isn't a connection request also a data packet? Can I just not forward everything to the channel (The ssh channel I am referring to is akin to a fd that I can simple write to).
I've been trying to do TCP communication using my Wavecom Fastrack modem. What I want to achieve is make the modem connect to a specified TCP server port to enable me to transfer data to and from the server. I found some information on than in the user's guide.
Basing on the information you can find on page 66 I created an application that opens the serial port to which the modem is connected and writes the following AT commands:
AT+WIPCFG=1 //start IP stack
AT+WIPBR=1,6 //open GPRS bearer
AT+WIPBR=2,6,11,"APN" //set APN of GPRS bearer
AT+WIPBR=2,6,0 //username
AT+WIPBR=2,6,1 //password
AT+WIPBR=4,6,0 //start GPRS bearer
AT+WIPCREATE=2,1,"server_ip_address",server_port //create a TCP client on port "server_port"
AT+WIPDATA=2,1,1 //switch do data exchange mode
This is exactly what the user's guide says. After the last command is sent to the modem, the device switches to data exchange mode and from then on everything what is written to the serial port opened by my application should be received by the server and everything the server sends should appear in the input buffer of that port.
The thing is that I did not manage to maintain stable bidirectional communication between the server and my modem. When I write some data to the serial port (only a few bytes), it takes a lot of time before the data appears on the server's side and in many cases the data does not reach the server at all.
I performed a few tests writing about 100 bytes to the serial port at once. Logging the data received by my server application I noticed that the first piece of data (8-35 bytes) is received after a second or two. The rest of the data appears in 2-5 seconds (either as a whole or in pieces of the said size) or does not appear at all.
I do not know where to look for the reason of that behaviour. Did I use wrong AT commands to switch the modem to TCP client mode? I can't believe the communication may be so slow and unstable.
Any advice will be appreciated. Thank you in advance.
what OS are you running? Windows does a pretty good job of hiding the messy details of communicating with the GPRS modem, all you have to do is create a new dial-up connection. To establish the connection you can make a call to the Win32 RasDial function. Once connected, you can use standard sockets to transfer data on a TCP port.
i have been using wavecomm modem for 2 years now.As far as i know from my experience is that if you are able to send some of the data then you can send all of the data.
the problem might be in the listening application which receives the data on the server side.
It could be that it is unable to deal with the amount of data that you are trying to send.
try sending the same data in smaller busts
with some delay in between them,then you might receive all data intact.