Skip to content

Instantly share code, notes, and snippets.

@ghtomcat
Last active March 16, 2021 19:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ghtomcat/9bd36816278b501df37719522af1bc1b to your computer and use it in GitHub Desktop.
Save ghtomcat/9bd36816278b501df37719522af1bc1b to your computer and use it in GitHub Desktop.
/**************************************************************************
read bme280 data and display on ssd1306
**************************************************************************/
#include <SPI.h>
#include <Wire.h>
#include <WiFi.h>
#include <AS5600.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
Adafruit_BME280 bme; // I2C
AMS_5600 ams5600;
volatile byte revolutions;
int ang;
// interrupt routine to count revolutions
void IRAM_ATTR isr() {
revolutions += 1;
}
// Replace with your network credentials
const char* ssid = "wetterstation";
// Set web server port number to 80
WiFiServer server(80);
// Variable to store the HTTP request
String header;
unsigned long lastTime = 0;
unsigned long timerDelay = 10000; // sensors are checked every 10s
void setup() {
Wire.begin();
Serial.begin(115200);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x64
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
if(ams5600.detectMagnet() == 0 ){
while(1){
if(ams5600.detectMagnet() == 1 ){
Serial.print("Current Magnitude: ");
Serial.println(ams5600.getMagnitude());
break;
}
else{
Serial.println("Can not detect magnet");
}
delay(1000);
}
}
unsigned status;
// default settings
status = bme.begin(0x76);
if (!status) {
Serial.println("Could not find a valid BME280 sensor, check wiring, address, sensor ID!");
Serial.print("SensorID was: 0x"); Serial.println(bme.sensorID(),16);
Serial.print(" ID of 0xFF probably means a bad address, a BMP 180 or BMP 085\n");
Serial.print(" ID of 0x56-0x58 represents a BMP 280,\n");
Serial.print(" ID of 0x60 represents a BME 280.\n");
Serial.print(" ID of 0x61 represents a BME 680.\n");
while (1) delay(10);
}
// Create Wi-Fi network with SSID and password
Serial.print("Setting AP (Access Point)…");
// Remove the password parameter, if you want the AP (Access Point) to be open
WiFi.softAP(ssid);
IPAddress IP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(IP);
server.begin();
// attach pin 12 to interrupt routine
pinMode(12, INPUT);
attachInterrupt(12, isr, FALLING);
}
/*******************************************************
/* Function: convertRawAngleToDegrees
/* In: angle data from AMS_5600::getRawAngle
/* Out: human readable degrees as float
/* Description: takes the raw angle and calculates
/* float value in degrees.
/*******************************************************/
float convertRawAngleToDegrees(word newAngle)
{
/* Raw data reports 0 - 4095 segments, which is 0.087 of a degree */
float retVal = newAngle * 0.087;
ang = retVal;
return ang;
}
void loop() {
// get data every timerDelay ms
if ((millis() - lastTime) > timerDelay) {
display.clearDisplay();
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(0,0);
display.print("Temperatur: ");
display.setCursor(70,0);
display.print(bme.readTemperature());
display.setCursor(100,0);
display.print(" *C");
display.setCursor(0,10);
display.print("Luftdruck: ");
display.setCursor(65,10);
display.print(bme.readPressure() / 100.0F);
display.setCursor(100,10);
display.print(" hPa");
display.setCursor(0,20);
display.print("Feuchtigkeit: ");
display.setCursor(80,20);
display.print(bme.readHumidity());
display.setCursor(110,20);
display.print(" %");
display.setCursor(0,30);
display.print("Wind: ");
display.setCursor(50,30);
display.print(revolutions*6*16*0.001885); // revolutions in 60sec to km/h at 16cm diameter
display.setCursor(80,30);
display.print(" km/h");
display.setCursor(0,40);
display.print("Wind: ");
display.setCursor(50,40);
display.print(convertRawAngleToDegrees(ams5600.getRawAngle()));
display.setCursor(80,40);
display.print(" Grad");
display.display();
lastTime = millis();
revolutions=0;
}
WiFiClient client = server.available(); // Listen for incoming clients
if (client) { // If a new client connects,
Serial.println("New Client."); // print a message out in the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
header += c;
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();
// Display the HTML web page
client.println("<!DOCTYPE html><html>");
client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
client.println("<link rel=\"icon\" href=\"data:,\">");
// CSS to style the table
client.println("<style>body { text-align: center; font-family: \"Trebuchet MS\", Arial;}");
client.println("table { border-collapse: collapse; width:35%; margin-left:auto; margin-right:auto; }");
client.println("th { padding: 12px; background-color: #0043af; color: white; }");
client.println("tr { border: 1px solid #ddd; padding: 12px; }");
client.println("tr:hover { background-color: #bcbcbc; }");
client.println("td { border: none; padding: 12px; }");
client.println(".sensor { color:white; font-weight: bold; background-color: #bcbcbc; padding: 1px; }");
// Web Page Heading
client.println("</style></head><body><h1>Wetterstation</h1>");
client.println("<table><tr><th>Messpunkt</th><th>Messwert</th></tr>");
client.println("<tr><td>Temperatur</td><td><span class=\"sensor\">");
client.println(bme.readTemperature());
client.println(" *C</span></td></tr>");
client.println("<tr><td>Luftfeuchtigkeit</td><td><span class=\"sensor\">");
client.println(bme.readHumidity());
client.println(" %</span></td></tr>");
client.println("<tr><td>Luftdruck</td><td><span class=\"sensor\">");
client.println(bme.readPressure() / 100.0F);
client.println(" hPa</span></td></tr>");
client.println("<tr><td>Windgeschwindigkeit</td><td><span class=\"sensor\">");
client.println(revolutions*6*16*0.001885);
client.println(" km/h</span></td></tr>");
client.println("<tr><td>Windrichtung</td><td><span class=\"sensor\">");
client.println(convertRawAngleToDegrees(ams5600.getRawAngle()));
client.println(" Grad</span></td></tr>");
client.println("</body></html>");
// The HTTP response ends with another blank line
client.println();
// Break out of the while loop
break;
} else { // if you got a newline, then clear currentLine
currentLine = "";
}
} else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
}
}
// Clear the header variable
header = "";
// Close the connection
client.stop();
Serial.println("Client disconnected.");
Serial.println("");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment