Issues with receiving gist text on esp8266

I have built a device based off of (GitHub - julisa99/Lovebox: DIY project to lasercut a lovebox on your own (see https://en.lovebox.love/), which would cost almost $100.) and I am having an issue with receiving the gist message.

I have confirmed the ESP8266 is connected to the wifi by visiting my routers UI and also connecting it to my phones hotspot.

The device screen displays <3LOVEBOX<3 and seems to be stuck there. After the first initial flash of the sketch after building the device (twice for verification) both times the servo spins for a second.

I have looked into serial monitor but with limited code knowledge, and this sketch not belonging to me, I am not sure what/where to input serial write lines to debug.

I was hoping someone could spot something obvious before I go a different route on this christmas gift.

lovebox.ino

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

#include "credentials.h"
const char* ssid = _ssid;
const char* password = _password;
const String url = _url;

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() {
  if (WiFi.status() != WL_CONNECTED) {
    WiFi.begin(ssid, password);

    // connecting to WiFi
    while (WiFi.status() != WL_CONNECTED) {
      delay(500);
    }
  }
}

void getGistMessage() {
  const int httpsPort = 443;
  const char* host = "gist.githubusercontent.com";
  const char fingerprint[] = "70 94 DE DD E6 C4 69 48 3A 92 70 A1 48 56 78 2D 18 64 E0 B7";
  
  WiFiClientSecure client;
  client.setFingerprint(fingerprint);
  if (!client.connect(host, httpsPort)) {
    return; // failed to connect
  }
  
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "User-Agent: ESP8266\r\n" +
               "Connection: close\r\n\r\n");

  while (client.connected()) {
    String temp = client.readStringUntil('\n');
    if (temp == "\r") {
      break;
    }
  }
  String id = client.readStringUntil('\n'); 
  if(id[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 = id[0];
    EEPROM.write(142, idSaved);
    EEPROM.write(144, wasRead);
    EEPROM.commit(); 
    drawMessage(line);
  }
}

void spinServo(){
    myservo.write(pos);      
    delay(50);    // wait 50ms to turn servo

    if(pos == 75 || pos == 105){ // 75°-105° range
      increment *= -1;
    }
    pos += increment;
}

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();
  }
  
  if(wasRead){
    getGistMessage();   
  }
  
  while(!wasRead){   
    yield();
    spinServo();    // turn heart
    lightValue = analogRead(0);      // read light value
    if(lightValue > 300) { 
      wasRead = 1;
      EEPROM.write(144, wasRead);
      EEPROM.commit();
    }
  }
  
  delay(60000); // wait a minute before request gist again
}

credentials.h

const char* _ssid = "test123123";
const char* _password = "lovebox2022";

// url to gist: /yourUsername/generatedHashValue/raw/yourFilename
const String _url = "/printedpineapples/708b32ab51210f1778b5c39b5582f7bb/raw/message.text";

I have tried:
-two different device builds -multiple and different board flashes -uninstalling IDE and reinstalling all libraries
-tried to learn MQTT/WS to try a different route with no success
-verified the LDR/servo function with sweep/ldr example scripts

Have you tried, within void setup()

  1. Enter Serial.begin(9600) after myservo.attach(16);
  2. Before wifiConnect(); enter Serial.println("BEFORE WIFI");
  3. AfterwifiConnect(); enter Serial.println("AFTER WIFI");

You should see "BEFORE WIFI" immediately after LOVEBOX... and if you get "AFTER WIFI" in the serial monitor, remove #2 and #3, and place something similar around other key parts of the sketch.

If you do not see "AFTER" then your issue might be inside 1wifiConnect()1

Thank you xfpd,

I inserted those lines and got after both responses up to:

while(!wasRead){   
    yield();
    spinServo();    // turn heart
    lightValue = analogRead(0);      // read light value
    if(lightValue > 300) { 
      wasRead = 1;
      EEPROM.write(144, wasRead);
      EEPROM.commit();

I tried covering the light sensor and shining a light in it to see if the response would change.
The section below is the last section I got a looped response from:

  if(wasRead){
    Serial.println("BEFORE READ");
    getGistMessage(); 
    Serial.println("AFTER READ"); 
  }

If you are not getting "before" and "after" output around getGistMessage(), your next steps would be to put your debug 'Serial.print("here")' inside the getGistMessage() function... and keep testing until you find the line or lines that do not work.

I should have clarified better, I got responses for the "getGistMessage(); " I was not getting anything for the code above.

I assume it enters while(!wasRead) then maybe you can use the Serial.print() around the yield() function to see if yield() is the problem.

I read a little on the yield() function, and it seems to be a tool to write libraries, so if yield() is creating the hangup, comment it out using //` at the beginning of the line.

Here is what I found on yield():

/**
 * Empty yield() hook.
 *
 * This function is intended to be used by library writers to build
 * libraries or sketches that supports cooperative threads.
 *
 * Its defined as a weak symbol and it can be redefined to implement a
 * real cooperative scheduler.
 */

I started with commenting out yield( ) with no change.
I am trying to learn more about Serial.print ( ) and how to utilize it to see data I need for troubleshooting. Being on a time crunch for a Christmas gift I am stressing and probably missing something. I have confirmed the servo and light sensor work using a example script and a flashlight aswell as verifying the esp connects to the network fine. The display instantly says <3LOVE BOX<3 (and changes if i change oled.drawString(30, 30, "TEST"); which makes me believe either something is wrong with the string_url or fingerprint or the code writing it to EEPROM/display.

Here is a 9 minute video on sprintf() which you create a buffer, load it with text, variables and formatting, all in one line (rather than two lines for each output):

Maybe you overlooked there is no "String_url" in your .INO code, only String(space)url = (underscore)url;

p.s. Where did you get SSD1306Wire.h? (the whole library, zipped)

Thank you!
I will watch this video and hopefully learn something!

It seems the sketch is pulling _url from the credentials.h.

The SSD1306wire.h is coming from:

It looks to me that the line #10 String url has two entities separated by a space, and should either be written String _url (String [space] [underscore] url) to create a String instance or String_url (one entity String(underscore)url) to hold the value of _url. (but I do not know what that portion of the code really requires)

I have the same problem... have you find any solution?

And I also discovered that the problem is in line #62

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.