1

I'm trying to control some RGB LEDs with ESP8266. I have built an HTML page to set the color and send the RGB color code to the ESP using HTTP get request. Everything works as expected. I just want to send an HTTP get request to the ESP and ask what the current color is. But after a couple of hours of searching and trying I don't know how to send data in response to get request, I just can send HTTP ok code. I'm using jquery to send get command as below

$.get("http://192.168.4.1/rgb(120,50,60)", function(data,status){   
    //I don't know how to send data fram ESP get it here.
});

this is the corresponding ESP response code written in Arduini IDE

WiFiClient client = server.available();
   if (client) {
  String req = client.readStringUntil('\r');
  client.print("HTTP/1.1 200 OK\r\n");
  client.print("Access-Control-Allow-Origin: *");
.
.
.

This is the HTTP header, How do I send some data(text)?

  • look at the webserver example. something like server.send(200, "text/plain", someStringOfContentHere); build up the string with the data you need. – dandavis Mar 30 '17 at 23:29
  • Thak you for your input. To use send method I have to include ESP8266WebServer.h and modify my code. I think there should an easy way to send some text (like HTML markup) after the header. – ahmadx87 Mar 31 '17 at 8:50
  • 1
    After the last header you need to send two newlines (\r\n\r\n) and then send your data. – Gerben Mar 31 '17 at 10:45
  • 1
    Thank you @Gerben! I spent hours searching but no success. It worked like a charm. – ahmadx87 Mar 31 '17 at 16:20
1

@Gerben answered my question in a comment all credit goes to him. I retype it here maybe it helps someone in the future.

I had to send two new line after setting up the header

//header
client.print("\r\n\r\n");
clinet.print("Hello, I'm comming from ESP!!");

And in jQuery I get it like this:

$.get("http://192.168.4.1/rgb(120,50,60)", function(data,status){   
    var receivedData=data;  //=Hello, I'm comming from ESP!!
});

It is simple but it may help some newbie like me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.