Commit ff5a06e9 authored by solidarityK123's avatar solidarityK123

First commit

parents
//This Program was made with help from templates provided by the nesscery libaries that where needed for the sensors and for the wifi to work. These templates are
//Thingspeaks libaries and the write mutiple fields template
//Adafruit BME280 Libery and the BME280test template
//And Finally hp_BH1750 Libary and the bare minimum example
//With these libaries I've been able to make a function program that works with my sensors
//Whilst I understand the code may look simmilar this is because there are many options as sensors have unique liberys meaning that there are specific ways to intalise and use them meaning there is little room for customisation
//Also the header file for BME280 sensor had to be editied to change the address it was looking for to 76 instead of 77
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#define BME_SCK 13
#define BME_MISO 12
#define BME_MOSI 11
#define BME_CS 10
Adafruit_BME280 bme;
#include <hp_BH1750.h>
hp_BH1750 BH1750;
#include <WiFiNINA.h>
#include "secrets.h"
#include "ThingSpeak.h" // always include thingspeak header file after other header files and custom macros
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password
WiFiClient client;
unsigned long ChannelNumber = SECRET_CH_ID;
const char * WriteAPIKey = SECRET_WRITE_APIKEY;
// Initialize our values
//String myStatus = "";
void setup() {
Serial.begin(115200); // Initialize serial
bool avail = BH1750.begin(BH1750_TO_GROUND);// init the sensor with address pin connetcted to ground
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo native USB port only
}
Serial.println(F("BME280 test"));
unsigned status;
status = bme.begin();
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);
}
Serial.println("-- Default Test --");
Serial.println();
// check for the WiFi module:
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Communication with WiFi module failed!");
// don't continue
while (true);
}
String fv = WiFi.firmwareVersion();
if (fv != "1.0.0") {
Serial.println("Please upgrade the firmware");
}
ThingSpeak.begin(client); //Initialize ThingSpeak
}
void loop() {
BH1750.start(); //starts a measurement
float lux=BH1750.getLux(); // waits until a conversion finished
Serial.println(lux);
Serial.println("lx");
float Temp = bme.readTemperature();
Serial.print(Temp);
Serial.println(" *C");
float Pressure = bme.readPressure() / 100.0F;
Serial.print(Pressure);
Serial.println(" hPa");
float Humidity = bme.readHumidity();
Serial.print(Humidity);
Serial.print(" %");
Serial.println();
// Connect or reconnect to WiFi
if(WiFi.status() != WL_CONNECTED){
Serial.print("Attempting to connect to SSID: ");
Serial.println(SECRET_SSID);
while(WiFi.status() != WL_CONNECTED){
WiFi.begin(ssid, pass); // Connect to WPA/WPA2 network. Change this line if using open or WEP network
Serial.print(".");
delay(5000);
}
Serial.println("\nConnected.");
}
// set the fields with the values
ThingSpeak.setField(1, lux);
ThingSpeak.setField(2, Temp);
ThingSpeak.setField(3, Humidity);
ThingSpeak.setField(4, Pressure);
// write to the ThingSpeak channel
int x = ThingSpeak.writeFields(ChannelNumber, WriteAPIKey);
if(x == 200){
Serial.println("Channel update successful.");
}
else{
Serial.println("Problem updating channel. HTTP error code " + String(x));
}
// change the values
delay(20000); // Wait 20 seconds to update the channel again
}
// Use this file to store all of the private credentials
// and connection details
#define SECRET_SSID "TALKTALKB8E57C" // replace MySSID with your WiFi network name
#define SECRET_PASS "3T943PQU" // replace MyPassword with your WiFi password
#define SECRET_CH_ID 1289753 // replace 0000000 with your channel number
#define SECRET_WRITE_APIKEY "EBHU1HP2RP11BI5K" // replace XYZ with your channel write API Key
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