Evan Rust
Published © GPL3+

Print Random Fact with Google Assistant

Use a thermal printer to print out a random fact with the help of Google Assistant.

IntermediateFull instructions provided3 hours1,014

Things used in this project

Hardware components

Photon
Particle Photon
×1
Adafruit Mini Thermal Receipt Printer
×1

Software apps and online services

Google Assistant
IFTTT
Python Flask

Story

Read more

Schematics

Printer Wiring

Code

Flask Server

Python
Run this in the background
import urllib.request
from bs4 import BeautifulSoup
import re
from flask import Flask, request
app = Flask(__name__)

url = "http://randomfactgenerator.net"

@app.route("/fact", methods=['GET'])
def get_fact():
    content = urllib.request.urlopen(url).read()

    parsedHTML = BeautifulSoup(content)

    tag = parsedHTML.find_all('div',{'id':'z'})[0]
    fact = re.search('<div id="z">(.+?)<br/>',str(tag))
    fact = fact.group(1)

    return fact

app.run(host='0.0.0.0')

Particle Code

C/C++
// This #include statement was automatically added by the Particle IDE.
#include <HttpClient.h>

// This #include statement was automatically added by the Particle IDE.
#include <Adafruit_Thermal.h>

HttpClient http;

http_header_t headers[] = {
    {"Accept", "*/*"},
    {NULL, NULL}
};

http_request_t request;
http_response_t response;

Adafruit_Thermal printer;

void setup(){
    Serial1.begin(19200);
    printer.begin(&Serial1);
    Serial.begin(9600);
    Serial.println("ready");
    Particle.function("fact-print",print_fact);
    request.hostname = "192.168.0.48"; //Local IP of the machine running the flask server goes here
    request.port = 5000;
    request.path = "/fact";
    printer.setTimes(35000,2100);
}

void loop() {
    
}

int print_fact(String data){
    String fact_returned = "hi";
    
    http.get(request,response, headers);
    Serial.println(response.status);
    Serial.println(response.body);
    
    printer.setSize('M');
    
    //printer.print('Did you know:');
    printer.feed(1);
    printer.println(response.body);
    printer.feed(3);
    printer.setDefault();
    
    return 0;
}

Credits

Arduino “having11” Guy

Arduino “having11” Guy

105 projects • 925 followers
21 year-old IoT and embedded systems enthusiast. Also produce content for Hackster.io and love working on projects and sharing knowledge.

Comments

Add projectSign up / Login