1

I'm currently building a new plugin for OctoPrint running on Rasberry Pi 3. What I want it to do is to have a HTML front-end tab that contains two buttons. Then the first button should be able to trigger a command line to capture the image from mjpg-stremaer, and another button to run a python script with imported OpenCV that will do edge detection and output to the front-end the image after process.

From OctoPrint, it uses jinja2 template for front-end and KnockOut.js for view model. This is where I got stuck because I have no clue how to do next due to my limited knowledge about web application.

After online search, I found that either flask or tornado is mentioned a lot. I tried using flask as recommended after online search but it ran into the

Error 98: "Address already in use"

issue because OctoPrint is running on it. So, my question is if there is a way to do such things? In other words, is there a way to click on HTML buttons to trigger python and command line in the Raspberry Pi without having the port or socket issue?

The python code got from flask, which will be the python code with OpenCV imported and do image processing:

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"

if __name__ == "__main__":
    app.run()

The HTML code using jinja2, currently using the button to display date and time, but will trigger the python code above and a command line:

<html>
<body>

<h1>Java Script</h1>

<button type="button" onclick="document.getElementById('demo').innerHTML = Date()">
Click me to display Date and Time.</button>

<p id="demo"></p>

</body>
</html>

And my command line to capture picture from mpjg-streamer is:

wget http://localhost:8080/?action=snapshot -O output.jpg

If possible, please provide me an example with explanation for it and also introduction about server and client side concepts? Thanks. My apology if there is any false usage of terminology here.

Wolfgang Fahl
  • 15,016
  • 11
  • 93
  • 186
Iker Hua
  • 403
  • 4
  • 14
  • 1
    Use a different port, then. http://stackoverflow.com/a/29079598/2308683 – OneCricketeer Nov 28 '16 at 22:41
  • Thanks @cricket_007. Would using different port prevent me from running the python script? Or the python script would still be able to interface with the html/jinja2? – Iker Hua Nov 28 '16 at 22:43
  • 1
    Flask defaults to port 5000, I believe. I do not know what this Octoprint thing does, but it seems to conflict with Flask, so moving Flask to a different port is the solution. I don't know what script you are taking about. – OneCricketeer Nov 28 '16 at 22:44
  • I'll give it a try. Thanks @cricket_007! – Iker Hua Nov 28 '16 at 22:47
  • 1
    Along with that point, I don't think `http://localhost:8080` is accessible. It seems Octoprint also runs on port 5000. And if you do use port `80`, you have to run the script with `sudo`. Using something in the 8000's is "better" – OneCricketeer Nov 28 '16 at 22:49
  • I think you're right. OctoPrint is not running on that one but on port 5000 instead. `http://localhost:8080` is actually accessible because that one is used for mjpg-streamer for webcam streaming. – Iker Hua Nov 28 '16 at 22:57

0 Answers0