1

I used Postman to debug my PHP POST API, and it works (ignoring the json versos non-json input).

<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
//
// include database and object files
require_once '../config/database.php';
require_once '../objects/resdatalog.php';

// instantiate database and resdatalog object
$database = new Database();
$db = $database->getConnection();

// initialize object
$rdl = new Resdatalog($db);

$rdl->Location = $_GET['Location'];
$rdl->Temperature = $_GET['Temperature'];
$rdl->Humidity = $_GET['Humidity'];
$rdl->Pressure = $_GET['Pressure'];
$rdl->RecDate = $_GET['RecDate'];
$rdl->AmbientTemp = $_GET['AmbientTemp'];
$rdl->AmbientHum = $_GET['AmbientHum'];
$rdl->AmbientPressure = $_GET['AmbientPressure'];

$ret=$rdl->insert();

?>

But, the client needs to be an ESP8266 module monitoring the environmentals in various locations. I have been trying to make that work, but debugging has just baffled me. No matter what I display on the client side, it looks OK. But it seems never to actually get to the web page. I put code in the web page to "echo" confirmations, but nothing shows up in the client. What is the best way to debug this setup:

/*
BME280 I2C Test.ino

This code shows how to record data from the BME280 environmental sensor
using I2C interface. This file is an example file, part of the Arduino
BME280 library.

GNU General Public License

Written: Dec 30 2015.
Last Updated: Oct 07 2017.

Connecting the BME280 Sensor:
Sensor              ->  Board
-----------------------------
Vin (Voltage In)    ->  3.3V
Gnd (Ground)        ->  Gnd
SDA (Serial Data)   ->  A4 on Uno/Pro-Mini, 20 on Mega2560/Due, 2 Leonardo/Pro-Micro
SCK (Serial Clock)  ->  A5 on Uno/Pro-Mini, 21 on Mega2560/Due, 3 Leonardo/Pro-Micro

 */

#include <BME280I2C.h>
#include <Wire.h>
#include <ESP8266WiFi.h>
#include <Arduino.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <ArduinoJson.h>
#include <ezTime.h>

// ESP8266WiFiMulti WiFiMulti;

#define SERIAL_BAUD 115200

BME280I2C bme;    // Default : forced mode, standby time = 1000 ms
                  // Oversampling = pressure ×1, temperature ×1, humidity ×1, filter off,

Timezone Charlotte;   

String Location;
String Temperature;
String Humidity;
String Pressure;
String RecDate;
String AmbientTemp="0";
String AmbientHum="0";
String AmbientPressure="0";      

const char* ssid     = "mySSID";         // The SSID (name) of the Wi-Fi network you want to connect to
const char* password = "mySSIDPW";     // The password of the Wi-Fi network
//////////////////////////////////////////////////////////////////
void setup()
{
  //set up the serial output
  Serial.begin(SERIAL_BAUD);

  for (uint8_t t = 4; t > 0; t--) {
    Serial.printf("[SETUP] WAIT %d...\n", t);
    Serial.flush();
    delay(1000);
  }


  WiFi.begin(ssid, password);             // Connect to the network
  Serial.print("Connecting to ");
  Serial.print(ssid); Serial.println(" ...");

  int i = 0;
  while (WiFi.status() != WL_CONNECTED) { // Wait for the Wi-Fi to connect
    delay(1000);
    Serial.print(++i); Serial.print(' ');
  }

  Serial.println('\n');
  Serial.println("Connection established!");  
  Serial.print("IP address:\t");
  Serial.println(WiFi.localIP());         // Send the IP address of the ESP8266 to the computer

  waitForSync();


//  Begin the BME setup:

  Wire.begin(4,5);

  while(!bme.begin())
  {
    Serial.println("Could not find BME280 sensor!");
    delay(1000);
  }
  // bme.chipID(); // Deprecated. See chipModel().
  switch(bme.chipModel())
  {
     case BME280::ChipModel_BME280:
       Serial.println("Found BME280 sensor! Success.");
       break;
     case BME280::ChipModel_BMP280:
       Serial.println("Found BMP280 sensor! No Humidity available.");
       break;
     default:
       Serial.println("Found UNKNOWN sensor! Error!");
  }
}

//////////////////////////////////////////////////////////////////
void loop()
{
   printBME280Data(&Serial);
   delay(500);
}

//////////////////////////////////////////////////////////////////
void printBME280Data(Stream* serialclient)
{
   float temp(NAN), hum(NAN), pres(NAN);

   BME280::TempUnit tempUnit(BME280::TempUnit_Fahrenheit);
   BME280::PresUnit presUnit(BME280::PresUnit_inHg);
   bme.read(pres, temp, hum, tempUnit, presUnit);

  Charlotte.setLocation("America/New_York");  
  String localdatetime = Charlotte.dateTime("Y-m-d H:i:s");

   serialclient->print("\tTemp: ");
   serialclient->print(temp);
   serialclient->print("°"+ String(tempUnit == BME280::TempUnit_Celsius ? 'C' :'F'));
   serialclient->print("\t\tHumidity: ");
   serialclient->print(hum);
   serialclient->print("% RH");
   serialclient->print("\t\tPressure: ");
   serialclient->print(pres);
   serialclient->print(" Inches Hg");
   serialclient->println(" Date/Time: "+ localdatetime);
// Now, I'll try to build and post an HTTP command to my cloud API
//
  Location="TiVo Cabinet";
  Temperature=String(temp);
  Humidity=String(hum);
  Pressure=String(pres);
  RecDate=localdatetime;
  AmbientTemp=String(AmbientTemp);
  AmbientHum=String(AmbientHum);
  AmbientPressure=String(AmbientPressure);

    String content = 
  "{\"Location\": \"" + String(Location) + "\"" +
  " , \"Temperature\" : \"" + String(Temperature) + "\"" +
  " , \"Humidity\" : \"" + String(Humidity) + "\"" +
  " , \"Pressure\" : \"" + String(Pressure) + "\"" +
  " , \"RecDate\" : \"" +  String(RecDate) + "\"" +
  " , \"AmbientTemp\" : \"" + String(AmbientTemp) + "\"" +
  " , \"AmbientHum\" : \"" + String(AmbientHum) + "\"" +
  " , \"AmbientPressure\" : \"" + String(AmbientPressure) +  "\"" +
  "}";

  serialclient->println("Content: " + content);


//  Now, we're gonna try to send this line to the server....

  WiFiClient client;
  if (!client.connect("telemetry.shafferassoc.com", 80)) {
    Serial.println("WiFiClient connection failed");
    delay(5000);
  }  else {Serial.println("WiFiClient connection OK");
    }
  client.println("Host: telemetry.shafferassoc.com:80\r\n");
  client.println("POST TelemetryWebSite/API/Resdatalog/Insert.php HTTP/1.1");
  client.println("Accept: */*");
  client.println("Content-Length: " + String(content.length()));
  client.println("Content-Type: application/json");
//  client.println();

  client.println(content);
  Serial.println("Length: " +String(content.length()));
  Serial.println(content);
    Serial.println("receiving from remote server");
  // not testing 'client.connected()' since we do not need to send data here
  while (client.available()) {
    char ch = static_cast<char>(client.read());
    Serial.print(ch);
  }


   delay(5000);
}

Here is a snippet of the Serial output:

11:06:49.892 -> WiFiClient connection OK
11:06:49.892 -> Length: 204
11:06:49.892 -> {"Location": "TiVo Cabinet" , "Temperature" : "73.27" , "Humidity" : "0.00" , "Pressure" : "29.57" , "RecDate" : "2020-04-28 11:06:50" , "AmbientTemp" : "0" , "AmbientHum" : "0" , "AmbientPressure" : "0"}
11:06:49.933 -> receiving from remote server`
8
  • Try to change the Serial.println("Host: telemetry.shafferassoc.com:80\r\n") to Serial.println("Host: telemetry.shafferassoc.com"). also why do you comment out the client.println()?, you will need that line between the HTTP headers and the HTTP content. Commented Apr 29, 2020 at 11:15
  • hcheung, thank you for your help. I made the changes you suggested (I assume you meant "client.println..." as opposed to "Serial.println...". No help. How do I see what is being sent to the server, and how do I see the server's responses? That's my frustration! Commented Apr 29, 2020 at 13:11
  • Check your server log to see what's going on or add print_r($_REQUEST); to your php code to see what you received. BTW, make sure that TelemetryWebSite/API/Resdatalog/Insert.php is the correct endpoint, I run postman with this endpoint and I got 404 (page not found). Commented Apr 30, 2020 at 4:01
  • Using Postman, I can post an insert to: telemetry.shafferassoc.com/TelemetryWebSite/api/resdatalog/insert.php?Location=Desktop&Temperature=36.9&Humidity=48.5&Pressure=1006.5&RecDate=2020-05-30 11:05:25&AmbientPressure=1005.8&AmbientHum=83&AmbientTemp=71.5 Clearly, I'm not using JSON, but I just need to get to the code first, then deal with the params.... As you said, I'm likely not getting to the site at all. At least that's my conclusion. I don't know how to read server log on Ionos system. I'll go there now and see if I can figure that out. Commented Apr 30, 2020 at 16:04
  • I also added the print_r statement to insert.php. Using Postman, it printed the payload array perfectly. Not so much on the esp8266 program. Commented Apr 30, 2020 at 16:25

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.