Skip to content

Help HTTPS request doesn't work #11

Description

@thomasmore72

Hi,
I have been at this for 2 months (crying face).
This is my first time using Arduino so could be a very easy fix.
When this code is run it prints "Connection to GitHub lost" which from what I know just means it's not even connecting to the git file.

Here is the txt file : https://gist.githubusercontent.com/thomasmore72/b3ca747d9852fbe407a11e629d854f96/raw/28a9bccb6b4e0f4d163644063b5a092e29700ed0/gistfile1.txt

Like I said brand new to this so if you have a potential solution I might need a bit of code to understand :D

#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <EEPROM.h>
#include <Servo.h>
#include "SSD1306Wire.h"

const String url = "/thomasmore72/b3ca747d9852fbe407a11e629d854f96/raw/28a9bccb6b4e0f4d163644063b5a092e29700ed0/gistfile1.txt";

SSD1306Wire oled(0x3C, D2, D1);
Servo myservo; 
int pos = 90;
int increment = -1;
int lightValue;
String line;
String messageMode;
char idSaved; 
bool wasRead;  

void drawMessage(const String& message) {
  oled.clear();

  // differentiat between 't'ext and image message
  if(messageMode[0] == 't'){
    oled.drawStringMaxWidth(0, 0, 128, message);    
  } 
  else {
    for(int i = 0; i <= message.length(); i++){
      int x = i % 129;
      int y = i / 129;
    
      if(message[i] == '1'){
        oled.setPixel(x, y);
      }
    } 
  }    
  oled.display();
}

void wifiConnect() {
  Serial.begin(9600);
  Serial.println();

  WiFi.begin("Wifi Name", "Wifi Pass");

  Serial.print("Connecting");

  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
  }
  
  if (WiFi.status() == WL_CONNECTED) {
    Serial.print("Connected, IP address: ");
    Serial.println(WiFi.localIP());
  }
}

void getGistMessage() {
  const int httpsPort = 443;
  const char* host = "gist.githubusercontent.com";
  const char fingerprint[] = "A1 46 14 C7 2A 1D 52 79 F6 AA 2B B2 C5 0A 3B D3 F5 02 06 75";
  
  WiFiClientSecure client;
  client.setFingerprint(fingerprint);
  
  if (!client.connect(host, httpsPort)) {
    Serial.println("Failed to connect to GitHub");
    return; // failed to connect
  }
  
  client.print(String("GET ") + "/thomasmore72/b3ca747d9852fbe407a11e629d854f96/raw/28a9bccb6b4e0f4d163644063b5a092e29700ed0/gistfile1.txt" + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "User-Agent: ESP8266\r\n" +
               "Connection: close\r\n\r\n");

  // Wait for a response
  if (!client.connected()) {
    Serial.println("Connection to GitHub lost");
    client.stop();
    return;
  }

  // Read the response status code
  String response = client.readStringUntil('\n');
  if (!response.startsWith("HTTP/1.1 200 OK")) {
    Serial.println("GitHub request failed with status: " + response);
    client.stop();
    return;
  }

  while (client.connected()) {
    String temp = client.readStringUntil('\n');
    if (temp == "\r") {
      break;
    }
  }

  //String id = client.readStringUntil('\n'); 
  if(response[0] != idSaved){ // new message
    messageMode = client.readStringUntil('\n');
    if (messageMode[0] == 't'){
      line = client.readStringUntil(0);
    } else {
      // binary image is corrupted if readStringUntil() takes too long
      // fix: read string line by line
      line = "";
      for (int i = 0; i < 64; i++)     
      {
        line += client.readStringUntil('\n');
        line += "\n";
      }
      if (line.length() != 8256)
      {
        getGistMessage();
      }
    }
    wasRead = 0;
    idSaved = response[0];
    EEPROM.write(142, idSaved);
    EEPROM.write(144, wasRead);
    EEPROM.commit(); 
    drawMessage(line);
  }
}

void setup() {
  myservo.attach(16);       // Servo on D0
  
  oled.init();
  oled.flipScreenVertically();
  oled.setColor(WHITE);
  oled.setTextAlignment(TEXT_ALIGN_LEFT);
  oled.setFont(ArialMT_Plain_10);
     
  oled.clear();
  oled.drawString(30, 30, "<3 LOVEBOX <3");
  oled.display();
  
  wifiConnect();

  EEPROM.begin(512);
  idSaved = EEPROM.get(142, idSaved);
  wasRead = EEPROM.get(144, wasRead);
}

void loop() {
  if (WiFi.status() != WL_CONNECTED) {
    wifiConnect();
  }
  
  getGistMessage();   
  Serial.print("/");
  delay(5000); // wait a minute before request gist again
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions