I have a django server running on localhost (127.0.0.1:8000), and I have trying to post sensor data to this url using ESP8266 but I can't connect with localhost keep getting connection failed message this is the code iam using in arduino
#include <ESP8266WiFi.h> //#include <WiFiClient.h> //#include <ESP8266WebServer.h> const char* ssid = "ssid"; const char* password = "password"; const char* host = "127.0.0.1"; //String page = ""; double data; void setup(void){ pinMode(A0, INPUT); delay(1000); Serial.begin(115200); WiFi.begin(ssid, password); Serial.println(""); 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 loop(void){ WiFiClient client; const int httpPort = 8000; if (!client.connect(host, httpPort)) { Serial.println("connection failed"); delay(1000); return; } data = analogRead(A0); Serial.print("Requesting POST: "); // Send request to the server: client.println("POST / HTTP/1.1"); client.println("Host: server_name"); client.println("Accept: */*"); client.println("Content-Type: application/x-www-form-urlencoded"); client.print("Content-Length: "); client.println(String(data).length()); client.println(); client.print(data); delay(500); if (client.connected()){ client.stop(); } Serial.println(); Serial.println("closing connection"); delay(5000); } https://stackoverflow.com/questions/65746105/esp8266-with-localhost January 16, 2021 at 11:06AM
没有评论:
发表评论