1

I want to display sensor data on web server. I measure sensor data by arduino and it sends sensor data to raspberry pi via bluetooth(wireless). There are codes for logging data.

code of arduino

#include <SoftwareSerial.h>
#include "HX711.h"
#define calibration_factor -7050.0
#define DAT 5
#define CLK 6
SoftwareSerial bt (2,3);
HX711 scale(DAT,CLK);
void setup(){
  bt.begin(9600);
  scale.set_scale(calibration_factor);
  scale.tare();
}
void loop(){
  float h=scale.get_units();
  float t=0.454*scale.get_units();
  bt.print(String(h) + "," + String(t));
  bt.pring("\n");
  delay(2000);
}

code of raspberry pi

import bluetooth
bd_addr="xx:xx:xx:xx:xx:xx"
port=1
sock=bluetooth.BluetoothSocket (bluetooth.RFCOMM)
sock.connect((bd_addr.port))
data=""
while 1:
try:
  data += sock.recv(1024)
  data_end=data.find('\n')
  if data_end!=-1:
    rec=data[:data_end]
    print data
    data = data[data_end+1:]
  except KeyboardInterrupt:
    break
sock.close()

And I compile these codes and I can check sensor data in raspberry pi. How can I display sensor data in web server(LAMP)?

5
  • 1
    That may be too broad a question for this site.
    – joan
    Mar 9, 2018 at 8:52
  • No doubt it is too broad a question, but this is probably many beginners are facing. I'm providing a simple solution to address his question.
    – hcheung
    Mar 9, 2018 at 9:02
  • In your RPi code, would a "print rec" not be better than your "print data"? Mar 9, 2018 at 10:55
  • Do you need TLS/SSL? Do you want to restrict the access to the website with username/password? I would use Apache and PHP, without a database. PHP can execute your script and collect the output (sensor data) and display it as html: secure.php.net/manual/en/function.exec.php
    – Fabian
    Mar 9, 2018 at 15:47
  • Yes I want to display output(sensor data) without a database. So I make php file like <?php $output=shell_exec(python /home/pi/hello.py(filename); echo "<pre>$output</pre>";?> and In web broswer 'HTTP ERROR 500' is displayed lol
    – 김진수
    Mar 10, 2018 at 18:23

1 Answer 1

2

Since you are aware of LAMP, the short answer would be go ahead to install LAMP and learn how to use it.

But there is a simpler solution for beginners and for python programmers. Since you are using python, so you don't have to use PHP, and you probably don't need to have a full feature web server yet. Python 3 actually come with a build-in http server that is good enough for testing and learning purpose. You can run it with a simple command at the directory where your code located:

python3 -m http.server

This will create a web server serving HTTP on your local network at http://0.0.0.0:8000/ (i.e. http://localhost:8000).

You will need to install (and learn) Flask for python web development. The simplest flask python code that addressed your question of serving the sensor data to a web page would be:

from flask import Flask, render_template_string


app = Flask(__name__)
data = 200    #assuming this is your sensor data, for illustration purpose

@app.route('/')
def index():
    return render_template_string('''
         <h1>My Sensor Web Page</h1>
         <p>My sensor reading is {}".format(data))</p>
         '''


if __name__ == '__main__':
    app.run(debug=True)

Launch your browser and point it to http://localhost:8000, you should see the data to be rendered as webpage per our simple example code.

This simple solution could carry a long way before you need to install a full feature web server like Apache or Nginx on your Raspberry Pi.

3
  • Sorry sir I can't understand. I'm using python file that I show you in my question. You mean make another python file like your answer?
    – 김진수
    Mar 10, 2018 at 18:20
  • Your question was how to display data on web server using PHP. If you want to display your result as a web page, you need a web server and you need to be able to render the data in a web page, I'm showing you 1)how to run a web server on python; 2) how to render the data using python (without using php and mysql database) flask web development framework.
    – hcheung
    Mar 11, 2018 at 0:39
  • Your answer means make another python file??
    – 김진수
    Mar 11, 2018 at 21:51

Not the answer you're looking for? Browse other questions tagged or ask your own question.