[英]how to send data to to mssql server from python raspberry pi?
[英]I don't know how to send sensor data from a Raspberry Pie to MySQL Server with Python
我正在处理一个工作项目,但遇到了一些麻烦,我有一个安装了 Ubuntu 18.04 服务器的 LAMP 服务器,在服务器端我有一个 PHP 脚本,该脚本在 Z673A01ABBFA4759DCCABB7 的表中实现从客户端接收的数据数据库通过 URl。 我正在谈论的客户端是通过串行连接到 DHT22 传感器的 Raspberry Pie 3。
我现在遇到的问题是我对 Python 没有太多经验。 我制作了一个脚本,可以在控制台中打印来自传感器的数据。 但我不知道如何将数据从客户端发送到数据库。
If I am going to use the PHP script, the client is going to need to print the data in the URl path of the PHP script (like so: http://192.168.0.101?temp=20.1&hum=30.2 ), but i希望你们能告诉我该怎么做,或者是否有任何方法可以跳过整个 PHP 脚本并实现一种连接数据库的方法,并使用 Python 脚本将数据直接打印到表中。
我正在使用的 PHP 脚本:
<?php
class DBtable {
public $link='';
function __construct($temperature, $humidity) {
$this->connect();
$this->storeInDB($temperature, $humidity);
}
function connect() {
$this->link = mysqli_connect('localhost','Generic_user','Generic_passwd') or die('Cannot connect to DB');
mysqli_select_db($this->link,'DBtable') or die('Cannot choose DB.');
}
function storeInDB($temperature, $humidity) {
$query = "insert into DBtable set Hum='".$humidity."', Temp='".$temperature."'";
$result =mysqli_query($this->link,$query) or die('Errant query: '.$query);
("Data was implemented correct! Temp = '".$temperature."'°C og Hum = '".$humidity."'%'");
}
}
if($_GET['Temp'] != '' and $_GET['Hum'] != '') {
$DBtable=new DBtable($_GET['Temp'],$_GET['Hum']);
}
?>
(出于安全原因,我更改了一些信息,但我可以向您保证,我已经对其进行了测试并且可以正常工作。)
该脚本最初是为 Arduino 制作的,但我无法让它工作,所以我切换到了 Raspberry 平台。
客户端上的 Python 脚本:
import Adafruit_DHT as dht
from time import sleep
DHT = 3
while True:
h,t = dht.read_retry(dht.DHT22, DHT)
print('temp={0:0.1f}*C hum={1:0.1f}%'.format(temperature, humidity))
sleep(1)
output 到控制台:
temp=20.1*C hum=30.2%
那么问题是,我该怎么做才能将数据发送到 PHP 脚本,或者,如果失败,跳过 PHP 部分并直接使用 Python 脚本?
如果你们需要更多信息,请询问,我会尽我所能提供。 谢谢!
- - -更新 - - -
所以,我发现了一些可能Requests
POST
数据。 我正在服务器上制作一个PHP
脚本,它将接收到的数据注入到 MySQL DB 中。 然后是测试,我将向你们更新它的进展情况。
如果您有任何建议,请直接拍摄,如果您需要更多信息或有任何问题,我仍然会在这里!
所以。 我发现了如何将温度和湿度数据从 Raspberry Pi 3 发送到数据库。我使用了@furas 建议的外部模块requests
,这是我编写的 Python 脚本。
import Adafruit_DHT
import requests
sensor = Adafruit_DHT.DHT22
pin = 2
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
if humidity is not None and temperature is not None:
print('Temperature ={0:0.1f}*C Humidity = {1:0.1f}%'.format(temperature, humidity))
payload = {'temp': temperature, 'hum': humidity}
r = requests.post('http://192.168.1.7/test/MySQL_POST_Test.php, data=payload')
print(r.text)
else:
print('Failed to read the sensor, try "sudo python3 sensor-post.py" again.')
和 PHP 脚本:
$servername = "localhost";
$username = "Test"
$password = "passwd";
$dbname = "TestDB";
$temperature = $_POST["temp"];
$humidity = $_POST["hum"];
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO SensorTest (Temperature, Humidity) VALUES ($temperature, $humidity)";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully!";
}
else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
运行 Python 脚本后的结果:
Temperature =22.1*C Humidity =34.2%
New record created successfully!
我尝试向 Python 脚本添加schedule
,但我还没有让它工作,但是这些脚本没有schedule
就可以正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.