Go Down

Topic: Wifi Anomemeter code check please (Read 559 times) previous topic - next topic

tecjosh

Hi Guys ive got this working with the wind speed in mph via serial but am struggling to work out why the mph int isn't showing via the web browser?

Can you please put me out of my misery? Many thanks

Code: [Select]
#include <ESP8266WiFi.h>
 
const char* ssid = "ssid";
const char* password = "pass";
// NETWORK: Static IP details...
IPAddress ip(192, 168, 8, 98);
IPAddress gateway(192, 168, 8, 1);
IPAddress subnet(255, 255, 255, 0);

// diameter of anemometer
 float diameter = 5.5; //inches from center pin to middle of cup
 float mph;
 
 // read RPM
 int revolutions = 0;
 int rpm = 0;
 unsigned long lastmillis = 0;
  unsigned long fourSecMillis = 0;
WiFiServer server(80);
 
 void setup(){
  Serial.begin(115200);
  delay(10);
 pinMode(10, INPUT_PULLUP);
 attachInterrupt(10, rpm_fan, FALLING);
  // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  // Static IP Setup Info Here...
WiFi.config(ip, gateway, subnet);
 
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
 
  // Start the server
  server.begin();
 // Serial.println("Server started");
 
  // Print the IP address
 /* Serial.print("Use this URL to connect: ");
  Serial.print("http://");
  Serial.print(WiFi.localIP());
  Serial.println("/");
  */

 }

 void loop(){

getrpm;

// Listenning for new clients
  WiFiClient client = server.available();
  
  if (client) {
    Serial.println("New client");
    // bolean to locate when the http request ends
    boolean blank_line = true;
 while (client.connected())
    {
     if (client.available())
      {
        char c = client.read();
        
        if (c == '\n' && blank_line) {
            client.println("HTTP/1.1 200 OK");
            client.println("Content-Type: text/html");
            client.println("Connection: close");
            client.println();
            // your actual web page that displays temperature
            client.println("<!DOCTYPE HTML>");
            client.println("<html>");
           client.println("<meta http-equiv='refresh' content='3'>");
            client.println("<head></head><body><h1>ESP8266 - Wind Speed </h1><h3>Speed in MPH: ");
            client.print(mph);
            client.print("\t MPH=\t"); //print the word "MPH".
            client.print("\t Hz=\t"); //print the word "Hz".
            client.println(rpm); // print the rpm value.    
          break;
        }
        if (c == '\n') {
          // when starts reading a new line
          blank_line = true;
        }
        else if (c != '\r') {
          // when finds a character on the current line
          blank_line = false;
        }
      }
    }  
    // closing the client connection
    delay(1);
    client.stop();
Serial.println("Client disconnected.");
  }




    }
  
 
 // this code will be executed every time the interrupt 0 (pin10) gets low.
 void rpm_fan(){
  revolutions++;
 }

 void getrpm(){
     if (millis() - lastmillis == 1000){ //Update every one second, this will be equal to reading frequency (Hz).
 detachInterrupt(10);//Disable interrupt when calculating
 rpm = revolutions * 60; // Convert frequency to RPM, note: 60 works for one interruption per full rotation. For two interrupts per full rotation use half_revolutions * 30.
Serial.print("RPM =\t"); //print the word "RPM" and tab.
 Serial.print(rpm); // print the rpm value.
 Serial.print("\t MPH=\t"); //print the word "MPH".
 Serial.print(mph);
 Serial.print("\t Hz=\t"); //print the word "Hz".
 Serial.println(revolutions); //print revolutions per second or Hz. And print new line or enter. divide by 2 if 2 interrupts per revolution

 revolutions = 0; // Restart the RPM counter
 lastmillis = millis(); // Update lastmillis
 attachInterrupt(10, rpm_fan, FALLING); //enable interrupt
 mph = diameter / 12 * 3.14 * rpm * 60 / 5280;
 // mph = mph * 3.5; // calibration factor for anemometer accuracy, adjust as necessary
     }
 }

gdsports

To call a function, it must be followed by (). The following does not call getrpm. Add the parentheses and see what happens.

Code: [Select]
void loop(){

  getrpm;

rw950431

#2
Oct 12, 2017, 08:09 am Last Edit: Oct 12, 2017, 08:11 am by rw950431
Your code also never sends the closing </h3></body></html> tags which may be causing some issues for you.

You can examine whats coming in using the developer tools in your browser- this enables you to see whats actually been recieved.

tecjosh

Thanks guys, i've amended both those as suggested.

Here's the serial monitor output.

It looks if once the while loop is entered getrpm function does not get called.

i've tried adding a call to it after the while

still doesn't work... anymore suggestions?

Code: [Select]
while (client.connected())
    {
      getrpm();
     if (client.available())
      {


Quote
.

RPM =   660    MPH=   0.00    Hz=   11
RPM =   960    MPH=   10.79    Hz=   16
RPM =   1020    MPH=   15.70    Hz=   17
RPM =   1260    MPH=   16.68    Hz=   21
RPM =   780    MPH=   20.61    Hz=   13
RPM =   900    MPH=   12.76    Hz=   15
New client
Client disconnected.
New client
Client disconnected.
New client

gdsports

Ident your code so it is readable. In the IDE press Ctrl-T.

This

Code: [Select]
   if (millis() - lastmillis == 1000){

should probably be

Code: [Select]
   if ((millis() - lastmillis) > 1000){

tecjosh

Thanks gdsports! it works perfectly now.

millis was never going to equal exactly 1000. such a simple oversight can cause so many problems.

Go Up