ต้องการให้Arduinoส่งค่าไปยัง database แล้วอ่านค่าที่ database
Code Arduino Mega
[Spoil] คลิกเพื่อดูข้อความที่ซ่อนไว้
#include <Dhcp.h>
#include <Dns.h>
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetClient.h>
#include <EthernetServer.h>
#include <EthernetUdp.h>
#include <SoftwareSerial.h>
IPAddress ip(192, 168, 0, 177);
EthernetClient client;
EthernetServer server(80);
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
//byte server[] = { 107,180,41,159 };
volatile int NbTopsFan;
int Calc=0;
int hallsensor = 2;
volatile int NbTopsFan0;
int Calc0=0;
int hallsensor0 = 3;
volatile int NbTopsFan1;
int Calc1=0;
int hallsensor1 = 4;
volatile int NbTopsFan2;
int Calc2=0;
int hallsensor2 = 5;
long int total;
long int total1=0;
char serverName[] = "www.watersensor.tk";
int val=50;
void rpm ()
{
NbTopsFan++;
}
void rpm0 ()
{
NbTopsFan0++;
}
void rpm1 ()
{
NbTopsFan1++;
}
void rpm2 ()
{
NbTopsFan2++;
}
void add(){
if (client.connect(serverName, 80)) {
Serial.println("connected");
client.print("GET /add.php?");
client.print("main=");
client.print(Calc);
client.print("&&");
client.print("sensor1=");
client.print(Calc0);
client.print("&&");
client.print("sensor2=");
client.print(Calc1);
client.print("&&");
client.print("sensor3=");
client.print(Calc2);
client.print("&&");
client.print("all=");
client.print(total);
client.println(" HTTP/1.0");
client.println("Host: www.watersensor.tk");
client.println( "Connection: close" );
client.println();
client.println();
client.stop();
}else {
Serial.println("connection failed");
}
}
void valu(){
if (client.connect("www.watersensor.tk/get.php", 80)) {
Serial.println("Read");
char c = client.read();
Serial.print(c);
}
client.stop();
}
void setup()
{
pinMode(hallsensor, INPUT);
pinMode(hallsensor0, INPUT);
pinMode(hallsensor1, INPUT);
pinMode(hallsensor2, INPUT);
pinMode(val, OUTPUT);
Serial.begin(9600);
attachInterrupt(2, rpm, RISING);
attachInterrupt(3, rpm0, RISING);
attachInterrupt(4, rpm1, RISING);
attachInterrupt(5, rpm2, RISING);
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
Ethernet.begin(mac, ip);}
//data="";
total1=0;
Serial.println("connecting...");
}
void loop () {
NbTopsFan = 0;
NbTopsFan0 = 0; //Set NbTops to 0 ready for calculations
NbTopsFan1 = 0;
NbTopsFan2 = 0;
sei(); //Enables interrupts
delay (1000); //Wait 1 second
cli(); //Disable interrupts
Calc = (NbTopsFan / 7.5);
Calc0 = (NbTopsFan0 / 7.5); //(Pulse frequency x 60) / 7.5Q, = flow rate
Calc1 = (NbTopsFan1 / 7.5); //Set NbTops to 0 ready for calculations
Calc2 = (NbTopsFan2 / 7.5);
total= (Calc/60)+total1;
add();
valu();
total1=total;
}
Code add.php
[Spoil] คลิกเพื่อดูข้อความที่ซ่อนไว้<?php
include('db.php');
$sensor1 = $_GET['main'];
$sensor2 = $_GET['sensor1'];
$sensor3 = $_GET['sensor2'];
$sensor4 = $_GET['sensor3'];
$all = $_GET['all'];
$query = "SELECT * FROM watersensor order by id DESC LIMIT 1";
$result = mysqli_query($db, $query);
$array = mysqli_fetch_row($result);
//echo "valus =";
//echo $sensor=$_GET['sensor1'];
$sql="INSERT INTO `watersensor`(`date`, `Main`, `Sensor1`, `Sensor2`, `Sensor3`, `total`,`On/Off`) VALUES (NOW(), '".$sensor1."','".$sensor2."','".$sensor3."','".$sensor4."','".$all."','".$array[7]."')";
//echo $_GET['sensor1'];
//echo $sql;
mysqli_query($db,$sql);
header("Location: index.php");
?>
Code get.php
[Spoil] คลิกเพื่อดูข้อความที่ซ่อนไว้
<?php
include("./db.php");
//$result=mysqli_query($db,"select * from waterflow order by id desc");
$query = "SELECT * FROM watersensor order by id DESC LIMIT 1";
$result = mysqli_query($db, $query);
$array = mysqli_fetch_row($result);
echo $array[7];
?>
ผลที่แสดงคือ
[IMG]http://images.temppic.com/29-01-2016/images_vertis/1454069301_0.74635200.png[/IMG]
ผมอยากถามว่าเพราะเหตุใดต้องแก้อย่างไร
ขอความกรุณาผู้รู้มาตอบด้วยครับ (ผมทำมาเป็นเดือนแล้วT_T)
เกี่ยวกับโค้ดArduino+database
Code Arduino Mega
[Spoil] คลิกเพื่อดูข้อความที่ซ่อนไว้
Code add.php
[Spoil] คลิกเพื่อดูข้อความที่ซ่อนไว้
Code get.php
[Spoil] คลิกเพื่อดูข้อความที่ซ่อนไว้
ผลที่แสดงคือ
[IMG]http://images.temppic.com/29-01-2016/images_vertis/1454069301_0.74635200.png[/IMG]
ผมอยากถามว่าเพราะเหตุใดต้องแก้อย่างไร
ขอความกรุณาผู้รู้มาตอบด้วยครับ (ผมทำมาเป็นเดือนแล้วT_T)