hi guys
Im trying to build a people counting project based on ultrasonic sensors.
The idea is that the sensor will count and the client can contact the server and retrieve the current amount of people.
Im using Arduino Uno and on top Ethernet board.
I started by writing 2 separate sketches:
- The sensor sketch is working perfectly.
- The server sketch is working fine.
When I combined both sketches to the same project the ultrasonic sensor always returned 0 (zero).
I found that it happens only after I execute the following line related to the EthernetServer:
// The problematic line that kills the ultrasonic distance:
EthernetClient client = server.available();
Im trying to understand why the server and the sensor do not work together.
I believe the code and also the electronics that Im using is very much straightforward and is based on tutorials in this site. In order to avoid ping pong questions I will submit my code and picture of the electronics.
any help will be very much appreciated.
#include <SPI.h>
#include <Ethernet.h>
#define TRIG_PIN 10
#define ECHO_PIN 13
#define TOLERANCE_CM 20
#define ULTRA_SONIC_DELAY 400
float constandDistance;
boolean isMoving = false;
int counter = 0;
long currentMillis = 0;
// the media access control (ethernet hardware) address for the shield
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
//the IP address for the shield:
byte ip[] = {192, 168, 1, 177};
// the router's gateway address:
byte gateway[] = { 10, 0, 0, 1 };
// the subnet:
byte subnet[] = { 255, 255, 0, 0 };
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
void setup() {
// Open serial communications and wait for port to open:
Serial.begin (9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// initialize the ethernet device
Ethernet.begin(mac, ip, gateway, subnet);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
// setup pins for ultrasound
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
// define constand distance
constandDistance = initConstantDistance() - TOLERANCE_CM;
Serial.print("constandDistance=");
Serial.println(constandDistance);
}
/* *******************************
Determine the constant distance to teh wall
*/
float initConstantDistance() {
float distance, avg;
for (int i = 0; i < 5; i++) {
distance = getUltrasonicDistance();
if (i > 0) {
avg = (distance + avg) / 2;
} else {
avg = distance;
}
Serial.print("Distance[");
Serial.print(i);
Serial.print("] =");
Serial.print(distance);
Serial.print(", AVG=");
Serial.print(avg);
Serial.println(" cm");
}
return avg;
}
void loop() {
// listen for incoming clients
EthernetClient client = server.available();
// if (client) {
// writeHTTPResponse(client);
// }
if (millis() - ULTRA_SONIC_DELAY > currentMillis) {
detectUltrasonicMove();
}
}
float getUltrasonicDistance() {
float duration;
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
duration = pulseIn(ECHO_PIN, HIGH);
return ((duration / 2) * 0.0344);
}
void detectUltrasonicMove() {
float distance;
distance = getUltrasonicDistance();
//Serial.print(distance);
if (distance < constandDistance) {
if (isMoving == false) {
counter++;
isMoving = true;
Serial.println("");
Serial.print("Move:");
Serial.println(distance);
} else {
Serial.print("B");
// Serial.println(distance);
}
} else {
isMoving = false;
Serial.print(".");
}
currentMillis = millis();
}
void writeHTTPResponse(EthernetClient client) {
Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close"); // the connection will be closed after completion of the response
//client.println("Refresh: 5"); // refresh the page automatically every 5 sec
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
// output the value of each analog input pin
client.print("Moves=");
client.print(counter);
client.println("
");
client.println("</html>");
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
} else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
Serial.println("client disconnected");
Ethernet.maintain();
}