Arduino Windsensor with WebServer to pull values

Hi there,

I want to build an Arduino Windsensor combined with a WebServer which shows the measured values.
Here is my Code.
(I can’t find the error right now)

#include <SPI.h>
#include <Ethernet.h>
#include <Time.h>
#include <TimeLib.h>
#define DELAY 1000
#define SENSORID 2
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac = { }; //MAC-ID, steht auf den Ethershield
IPAddress ip(192,168,2,36);
String cmd="";
int cmd_length=0;
unsigned long last_upload=0;
float current_speed=0.0f;
float accum_speed;
unsigned long sum_pulses=0;
unsigned long sum_delay=0;
unsigned long samples;
float current_direction=0.0f;
unsigned long pulses = 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() {
// start the serial library:
Serial.begin(9600);
Serial.println(“Hello”);
pinMode(4,OUTPUT);
digitalWrite(4,HIGH);
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
Serial.print(“server is at “);
Serial.println(Ethernet.localIP());
}
void windpulscallback() {
// latest_interrupted_pin=PCintPort::arduinoPin;
// interrupt_count[latest_interrupted_pin]++;
pulses++;
}
void calculateSpeed() {
unsigned long start;
unsigned long end;
unsigned long diff;
// Vorbereitung
// PCintPort::attachInterrupt(WINDSPEEDPIN, &windpulscallback, RISING); // add more attachInterrupt code as required
attachInterrupt(1, windpulscallback, RISING);
// Messung starten (5 Sekunden)
pulses=0;
start=micros();
delay(DELAY); // in milliseconds
end=micros();
// shutdown
// PCintPort::detachInterrupt(WINDSPEEDPIN);
detachInterrupt(1);
// Berechnung
diff=end-start;
Serial.print(“Time(in microsec):”);
Serial.println(diff);
Serial.print(“Pulses:”);
Serial.println(pulses);
sum_pulses+=pulses;
sum_delay+=diff;

current_speed=pulses1000.0fDELAY/6.0f/diff;
Serial.print(“Speed:”);
Serial.println(current_speed);
}
void loop(){
// listen for incoming clients
EthernetClient client = server.available();
if (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) {
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;
if (c==’?’){
Serial.print(“Commando ? gesendet\n\n”);
while (c!=’\r’)
{
cmd+=c;
// Serial.print(c);
c=client.read();
}
Serial.println(cmd);
if (cmd.startsWith(”?cmd”)) {
Serial.println(“Kommando erkannt”);
cmd_length=cmd.length();
Serial.println (cmd_length);
if (cmd.startsWith("?cmd=read_t0"))
Serial.println(“Kommando lese T0”);

client.print(current_speed);
cmd="";
}
else{
Serial.println(“kein gueltiges Kommando erkannt”);
client.println(“Nicht gültig”);
cmd="";
}


}
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
Serial.println(“client disonnected”);
}
}

calculateSpeed();
accum_speed+=current_speed;
samples++;
}

When I access the URL I can’t get back the actual values of the wind speed.
What is wrong here …?!

Regards,
Alex

Please comment your code a little to make it easier to read and focus the question more.

Do the calculations work? Is it just a transmitting problem? Or is the reading a problem?

If you help us to understand you more we can help you aswell :wink:

Best regards
MajorProb