esp8266 send number to web server

Does anyone know how to send a number to a web server from the esp8266?

I am working on a project that will eventually do math with sensor data and send the resulting numbers from the esp8266 to a PC running Matlab for further analysis. I have been able to run the "Hello Server" example from the ESP8266EbServer library, and edited it to post numbers converted to a text string. However, Matlab acting as a client can only read number formats (uint_8, float, double, etc). It cannot read these from text and convert them. I have even had matlab ping the IP address of the ESP8266 and return an error message confirming that a connection was made but it could not read the numbers.

The key function call from handler in the "Hello Server" example is:

server.send(200, "text/plain", "hello from esp8266!");

From the comments in the ESP8266WebServer.h library file, I can see that the second argument is the HTTP content type code. I can find lists online of HTTP codes that list every file type I can think of, but not the code to just send a number. Actually, I will eventually need to send multiple numbers; at least 2, possibly 6. Sending doubles would be preferred, but I could make a float or int also work.

How can I adjust this function call to send number(s)?

Your text has a lot of information and is a bit confusing. I'd like to confirm some information:

  1. Do you want to send ESP8266 information (numbers) to a web server? If so, then on ESP8226 you should create a "Client" and not a "Server". On the other hand, in the Web server, you must create a server where will "listening" to a port (your choice) opened to the Web;

  2. Have you ever been able to send any data from the ESP8266 to your server?

It can not read these from text and convert them

To send numbers, you'll actually send texts that represent numbers, because on the Web everything is transported from one side to another via text only. So, on the web server side, you should check how to convert this text representing numbers. Have you reached that point?

  1. It might be more interesting to create a server in "NodeJS" for example, save the information in a text file or database and then, manipulate the information in Matlab. Did you consider it?

Assuming you are good with setting up the and connecting the Esp8266 to wifi, add

#include <ESP8266HTTPClient.h>

server.begin();

HTTPClient http;
http.begin("192.168.1.200/Putyourtexthere"); //Specify request destination and contents
int httpCodeOn = http.GET(); //Send the request
http.end();

Assuming the server is at 192.168.1.200, once received at the other end, you can act on whatever text you have put after 200/.

If you need it as number you will have to covert it.

Alternative is to send it as an argument 192.168.1.200/?AnyName=652 where AnyName is anything you want and 652 is a sample value.

This shows the receive side of the arguments.

Here is the client side:

NextPirate:
The two examples are perfect, ie the ESP operating as a Server or as a Client, they probably work correctly individually but don't communicate with each other (I didn't test these two examples, but I already needed both cases, Server and Client very similar).

In the ESP Server, where it is shown "server.arg("Temperature");".
That Temperature parameter should have been sent to the Client request via a URL similar to this:
http://website.com/path?parameter1=value1&parameter2=value2&Temperature=myTemp

====================

Another case is the example of the Client ESP that you pointed out, which in that case wont be able to communicate with the ESP Server, since the Client has to be modified. That example of ESP Client uses what I used to send data, that is, a RESP API as server, which is waiting to receive the data through an HTTP GET method, but a REST API application doesn't use URL as the example that I pointed out before. It uses something like this: "http://website.com/users/1". Where the information being sent is USER = 1.

Why this ESP Client will not communicate with the ESP Server? Because on ESP Server you can't create a REST API.

====================

How can you solve the problem?
Regardless of who your client is (it can be ESP, a PC or a PLC, etc.), that Client must to make a request to his server by using a URL format accepted to it. As far as I understand, your server will be an ESP8266 as well, so the Client must make an HTTP GET request in the format:
http://website.com/path?parameter1=value1&parameter2=value2&Temperature=myTemp

So, on the ESP8266 server, you'll get the value with the function "server.arg("Temperature");", as shown in his example.

In Arduino IDE you can do the text-to-number conversion this way:

String st = "";
unsigned int myTemperature = 0;

st = server.arg("Temperature");
myTemperature = st.toInt();

Check out: server.uri()

On the server side (i.e. ESP8266Webser.h)

"http://website.com/users/1" server.uri() would return "/user/1"

You could parse that.

Server Side:

server.on("/user/1", HTTP_GET, FunctionX);

void FuntionX() {

server.send(200, "text//plain", server.uri() );

}

Thank You both for the responses! I'll look into your suggestions.

In response to your questions: I am trying to send info from the ESP8266 to a PC (Matlab). I may be confused about which is the server and which is the client.

Yes, I have been able to send data to the server from the ESP8266. I edited the basic "Hello Server" example to use 3 changing random numbers converted to a text string in place of the "hello" text. It worked and I was able to see the numbers in a web browser.

A teammate found a matlab example script to read a number from the server (or client). However, when we pointed it to the IP address of the ESP8266, it confirmed that it made the connection but gave an error message that it could not find a number to import. On the serial monitor, I could also see a test output that confirmed that the handler was called on the ESP8266. My teammate found a long list of number types that Matlab could look for, but not text. Perhaps it can, and we just haven't found it yet. I think I understand what you are saying about all info on the Web being text, but that makes me even more confused why Matlab is only looking for numbers. Converting to text isn't a big deal, but it'd be nice if we could give Matlab exactly what it's looking for.

I'm also open to the idea that a web server/client isn't even the best way to do this. We noticed that Matlab could only find the IP address if connected to the same router. That's ok because we're only concerned about these two devices which would normally be on the same router anyway and don't need it to be accessible from the whole internet. (We could even call that a security "feature.")

Let's do it by steps:

  1. On your ESP8266, you must create a code for it to become a Client, that is, ESP will send information (whatever it is) to somewhere. In your case, ESP will send information to a Server that is also on your internal network, connected to the same router. OK! The example indicated by NextPirate can be useful;

  2. On your PC, where information received from ESP8266 will be handled, you must have some app service that receives this information. That "service" to which I refer also is called Server (or WebServer), which is an application made in any programming language. Matlab is an incredible tool, but I don't know if it has the resources to become a WebServer;

  3. If Matlab can't become a webserver, then on your PC you should create one with the language you want, save to disk the information sent by ESP and then access them through Matlab;
    Another option is, considering that all equipment is in one place, use the ESP serial port, via RS232 communication, to send the information to the PC and retrieve it in Matlab and for the time being, forget the issues related to the "Client - Server" environment.

I hope I have helped and lessened the confusion.