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.
Related
Hi there,
I will search and tried lots of solution for our problem but none of works. This will be a long post so be ready.
Our System:
We have an arduino uno r3 clone and ethernet shield wiznet w5100,
This Arduino makes a http post request to a windows web service.
According to response arduino will make something that is not revelant to question.
So in our test environment, we will install a windows webservice to our windows machine and plug this machine to our local network. Then plug arduino to our local network too. than with our server ip ,arduino made an dhcp request get ip from our modem-router and can call webservices from our server.
From now on everything works fine.
Here is some sample code from our arduino.(I only use this extra library for arduino:"RestClient.h"
#include <Ethernet.h>
#include <SPI.h>
#include "RestClient.h"
RestClient client = RestClient("192.168.100.17",51200);
String response;
String PostData;
void setup()
{
Serial.begin(9600);
byte mac[] = { 0x04, 0xD6, 0x2E, 0x81, 0x01, 0xB0 };
if ( Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
}
delay(1000);
Serial.println(Ethernet.localIP());
}
void postDataToServer(long rfidnumber,int rfidsource){
String postedRFIDNumber=String(rfidnumber);
postedRFIDNumber="000"+postedRFIDNumber;
response = "";
PostData="RFID="+postedRFIDNumber+"&SOURCE="+rfidsource;
const char * myPost = PostData.c_str();
int statusCode =
client.post("/sqlpublish/TTSWebService.asmx/INSERT_INDEXRFID",myPost,&response);
}
We need to install our system to a company. This Company has its own local network. they have very restricted local network. You can connect their network but can not go to the "www" without their permissions. But that is not problem. We will use only local connection for our web services because we will also use an windows server which will inside local network.
This is a picture of their network system schema:
In this picture switches is missing but you can simply guess they uses lots of swtiches .Because company is very wide and have lots of device.
So if I connect any device to their local network , this device first call dhcp protocals get ip from Windows DHCP Server and then can communicate in the local network with other devices. But can not get through the internet modem because of the firewall in the router.
Then We will setup our system like this:
In this setup switch models are : "AVAYA".
VSP7000 XLS
ERS4826 GTS-PWR+
ERS3549 GTS-PWR+
Firewall is: Watchguard Firebox M300
When we setup the system something weird is going on. First of all when I connect my PC(My Device) to local network I can call webservices in the windows server. But Arduino cannot get IP from DHCP Server, and naturaly cannot connect to web services. So we think that "ok we can give IP static". Than we will give IP to our Arduino manually. After that weird things started to begin. When Arduino try to conenct our Windows webservices it only get response some times. Roughly 1 of 20 has got response from server anything else get time out connection.And also succeded response time also too long.
If we ping our static arduino IP From another Device(For example Device_1), It get response sometimes again. (Same Amount)
So then we try to narrow down our problem.
First Of All We Change the setup like this for once to make sure problem in local network.
And Normaly all systems works perfectly. So our Modem's DHCP server make it works perfectly.(Also in this setup If I give Ip static it worked too).
So there is these possibilties for connection error.
1-Firewall
2-Switch problem
3-Arduino Clon Problem.
1-Firewall
When We talked about the problem with system admin, He told ust every local network connection and port is open in the firewall. He is probably right because any pc connect to local network can call the web sevice.
2-Switch problem
This Question, talks about it as a solution it says use static ip, bu in our case it did not solve the problem.
In this Question jdr5ca answer make sense but ı hve no idea how can test the problem or solve the problem
In this Question answer tried but not working.
Lastly this post , but it is so general. and also what ı should use replace for "arping" in windows.
3-Arduino Clon Problem.
next week we try it with with original arduino ,
I will inform everybody.
SO, any suggestion,tool, or some diagnostic tool for the problem I'm open all the suggestion.
Apparently Our Problem is a specific switch model!
Here is the model: https://www.zyxel.com/tr/tr/products_services/es_108a.shtml?t=p (zyxel ES-108E)
When we directly connect arduino to zyxel switch, and ping arduino from another device in the network only some of pings successful.(Roughly %15 succeeded).
But if we use another switch model or don't directly connect to zyxel switch, it works fine.
I dont know why it is not working with zyxel but problem is the switch!
I looking for clarification on how the WiFiClient and WiFiServer objects in this example ESP8266 sketch starts a TCP connection and allows an Android app to read and write to buffers that's setup in the sketch.
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
WiFiServer server(port);
WiFiClient client;
uint8_t buf1[1024];
server.begin(); // start TCP server
client = server.available(); // wait for it to connect
buf1[i1] = (uint8_t)client.read(); // read char from client (RoboRemo app)
client.write((char*)buf2, i2);
This sketch talks to a closed-source Android app call Roboremo. It uses WiFiServer to create a TCP server, and WiFiClient to read/write to buffers. What is this TCP server, and what mechanism is being used to read/write to the ESP8266? So, if I were having a conversation with an app developer, how do I tell them how to write to this TCP server (other than IP and port number)? Is this a "TCP socket", and does that translate into something that mobile app people would know how to proceed?
Another way to ask this question: I'd like to be able to test read/write to the ESP8266 without the Android app. So if I have a Raspberry Pi on the same network as this ESP8266, what utility (mechanism again) can I use to read/write to those buffers from the Pi?
Checking the Roboremo sketch shows that you will be working with esp8266 on STA mode by providing your ssid and pass into the sketch. That means you need to find the ip address of esp8266 before making a connection. A ridiculous comment from sketch is :
Then somehow find the IP that the ESP got from router
So, if you will continue with STA mode, you may go to your router settings and find the ESP8266 from DHCP clients.
For a raspberry TCP client connection, you need nothing else from a Linux socket client. Here a basic python client to test on raspberry :
import socket
import sys
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error, msg:
print 'Failed to create socket. Error code: ' + str(msg[0]) + ' , Error message : ' + msg[1]
sys.exit();
print 'Socket Created'
host = 'YOUR_ESP8266_IP '
port = 9876
s.connect((remote_ip , port))
print 'Socket Connected to ' + host
#Send some data to remote server
message = "test esp8266 server"
try :
s.sendall(message)
except socket.error:
print 'Send failed'
sys.exit()
print 'Message send successfully'
I am running an application on my microcontroller(MSP432), which writes data to an Ethernet cable to send it over to PC.
I am using Packet sender to view the data received on the port(502) on PC from MC.
Data received on PC
As we can see in the above picture, the port numbers of MC are increment for every packet sent.
What will happen when it reaches to the maximum number?
Will it restart at some other port number and proceed with the process or will it stop?
Edit1: Modbus protocol library used from http://myarduinoprojects.com/modbus.html
Edit2:
Making a call to this function everytime i have a new data to send through MODBUS. Mb.Req(MB_FC_WRITE_MULTIPLE_REGISTERS, 0,11,0);
if (MbmClient.connect(ServerIp,502)) {
digitalWrite(GREEN_LED, HIGH);
#if DEBUG
//Serial.println("connected with modbus slave");
// Serial.print("Master : ");
for(int i=0;i<MbmByteArray[5]+6;i++) {
if(MbmByteArray[i] < 16){
//Serial.print("0");
}
//Serial.print(MbmByteArray[i],HEX);
if (i != MbmByteArray[5]+5) {
//Serial.print(".");
} else {
//Serial.println();
}
}
#endif
MbmClient.write(MbmByteArray,13+(Count*2));
MbmCounter = 0;
MbmByteArray[7] = 0;
MbmPos = Pos;
MbmBitCount = Count;
*state= true;
MbmClient.stop();
delay(100);
digitalWrite(GREEN_LED, LOW);
} else {
*state= false;
MbmClient.stop();
}
It seems you are using this Modbus example
I have never worked with that but I suppose that because the destination port in the code is the same you have in your sniffing image: 502
Probably you are repeatedly calling this method:
void MgsModbus::Req(MB_FC FC, word Ref, word Count, word Pos)
Inside this method you can see this line:
if (MbmClient.connect(ServerIp,502)) {
...
So every time you call that function a new connection is open. When you open a connection through a socket, the operating system or the network stack needs to select a source port and IP address from where the TCP message is sent.
This is why you see always a new source port and that port is increasing. This is what is called an ephemeral port. How the source port is selected by the TCP stack you are using is implementation dependent, though it's very common to begin with some port and every time a connection is open, it selects the next available port.
If the stack is well programmed, most probably your TCP stack will wrap around and begin with some specific port from 1024 up (First 1024 ports are restricted). The code I saw seems to close the port with this function:
MbmClient.stop()
You need to check ports, after being used, are closed. Otherwise, at some point you will run out of available ports (resource leak).
If you want your socket bound to a specific source port, you need to use a function similar to Linux socket bind
Now, a wiser way is to use all the time the same connection. You may need to modify that example.
I'm completely new at using the QtNetwork for connecting computers.
Right now all I want is to see an attempt at a connection. So I create a GUI application and on the mainwindow.cpp I write these two functions as slots for two buttons:
void MainWindow::on_pbTalk_clicked(){
QString IP = ui->leIP->text();
ui->pteLog->appendPlainText("Now Talking to IP: " + IP);
talker = new Talker();
talker->connectToHost(IP,25000);
}
void MainWindow::on_pbListen_clicked(){
ui->pteLog->appendPlainText("Now listening on any port, I think");
listener = new Listener(this);
if (!connect(listener, SIGNAL(newConnection()), this, SLOT(on_newConnections()))){
ui->pteLog->appendPlainText("The connection of slot and signal failed!");
}
}
Now Talker is essentially a QTcpSocket there is nothing reimplemented just yet.
Listener is a QTcpServer with the following code con Listener.cpp:
Listener::Listener(QObject *parent) :
QTcpServer(parent)
{
qDebug() << "Listening on any port";
listen(QHostAddress::Any);
}
void Listener::incomingConnection(int socketDescriptor){
qDebug() << "New connection: " << socketDescriptor;
}
So I run two instances of the same program. One is in my machine. I run the program and push the Listen button (IP 10.255.255.101).
The second instance is run in a virtual machine (IP 10.255.255.215) where I run the program and push the Talk button. This, as I understand should attempt to open a connection to IP (which is 10.255.255.101) at port 25000 and I should get a "New connection" message in the console. However no such message appears. And since this is not working, I'm not moving on.
Can any one tell me what I might be doing wrong?
Check the documenation of QTcpServer::listen - it says:
Tells the server to listen for incoming connections on address address
and port port. If port is 0, a port is chosen automatically. If
address is QHostAddress::Any, the server will listen on all network
interfaces.
QHostAddress::Any means that you are listening on all network interfaces, not ports. (For example, if you want to have a local server only, you could use QHostAddress::LocalHost - check QHostAddress::SpecialAddress for more like that.
If you want to set the port manually, you have to call:
listen(QHostAddress::Any, 25000);
If not, you can get the automatically chosen port by calling
quint16 port = serverPort();
The "listening on any port" idiom isn't available for your use; it's not how UDP and TCP were meant to be used. Most likely you shouldn't be designing your communications that way. Use a dedicated port.
If you want to build a packet sniffer, you'll have to use the platform-specific mechanisms designed for that. There are libraries that help you with that task, the most notable would be WinPcap on Windows and cross-platform libpcap from the TcpDump project.
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?