HTTPSClient example, Wifi shield REV 2.0

Bluetooth, XBee, RF......

HTTPSClient example, Wifi shield REV 2.0

Postby munajaf » Sun Nov 20, 2016 4:23 pm

hi guys, i have a problem with the programming,
it seems that i cant connect to a server when it contains https which use a 443 port,
i have tried to use the example given. HTTPSClient.
it successfully gain an ip from my router, but fail to connect to the server

Image

CODE: SELECT_ALL_CODE
/*
 *  HTTP over TLS (HTTPS) example sketch
 *
 *  This example demonstrates how to use
 *  secure_connect function to access HTTPS API.
 *  We fetch and display the status of
 *  esp8266/Arduino project continuous integration
 *  build.
 *
 *  Created by Ivan Grokhotkov, 2015.
 *  This example is in public domain.
 * 
 *  Modified by Ng Beng Chet, 2016
 */

#include <CytronWiFiShield.h>
#include <CytronWiFiClient.h>
#include <SoftwareSerial.h>

const char* ssid = "Amir0712";
const char* pass = "abc5272303";

const char* host = "api.github.com";
const int httpsPort = 443;

void setup() {
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  if(!wifi.begin(12, 13))
    Serial.println(F("Error talking to shield"));
  Serial.println(F("Start wifi connection"));
  if(!wifi.connectAP(ssid, pass))
    Serial.println(F("Error connecting to WiFi"));
  Serial.print(F("Connected to: "));Serial.println(wifi.SSID());
  Serial.print(F("IP address: "));Serial.println(wifi.localIP());

  ESP8266Client client;
  Serial.print("Connecting to ");
  Serial.println(host);
  if (!client.secure_connect(host, httpsPort))
  {
    Serial.println(F("Failed to connect to server."));
    client.stop();
    return;
  }
 
  String url = "/repos/esp8266/Arduino/commits/master/status";
  Serial.print("requesting URL: ");
  Serial.println(url);

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

  Serial.println("request sent");
  //set 5 seconds timeout for response from server
  int i=5000;
  while (client.available()<=0&&i--)
  {
    delay(1);
    if(i==1) {
      Serial.println(F("Timeout"));
      client.stop();
      return;
      }
  }
 
  if(client.available())
  {
    if (client.find("\r\n\r\n"))
      Serial.println(F("headers received"));

    String line = client.readStringUntil('\n');
    if (line.startsWith("{\"state\":\"success\""))
      Serial.println(F("esp8266/Arduino CI successfull!")); 
    else
      Serial.println(F("esp8266/Arduino CI has failed"));
   
    Serial.println(F("reply was:"));
    Serial.println(F("=========="));
    Serial.println(line);
    Serial.println(F("=========="));
    Serial.println(F("closing connection"));
   
    //flush the rest of received data
    while(client.available()>0)
       client.flush(); 
  }
 
  client.stop();
}

void loop() {
}

when i change secure_connect to connect
it gives a response, but it appeard that the server reject my connection since im not "secure" connecting to it, blblabalbalbal
change the port from 443 to 80 thus the result as below

Image
code that i made some changes
CODE: SELECT_ALL_CODE
/*
 *  HTTP over TLS (HTTPS) example sketch
 *
 *  This example demonstrates how to use
 *  secure_connect function to access HTTPS API.
 *  We fetch and display the status of
 *  esp8266/Arduino project continuous integration
 *  build.
 *
 *  Created by Ivan Grokhotkov, 2015.
 *  This example is in public domain.
 * 
 *  Modified by Ng Beng Chet, 2016
 */

#include <CytronWiFiShield.h>
#include <CytronWiFiClient.h>
#include <SoftwareSerial.h>

const char* ssid = "Amir0712";
const char* pass = "abc5272303";

const char* host = "api.github.com";
const int httpsPort = 80;

void setup() {
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  if(!wifi.begin(12, 13))
    Serial.println(F("Error talking to shield"));
  Serial.println(F("Start wifi connection"));
  if(!wifi.connectAP(ssid, pass))
    Serial.println(F("Error connecting to WiFi"));
  Serial.print(F("Connected to: "));Serial.println(wifi.SSID());
  Serial.print(F("IP address: "));Serial.println(wifi.localIP());

  ESP8266Client client;
  Serial.print("Connecting to ");
  Serial.println(host);
  if (!client.connect(host, httpsPort))
  {
    Serial.println(F("Failed to connect to server."));
    client.stop();
    return;
  }
 
  String url = "/repos/esp8266/Arduino/commits/master/status";
  Serial.print("requesting URL: ");
  Serial.println(url);

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

  Serial.println("request sent");
  //set 5 seconds timeout for response from server
  int i=5000;
  while (client.available()<=0&&i--)
  {
    delay(1);
    if(i==1) {
      Serial.println(F("Timeout"));
      client.stop();
      return;
      }
  }
 
  if(client.available())
  {
    if (client.find("\r\n\r\n"))
      Serial.println(F("headers received"));

    String line = client.readString();
    if (line.startsWith("{\"state\":\"success\""))
      Serial.println(F("esp8266/Arduino CI successfull!")); 
    else
      Serial.println(F("esp8266/Arduino CI has failed"));
   
    Serial.println(F("reply was:"));
    Serial.println(F("=========="));
    Serial.println(line);
    Serial.println(F("=========="));
    Serial.println(F("closing connection"));
   
    //flush the rest of received data
    while(client.available()>0)
       client.flush(); 
  }
 
  client.stop();
}

void loop() {
}


i hope that someone can really truly help me,
i sincerely apologize for any inconvenience that i have caused
munajaf
Fledgling
 
Posts: 1
Joined: Sun Nov 20, 2016 4:12 pm

Re: HTTPSClient example, Wifi shield REV 2.0

Postby bengchet » Tue Nov 22, 2016 8:59 am

Hi,

The original example should be working and the only thing you have to do is insert SSID and password.

The root cause might be that you are using ESP shield slightly older version. You can also run CytronWiFiDemo sketch to check the AT version and post it here. You can attach a screenshot here.

If so, in order to solve this problem, you will have upload new AT firmware to the shield, then only the HTTPSClient example can work. The ways to upload new firmware is stated in user manual.
bengchet
Moderator
 
Posts: 237
Joined: Tue Aug 25, 2015 8:29 am


Return to Wireless Device

Who is online

Users browsing this forum: No registered users and 11 guests

cron