Osprey72
Posts: 36
Joined: Sun Apr 24, 2016 7:07 pm
Location: Michigan

How to read data using Python/C++/JavaScript/HTML

Sat Jun 23, 2018 10:56 pm

I have the following code where it begins with Python calling HTML. Then I point the code to the file where I want the current single piece of data to be pulled from (home/pi/blower.py). Then I get into the HTML/JS where 5 gauges will be displayed, the first will be the actual temp variable and then 4 with random numbers generated. Chrome is not displaying anything now. When I run straight from Notepad++, the 4 gauges are fine and the python code gets translated as text.

Code: Select all

#!/usr/bin/env python

# print HTTP header
print "Content-type: text/html\n\n"

# open file with data from script
temp = 0;
datafile = open("/home/pi/Blower.py", "r")
page_data = datafile.read(temp)
datafile.close()

# define an HTML template
page_str = """<!DOCTYPE html>
<html>
    <head>
		<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
		<script type="text/javascript">
		google.charts.load('current', {'packages':['gauge']});
		google.charts.setOnLoadCallback(drawChart);

      function drawChart() {
		// Set the number of guages and guage names
        var data = google.visualization.arrayToDataTable([['Label', 'Value'],['ManTemp1', 65],['ManTemp2', 65],['ManTemp3', 65],['ManTemp4', 65],['ManTemp5', 65]]);
		// Set the guage parameters
        var options = {width: 400, height: 120, redFrom: 90, redTo: 100, yellowFrom: 75, yellowTo: 90, greenFrom: 50, greenTo: 75, minorTicks: 5};
		// Set the chart type
        var chart = new google.visualization.Gauge(document.getElementById('chart_div'));

        chart.draw(data, options);
		//Begin collecting data
        setInterval(function() {
          data.setValue(0, 1, 40 + page_data);
          chart.draw(data, options);
        }, 13000);
        setInterval(function() {
          data.setValue(1, 1, 40 + Math.round(60 * Math.random()));
          chart.draw(data, options);
        }, 5000);
        setInterval(function() {
          data.setValue(2, 1, 60 + Math.round(20 * Math.random()));
          chart.draw(data, options);
        }, 26000);
		setInterval(function() {
          data.setValue(3, 1, 60 + Math.round(40 * Math.random()));
          chart.draw(data, options);
        }, 26000);
		setInterval(function() {
          data.setValue(4, 1, 60 + Math.round(15 * Math.random()));
          chart.draw(data, options);
        }, 26000);
      }
		</script>
	</head>
	<body>
		<!-- Identify where the chart should be drawn. -->
		<div id="chart_div" style="width: 400px; height: 120px;"></div>
	</body>
</html>"""

# format and print the page
print page_str.format (data = page_data)
Thanks in advance :geek:
Attachments
Capture1.PNG
Capture1.PNG (13.6 KiB) Viewed 419 times
Capture1.PNG
Capture1.PNG (13.6 KiB) Viewed 419 times

User avatar
DougieLawson
Posts: 32995
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: How to read data using Python/C++/JavaScript/HTML

Sun Jun 24, 2018 8:27 am

What webserver are you running is it Apache2, Lighttpd or NGinx?

Have you enabled python CGI programs in your web server?

https://www.linux.com/blog/configuring- ... on-scripts
https://redmine.lighttpd.net/projects/l ... PythonWSGI
https://www.nginx.com/blog/maximizing-p ... d-caching/
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

Osprey72
Posts: 36
Joined: Sun Apr 24, 2016 7:07 pm
Location: Michigan

Re: How to read data using Python/C++/JavaScript/HTML

Sun Jun 24, 2018 7:40 pm

I am using Apache2 and I've looked up several of the config files that have been listed in the reading material listed and have found none of them. I have done a fresh reinstall and re-searched and still cannot find them. Does this mean I have to build them?

User avatar
DougieLawson
Posts: 32995
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: How to read data using Python/C++/JavaScript/HTML

