Created
October 29, 2020 07:02
-
-
Save ghtomcat/fbcb07f9d5c75b409803f39f0a21231b to your computer and use it in GitHub Desktop.
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
/************************************************************************** | |
read bme280 data and display on ssd1306 | |
**************************************************************************/ | |
#include <SPI.h> | |
#include <Wire.h> | |
#include <Adafruit_GFX.h> | |
#include <Adafruit_SSD1306.h> | |
#include <Adafruit_Sensor.h> | |
#include <Adafruit_BME280.h> | |
#include <AS5600.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; | |
int ang, lang = 0; | |
#include <WiFi.h> | |
#include <InfluxDbClient.h> | |
#include <InfluxDbCloud.h> | |
// Replace with your network credentials | |
const char* ssid = "xxxxxx"; | |
const char* password = "yyyyyy"; | |
volatile byte revolutions; | |
// interrupt routine to count revolutions | |
void IRAM_ATTR isr() { | |
revolutions += 1; | |
} | |
// InfluxDB v2 server url, e.g. https://eu-central-1-1.aws.cloud2.influxdata.com (Use: InfluxDB UI -> Load Data -> Client Libraries) | |
#define INFLUXDB_URL "<url>" | |
// InfluxDB v2 server or cloud API authentication token (Use: InfluxDB UI -> Data -> Tokens -> <select token>) | |
#define INFLUXDB_TOKEN "<token>" | |
// InfluxDB v2 organization id (Use: InfluxDB UI -> User -> About -> Common Ids ) | |
#define INFLUXDB_ORG "<org>" | |
// InfluxDB v2 bucket name (Use: InfluxDB UI -> Data -> Buckets) | |
#define INFLUXDB_BUCKET "<bucket>" | |
#define TZ_INFO "CET-1CEST,M3.5.0,M10.5.0/3" | |
// InfluxDB client instance with preconfigured InfluxCloud certificate | |
InfluxDBClient client(INFLUXDB_URL, INFLUXDB_ORG, INFLUXDB_BUCKET, INFLUXDB_TOKEN, InfluxDbCloud2CACert); | |
/* | |
// InfluxDB server url. Don't use localhost, always server name or ip address. | |
// For InfluxDB 1 e.g. http://192.168.1.48:8086 | |
#define INFLUXDB_URL "http://<ip>:8086" | |
// InfluxDB v1 database name | |
#define INFLUXDB_DB_NAME "<dbname>" | |
// InfluxDB v1 credentials | |
#define INFLUXDB_USER "<user>" | |
#define INFLUXDB_PASSWORD "<password>" | |
// InfluxDB client instance for InfluxDB 1 | |
InfluxDBClient client(INFLUXDB_URL, INFLUXDB_DB_NAME); | |
*/ | |
// Data point | |
Point sensor("weather_data"); | |
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 setup() { | |
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 | |
} | |
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); | |
} | |
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); | |
} | |
} | |
// Connect to Wi-Fi network with SSID and password | |
Serial.print("Connecting to "); | |
Serial.println(ssid); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
// Print local IP address | |
Serial.println(""); | |
Serial.println("WiFi connected."); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); | |
// Set InfluxDB 1 authentication params | |
//client.setConnectionParamsV1(INFLUXDB_URL, INFLUXDB_DB_NAME, INFLUXDB_USER, INFLUXDB_PASSWORD); | |
// Add tags | |
sensor.addTag("device", "ESP32"); | |
timeSync(TZ_INFO, "pool.ntp.org", "time.nis.gov"); | |
// Check server connection | |
if (client.validateConnection()) { | |
Serial.print("Connected to InfluxDB: "); | |
Serial.println(client.getServerUrl()); | |
} else { | |
Serial.print("InfluxDB connection failed: "); | |
Serial.println(client.getLastErrorMessage()); | |
} | |
// attach pin 12 to interrupt routine | |
pinMode(12, INPUT); | |
attachInterrupt(12, isr, FALLING); | |
} | |
void loop() { | |
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 10sec to km/h at 16cm diameter | |
display.setCursor(80,30); | |
display.print(" km/h"); | |
display.display(); | |
// Clear fields for reusing the point. Tags will remain untouched | |
sensor.clearFields(); | |
// Store measured value into point | |
sensor.addField("temp", bme.readTemperature()); | |
sensor.addField("pressure", bme.readPressure() / 100.0F); | |
sensor.addField("humidity", bme.readHumidity()); | |
// add and reset wind speed counter | |
sensor.addField("wind_counter", revolutions); | |
revolutions=0; | |
// add wind direction | |
sensor.addField("wind_direction",convertRawAngleToDegrees(ams5600.getRawAngle())); | |
// Print what are we exactly writing | |
Serial.print("Writing: "); | |
Serial.println(sensor.toLineProtocol()); | |
// Write point | |
if (!client.writePoint(sensor)) { | |
Serial.print("InfluxDB write failed: "); | |
Serial.println(client.getLastErrorMessage()); | |
} | |
// wait 10s | |
delay(10000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment