Php 节点MCU温度读数至Raspberry Pi无线

Php 节点MCU温度读数至Raspberry Pi无线,php,arduino,raspberry-pi,nodemcu,Php,Arduino,Raspberry Pi,Nodemcu,我目前无法将从温度传感器收集的数据发送到我的Raspberry Pi。Pi表示还没有收到任何数据。我也不知道如何得到和张贴工作。我对这东西还不太熟悉,所以任何帮助都将不胜感激 还有人可以查看我的sendTemperatureTS方法和我的PHP代码,因为正如我所说,我是新手。顺便说一下,我在Raspberry Pi的/var/www中的PHP文件夹中创建了我的PHP文件。我使用php文件夹中的命令sudo nano collectdata.php来编写代码 节点MCU的Arduino代码: #i

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

还有人可以查看我的
sendTemperatureTS
方法和我的PHP代码,因为正如我所说,我是新手。顺便说一下,我在Raspberry Pi的/var/www中的PHP文件夹中创建了我的PHP文件。我使用php文件夹中的命令
sudo nano collectdata.php
来编写代码

节点MCU的Arduino代码:

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

#define myPeriodic 15 //in sec | Thingspeak pub is 15sec
#define ONE_WIRE_BUS 2  // DS18B20 on arduino pin2 corresponds to D4 on physical board

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);
float prevTemp = 0;
const char* server = "172.168.2.143";
const char* MY_SSID = "AkhuogTkhbbEjhbuvouvr7i2"; 
const char* MY_PWD = "2pkpmbipsrbeirbp3niag%";
int sent = 0;

void setup() {
  Serial.begin(115200);
  connectWifi();
}

void loop() {
  float temp;
  //char buffer[10];
  DS18B20.requestTemperatures(); 
  temp = DS18B20.getTempCByIndex(0);
  //String tempC = dtostrf(temp, 4, 1, buffer);//handled in sendTemp()
  Serial.print(String(sent)+" Temperature: ");
  Serial.println(temp);

  sendTemperatureTS(temp);
  int count = myPeriodic;
  while(count--)
  delay(1000);
}

void connectWifi() {
  Serial.print("Connecting to "+*MY_SSID);
  WiFi.begin(MY_SSID, MY_PWD);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("Connected");
  Serial.println("");  
}//end connect

void sendTemperatureTS(float temp) {
  WiFiClient client;
  if (client.connect(server, 80)) {
    Serial.println("WiFi Client connected ");
    String postStr = "/php/";
    postStr += "?temp=";
    postStr += String(temp);
    postStr += "\r\n\r\n";
    client.print("POST 172.168.2.143/php/collectdata.php HTTP/1.1\n");
    client.print("Host: 122.168.2.143\n");
    client.print("Connection: close\n");
    client.print("Content-Type: application/x-www-form-urlencoded\n");
    client.print("Content-Length: ");
    client.print(postStr.length());
    client.print("\n\n");
    client.print(postStr);
    delay(1000);
  }//end if
  sent++;
  client.stop();
}
#包括
#包括
#包括
#定义myPeriodic 15//秒| Thingspeak pub是15秒
#在arduino引脚2上定义一条与物理板上的D4对应的\u线\u总线2//DS18B20
单线单线(单线总线);
达拉斯温度DS18B20(&oneWire);
浮动温度=0;
const char*server=“172.168.2.143”;
const char*MY_SSID=“AkhuogTkhbbEjhbuvouvr7i2”;
const char*MY_PWD=“2pkmbipsrbeirbp3niag%”;
int sent=0;
无效设置(){
序列号开始(115200);
连接WiFi();
}
void循环(){
浮子温度;
//字符缓冲区[10];
DS18B20.requestTemperations();
temp=DS18B20.getTempCByIndex(0);
//字符串tempC=dtostrf(temp,4,1,buffer);//在sendTemp()中处理
串行打印(字符串(已发送)+“温度:”;
串行打印LN(温度);
发送温度(temp);
int count=myPeriodic;
而(计数--)
延迟(1000);
}
void connectWifi(){
Serial.print(“连接到”+*我的SSID);
WiFi.begin(我的SSID,我的PWD);
while(WiFi.status()!=WL_已连接){
延迟(1000);
连续打印(“.”);
}
Serial.println(“”);
Serial.println(“已连接”);
Serial.println(“”);
}//端接
无效发送温度集(浮动温度){
无线客户端;
if(客户端连接(服务器,80)){
Serial.println(“已连接WiFi客户端”);
字符串postStr=“/php/”;
postStr+=“?temp=”;
postStr+=字符串(临时);
postStr+=“\r\n\r\n”;
client.print(“POST 172.168.2.143/php/collectdata.php HTTP/1.1\n”);
client.print(“主机:122.168.2.143\n”);
client.print(“连接:关闭\n”);
client.print(“内容类型:application/x-www-form-urlencoded\n”);
client.print(“内容长度:”);
client.print(postStr.length());
client.print(“\n\n”);
client.print(postStr);
延迟(1000);
}//如果结束
发送++;
client.stop();
}
PHP代码:

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

在ESP的HTTP请求中,您有:

client.print("POST /php/collectdata.php HTTP/1.1\n");
client.print("Host: 172.168.2.143\n");
122.168.2.143可能输入错误。应为172.168.2.143

另外,您可能希望在HTTP请求行的和处插入\r\n。包括
    #include <ESP8266WiFi.h>
    #include <OneWire.h>
    #include <DallasTemperature.h>
    #include <WiFiClient.h>

    #define myPeriodic 15 //in sec | Thingspeak pub is 15sec
    #define ONE_WIRE_BUS 2  // DS18B20 on arduino pin2 corresponds to D4 on physical board

    OneWire oneWire(ONE_WIRE_BUS);
    DallasTemperature DS18B20(&oneWire);
    float prevTemp = 0;
    const char* server = "172.168.2.143";

    const char* MY_SSID = "AkhuogTkhbbEjhbuvouvr7i2"; 
    const char* MY_PWD = "2pkpmbipsrbeirbp3niag%";
    int sent = 0;

    void setup() {
      Serial.begin(115200);
      connectWifi();
    }

    void loop() {
      float temp;
      //char buffer[10];
      DS18B20.requestTemperatures(); 
      temp = DS18B20.getTempCByIndex(0);
      //String tempC = dtostrf(temp, 4, 1, buffer);//handled in sendTemp()
      Serial.print(String(sent)+" Temperature: ");
      Serial.println(temp);

      //if (temp != prevTemp)
      //{
      //sendTemperatureTS(temp);
      //prevTemp = temp;
      //}

      sendTemperatureTS(temp);
      int count = myPeriodic;
      while(count--)
      delay(1000);
    }

    void connectWifi()
    {
      Serial.print("Connecting to "+*MY_SSID);
      WiFi.begin(MY_SSID, MY_PWD);
      while (WiFi.status() != WL_CONNECTED) {
        delay(1000);
        Serial.print(".");
      }

      Serial.println("");
      Serial.println("Connected");
      Serial.println("");  
    }//end connect

    void sendTemperatureTS(float temp)
    {  
       WiFiClient client;

       if (client.connect(server, 80)) { 
         Serial.println("WiFi Client connected ");

         String url = "/collectdata.php?";
    url += "&temp="; 
    url += temp;
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Server: " + server + "\r\n" + 
               "Connection: close\r\n\r\n");
    }
#包括 #包括 #包括 #定义myPeriodic 15//秒| Thingspeak pub是15秒 #在arduino引脚2上定义一条与物理板上的D4对应的\u线\u总线2//DS18B20 单线单线(单线总线); 达拉斯温度DS18B20(&oneWire); 浮动温度=0; const char*server=“172.168.2.143”; const char*MY_SSID=“AkhuogTkhbbEjhbuvouvr7i2”; const char*MY_PWD=“2pkmbipsrbeirbp3niag%”; int sent=0; 无效设置(){ 序列号开始(115200); 连接WiFi(); } void循环(){ 浮子温度; //字符缓冲区[10]; DS18B20.requestTemperations(); temp=DS18B20.getTempCByIndex(0); //字符串tempC=dtostrf(temp,4,1,buffer);//在sendTemp()中处理 串行打印(字符串(已发送)+“温度:”; 串行打印LN(温度); //如果(温度!=prevTemp) //{ //发送温度(temp); //prevTemp=温度; //} 发送温度(temp); int count=myPeriodic; 而(计数--) 延迟(1000); } void connectWifi() { Serial.print(“连接到”+*我的SSID); WiFi.begin(我的SSID,我的PWD); while(WiFi.status()!=WL_已连接){ 延迟(1000); 连续打印(“.”); } Serial.println(“”); Serial.println(“已连接”); Serial.println(“”); }//端接 无效发送温度集(浮动温度) { 无线客户端; if(client.connect(server,80)){ Serial.println(“已连接WiFi客户端”); 字符串url=“/collectdata.php?”; url+=“&temp=”; url+=temp; client.print(字符串(“GET”)+url+“HTTP/1.1\r\n”+ 服务器:“+Server+”\r\n“+ “连接:关闭\r\n\r\n”); }
您的PHP代码无效。您使用了错误的引号。请查找更好的PHP教程。
mysql.*
API因5.5.0在7.0.0中被删除而不受欢迎。现在也不应该出现这种情况。请重新检查在回答中修改的帖子标题。快速提问。我如何修改html代码以添加用户输入。该输入被分配为如果某个变量超过50,则某些管脚打开。请将答案标记为解决方案,以帮助社区。