ESP8266 Trying to get a JSON from server

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)

192.168.0.58 looks like a local server. What script are you running on this server? What program is listening on port 3333?

As far as I know, port 3333 is not an HTTP port. And you say that the server doesn't use HTTP, yet you use a request that looks an awful lot like a malformed HTTP request.

You need to escape the quotes in your JSON string.

Pieter

Yes I'm connecting through my local router to ethereum Miners using Claymore's dual ethereum miner software Claymore's Dual Ethereum AMD+NVIDIA GPU Miner v15.0 (Windows/Linux) . Yes, I used a WIFICLient for the esp8266 the help me, I'm really a noob.

This is the file that tells me how to connect to API on the servers

Thank for the escape the quotes. But I get a client timeout now when using POST instead of GET.

API.txt (1.86 KB)

Don't use POST nor GET. That's HTTP. Also get rid of the HTTP headers, just send the JSON, without anything else.

Pieter