// Create a WiFi access point and provide a web server on it. // To send weather data #include #include #include #include #include #include "I2Cdev.h" #include "MPU6050.h" #include "HMC5883L.h" #include "BMP085.h" #include "Wire.h" /* Set these to your desired credentials. */ const char *ssid = "ESPap"; const char *password = "iot"; #define DHTTYPE DHT11 // DHT type (DHT11, DHT22) #define DHTPIN D7 // DHT Pin #define intPin D6 // interrupt pin volatile int ticks = 0;//see http://arduino.cc/en/Reference/Volatile int rpm = 0; unsigned long lastmillis = 0; float radius= 0.05; // for 10cm pc fan float velocity= 0; float t = 0 ; float h = 0 ; char *dir =""; float heading ; float wind; // create Objects DHT dht(DHTPIN, DHTTYPE); // Magnetometer class default I2C address is 0x1E // specific I2C addresses may be passed as a parameter here HMC5883L mag; int16_t mx, my, mz; // Accel/Gyro class default I2C address is 0x68 (can be 0x69 if AD0 is high) // specific I2C addresses may be passed as a parameter here MPU6050 accelgyro; int16_t ax, ay, az; int16_t gx, gy, gz; // Barometer class default I2C address is 0x77 // specific I2C addresses may be passed as a parameter here // (though the BMP085 supports only one address) BMP085 barometer; float temperature; float pressure; int32_t lastMicros; ESP8266WebServer server(80); String getPage(){ String page = ""; page += ""; page += "ESP8266 Demo - borsaci06.com"; page += ""; page += "

WiFi IOT Terminal

"; page += "

DHT11

"; page += "
  • Temperature : "; page += t; page += "*C
  • "; page += "
  • Humidity : "; page += h; page += "%
"; page += "

WIND

"; page += "
  • Wind : "; page += wind; page += " deg "; page += dir; page += "
  • "; page += velocity; page += " Mt/sec
"; page += "

BMP85

"; page += "
  • Pressure : "; page += (pressure/100); page += " Mbar
"; page += "

 

"; page += "

© Dincer Hepguler, January 2018

"; page += "

borsaci06.com

"; page += ""; return page; } /* Go to http://192.168.4.1 in a web browser * connected to this access point to access esp page. */ void handleRoot() { server.send(200, "text/html", getPage() ); } void handleSubmit(){ server.send(200, "text/html", getPage() ); } void setup() { //delay(1000); Serial.begin(9600); dht.begin(); // join I2C bus (I2Cdev library doesn't do this automatically) Wire.begin(); // ==================== MPU6050 ============================ accelgyro.initialize(); Serial.print("Testing Accel/Gyro... "); Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed"); // Starts up with accel +/- 2 g and gyro +/- 250 deg/s scale accelgyro.setI2CBypassEnabled(true); // set bypass mode // Now we can talk to the HMC5883l mag.initialize(); // verify connection Serial.println("Testing device connections..."); Serial.println(mag.testConnection() ? "HMC5883L connection successful" : "HMC5883L connection failed"); // ==================== BMP085 ============================ barometer.initialize(); Serial.print("Testing Pressure... "); Serial.println(barometer.testConnection() ? "BMP085 connection successful" : "BMP085 connection failed"); Serial.println("Setup Complete"); pinMode(DHTPIN,INPUT_PULLUP); //digitalWrite(DHTPIN,HIGH); pinMode(intPin,INPUT_PULLUP); digitalWrite(intPin,HIGH); //attachInterrupt(digitalPinToInterrupt(intPin), rpm_fan, FALLING); //interrupt is on pin D6 Serial.println(); Serial.print("Configuring access point..."); /* You can remove the password parameter if you want the AP to be open. */ WiFi.softAP(ssid); IPAddress myIP = WiFi.softAPIP(); Serial.print("AP IP address: "); Serial.println(myIP); server.on("/", handleRoot); server.begin(); Serial.println("HTTP server started"); attachInterrupt(digitalPinToInterrupt(intPin), rpm_fan, FALLING); //interrupt is on pin D6 } void loop() { //server.handleClient(); if (millis() - lastmillis == 1000){ //Uptade every one second, equal to reading frequency (Hz) detachInterrupt(digitalPinToInterrupt(intPin)); //Disable interrupt when calculating //frequency to RPM, 1 interrupt per rotation. For 2 interrupts per rotation use ticks * 30. rpm = ticks * 60; velocity = (radius*rpm*0.10472); //rpm to linear velocity calculation Serial.print("RPM="); //print the word "RPM" Serial.print(rpm); // print the rpm value. Serial.print("\t Hiz="); //print the word "Hiz". Serial.print(velocity); // print linear velocity Serial.print(" Mt/sec\t"); t = dht.readTemperature(); h = dht.readHumidity(); static unsigned long ms = 0; // Serial Output Format // Heading | Temp | Pressure | if (millis() - ms > 100) { // read raw heading measurements from device mag.getHeading(&mx, &my, &mz); // To calculate heading in degrees. 0 degree indicates North heading = atan2(my, mx); if(heading < 0) heading += 2 * M_PI; // Convert radians to degrees for readability. wind = heading * 180/M_PI; Serial.print(wind); Serial.print("\t"); if (wind >0 && wind <22) { Serial.print("N \t");dir="N";} if (wind >22 && wind <67){ Serial.print("NE \t");dir="NE";} if (wind >67 && wind <112){ Serial.print("E \t");dir="E";} if (wind >112 && wind <157){ Serial.print("SE \t");dir="SE";} if (wind >157 && wind <202){ Serial.print("S \t");dir="S";} if (wind >202 && wind <247){ Serial.print("SW \t");dir="SW";} if (wind >247 && wind <292){ Serial.print("W \t");dir="W";} if (wind >292 && wind <337){ Serial.print("NW \t");dir="NW";} if (wind >337 && wind <360){ Serial.print("N \t");dir="N";} // request temperature //barometer.setControl(BMP085_MODE_TEMPERATURE); // wait appropriate time for conversion (4.5ms delay) //lastMicros = micros(); //while (micros() - lastMicros < barometer.getMeasureDelayMicroseconds()); // read calibrated temperature value in degrees Celsius //temperature = barometer.getTemperatureC(); // request pressure (3x oversampling mode, high detail, 23.5ms delay) barometer.setControl(BMP085_MODE_PRESSURE_3); while (micros() - lastMicros < barometer.getMeasureDelayMicroseconds()); // read calibrated pressure value in Pascals (Pa) pressure = barometer.getPressure(); // display measured values if appropriate //Serial.print(temperature);Serial.print(" *C");Serial.print("\t"); Serial.print(pressure/100);Serial.print(" mBar");Serial.print("\t"); Serial.print("Humidity: "); Serial.print(h); Serial.print(" %\t"); Serial.print("Temp: "); Serial.print(t); Serial.println(" *C\t"); //Serial.print(" Temp="); //Serial.print(t); //Serial.print(" *C"); //erial.print("\t Humidity="); //Serial.print(h); //Serial.println(" %\t"); //server.handleClient(); server.send ( 200, "text/html", getPage() ); ticks = 0; // Restart the RPM counter lastmillis = millis(); // Uptade lastmillis ms = millis(); } attachInterrupt(digitalPinToInterrupt(intPin), rpm_fan, FALLING); //enable interrupt again } server.handleClient(); //delay(100); } void rpm_fan(){ // executed every time the interrupt 0 (pin2) gets low. ticks++; }