I'm trying to connect to a TCP/IP server and send a JSON {"id":0,"jsonrpc":"2.0","method":"miner_getstat1"} to get a JSON back. I tried using GET but I would have an HTML in response. So I tried POST but I can’t get it to work.
Any help would be appreciated
#include <ESP8266WiFi.h>
const char* ssid = "VIDEO";
const char* password = "3Y4U";
const char* host = "192.168.0.58";
void setup() {
Serial.begin(115200);
delay(10);
// Connecting to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
int value = 0;
void loop() {
delay(5000);
++value;
Serial.print("connecting to ");
Serial.println(host);
// Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 3333;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
// We now create a URI for the request
// String url = "/";
// url += streamId;
// url += "?private_key=";
// url += privateKey;
// url += "&value=";
// url += value;
Serial.print("Requesting URL: ");
// Serial.println(url);
// Send the request to the server
// The server uses TCP/IP (not HTTP) for statistics, thats why I am tryng to use POST instead of GET
// I want to send {"id":0,"jsonrpc":"2.0","method":"miner_getstat1"}
client.print(String("POST ") + "{"id":0,"jsonrpc":"2.0","method":"miner_getstat1"}" + " \n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
unsigned long 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");
}
test_internal_ip_tcp_2.ino (1.93 KB)