我的python代码应该发送“消息”

import requests
r = requests.post('http://192.168.0.100:7777',data="Hello")
print(r.text)


我的ESP32代码,我认为问题是由于网址错误引起的,因为我不确定在那里应该使用哪个IP。是运行python代码的计算机的IP吗?还是ESP32的IP?还是我完全错了?

#include <WiFi.h>
#include <HTTPClient.h>
#include <WebServer.h>

WebServer server(7777);
HTTPClient http;

const char* ssid = "SSID";
const char* password =  "PASSWORD";

void handleRoot() {
  server.send(200, "text/plain", "Hello World!");
}

void handleNotFound() {
  server.send(404, "text/plain", "Hello World!");
}

void setup() {
  Serial.begin(115200);

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    Serial.println("Connecting to WiFi..");
  }
  Serial.println(WiFi.localIP());

  server.on("/", handleRoot);
  server.onNotFound(handleNotFound);
  server.begin();
  http.begin("http://192.168.0.100/");
}

void loop() {
 server.handleClient();
 int httpCode = http.GET();
 String payload = http.getString();
 Serial.println(httpCode);
 Serial.println(payload);
}


编辑:现在,ESP应该充当服务器

最佳答案

发送GET / POST请求时,必须放置目标IP,在这种情况下,应输入ESP32的IP。

首先要注意的是,默认情况下,我的server.begin()正在监听192.168.4.1:80作为访问点。

现在,要创建一个包含json数据的GET和POST请求的异步服务器,您可以具有以下功能:

文件http_server.h:

#include "WiFi.h"
#include "ESPAsyncWebServer.h"
#include "ArduinoJson.h"

#ifndef _HTTP_SERVER_
#define _HTTP_SERVER_

void setup_http_server();

#endif


文件http_server.cpp的示例:

#include "http_server.h"

AsyncWebServer server(80);  // start server listening on port 80
void setup_http_server(){

    // Example of function that holds and HTTP_GET request on 192.168.4.1:80/get_data
    server.on("/get_data", HTTP_GET, [](AsyncWebServerRequest *request){
        // Code that holds the request
        Serial.println("Get received"); // Just for debug
        request->send(HTTP_200_code, "text/plain", "Here I am");
    });

    // Example of function that holds and HTTP_POST request on 192.168.4.1:80/set_data
    server.on("/set_data",
        HTTP_POST,
        [](AsyncWebServerRequest * request){},
        NULL,
        [](AsyncWebServerRequest * request, uint8_t *data, size_t len,
        size_t index, size_t total) {
            // Here goes the code to manage the post request
            // The data is received on 'data' variable
            // Parse data
            Serial.printlnt("POST RECEIVED"); // Just for debug
            StaticJsonBuffer<50> JSONBuffer; // create a buffer that fits for you
            JsonObject& parsed = JSONBuffer.parseObject(data); //Parse message
            uint8_t received_data = parsed["number"]; //Get data
            request->send(HTTP_200_code, "text/plain", "Some message");
    });

    server.begin();  // starts asyncrhonus controller
    Serial.println(WiFi.softAPIP());  // you can print you IP yo be sure that it is 192.168.4.1
}


最后,如果您想通过移动设备/计算机直接连接到ESP32,则需要将其wifi连接配置为ACCESS_POINT。因此,您的setup()中的loop()main.ino可能看起来像:

文件main.ino:

#include "http_server.h"

void setup(){
     Serial.begin(9600);  // setup serial communication
     // Set WiFi to station(STA) and access point (AP) mode simultaneously
     WiFi.mode(WIFI_AP_STA);
     delay(100); // Recommended delay
     WiFi.softAP("choose_some_ssid", "choose_some_password");

     // Remember to configure the previous server
     setup_http_server();  // starts the asyncronus https server*/
}

void loop(){
     delay(1000);
}


就这样。要检查一切是否正常,您可以使用密码choose_some_ssid将手机连接到wifi chose_some_password,打开浏览器并转到192.168.4.1/get_data,您应该得到“ Here I am”作为答复。

就像您在问题上说的那样,如果要发送python帖子,则可以执行以下操作:

import requests
r = requests.post('http://192.168.4.1:80/set_data', data="{'number':55}") // remember that you get the keyword 'number' in the server side
print(r.text) // should print "Some message"


可以在https://techtutorialsx.com/2018/10/12/esp32-http-web-server-handling-body-data/上找到更多信息。
希望能帮助到你!

10-04 10:55