This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ESP8266WiFi.h> | |
#include "secrets.h" | |
#include <math.h> | |
#define WindSensorPin 4 // The pin location of the anemometer sensor | |
volatile unsigned long Rotations; // cup rotation counter used in interrupt routine | |
volatile unsigned long ContactBounceTime; // Timer to avoid contact bounce in interrupt routine | |
float WindSpeed; // speed miles per hour | |
#include "ThingSpeak.h" // always include thingspeak header file after other header files and custom macros | |
char ssid[] = "YOUR SSID"; // your network SSID (name) | |
char pass[] = "YOUR PASS"; // your network password | |
int keyIndex = 0; // your network key Index number (needed only for WEP) | |
WiFiClient client; | |
unsigned long myChannelNumber = CHANNEL; | |
const char * myWriteAPIKey = "API_KEY"; | |
bool incrementRotation = false; | |
// This is the function that the interrupt calls to increment the rotation count | |
ICACHE_RAM_ATTR void isr_rotation () { | |
incrementRotation = true; | |
Serial.print("ISR"); | |
} | |
void setup() { | |
Serial.begin(9600); // Initialize serial | |
pinMode(WindSensorPin, INPUT); | |
attachInterrupt(digitalPinToInterrupt(WindSensorPin), isr_rotation, FALLING); | |
WiFi.mode(WIFI_STA); | |
ThingSpeak.begin(client); // Initialize ThingSpeak | |
} | |
//pinMode(WindSensorPin,INPUT); | |
//attachInterrupt(digitalPinToInterrupt(WindSensorPin), isr_rotation, FALLING); | |
// Serial.println("Rotations\tMPH"); //use for debug | |
void loop() { | |
// Connect or reconnect to WiFi | |
if (WiFi.status() != WL_CONNECTED) { | |
Serial.print("Attempting to connect to SSID: "); | |
Serial.println(SECRET_SSID); | |
while (WiFi.status() != WL_CONNECTED) { | |
WiFi.begin(ssid, pass); // Connect to WPA/WPA2 network. Change this line if using open or WEP network | |
Serial.print("."); | |
delay(5000); | |
} | |
Serial.println("\nConnected."); | |
} | |
Rotations = 0; // Set Rotations count to 0 ready for calculations | |
//sei(); // Enables interrupts | |
//delay (3000); // Wait 3 seconds to average | |
//cli(); // Disable interrupts | |
// convert to mp/h using the formula V=P(2.25/T) | |
// V = P(2.25/3) = P * 0.75 | |
if (incrementRotation){ | |
if ((millis() - ContactBounceTime) > 15 ) { // debounce the switch contact. | |
Rotations++; | |
ContactBounceTime = millis(); | |
// Write to ThingSpeak. There are up to 8 fields in a channel, allowing you to store up to 8 different | |
// pieces of information in a channel. Here, we write to field 1. | |
WindSpeed = Rotations * 0.75; | |
int x = ThingSpeak.writeField(myChannelNumber, 1, WindSpeed, myWriteAPIKey); | |
if (x == 200) { | |
Serial.println("Channel update successful."); | |
} | |
else { | |
Serial.println("Problem updating channel. HTTP error code " + String(x)); | |
} | |
incrementRotation = false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment