Arduino “having11” Guy
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 hours9

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

Dsc 9346 6b4ibn2k9b

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

36 projects • 242 followers
IoT, Arduino, and Raspberry Pi. I can create designs, code, and build projects for only $22/hr. Just message me with your project request.

Comments

Add projectSign up / Login