Menu
Cart 0

Quotation Machine using the CheeseBoard

Posted by Matthew Little on

Setting yourself a small challenge in the form of an interesting project is a great way of learning - even if it involves a fair bit of failing!

quotation machine

I set myself a weekend challenge of using our CheeseBoard ESP8266 development board to obtain and display some data from a website using a wifi connection.

My original challenge was to show real time data about the electricity supply mix within the UK. As I looked into this problem I realised that it was far too complex for me to start with. So I reduced the challenge to display some data from a website onto the OLED screen on the CheeseBoard ESP8266 development board.

Quotation Machine 1

Quotation API

I found a website which returns an inspiring quotation with a very simple API call. I decided to build a Quotation Machine. This would show me an inspiring quotation for whenever I needed a lift.

I found the following website, https://forismatic.com/en/, which returns a quote and the author as a line of text.

I set about figuring out the http request to send and return a quote in a variety of formats (text, json, xml). This was helped through the use of their API help pages.

The final URL I used to send via an http request is: http://api.forismatic.com/api/1.0/?method=getQuote&key=&format=text&lang=en

Quotation Machine

HTTP request

The next step was to figure out how to send a basic http request. The CheeseBoard software already written has a great configuration library. So the connection to my local wifi network was already handled through the EspApConfigurator. I added a couple of parameters for generating the quotation URL, including the language (although this has not yet been tested).

I then followed the Arduino tutorials for HttpClient which lead me to a solution.

The final code is:

    HTTPClient http;

    // Here we create the URL to check for getting the quote information
    String url = EspApConfigurator[SET_QUOTE_URL]->get();
    // Create the full URL with method, format and language
    url += "?method=getQuote&key=";
    url += EspApConfigurator[SET_SEED]->get();
    url += "&format=text&lang=";
    url += EspApConfigurator[SET_LANG]->get();

    http.begin(url);

    // start connection and send HTTP header
    int httpCode = http.GET();

    // httpCode will be negative on error
    if (httpCode > 0) {
      // HTTP header has been send and Server response header has been handled
      Serial.printf("[HTTP] GET... code: %d\n", httpCode);

      // file found at server
      if (httpCode == HTTP_CODE_OK) {
        payload = http.getString();
        DBLN(payload);
      }
    } else {
      Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
    }
    http.end();

After quite a few iterations and some additional parsing to strip out the author and the quote I had a working example.

When the button is pressed then the http request is sent and the quote is examined. If it is too long to display on our screen then I decided to ask for another quote and keep doing this until the quote is shorter than 130 characters.

If the encoder is twisted left or right then display will update with either the quote or the author.

I've been pretty pleased with my code and the simplicity of getting going with the CheeseBoard: Cheddar. I'll use the machine when I need a bit of uplifting quotation inspiration, except when I saw a Donald Trump quote!

This quote was pretty appropriate which appeared when I took some photos for this blog:

quotation machine cheeseboard

Build your own

You can download this example on our github example page: https://github.com/curiouselectric/CheeseBoard_Software.

Try it out and build your own one, or buy our kit and upload the code to get up and running straight away.


Share this post



← Older Post Newer Post →