从arduino发送的数据未出现在数据库中

时间:2018-11-06 06:50:44

标签: php database arduino

在我的学校项目和本地主机上使用Arduino和PHP。我们遇到了这个问题,我们需要将数据从Arduino的串行监视器发送到数据库。但是,我们在PHP和Arduino方面的编码都没有问题,但是数据本身并未出现在dB中。

这是我们的PHP代码。 该代码用于连接dB并插入数据

<?php
//connecting to database
require('database.php');
$conn = mysqli_connect($servername, $username, $password, $dbname);

if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$firstReading = $_GET['fsrReading'];
/*$secondReading = $_GET['fsrReading1'];
$thirdReading = $_GET['fsrReading2'];
$finalReading = $_GET['finalReading'];*/
$secondReading = 0;<
$thirdReading = 0;
$finalReading = 0;

 //insert Statement
$readings = "INSERT INTO readings
    SET 
    fsrReading = '$firstReading'";

$conn->query($readings);
?>

这是Arduino代码:

#include <SPI.h>
#include <WiFi.h>

char ssid[] = "*****";    //"TP-STUDENT"; //"Hongmi"; //"TP-SURF"; 
//"SINGTEL-14E7"; //  your network SSID (name)
char pass[] =  "******";   //"tB621011"; //"12341234";  //"sotnal88"; 
//"0016651495";    // your network password (use for WPA, or use as key for 
WEP)
int keyIndex = 0;            // your network key Index number (needed only 
for WEP)
int fsrAnalogPin = 0; // FSR is connected to analog 0
/*int fsrAnalogPin1 = 1; // FSR is connected to analog 1
int fsrAnalogPin2 = 2;
int LEDpin = 3;  */    // connect Red LED to pin 11 (PWM pin)
int fsrReading = 0;      // the analog reading from the FSR resistor divider
/*int fsrReading1;
int fsrReading2;
int LEDbrightness;*/
int sensorValue = 0;
/*int finalReading;*/


int status = WL_IDLE_STATUS;
IPAddress server(*,*,*,*);    // name address for Google (using DNS)

WiFiClient client;



 void setup() {
   Serial.begin(9600);
    while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // attempt to connect to Wifi network:
  while (status != WL_CONNECTED) {
  Serial.print("Attempting to connect to SSID: ");
  Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP 
network:
status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
  }
  Serial.println("Connected to wifi");
  //printWifiStatus();

  Serial.println("\nStarting connection to server...");
  // if you get a connection, report back via serial:


  }

void loop(){
  client.stop();
  fsrReading = analogRead(fsrAnalogPin);
  //Serial.print("1st reading = "); 
  Serial.println(fsrReading);

  /*fsrReading1 = analogRead(fsrAnalogPin1);
  Serial.print("2nd reading = ");
  Serial.println(fsrReading1);

  fsrReading2 = analogRead(fsrAnalogPin2);
  Serial.print("3rd reading = ");
  Serial.println(fsrReading2);

  double finalReading =(fsrReading+fsrReading1+fsrReading2);

  Serial.print("Final Reading = ");
  Serial.println(finalReading);

  // we'll need to change the range from the analog reading (0-1023) down to the range
  // used by analogWrite (0-255) with map!
  if(finalReading > 2500){
  LEDbrightness = map(fsrReading, 0, 1023, 0, 255);
  analogWrite(LEDpin, LEDbrightness);
  }    
  else analogWrite(LEDpin,LOW);*/


if (client.connect(server, 80)) {
    Serial.println("connected to server");
    // Make a HTTP request:
    client.print("GET /connect.php?");
    //client.println("GET /connect.php?sensordata=666  HTTP/1.1");
    client.print("fsrReading=");
    client.print(fsrReading);
    client.println(" HTTP/1.1");
    client.println("Host: ******");
    client.println("Connection: close");
    client.println();
    //client.println();
    client.stop();
}
else {
Serial.println("--> connection failed\n");
}
delay(10000);
}

0 个答案:

没有答案