Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By GastonMelo
#91330 Hi, thanks for your reply and help on this thread.
I'm trying to use the link you send me and this is what I did:
in the esp8266 side:
Code: Select all  client.println("<meta http-equiv='content-type' content='gzip; charset=UTF-8'>");
  client.println(index_html_gz[322]);


where index_html_gz is the html gziped.

In the chrome view I got this:

[img]
file:///tmp/Spectacle.YYACqS/Screenshot_20210505_200318.png
[/img]

Thanks
Gastón
User avatar
By btidey
#91344 Maybe share your complete code as it is difficult to diagnose from just a snippet.

For example I am puzzled by why you are using client.println methods. In a server I would expect to see something like

server.send(code, content_type, content, content_length);
User avatar
By GastonMelo
#91345 this is the code:

Code: Select all#include <FS.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <DNSServer.h>
#include <WiFiManager.h>
#include <DoubleResetDetector.h>
#include "ArduinoJson.h"
#include "index_html.h"
#define DRD_TIMEOUT 10

// RTC Memory Address for the DoubleResetDetector to use
#define DRD_ADDRESS 0

DoubleResetDetector drd(DRD_TIMEOUT, DRD_ADDRESS);
//#include <EEPROM.h>
char WiFiRelay[21] = "xxxxxxxxxxxxxxxxxxx";//max 32 characters
WiFiServer server(80);

String datos = "bateria:XXX,corriente:XXX,temp_bat:XXX,temp_mosf:XXX";
String header = "HTTP/1.1 200 OK \r\n value=off\r\n\r\n";
String html_1 = "<!DOCTYPE html><html><head><link href='data:image/x-icon;base64,AAABAAEAEBAQAAEABAAoAQAAFgAAACgAAAAQAAAAIAAAAAEABAAAAAAAgAAAAAAAAAAAAAAAEAAAAAAAAAAAAAAA/4QAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAERABAAEAEBAQABAQEBABAAEAEQARABAQABAQABAAAQARAAEQARAAAAAAAAAAAAAAAAAAAAAAEREAEQAQAAAQAAEAEBAAABAAAAAQEAAAERAAEQAREAAQAAEAABABABAAAQAQEAEAEREAEQAREAAAAAAAAAAAD//wAA2N0AAKuqAADdmQAArrsAANnMAAD//wAA//8AAIZvAAC9rwAAv68AAI5jAAC97QAAva0AAIZjAAD//wAA' rel='icon' type='image/x-icon' /><meta name='referer' http-equiv='refresh' content='200' content='origin' name='viewport' content='width=device-width, initial-scale=1.0, maximun-scale= 1.0, user-scalable=0'><meta charset='utf-8'><style>body {background-color: white;}.button {background-color: #4CAF50; /* Green */border: none;color: white;padding: 20px;text-align: center;text-decoration: none;display: inline-block;font-size: 16px;margin: 4px 2px;cursor: pointer;}h {display: inline-block;margin: 0;}.button1 {border-radius: 12px;}.button2 {border-radius: 12px;}.button3 {border-radius: 12px;}.button4 {border-radius: 12px;}</style></head><body><form method = 'get' action = 'evento'>";
String html_2 = "<button1 class='button button1' name = 'battery'>Battery</button1><h>0V</h><br>";
String html_3 = "<button2 class='button button2' name = 'current'>Current</button2><h>0A</h><br>";
String html_4 = "<button3 class='button button3' name = 'temp_bat'>Batt_Temp</button3><h>0°C</h><br>";
String html_5 = "<button4 class='button button4' name = 'temp_mosf'>Mosf_Temp</button4><h>0°C</h><br>";
String html_6 = "<button class='button button4' name = 'Iniciar' type = 'submit' value='0' >Iniciar</button><br>";
String html_7 = "</form></body></html>";
String request = "";
String name_device = "";
bool shouldSaveConfig = false;
bool on_btn_flag = false;


