-->
Page 1 of 1

HTTPSRequest example loading json ...

PostPosted: Tue Apr 23, 2019 3:49 am
by JonasVorwerk
I'm stuck, help needed ;)

The HTTPSRequest example works perfectly without any modifications.
Then I modified it to load some stuff from my webserver.
Loading a .json file works as expected
Then I try to load a php file that outputs the same simple json, and this keeps failing. This .php is just echoing exactly the same json as the .json file, no errors, but can't figure out the difference.

https://moveadot.nl/test/json.php
https://moveadot.nl/test/json.json

Thanks in advance

Code: Select all/*
    HTTP over TLS (HTTPS) example sketch

    This example demonstrates how to use
    WiFiClientSecure class to access HTTPS API.
    We fetch and display the status of
    esp8266/Arduino project continuous integration
    build.

    Limitations:
      only RSA certificates
      no support of Perfect Forward Secrecy (PFS)
      TLSv1.2 is supported since version 2.4.0-rc1

    Created by Ivan Grokhotkov, 2015.
    This example is in public domain.
*/

#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>

#ifndef STASSID
#define STASSID "Network"
#define STAPSK  "Password"
#endif

const char* ssid = STASSID;
const char* password = STAPSK;

const char* host = "moveadot.nl";
const int httpsPort = 443;

// Use web browser to view and copy
// SHA1 fingerprint of the certificate
const char fingerprint[] PROGMEM = "4B 06 33 A8 56 C3 E4 DA B3 4F 76 6B 5D BF 4D DB F0 12 47 EE";

void setup() {
  Serial.begin(115200);
  Serial.println();
  Serial.print("connecting to ");
  Serial.println(ssid);
  WiFi.mode(WIFI_STA);
  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());

  // Use WiFiClientSecure class to create TLS connection
  WiFiClientSecure client;
  Serial.print("connecting to ");
  Serial.println(host);

  Serial.printf("Using fingerprint '%s'\n", fingerprint);
  client.setFingerprint(fingerprint);

  if (!client.connect(host, httpsPort)) {
    Serial.println("connection failed");
    return;
  }

  String url = "/test/json.php";
  //String url = "/test/json.json";
 
  Serial.print("requesting URL: ");
  Serial.println(url);

  client.print(String("POST ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "User-Agent: ESP8266\r\n" +
               "Connection: close\r\n\r\n");

  Serial.println("request sent");
  while (client.connected()) {
    String line = client.readStringUntil('\n');
    if (line == "\r") {
      Serial.println("headers received");
      break;
    }
  }
  String line = client.readStringUntil('\n');
  if (line.startsWith("{\"state\":\"success\"")) {
    Serial.println("esp8266/Arduino CI successfull!");
  } else {
    Serial.println("esp8266/Arduino CI has failed");
  }
  Serial.println("reply was:");
  Serial.println("==========");
  Serial.println(line);
  Serial.println("==========");
  Serial.println("closing connection");
}

void loop() {
}

Re: HTTPSRequest example loading json ...

PostPosted: Wed Apr 24, 2019 2:54 am
by JonasVorwerk
When adding
Code: Select all 
while (client.connected()) {
    //output all data
    Serial.write(client.read());
    delay(10);
  }


it outputs,

HTTP/1.1 200 OK
Date: Wed, 24 Apr 2019 07:50:59 GMT
Server: Apache
Connection: close
Transfer-Encoding: chunked
Content-Type: application/json
X-TransIP-Backend: web758
X-TransIP-Balancer: lb0

42
{"state":"success","ip_local":"192.168.0.25","post_id":1234456789}
0

So I'm wondering, what are the 42 and 0, and how to extract just the json data.
Any insight would be appreciated ;)

Re: HTTPSRequest example loading json ...

PostPosted: Wed Nov 23, 2022 4:22 am
by RobertTBob
Hello,
I am trying te extract data from my weather station that posts to WunderGround. I have searched www for code and this is the best so far however I cannot get it to load the data from the API.
Connection to my wifi is successful however however it returns the following text:

22:05:03.487 -> WiFi connected
22:05:03.487 -> IP address:
22:05:03.487 -> 192.168.20.243
22:05:03.487 -> connecting to https://api.weather.com/v2/pws/observat ... on=decimal
22:05:03.536 -> Using fingerprint 'D4 81 63 98 5D 9E 06 06 85 C8 FE 8F 10 E8 FF DB B5 91 94 E0'
22:05:03.536 -> connection failed

I am really stuck. I have searched www for answers and this code is the best so far. Can someone please help me with what I am doing incorrectly? My code is below:

Thanks for any pointers in advance :)

/*
HTTP over TLS (HTTPS) example sketch

This example demonstrates how to use
WiFiClientSecure class to access HTTPS API.
We fetch and display the status of
esp8266/Arduino project continuous integration
build.

Limitations:
only RSA certificates
no support of Perfect Forward Secrecy (PFS)
TLSv1.2 is supported since version 2.4.0-rc1

Created by Ivan Grokhotkov, 2015.
This example is in public domain.
*/

#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>

#ifndef STASSID
#define STASSID "Your Fat Arse 2.4GHz"
#define STAPSK "******************"
#endif

const char* ssid = STASSID;
const char* password = STAPSK;

//const char* host = "http://arduinojson.org/example.json";
const char* host = "https://api.weather.com/v2/pws/observations/current?stationId=IAUCKL739&format=json&units=s&apiKey=a228a6ac19844a25a8a6ac19843a255d&numericPrecision=decimal";
const int httpsPort = 443;

// Use web browser to view and copy
// SHA1 fingerprint of the certificate
const char fingerprint[] PROGMEM = "D4 81 63 98 5D 9E 06 06 85 C8 FE 8F 10 E8 FF DB B5 91 94 E0";

void setup() {
Serial.begin(115200);
Serial.println();
Serial.print("connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
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());

// Use WiFiClientSecure class to create TLS connection
WiFiClientSecure client;
Serial.print("connecting to ");
Serial.println(host);

Serial.printf("Using fingerprint '%s'\n", fingerprint);
client.setFingerprint(fingerprint);

if (!client.connect(host, httpsPort)) {
Serial.println("connection failed");
return;
}

String url = "/test/json.php";
//String url = "/test/json.json";

Serial.print("requesting URL: ");
Serial.println(url);

client.print(String("POST ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"User-Agent: ESP8266\r\n" +
"Connection: close\r\n\r\n");

// Serial.println("request sent");
// while (client.connected()) {
// String line = client.readStringUntil('\n');
// if (line == "\r") {
// Serial.println("headers received");
// break;
// }
// }
while (client.connected()) {
//output all data
Serial.write(client.read());
delay(10);
}
String line = client.readStringUntil('\n');
if (line.startsWith("{\"state\":\"success\"")) {
Serial.println("esp8266/Arduino CI successfull!");
} else {
Serial.println("esp8266/Arduino CI has failed");
}
Serial.println("reply was:");
Serial.println("==========");
Serial.println(line);
Serial.println("==========");
Serial.println("closing connection");
}

void loop() {
}