wirte and explain arduino code. get outside temp and presure. send to arduino cloud.…
- User
wirte and explain arduino code. get outside temp and presure. send to arduino cloud. make 7 day chart of theese 2 values in arduino cloud network.
Assistant
wirte and explain arduino code. get outside temp and presure. send to arduino cloud. make 7 day chart of theese 2 values in arduino cloud network.
To accomplish the task of reading outside temperature and pressure, sending this data to the Arduino Cloud, and then creating a 7-day chart of these two values within the Arduino Cloud, we will break down the process into several steps. We'll start by explaining the required hardware and then dive into the Arduino code and the setup in the Arduino Cloud.
First, let's write the Arduino code to read temperature and pressure from the BME280 sensor and send this data to the Arduino Cloud.
#include <Arduino_ConnectionHandler.h>
#include <ArduinoIoTCloud.h>
#include <Arduino_BuiltIn.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
// Define the BME280 sensor object
Adafruit_BME280 bme;
WiFiConnectionHandler ArduinoIoTPreferredConnection("YourSSID", "YourPassword");
float temperature;
float pressure;
void initProperties(){
ArduinoCloud.setBoardId("YOUR_BOARD_ID");
ArduinoCloud.setSecretDeviceKey("YOUR_DEVICE_KEY");
ArduinoCloud.setThingId("YOUR_THING_ID");
ArduinoCloud.addProperty(temperature, READ, ON_CHANGE, NULL);
ArduinoCloud.addProperty(pressure, READ, ON_CHANGE, NULL);
}
void setup() {
Serial.begin(9600);
// Wait for serial monitor to open
delay(1500);
// Initialize BME280 sensor
if (!bme.begin(0x76)) {
Serial.println("Could not find a valid BME280 sensor, check wiring!");
while (1);
}
// Defined in thingProperties.h
initProperties();
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
// Set a callback for when the connection is established
ArduinoCloud.addCallback(ArduinoIoTCloudEvent::CONNECT, onCloudConnect);
}
void loop() {
ArduinoCloud.update();
// Read temperature and pressure
temperature = bme.readTemperature();
pressure = bme.readPressure() / 100.0F; // Convert Pa to hPa
Serial.print("Temperature = ");
Serial.print(temperature);
Serial.println(" *C");
Serial.print("Pressure = ");
Serial.print(pressure);
Serial.println(" hPa");
delay(15000); // Wait 15 seconds before sending the next update
}
void onCloudConnect() {
Serial.println("Connected to Arduino IoT Cloud");
}
temperature and pressure, both of type float.initProperties() function is automatically generated when you create a Thing and download the sketch from the Arduino Cloud.temperature and pressure as the properties to display.After completing these steps, your Arduino board should be sending temperature and pressure data to the Arduino Cloud, and you should be able to see a 7-day chart of these values in your Dashboard. Make sure your Arduino board is connected to the internet and that the BME280 sensor is properly connected and configured.