Skip to content

Instantly share code, notes, and snippets.

@vimarsh244
Last active October 12, 2019 16:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vimarsh244/10332e59a210b5e5a4f29d870b0296c8 to your computer and use it in GitHub Desktop.
Save vimarsh244/10332e59a210b5e5a4f29d870b0296c8 to your computer and use it in GitHub Desktop.
A low power Weather Station that doesn't need to be charged for months
#include <Wire.h>
#include <ESP8266WiFi.h>
String apiKey = "<API_KEY>"; // Enter your Write API key from ThingSpeak
const char *ssid = "ssid"; // replace with your wifi ssid and wpa2 key
const char *pass = "pass";
const char* server = "api.thingspeak.com";
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#define SEALEVELPRESSURE_HPA (1013.25)
Adafruit_BME280 bme; // I2C
WiFiClient client;
void setup()
{
//Serial.begin(115200);
delay(10);
Serial.println("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
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);
}
send_data();
ESP.deepSleep(600e6);
}
void send_data()
{
float h = bme.readHumidity();
float t = bme.readTemperature();
float p = bme.readPressure() / 100.0;
if (isnan(h) || isnan(t) || isnan(p))
{
Serial.println("Failed to read from BME280 sensor!");
return;
}
if (client.connect(server, 80)) // "184.106.153.149" or api.thingspeak.com
{
String postStr = apiKey;
postStr += "&field1=";
postStr += String(t);
postStr += "&field2=";
postStr += String(h);
postStr += "&field3=";
postStr += String(p);
postStr += "\r\n\r\n\r\n";
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("\n\n");
client.print(postStr);
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" degrees Celcius, Humidity: ");
Serial.print(h);
Serial.print(" %. Pressure: ");
Serial.print(p);
Serial.println("mBar. Send to Thingspeak.");
}
client.stop();
Serial.println("Deepsleep...");
Serial.println(millis());
}
void loop(){
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment