Help with an ESP8266 handling http requests

Hi! I'm very new in this area and I'm struggling with an ESP8266 project, I need to connect to a Django website that I'm hosting and I just need the microcontroller to use my local WiFi to connect to it depending on the current route. I found a very similar example of what I'm trying to achieve, I just need it to work like that but connect to my website instead of creating the website in the Arduino code.

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>

/*Put your SSID & Password*/
const char* ssid = "my wifi";
const char* password = "my password";
//String url = "my website";

ESP8266WebServer server(80);

uint8_t led = LED_BUILTIN;
bool ledStatus = LOW;

void setup() {
  Serial.begin(115200);
  delay(100);
  pinMode(led, OUTPUT);

  Serial.println("Connecting to ");
  Serial.println(ssid);

  //connect to your local wi-fi network
  WiFi.begin(ssid, password);

  //check wi-fi is connected to wi-fi network
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected..!");
  Serial.print("Got IP: ");
  Serial.println(WiFi.localIP());

  server.on("/", handle_OnConnect);
  server.on("/ledOn", handle_ledOn);
  server.on("/ledOff", handle_ledOff);
  server.onNotFound(handle_NotFound);

  server.begin();
  Serial.println("HTTP server started");
}
void loop() {
  server.handleClient();
  if (ledStatus) {
    digitalWrite(led, HIGH);
  } else {
    digitalWrite(led, LOW);
  }
}

void handle_OnConnect() {
  ledStatus = LOW;
  Serial.println("GPIO7 Status: OFF | GPIO6 Status: OFF");
  server.send(200, "text/html", SendHTML(ledStatus));
}

void handle_ledOn() {
  ledStatus = HIGH;
  Serial.println("GPIO7 Status: ON");
  server.send(200, "text/html", SendHTML(true));
}

void handle_ledOff() {
  ledStatus = LOW;
  Serial.println("GPIO7 Status: OFF");
  server.send(200, "text/html", SendHTML(false));
}

void handle_NotFound() {
  server.send(404, "text/plain", "Not found");
}

String SendHTML(uint8_t letStat) {
  String ptr = "<!DOCTYPE html> <html>\n";
  ptr += "<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
  ptr += "<title>LED Control</title>\n";
  ptr += "<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n";
  ptr += "body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;} h3 {color: #444444;margin-bottom: 50px;}\n";
  ptr += ".button {display: block;width: 80px;background-color: #1abc9c;border: none;color: white;padding: 13px 30px;text-decoration: none;font-size: 25px;margin: 0px auto 35px;cursor: pointer;border-radius: 4px;}\n";
  ptr += ".button-on {background-color: #1abc9c;}\n";
  ptr += ".button-on:active {background-color: #16a085;}\n";
  ptr += ".button-off {background-color: #34495e;}\n";
  ptr += ".button-off:active {background-color: #2c3e50;}\n";
  ptr += "p {font-size: 14px;color: #888;margin-bottom: 10px;}\n";
  ptr += "</style>\n";
  ptr += "</head>\n";
  ptr += "<body>\n";
  ptr += "<h1>ESP8266 Web Server</h1>\n";
  ptr += "<h3>Using Station(STA) Mode</h3>\n";

  if (letStat) {
    ptr += "<p>LED1 Status: ON</p><a class=\"button button-off\" href=\"/ledOff\">OFF</a>\n";
  } else {
    ptr += "<p>LED1 Status: OFF</p><a class=\"button button-on\" href=\"/ledOn\">ON</a>\n";
  }

  ptr += "</body>\n";
  ptr += "</html>\n";
  return ptr;
}

so you want to be a client of a web server

the code you found makes the arduino the web server, not the client...


➜ you'll see here an example of both codes for server and client

look at the client side.

1 Like

As @J-M-L says, this example is not similar to what you want. It is the opposite of what you want. This example makes your arduino into a server. You want the Arduino to be the client and your website to be the server.

Thanks for the replies and sorry for the confusión, and pretty new to Arduino and web backend, I was looking at the example provided by @J-M-L in the client side, but does anybody have an example of how I can do something like turn on the buit-in LED based on the current route of the website?

Do you mean that you want the arduino to connect to a web service using HTTP and this service returns ON or OFF and you use this answer to set the led status?

Explain what http request it is you want to send and what’s the expected answer’s format

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.