Dear all
i have code as below. This thing tried on below program.
- Individually both functions are working fine
- When i put all together web browser doesn't show any data.
#include <SPI.h>
#include <Ethernet.h>
const int SENSOR_PIN = A0;
const int SAMPLE_COUNT = 10;
const unsigned long SAMPLE_RATE = 1000UL; // every two seconds
int local_day=12;
int local_month=4;
int local_year=2014;
int local_h=12;
int local_m=12;
int local_s=12;
int LED = 3; // led is connected to digital pin 3
int PIR = 2; // PIR sensor is connected to digital pin 2
int LDR = 1; // LDR sensor is connected to analog in 5
int PIRstate = 0; // variable for PIR sensor status
float photocell = 0; // variable for photocell (LDR) analog value
char c = 0; // received data
char command[2] = "\0"; // command
// array to store the samples
float sample[SAMPLE_COUNT];
byte sampleIndex = 0;
float sampleAverage(void);
static int Wind_Exceed_count;
int Stow_flag=0;
float Wind_Speed;
float Wind_Kmph;
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = {
192, 168,1, 120 };
EthernetServer server(80);
float sampleAverage(void)
{
float accumulator = 0;
for (int i = 0; i < SAMPLE_COUNT; i++)
{
accumulator += sample[i];
}
return accumulator / SAMPLE_COUNT;
}
void Wind_calc()
{
static unsigned long lastSampleTime = 0;
unsigned long currentTime = millis();
// check if time for new reading
if (currentTime - lastSampleTime >= SAMPLE_RATE)
{
lastSampleTime = currentTime;
int sensorValue = analogRead(SENSOR_PIN);
float voltage = sensorValue * (5.0 / 1023.0);
sample[sampleIndex] = voltage;
sampleIndex++;
if (sampleIndex == SAMPLE_COUNT)
{
sampleIndex = 0;
}
float average = sampleAverage();
Wind_Speed = (6 * average);
Wind_Kmph = 3.6 * Wind_Speed;
}
}
void setup()
{
pinMode(8,OUTPUT);
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Ethernet.begin(mac, ip);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}
void loop()
{
Wind_calc();
Ethernet_Control();
}
void Ethernet_Control()
{
EthernetClient client = server.available();
// detect if current is the first line
boolean current_line_is_first = true;
if (client) {
// an http request ends with a blank line
boolean current_line_is_blank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
// if we've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so we can send a reply
if (c == '\n' && current_line_is_blank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
// auto reload webpage every 5 second
client.println("<META HTTP-EQUIV=REFRESH CONTENT=5 URL=>");
// webpage title
client.println("<center><p><h1>NORDIC INDIA SOLAR WEB APPPLICATION V1.0</h1></p><center><hr>
");
client.print("<p><h2>DATE: = <font color=indigo>");
client.print(local_day);
client.print("/");
client.print(local_month);
client.print("/");
client.print(local_year);
client.println("</font></h2></p>");
client.print("<p><h2>TIME: = <font color=indigo>");
client.print(local_h);
client.print("/");
client.print(local_m);
client.print("/");
client.print(local_s);
client.println("</font></h2></p>");
client.print(Wind_Speed);
client.println("</font></h2></p>");
client.print("<p><h2>WIND_SPEED KMPH: = <font color=indigo>");
client.print(Wind_Kmph);
client.println("</font></h2></p>");
// read digital pin 13 for the state of PIR sensor
PIRstate = digitalRead(8);
if (PIRstate == HIGH) { // PIR sensor detected movement
client.println("<p><h2><font color=red>Motion Detected!</font></h2></p>");
}
else { // No movement is detected
client.println("<p><h2><font color=green>No Movement</font></h2></p>");
}
// button functions
client.println("<form method=get name=form>");
client.println("<button name=b value=1 type=submit style=height:80px;width:150px>LED On</button>");
client.println("<button name=b value=2 type=submit style=height:80px;width:150px>LED Off</button>");
client.println("</form>
");
// webpage footer
client.println("<hr><center><a href=http://www.robothead2toe.com.my>Robot.Head to Toe</a>
");
client.println("<p>P.S.: This page will automatically refresh every 5 seconds.</p></center>");
break;
}
if (c == '\n') {
// we're starting a new line
current_line_is_first = false;
current_line_is_blank = true;
}
else if (c != '\r') {
// we've gotten a character on the current line
current_line_is_blank = false;
}
// get the first http request
if (current_line_is_first && c == '=') {
for (int i = 0; i < 1; i++) {
c = client.read();
command[i] = c;
}
// LED control
if (!strcmp(command, "1")) {
digitalWrite(LED, HIGH);
}
else if (!strcmp(command, "2")) {
digitalWrite(LED, LOW);
}
}
}
}
// give the web browser time to receive the data
delay(1);
client.stop();
}
}