I developed a code that counts the number of people passing in front of the infrared sensors.
But I wanted the arduino was a client and the FTP server consume these values. But I do not know php, but it would be something like this. The server is localhost.
#include <UIPEthernet.h>
EthernetServer server = EthernetServer(80);
int sensor1 = 8;
int sensor2 = 9;
unsigned long timeS1 = 0, timeS2 = 0;
unsigned long dif;
int nPessoas = 40;
void setup() {
pinMode(sensor1, INPUT);
pinMode(sensor2, INPUT);
Serial.begin(9600);
uint8_t mac[6] = {0x00,0x01,0x02,0x03,0x04,0x05};
IPAddress myIP(192,168,0,105);
Ethernet.begin(mac,myIP);
server.begin();
}
void loop() {
if (!digitalRead(sensor1)) {
timeS1 = millis();
}
if (!digitalRead(sensor2)) {
timeS2 = millis();
}
dif = timeS2 - timeS1;
dif = timeS2 - timeS1;
if(dif >= 500 && dif <= 1500){
timeS1 = timeS2 = 0;
nPessoas++;
}
size_t size;
if (EthernetClient client = server.available()) {
while((size = client.available()) > 0) {
uint8_t* msg = (uint8_t*)malloc(size);
size = client.read(msg,size);
Serial.write(msg,size);
free(msg);
}
String data = "contador=" + String(nPessoas);
if (client.connect("http://192.168.0.101",80)) {
client.println("POST /test.php HTTP/1.1");
client.println("Host: 192.168.0.101");
client.println("User-Agent: Arduino/1.6");
client.println("Connection: close");
client.println("Content-Type: application/x-www-form-urlencoded;");
client.print("Content-Length: ");
client.println(data.length());
client.println();
client.println(data);
} else{
client.println("Erro de conexao...");
}
if (client.connected()) {
client.stop();
}
}
delay(10);
}
file.php
<?php
$value = ($_REQUEST['contador']);
echo $value;
?>
OUTPUT
intranet
POST /test.php HTTP/1.1
Host: 192.168.0.101
User-Agent: Arduino/1.6
Connection: close
Content-Type: application/x-www-form-urlencoded;
Content-Length: 11
contador=40
localhost
Notice: Undefined index: contador in C:\xampp\htdocs\teste.php on line 2