void saveConfigCallback () {
  Serial.println("Should save config");
  shouldSaveConfig = true;
}
void configModeCallback (WiFiManager *myWiFiManager) {
    Serial.println("Entered config mode");
  Serial.println(WiFi.softAPIP());
  drd.stop();
}
void setup() {
  Serial.begin(115200);
  Serial.println("WifiRelay activado");
  //Serial.println();
  //EEPROM.begin(512);
  delay(10);
  //Serial.println("mounting FS...");
  //comienza lectura memoria
  if (SPIFFS.begin()) {
    if (SPIFFS.exists("/config.json")) {
      //file exists, reading and loading
      File configFile = SPIFFS.open("/config.json", "r");
      if (configFile) {
        size_t size = configFile.size();
        // Allocate a buffer to store contents of the file.
        std::unique_ptr<char[]> buf(new char[size]);
        configFile.readBytes(buf.get(), size);
        DynamicJsonBuffer jsonBuffer;
        JsonObject& json = jsonBuffer.parseObject(buf.get());
        json.printTo(Serial);
        if (json.success()) {
          Serial.println("\nparsed json");
          //recupera variables almacenadas
          strcpy(WiFiRelay, json["WiFiRelay"]);
          Serial.println("nombre dispositivo");
          Serial.println(WiFiRelay);
        } else {
          //operacion
        }
        configFile.close();
      }
    }
  } else {
    //operacion
  }
  //finaliza operacion lectura
  pinMode(3,OUTPUT);
  digitalWrite(3,0);
  // Connect to WiFi network
  WiFiManager wifiManager;
  wifiManager.setAPCallback(configModeCallback);
  //set config
  wifiManager.setSaveConfigCallback(saveConfigCallback);
    //agregar custom request to wifimanager
  WiFiManagerParameter custom_WiFiRelay("WiFiRelay", "WiFiRelayName", WiFiRelay, 21);
  //agregar nuevo parametro
  wifiManager.addParameter(&custom_WiFiRelay);
  //***************RESET WIFIMANAGER***********************
  //wifiManager.resetSettings();
  //*******************************************************
  wifiManager.autoConnect("WiFiRelay");//coment serial.begin()
  WiFi.setSleepMode(WIFI_NONE_SLEEP);
  if (!MDNS.begin("esp01")) {}
  server.begin();
  strcpy(WiFiRelay, custom_WiFiRelay.getValue());
  if (drd.detectDoubleReset()) {
    WiFiManager wifiManager;
    wifiManager.startConfigPortal("WiFiRelay");
  }
  //save the custom parameters to FS
  if (shouldSaveConfig) {
    Serial.println("saving config");
    DynamicJsonBuffer jsonBuffer;
    JsonObject& json = jsonBuffer.createObject();
    json["WiFiRelay"] = WiFiRelay;
    File configFile = SPIFFS.open("/config.json", "w");
    if (!configFile) {
      Serial.println("failed to open config file for writing");
    }
    json.printTo(Serial);
    json.printTo(configFile);
    configFile.close();
  }
  drd.stop();
}

void loop() {
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
  unsigned long timeout = millis() + 500;
  Serial.println("new client");
  while (!client.available() && millis() < timeout) {
    delay(1);
  }
  if (millis() > timeout) {
    Serial.println("timeout");
    client.flush();
    client.stop();
    return;
  }
  // Read the first line of the request
  String req = client.readStringUntil('\r');
  Serial.println(req);
  client.flush();
  //Setea ON BUTTON
  //bandera request
  int val_bool;
  Serial.println("seleccion");
  int index = req.indexOf('?') > 0;
  String comando = req.substring(index+1);
  Serial.println(comando);

  Serial.println("fin seleccion\r\n");
  client.flush();
  //html_2 = name_device;
  //client.println("HTTP/1.1 200 OK");
  //client.println("Content-Type: text/html");
  //client.println("Content-Encoding: gzip; charset=UTF-8'>");
  //client.println("<meta http-equiv='content-type' content='gzip; charset=UTF-8'>");
  //client.println(index_html_gz[322]);
 
  //client.print(header);
  header = "HTTP/1.1 200 OK\r\n" + datos + "\r\n";
  client.println(header);
  client.print(html_1);
  client.print(html_2);
  client.print(html_3);
  client.print(html_4);
  client.print(html_5);
  client.print(html_6);
  client.print(html_7);
  delay(1);
}


I modify the esp8266 WiFiHTTPSServer - Simple SSL server example.
I send the html page as shown in the code.
Regards
Gastón