错误HTTP / 1.1 400使用nodemcu在mysql中输入数据的错误请求

问题描述 投票:0回答:1

im试图用php(laravel)连接mysql和nodemcu,但是当我运行arduino脚本时,它带有我不知道的错误。有人可以帮我吗?

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>
#include<DHT.h> DHT dht(14, DHT11);

//#define TRIGGER_PIN  5  //D1
//#define ECHO_PIN     4  //D2

const char* ssid     = "PS-E3";
const char* password = "Pesona30";

const char* host = "192.168.1.19";

WiFiClient client;
const int httpPort = 80;
String url; long duration, distance;
float kelembaban, suhu;
unsigned long timeout;
void setup() {
   Serial.begin(9600);
   dht.begin();
   delay(10);
   //  pinMode(TRIGGER_PIN, OUTPUT);
   //  pinMode(ECHO_PIN, INPUT);
   // We start by connecting to a WiFi network
   Serial.println();
   Serial.println();
   Serial.print("Connecting to ");
   Serial.println(ssid);
   WiFi.mode(WIFI_STA);
   WiFi.begin(ssid, password);

   while (WiFi.status() != WL_CONNECTED) {
     delay(500);
     Serial.print(".");   
   }

   Serial.println("");
   Serial.println("WiFi connected");
   Serial.print("IP address: ");
   Serial.println(WiFi.localIP());
}

void baca_suhu(){
   kelembaban = dht.readHumidity();
   suhu = dht.readTemperature();
   Serial.print("kelembaban : ");
   Serial.println(kelembaban);
   Serial.print("Suhu : ");
   Serial.println(suhu); 
}

void loop() {
   //  Serial.print("baca jarak ");
   //  baca_jarak();   baca_suhu();
   Serial.print("connecting to ");
   Serial.println(host);

   if (!client.connect(host, httpPort)) {
     Serial.println("connection failed");
     //return;  
   } 

   // We now create a URI for the request
   url = "localhost:8000/cuaca/tampil/save/";
   url += suhu;
   url += "/";
   url += kelembaban;
   Serial.print("Requesting URL: ");
   Serial.println(url);

  // This will send the request to the server
  client.print(String("POST ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" + 
               "Connection: close\r\n\r\n");
  timeout = millis();
  while (client.available() == 0) {
      if (millis() - timeout > 5000) {
         Serial.println(">>> Client Timeout !");
         client.stop();
         return;
      }
  }

  // Read all the lines of the reply from server and print them to Serial   
  while(client.available()){
      String line = client.readStringUntil('\r');
      Serial.print(line);   
  }

  Serial.println();
  Serial.println("closing connection");
  Serial.println();
  delay(5000);
}

在该主机IP上已经与我的计算机的IP相匹配。和http端口已经与xampp相同,我不明白为什么会出现错误无论如何,这是我在laravel中的脚本

<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Http\Controllers;

class SimpanController extends Controller
{
    public function index(){
        $cuaca = DB::table('tb_cuaca')->orderBy('Kode','ASC')->paginate(10);

        //passing data ke view
        return view('tampilcuaca',['cuaca' => $cuaca]);
    } 

public function save($suhu, $kelembaban)
{
    $date = date("Y-m-d");
    // insert data ke table sekolah
    DB::table('tb_cuaca')->insert([
        'Suhu' => $suhu,
        'Kelembaban' => $kelembaban,
        'Tanggal' => $date
    ]);
    // alihkan halaman ke halaman tampilsekolah
    echo "berhasil";
    return redirect('/tampildata');
}

public function tampildata(){

}

// public function store(Request $request)
// {
//  // insert data ke table sekolah
//  DB::table('tb_prestasi')->insert([
//      'Nama_Sekolah' => $request->nama,
//      'Prestasi' => $request->prestasi
//  ]);
//  // alihkan halaman ke halaman tampilsekolah
//  return redirect('/prestasi');

// }

}

错误是这样的

Connecting to PS-E3
scandone
.....scandone
state: 0 -> 2 (b0)
.state: 2 -> 3 (0)
state: 3 -> 5 (10)
add 0
aid 7
cnt 

    connected with PS-E3, channel 5
    dhcp client start...
    ....ip:192.168.1.5,mask:255.255.255.0,gw:192.168.1.1
    .
    WiFi connected
    IP address: 192.168.1.5
    kelembaban : 79.00
    Suhu : 29.10
    connecting to 192.168.1.19
    Requesting URL: localhost:8000/cuaca/tampil/save/29.10/79.00
    HTTP/1.1 400 Bad Request
    Date: Thu, 16 Jan 2020 05:41:33 GMT
    Server: Apache/2.4.41 (Win64) OpenSSL/1.1.1c PHP/7.3.9
    Vary: accept-language,accept-charset
    Accept-Ranges: bytes
    Connection: close
    Content-Type: text/html; charset=utf-8
    Content-Language: en

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
    <head>
    <title>Bad request!</title>
    <link rev="made" href="mailto:postmaster@localhost" />
    <style type="text/css"><!--/*--><![CDATA[/*><!--*/ 
        body { color: #000000; background-color: #FFFFFF; }
        a:link { color: #0000CC; }
        p, address {margin-left: 3em;}
        span {font-size: smaller;}
    /*]]>*/--></style>
    </head>

    <body>
    <h1>Bad request!</h1>
    <p>


        Your browser (or proxy) sent a request that
        this server could not understand.

    </p>
    <p>
    If you think this is a server error, please contact
    the <a href="mailto:postmaster@localhost">webmaster</a>.

    </p>

    <h2>Error 400</h2>
    <address>
      <a href="/">192.168.1.19</a><br />
      <span>Apache/2.4.41 (Win64) OpenSSL/1.1.1c PHP/7.3.9</span>
    </address>
    </body>
    </html>


    closing connection

帮助我

mysql laravel arduino esp8266 nodemcu
1个回答
0
投票

我已经知道问题出在哪里。您只需要在主机变量中声明端口8000。它的请求很糟糕,因为我在:8000上使用了url(因为这样可以在本地主机上使用laravel)。它正在工作,数据可以记录在mysql中。

© www.soinside.com 2019 - 2024. All rights reserved.