[NodeMCUv2] Komunikacja sieciowa

0

Cześć,
Jestem początkujący w programowaniu sieciowym uC, więc proszę o pomoc dlaczego mój kod nie zwraca informacji ze strony na moim serwerze w funkcji Test::ret_POST.

Z góry dziękuję.

PS: funkcja Test::req_t działa.

//main.cpp
void begin(){
  Serial.begin(9600);
  WiFi.begin(ssid, pass);
  while (WiFi.status() != WL_CONNECTED){
    delay(500);
    Serial.print('.');
  }

  Serial.println();
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
}

void setup()
{
  begin();

  Test::ClientInfo_T clientInfoT;
  clientInfoT.host = "192.168.0.10";
  clientInfoT.port = 80;
  clientInfoT.path = "/test2/index.php?test=10&lol=GG";
  clientInfoT.data = "larry=5&curly=6&moe=7";
  clientInfoT.agent = "Arduino/1.0";

  Test::ret_POST(clientInfoT);

//  Test::req_t();

  while (Test::client.connected() || Test::client.available())
  {
    if (Test::client.available())
    {
      String line = Test::client.readStringUntil('\n');
      Serial.println(line);
    }
  }
}

void loop()
{}
//HttpConnects.h
#include <Arduino.h>
#include <ESP8266WiFi.h>

namespace Test
{
  WiFiClient client;

  [[deprecated]]
  void req_t()
  {
    if (client.connect("192.168.0.10", 80))
    {
      String PostData = "larry=5&curly=6&moe=7";
      client.println("POST /test2/index.php?test=10&lol=GG HTTP/1.1");
      client.println("Host: 192.168.0.10");
      client.println("User-Agent: Arduino/1.0");
      client.println("Connection: close");
      client.println("Content-Type: application/x-www-form-urlencoded;");
      client.print("Content-Length: ");
      client.println(PostData.length());
      client.println();
      client.println(PostData);
    }
  }

  typedef struct
  {
    String host;
    uint16_t port;
    String agent;
    String data;
    String path;
  } ClientInfo_T;

  void ret_POST(const ClientInfo_T &infoT)
  {
    WiFiClient client;
    if (client.connect(infoT.host, infoT.port))
    {
      String req = "POST " + infoT.path + " HTTP/1.1\r\n" +
                   "Host: " + infoT.host + "\r\n" +
                   "User-Agent: " + infoT.agent + "\r\n" +
                   "Connecttion: close\r\n" +
                   "Content-Type: application/x-www-form-urlencoded;" +
                   "Content-Lenght: " + String(infoT.data.length()) +
                   "\r\n\r\n" +
                   infoT.data;
      client.println(req);
    }
  }
}

#endif //NODEMCUV2_THERM_HTTPCONNECTS_H

// /test2/index.php
<?php

echo $_SERVER['HTTP_USER_AGENT']."<br/>";
echo "<p>_GET</p>\r\n";
print_r($_GET);
echo "<p>_POST</p>\r\n";
print_r($_POST);
0

Masz literówki w nagłówkach HTTP: Connecttion oraz Content-Lenght. 400 to akurat Bad Request, czyli serwer odrzucił, bo żądanie jest błędne.

1 użytkowników online, w tym zalogowanych: 0, gości: 1