I'm making a small lighthouse (5-6 feet tall). I want to integrate my mailbox, collect and report weather data from the lighthouse, shine a beacon when its gets dark outside. I'm doing it in segments.
Parts: Lolin Wemos D-1 Mini Pro ESP8266, Adafruit BME280
1. I2C: Verify I2C address
/*
Check for I2C connection to Adafruit BME280
Wemos D1 Mini Pro
SCK - I2C clock pin
SDI - I2C data pin
VIN - 3-5V
GND - GND
By default, the I2C address is 0x77
*/
#include <Wire.h>
void setup() {
Serial.begin(115200);
Wire.begin();
Serial.println("\nI2C Scanner");
}
void loop() {
byte error, address;
int nDevices;
Serial.println("Scanning...");
nDevices = 0;
for(address = 1; address < 127; address++ ) {
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0) {
Serial.print("I2C device found at address 0x");
if (address<16) {
Serial.print("0");
}
Serial.print(address,HEX);
Serial.println(" !");
nDevices++;
} else if (error==4) {
Serial.print("Unknow error at address 0x");
if (address<16) {
Serial.print("0");
}
Serial.println(address,HEX);
}
}
if (nDevices == 0) {
Serial.println("No I2C devices found\n");
} else {
Serial.println("done\n");
}
delay(5000);
}
2. Connect to BME, get Temp, Humidity, Barometric readings
/*
WiFi BME280 humidity, temperature & pressure sensor (I2C)
Reports current weather data via Mosquitto MQTT, WiFi, Raspberry Pi3(b)
*/
//#include <EEPROM.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <Wire.h>
//#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
// ASSIGN PINS **************************************************
//#define BME_SCK 5; //
//#define BME_MISO 12//
//#define BME_MOSI 11//
//#define BME_CS 4; //
#define SEALEVELPRESSURE_HPA (1013.25)
Adafruit_BME280 bme; // I2C
//Adafruit_BME280 bme(BME_CS);
// hardware SPI
//Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK);
// Connect to the WiFi
const char* ssid = "MyIPaddress";
const char* password = "MyPassword!";
const char* mqtt_server = "192.168.#.###";
WiFiClient(espClient);
PubSubClient client(espClient);
void reconnect() { // **********************************************************
// Loop until we're reconnected
while (!client.connected()) {
Serial.print(F("Contacting MQTT server..."));
// Attempt to connect
if (client.connect("BME280")) { //assign a "client name". Each wemos must have a unique name
Serial.println(F("connected"));
} else {
Serial.print(F("Failed to connect. "));
Serial.println(F(" Attempting connection again in 3 seconds"));
// Wait 3 seconds before retrying
delay(3000);
}
}
}
void setup() {
{
Serial.begin(115200);
Wire.begin();
client.setServer(mqtt_server, 1883);
}
// Connect to WiFinetwork
Serial.println();
Serial.println();
Serial.print(F("Connecting to "));
Serial.println(ssid);
WiFi.begin(ssid, password);
WiFi.mode(WIFI_STA);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(F("."));
}
Serial.println(F(""));
Serial.println(F("WiFi connected"));
// Print the IP address
Serial.print(F("Local IP: "));
Serial.println(WiFi.localIP());
// *********************************************************************************
{
Serial.println(F("BME280 located in Master Bedroom"));
if (!bme.begin()) {
Serial.println("Could not find a valid BME280 sensor, check wiring!");
while (1)
;
}
}
}
void loop() {
if (!client.connected()) {
reconnect();
}
// **************** CREATE BUFFER TO STORE BINARY DATA ************************
#define INT_STR_SIZE 16
char buffer[INT_STR_SIZE];
// **************** TEMPERATURE (F) ************************
dtostrf(bme.readTemperature() * 9.0 / 5.0 + 32.0, 3, 1, buffer);
Serial.print("Temperature = ");
Serial.print(buffer);
Serial.println(" *F");
client.publish("Office/BME280/TempF", buffer);
// **************** TEMPERATURE (C) ************************
dtostrf(bme.readTemperature(), 3, 0, buffer);
Serial.print("Temperature = ");
Serial.print(buffer);
Serial.println(" *C");
client.publish("Office/BME280/TempC", buffer);
// **************** HUMIDITY *********************
dtostrf(bme.readHumidity(), 3, 0, buffer);
Serial.print("Humidity = ");
Serial.print(buffer);
Serial.println(" %");
client.publish("Office/BME280/Humidity", buffer);
// **************** PRESSURE *********************
dtostrf(bme.readPressure() / 3386.39, 3, 2, buffer);
Serial.print("Pressure = ");
Serial.print(buffer);
Serial.println(" inHg");
client.publish("Office/BME280/Pressure", buffer);
// **************** ALTITUDE *********************
// Serial.print("Approx. Altitude = ");
// Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA)+4);// +4 to correct altitude?
// Serial.println(" m");
Serial.println();
delay(10000);
client.loop();
}
I'll update the rest later. I can't find or don't have a light sensor and I fried my GPS. Waiting on new parts
-
Light sensor for beacon to come on when it gets dark
-
Time: I could go one of 2 ways; RTC or GPS which will give me the current time and Lat/Lon of my location
