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

Moderator: igrr

User avatar
By Antimatter
#62315 I started from a working example by GengusKahn and modified it to use the ESP8266webserver library.
This subsequently broke it.
It displays the webpage but the 8266 server does not seem to be getting what it desires from the webpage to service the AJAX request.
The server.on() command does not call the specified function like it should to update the webpage parameters so this is where it is breaking down.
I have been floundering with this and unable to correct the issue and at this point I am not sure which side the issue is on.
Any help is greatly appreciated.
Thanks

Update
The JS appears to be sending the request but the
server.on("/ajax_switch", HTTP_POST, [](){ getTemperature(client);});
does not call the function getTemperature like it should upon getting the /ajax_switch from the JS on the client. Is there a way to print out what the server.on command is getting from the client?

It would be great if someone could give some insight on resolving this.

Code: Select all#include <ESP8266WiFi.h>
// #include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <Wire.h>
#include <SFE_BMP180.h>
#include <credentials.h>
extern "C"
{
  #include "user_interface.h"         // sdk library
}

#define SDA_PIN  0              // GPIO for i2c SDA
#define SCL_PIN  2              // GPIO for i2c SCL

SFE_BMP180 pressure;
double baseline;                // baseline pressure

// wifi setup
const char* ssid     = mySSID;
const char* password = myPASSWORD;
ESP8266WebServer server(80);
WiFiClient client;
String HTTP_req;


void setup()
{
  Serial.begin(115200);
  Wire.begin(SDA_PIN, SCL_PIN);           // set pins in library for i2c
  Serial.println("");
  // Connect to WiFi network
  WiFi.mode(WIFI_STA);                    // set to station mode so it only can connect to router
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
 
 // Once connected display wifi status to serial port     
  Serial.println("");     
  Serial.println("AJAX Server Example");     
  Serial.print("Connected to "); Serial.println(ssid);
  Serial.print("WiFi Station Hostname: "); Serial.println(wifi_station_get_hostname()); 
  Serial.print("IP Address: "); Serial.println(WiFi.localIP());
  Serial.print("MAC address: "); Serial.println(WiFi.macAddress());
  Serial.print("RSSI: "); Serial.print(wifi_station_get_rssi()); Serial.println("dB");     // signal strength

  // setup client events
  server.on("/", HTTP_GET, bmp);
  server.on("/ajax_switch", HTTP_POST, [](){ getTemperature(client);});
 
  // Start the server
  server.begin();
  Serial.println("Server started");

  if (pressure.begin())
  {
  }
  else
  {
  // Oops, something went wrong, this is usually a connection problem,
  // see the comments at the top of this sketch for the proper connections.

    Serial.println("BMP180 init fail (disconnected?)\n\n");
    while(1); // Pause forever.
  }
   baseline = getPressure();
}                                     // setup ends


void loop()
{
   // listen for incoming clients
  server.handleClient();   
}                           // void loop ends


double getPressure()
{
  char status;
  double T,P,p0,a;

  // You must first get a temperature measurement to perform a pressure reading.
 
  // Start a temperature measurement:
  // If request is successful, the number of ms to wait is returned.
  // If request is unsuccessful, 0 is returned.

  status = pressure.startTemperature();
  if (status != 0)
  {
    // Wait for the measurement to complete:

    delay(status);

    // Retrieve the completed temperature measurement:
    // Note that the measurement is stored in the variable T.
    // Use '&T' to provide the address of T to the function.
    // Function returns 1 if successful, 0 if failure.

    status = pressure.getTemperature(T);
    if (status != 0)
    {
      // Start a pressure measurement:
      // The parameter is the oversampling setting, from 0 to 3 (highest res, longest wait).
      // If request is successful, the number of ms to wait is returned.
      // If request is unsuccessful, 0 is returned.

      status = pressure.startPressure(3);
      if (status != 0)
      {
        // Wait for the measurement to complete:
        delay(status);

        // Retrieve the completed pressure measurement:
        // Note that the measurement is stored in the variable P.
        // Use '&P' to provide the address of P.
        // Note also that the function requires the previous temperature measurement (T).
        // (If temperature is stable, you can do one temperature measurement for a number of pressure measurements.)
        // Function returns 1 if successful, 0 if failure.

        status = pressure.getPressure(P,T);
        if (status != 0)
        {
          return(P);
        }
        else Serial.println("error retrieving pressure measurement\n");
      }
      else Serial.println("error starting pressure measurement\n");
    }
    else Serial.println("error retrieving temperature measurement\n");
  }
  else Serial.println("error starting temperature measurement\n");
}
//--------------------------------------------------------------------------

void getTemperature(WiFiClient cl)
{
  Serial.print("HELLO");
    char status;
    double T,a,P;
    P = getPressure();
    a = pressure.altitude(P,baseline);
    pressure.getTemperature(T);
      status = pressure.startTemperature();
  if (status != 0)
  {
    // Wait for the measurement to complete:
    delay(status);

    // Retrieve the completed temperature measurement:
    // Note that the measurement is stored in the variable T.
    // Function returns 1 if successful, 0 if failure.
  }
  status = pressure.getTemperature(T); 
  cl.print("Temperature: ");
  cl.print(T);
  cl.print(" C");
  cl.print("<BR>Absolute Pressure: ");
  cl.print(P,2);
  cl.print(" mb");
  cl.print("   -  ");
  cl.print(P*0.0295333727,2);
  cl.print(" inHg");   
  cl.print("<BR>Relative Altitude: ");
  cl.print(a,0);
  cl.print(" meters above Start Position ( ");
  cl.print(a*3.28084,0);
  cl.print(" Feet for OLD Git's)");   
}
//--------------------------------------------------------------------------

void bmp()
{
  WiFiClient client = server.client();
                        client.println("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n");
                        client.println("<!DOCTYPE html>");
                        client.println("<html>");
                        client.println("<head>");
                        client.println("<title>ESP BMP180 web display</title>");
                        client.println("<script>");
                        client.println("function getTemperature() {");
                        client.println("nocache = \"&nocache=\"+ Math.random() * 1000000;");
                        client.println("var request = new XMLHttpRequest();");
                        client.println("request.open(\"GET\", \"ajax_switch\" + nocache, true);");
                        client.println("request.onreadystatechange = function() {");
                        client.println("if (this.readyState == 4 && this.status == 200){");
                        client.println("document.getElementById(\"switch_txt\").innerHTML = this.responseText;");
                        client.println("}}");                 
                        client.println("request.send(null);");
                        client.println("setTimeout('getTemperature()', 1000);");
                        client.println("}");
                        client.println("</script>");
                        client.println("</head>");
                        client.println("<body onload=\"getTemperature()\">");
                        client.println("<h1>ESP8266 BMP180 web server</h1>");
                        client.println("<font color=\"#6a5acd\"><body bgcolor=\"#a0dFfe\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=yes\">\n</div>\n<div style=\"clear:both;\"></div><p>");                                               
                        client.println("<p id=\"switch_txt\">BMPSTATUS/p>");
                        client.println("<p>Example Display of BMP data</p>");
                        client.println("<p><a href=\"http://www.esp8266.com\">ESP8266 Support Forum</a></p>");
                        client.println("</body>");
                        client.println("</html>");

  delay(1);
  client.stop();
}