-2

I'm currently having issues sending data collected from a temperature sensor to my Raspberry Pi. The Pi says it hasn't received any data. I'm also not sure how GET and POST work. I'm fairly new to this stuff so any help would be much appreciated.

Also can someone review my sendTemperatureTS method and my PHP code because as I said I'm new to this. By the way I created my PHP file in a PHP folder in /var/www in the Raspberry Pi. I used the command sudo nano collectdata.php in the PHP folder to write my code in.

Arduino code for NodeMCU:

#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();
}

PHP code:

<?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);

?>

dda
  • 6,030
  • 2
  • 25
  • 34
heyyy
  • 29
  • 4
  • Your PHP code is invalid. You are using the wrong quotes. And find a better PHP tutorial. `mysql_*` API is deprecated since 5.5.0 an removed in 7.0.0. – gre_gor Apr 18 '17 at 13:35
  • Also [SQL injection](http://bobby-tables.com/) shouldn't be a thing nowadays. – gre_gor Apr 18 '17 at 14:04

2 Answers2

0

In the ESP's HTTP request you have :

client.print("POST /php/collectdata.php HTTP/1.1\n");
client.print("Host: 172.168.2.143\n");

122.168.2.143 is wrongly typed possible. Should be 172.168.2.143.

Plus, you may want to insert \r\n at the and of the HTTP request lines.

cagdas
  • 1,634
  • 2
  • 14
  • 27
0
    #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");
    }
Johny M
  • 1
  • 2