Sun Jun 24, 2018 7:52 pm

Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

Osprey72
Posts: 36
Joined: Sun Apr 24, 2016 7:07 pm
Location: Michigan

Re: How to read data using Python/C++/JavaScript/HTML

Sun Jun 24, 2018 9:45 pm

ok, I followed line by line what that last post said (https://python-forum.io/Thread-Run-Pyth ... rom-Apache). I'm still not getting any change. I have spent most of my day looking into this issue and am as still confused as when I began. I have been to the following sites:

https://www.linux.com/blog/configuring- ... on-scripts
https://askubuntu.com/questions/256013/ ... d-domain-n
http://httpd.apache.org/docs/2.2/howto/cgi.html

Am I THAT dense? From what I have read, from what I have copied to my Pi...this darn thing should be singing websites left and right. I do monthly updates and upgrades, maintain all of them well, plenty of memory...what am I doing wrong? I simply want to display a webpage with data from my Pi.
Attachments
Capture1.PNG
Capture1.PNG (22.8 KiB) Viewed 295 times

User avatar
DougieLawson
Posts: 32995
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: How to read data using Python/C++/JavaScript/HTML

Sun Jun 24, 2018 10:05 pm

You need to point your browser at the local URL for your python CGI program. The program gets the as an Apache2 subtask.

The normal place for CGI programs is /usr/lib/cgi-bin unless you've changed it.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

Osprey72
Posts: 36
Joined: Sun Apr 24, 2016 7:07 pm
Location: Michigan

Re: How to read data using Python/C++/JavaScript/HTML

Sun Jun 24, 2018 10:16 pm

I'm sorry Doug, I am so frustrated at this point that what you have said might as well been in Russian. I have been searching for files and folders that do not exist all day long. So many, what I think, are "wild goose chases" from either outdated posts or posts that no longer deal with a Pi3. I am still running "Jessie" due to a weather logging setup from an older Pi. Could this be any part of my issue?

User avatar
DougieLawson
Posts: 32995
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: How to read data using Python/C++/JavaScript/HTML

Mon Jun 25, 2018 8:41 am

Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

Osprey72
Posts: 36
Joined: Sun Apr 24, 2016 7:07 pm
Location: Michigan

Re: How to read data using Python/C++/JavaScript/HTML

Wed Jun 27, 2018 2:11 pm

Hey Doug, Chrome says your link cannot be reached...have any other links? Sorry :? Have been busy with work, we are prototyping a circuit board and the project is launching in 6 weeks so I've been busy busy busy.

User avatar
DougieLawson
Posts: 32995
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: How to read data using Python/C++/JavaScript/HTML

Wed Jun 27, 2018 2:28 pm

bugger, because that included some nice and simple diagrams.

https://www.electricmonk.nl/docs/apache ... ython.html has lots of words and some nice diagrams.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

Osprey72
Posts: 36
Joined: Sun Apr 24, 2016 7:07 pm
Location: Michigan

Re: How to read data using Python/C++/JavaScript/HTML

Wed Jun 27, 2018 2:47 pm

Doug, did the very first line of code in that tutorial and got this response:

[email protected]:~ $ sudo apt-get install libapache2-mod-fastcgi
Reading package lists... Done
Building dependency tree
Reading state information... Done
Package libapache2-mod-fastcgi is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source

E: Package 'libapache2-mod-fastcgi' has no installation candidate

This whole endeavor has been a nightmare!! How do people still continue to keep this tool as a hobby? I only find it useful as a controller and that's about it. Am I not holding my mouth right...not sacrificing the right animals...done something in a past life to anger the gods? I have to find humor in this quick!!!

User avatar
DougieLawson
Posts: 32995
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: How to read data using Python/C++/JavaScript/HTML

Wed Jun 27, 2018 8:53 pm

Try libapache2-mod-fcg*
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

Return to “Advanced users”

Who is online

Users browsing this forum: No registered users and 11 guests