#include <Wire.h>
#include <Adafruit_BMP085.h>
#define SEA_LEVEL_PRESSURE_SANTOS 101500
#include "secrets.h"
#include "ThingSpeak.h" // always include thingspeak header file after other header files and custom macros
Adafruit_BMP085 bmp;
char ssid[] = "Wokwi-GUEST"; // your network SSID (name)
char pass[] = ""; // your network password
int keyIndex = 0; // your network key Index number (needed only for WEP)
WiFiClient client;
unsigned long myChannelNumber = 2966573;
const char * myWriteAPIKey = "7M28RYM21PIEI3JZ";
int number = 0;
void setup() {
Serial.begin(115200);
if (!bmp.begin()) {
Serial.println("Could not find a valid BMP085/BMP180 sensor, check wiring!");
while (1) {}
}
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client); // Initialize ThingSpeak
}
void loop() {
// 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.");
}
Serial.print("Temperature = ");
Serial.print(bmp.readTemperature());
Serial.println(" *C");
Serial.print("Pressure = ");
Serial.print(bmp.readPressure());
Serial.println(" Pa");
// Calculate altitude assuming 'standard' barometric
// pressure of 1013.25 millibar = 101325 Pascal
Serial.print("Altitude = ");
Serial.print(bmp.readAltitude());
Serial.println(" meters");
Serial.print("Pressure at sealevel (calculated) = ");
Serial.print(bmp.readSealevelPressure());
Serial.println(" Pa");
// you can get a more precise measurement of altitude
// if you know the current sea level pressure which will
// vary with weather and such. If it is 1015 millibars
// that is equal to 101500 Pascals.
Serial.print("Real altitude = ");
Serial.print(bmp.readAltitude(SEA_LEVEL_PRESSURE_SANTOS));
Serial.println(" meters");
Serial.println();
delay(500);
/*
// Arrumar comunicação com thingspeak
ThingSpeak.setField(1, data.temperature);
ThingSpeak.setField(2, data.humidity);
int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if (x == 200) {
Serial.println("Channel update successful.");
} else {
Serial.println("Problem updating channel. HTTP error code " + String(x));
}
delay(20000);
*/
}