SparkFun Forums 

Where electronics enthusiasts find answers.

For the discussion of Arduino related topics.
By GIM42
#193467
I've been successful in getting a bme280 and ethernet shield to work together. I'm new to arduino and programming, but I am having problems with the anemometer. Wind speed is showing as 0.00 mph. The counter function seems to be working properly. If I use a non-ethernet version of the anemometer and bme280, everything works fine. Any guidance would be appreciated. Here's the code:
Code: Select all
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>

#define BME_SCK 13
#define BME_MISO 12
#define BME_MOSI 11
#define BME_CS 10

#define SEALEVELPRESSURE_HPA (1013.25)

#define uint unsigned int
#define ulong unsigned long
#define PIN_ANEMOMETER 2       //The pin location of the anemometer sensor
#define MSECS_CALC_WIND_SPEED 5000
volatile int numRevsAnemometer = 0;  // cup rotation counter used in interrupt routine
ulong nextCalcSpeed;  // Timer to avoid contact bounce in interrupt routine
ulong time;


Adafruit_BME280 bme; // I2C
//Adafruit_BME280 bme(BME_CS); // hardware SPI
//Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO,  BME_SCK);



#include <Wire.h>          // used to communicate via the I2C bus
#include <SPI.h>           // used to communicated via the spi bus
#include <Ethernet2.h>      // used to communicate with the ethernet controller





// Here we setup the webserver. We need to supply a mac address. Some ethernet boards have a label attached
// with a mac address you can use.
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 45);      // IP address, may need to change depending on network
EthernetServer server(80);           // create a server at port 80

void setup() {
  
  // disable the SD card by switching pin 4 high
  pinMode(4, OUTPUT);
  digitalWrite(4, HIGH);
  
  // activate the BME280 sensor
  if(!bme.begin()) {
      Serial.println("Could not find the BME280 Sensor, check wiring"); 
      while (1);
  }

  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
  
  Serial.begin(9600);
  Serial.println("cactus.io | Webserver Tutorial");
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
  
 pinMode(PIN_ANEMOMETER, INPUT_PULLUP);
  digitalWrite(PIN_ANEMOMETER, HIGH);
  attachInterrupt(0, countAnemometer, FALLING);
  nextCalcSpeed = millis() + MSECS_CALC_WIND_SPEED;
  
}



void windLoopCode() {

   time = millis();

   if (time >= nextCalcSpeed) {
       calcWindSpeed();
       nextCalcSpeed = time + MSECS_CALC_WIND_SPEED;
   }

 

}

void countAnemometer() {
  numRevsAnemometer++;
  }

void calcWindSpeed() {
  int x, iSpeed;
  long speed =14920;
  speed *= numRevsAnemometer;
  speed /= MSECS_CALC_WIND_SPEED;
  iSpeed = speed;
 
  int x2 =iSpeed * 1.085 /10;
 Serial.print( x);
  Serial.print('.');
  x = iSpeed % 10;
  Serial.print(x2);
  numRevsAnemometer = 0; 
}
int x;
void loop() {

   windLoopCode(); 
  // 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) {
             // 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>");
             client.println("<body>");
             // output the value from the BME280 temperature sensor
             client.print("Temperature = ");    
             client.print(bme.readTemperature() * 9.0 / 5.0 + 32 - 4.0);
             client.println(" *F");
             client.println("<br><br>");            
             client.print("Humidity = ");    
             client.print(bme.readHumidity());
             client.println(" %");
             client.println("<br><br>");
             client.print("Pressure = ");    
             client.print(bme.readPressure() /100.0F );
             client.println(" hPa");
             client.println("<br><br>");
             client.println("Wind Speed =");
             client.print(x);
             client.println("<br><br>");
             client.println("</body>");
             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");

}