Commit 1c062a55 authored by elliot.copeland's avatar elliot.copeland

Add new file

parents
#include <ArduinoBLE.h>
#include <SPI.h>
#include <WiFiNINA.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <dht.h>
#include <ThingSpeak.h>
#define dht_apin A0
BLEService HumidityService("180F");
BLEUnsignedCharCharacteristic HumidityLevelChar("2A19", BLERead | BLENotify);
int oldHumidityLevel = 0;
long previousMillis = 0;
LiquidCrystal_I2C lcd(0x27, 16, 2);
dht DHT;
WiFiClient client;
char ssid[] = "WIFISSID";
char pass[] = "WIFIPASSWORD";
int status = WL_IDLE_STATUS;
unsigned long myChannelNumber = 2402736;
const char * myWriteAPIKey = "5XC5K1SZ0RROOMTI";
void setup() {
lcd.begin();
lcd.backlight();
Serial.begin(9600);
WiFi.begin(ssid, pass);
ThingSpeak.begin(client);
if (!BLE.begin()) {
Serial.println("Starting BLE failed!");
}
BLE.setLocalName("HumidityMonitor");
BLE.setAdvertisedService(HumidityService);
HumidityService.addCharacteristic(HumidityLevelChar);
BLE.addService(HumidityService);
HumidityLevelChar.writeValue(oldHumidityLevel);
BLE.advertise();
Serial.println("Bluetooth device active, waiting for connections");
}
void updateHumidityLevel() {
DHT.read11(dht_apin);
int Humidity = analogRead(A0);
int HumidityLevel = DHT.humidity;
if (HumidityLevel != oldHumidityLevel) {
Serial.print("Humidity % is now: ");
Serial.println(HumidityLevel);
HumidityLevelChar.writeValue(HumidityLevel);
oldHumidityLevel = HumidityLevel;
}
}
void loop() {
BLEDevice central = BLE.central();
DHT.read11(dht_apin);
float h = DHT.humidity;
float t = DHT.temperature;
Serial.print(h);
if (central) {
Serial.print("Connected to central: ");
Serial.println(central.address());
while (central.connected()) {
long currentMillis = millis();
if (currentMillis - previousMillis >= 200) {
previousMillis = currentMillis;
updateHumidityLevel();
}
}
Serial.print("Disconnected from central: ");
Serial.println(central.address());
}
ThingSpeak.setField(1,t);
ThingSpeak.setField(2,h);
ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Humidity: ");
lcd.print(h);
lcd.setCursor(0,1);
lcd.print("Temp: ");
lcd.print(t);
delay(20000);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment