1

我使用 Postman 调试我的 PHP POST API,它可以工作(忽略 json 与非 json 输入)。

<?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();

?>

但是,客户端需要是一个监控不同位置环境的 ESP8266 模块。我一直在努力完成这项工作,但调试让我感到困惑。无论我在客户端显示什么,它看起来都不错。但它似乎从未真正进入网页。我将代码放在网页中以“回显”确认,但客户端中没有显示任何内容。调试此设置的最佳方法是什么:

/*
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);
}

这是串行输出的片段:

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`
4

0 回答 0