NodeMCU将数据发送到Raspberry Pi服务器-phpMyAdmin

NodeMCU将数据发送到Raspberry Pi服务器-phpMyAdmin,php,raspberry-pi,nodemcu,Php,Raspberry Pi,Nodemcu,我目前无法将从温度传感器收集的数据发送到我的Raspberry Pi。Pi表示还没有收到任何数据。我也不知道如何得到和张贴工作。我对这东西还不太熟悉,所以任何帮助都将不胜感激 NodeMCU代码: #include <ESP8266WiFi.h> #include <OneWire.h> #include <DallasTemperature.h> //#include <WiFiEspClient.h> /******************

我目前无法将从温度传感器收集的数据发送到我的Raspberry Pi。Pi表示还没有收到任何数据。我也不知道如何得到和张贴工作。我对这东西还不太熟悉,所以任何帮助都将不胜感激

NodeMCU代码:

#include <ESP8266WiFi.h>
#include <OneWire.h> 
#include <DallasTemperature.h>
//#include <WiFiEspClient.h>

/********************************************************************/
// Data wire is plugged into pin 2 on the Arduino 
#define ONE_WIRE_BUS D4

/********************************************************************/
// Setup a oneWire instance to communicate with any OneWire devices  
// (not just Maxim/Dallas temperature ICs) 
OneWire oneWire(ONE_WIRE_BUS); 

/********************************************************************/
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

// Init library
WiFiClient client;
//dht DHT;
// Wifi config, ssid = wifi name & password = wifi password
const char* ssid = "ATkudnisdb0TAbEdfhrsdhg7i2";
const char* password = "2pkpmp3niagfdhgsdfhg%";
// Get data host
const char* host = "172.168.2.143";
// Your domain remember don't add slash at the end or http it will not work
const char* streamId   = "collectdata.php";
// Sensor values
//int humidityValue = 0;
int tempValue = 0;

void sendRequest(){
  Serial.print("connecting to ");
  Serial.println(host);
  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
  }
  // We now create a URI for the request
  String url = "/php/";
  url += streamId;
  url += "?temp=";
  url += tempValue;
  Serial.print("Requesting URL: ");
  Serial.println(url);
  // This will send the request to the server
  client.print("POST " + url + " HTTP/1.1\r\n" +
    "Host: " + host + "\r\n" + 
    "Connection: close\r\n\r\n");
  delay(1000);
  Serial.println();
  Serial.println("closing connection");
  delay(2500);
}

void setup() {
  sensors.begin();
  Serial.begin(9600);
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");  
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  //digitalWrite(blue, HIGH);
}

void loop() {
  Serial.print(" Requesting temperatures...");
  sensors.requestTemperatures();
  tempValue = sensors.getTempCByIndex(0);
  Serial.print(tempValue);
  // Send the command to get temperature readings
  sendRequest();
}
#包括
#包括
#包括
//#包括
/********************************************************************/
//数据线插入Arduino上的针脚2
#定义一条线总线D4
/********************************************************************/
//设置oneWire实例以与任何oneWire设备通信
//(不仅仅是Maxim/Dallas温度ICs)
单线单线(单线总线);
/********************************************************************/
//将我们的单线参考传递到达拉斯温度。
达拉斯温度传感器(单线);
//初始化库
无线客户端;
//dht-dht;
//Wifi配置,ssid=Wifi名称和密码=Wifi密码
const char*ssid=“ATkudnisdb0TAbEdfhrsdhg7i2”;
const char*password=“2pkmp3niagfdhgsdfhg%”;
//获取数据主机
const char*host=“172.168.2.143”;
//您的域请记住不要在末尾添加斜杠,否则它将不起作用
const char*streamId=“collectdata.php”;
//传感器值
//int湿度值=0;
int tempValue=0;
void sendRequest(){
串行打印(“连接到”);
Serial.println(主机);
//使用WiFiClient类创建TCP连接
无线客户端;
常数int httpPort=80;
如果(!client.connect(主机,httpPort)){
Serial.println(“连接失败”);
}
//我们现在为请求创建一个URI
字符串url=“/php/”;
url+=streamId;
url+=“?temp=”;
url+=tempValue;
Serial.print(“请求URL:”);
Serial.println(url);
//这将向服务器发送请求
client.print(“POST”+url+“HTTP/1.1\r\n”+
主机:“+Host+”\r\n“+
“连接:关闭\r\n\r\n”);
延迟(1000);
Serial.println();
Serial.println(“关闭连接”);
延迟(2500);
}
无效设置(){
begin();
Serial.begin(9600);
Serial.println();
串行打印(“连接到”);
序列号println(ssid);
WiFi.begin(ssid,密码);
while(WiFi.status()!=WL_已连接){
延迟(500);
连续打印(“.”);
}
Serial.println(“”);
Serial.println(“WiFi连接”);
Serial.println(“IP地址:”);
Serial.println(WiFi.localIP());
//数字写入(蓝色,高);
}
void循环(){
串行打印(“请求温度…”);
传感器。请求温度();
tempValue=sensors.getTempCByIndex(0);
串行打印(tempValue);
//发送命令以获取温度读数
sendRequest();
}
PHP代码:

<?php
$servername = “localhost”;
$username = “esp8266”;
$password = “Tutorial”;
$dbname = “esp8266”;

$mood = $_GET[‘mood’];
$conn = mysql_connect(“localhost”,”esp8266”,”Tutorial”);
if(!$conn) {
  die(‘Could not connect: ’ . mysql_error());
}
$datenow = date(‘Y-m-d’);
$sql = “INSERT INTO `JSDataTable`(`logdate`,`mood`) VALUES (\”$datenow\”,\”$mood\”)”;
$result = mysql_query($sql);
if(!result) {
  die(‘Invalid query: ‘ . mysql_error());
}
echo “<h1>The data has been sent!</h1>”;
mysql_close($conn);
?>


似乎丢失了很多信息。您似乎正在将临时数据发送到串行文件,但没有发送到php脚本。应该有一些东西将数据发送到一个url,最终运行php脚本。哦,我有点明白你的意思。我把php文件放在一个名为php的文件夹中。该文件夹位于raspberry pi as/var/www/php/collectdata.php中。这是url吗?不是。对于url,请考虑在浏览器中键入什么内容来访问文件。如果你有dns到你的PI,它会像。如果不这样做,它将是PI的IP地址,然后是文件的路径(从根webserver文件夹)。顺便说一句,很抱歉问了这么多问题。如果您的web服务器的根文件夹包含变量,则不是通常的位置。它类似于/var/www。这意味着url将与之相关(上面从未提及)。因此,如果它是/var/www,并且您的php文件位于/var/www/php中,那么URL将是
http://172.168.2.143/php/collectdata.php