Created
June 19, 2017 13:29
-
-
Save honestcomrade/98910bea91d4042b9632b694ee80e7c2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Wire.h> | |
#include <Time.h> | |
#include <SPI.h> | |
#include <Adafruit_Sensor.h> | |
#include <Adafruit_BME280.h> | |
#include <ESP8266WiFi.h> | |
#include <DS1302.h> // rst - pin 12 // io - pin 13 // scl - pin 14 | |
#define BME_SCK 13 | |
#define BME_MISO 12 | |
#define BME_MOSI 11 | |
#define BME_CS 10 | |
#define SEALEVELPRESSURE_HPA (1013.25) | |
void send_data(float, float, float); | |
void print_sensors(); | |
double convert_F (double); | |
void print_time(); | |
uint8_t RST_PIN = 12; // RST pin attach to | |
uint8_t SDA_PIN = 13; // IO pin attach to | |
uint8_t SCL_PIN = 14; // clk Pin attach to | |
DS1302 rtc(RST_PIN, SDA_PIN, SCL_PIN); // create a variable type of DS1302 | |
Adafruit_BME280 bme; // I2C | |
WiFiClient client; | |
char buf[50]; | |
char day[10]; | |
const char* ssid = "Casetta"; | |
const char* password = "11OctopiArms"; | |
const char* server = "api.thingspeak.com"; | |
String apiKey = "1H7DXSXHC280CSGJ"; | |
double tempF = 0; | |
double pressure = 0; | |
double humidity = 0; | |
bool status = 0; | |
unsigned long delayTime; | |
void setup() { | |
Serial.begin(115200); | |
rtc.writeProtect(false); | |
rtc.halt(false); | |
// Make a new time object to set the date and time. | |
Time t(2017, 6, 17, 9, 38, 50, Time::kSunday); | |
// Set the time and date on the chip. | |
rtc.time(t); | |
print_time(); | |
// default settings | |
status = bme.begin(); | |
if (!status) { | |
Serial.println("Could not find a valid BME280 sensor, check wiring!"); | |
while (1); | |
} | |
Serial.println("-- Default Test --"); | |
delayTime = 10000; // 10 seconds | |
Serial.println(); | |
Serial.println(); | |
Serial.println(); | |
Serial.print("Connecting to "); | |
Serial.println(ssid); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); | |
delay(1000); | |
} | |
void loop() { | |
delayTime = 3000; | |
tempF = convert_F(bme.readTemperature()); | |
pressure = (bme.readPressure() / 1000.0F);//kPa | |
humidity = bme.readHumidity(); | |
send_data(tempF, humidity, pressure); | |
print_sensors(); | |
print_time(); | |
Serial.println(); | |
delay(delayTime); | |
} | |
void send_data(float temp1, float humid, float pressu) { | |
if (client.connect(server, 80)) { | |
String postStr = apiKey; | |
postStr += "&field1="; | |
postStr += String(temp1); | |
postStr += "&field2="; | |
postStr += String(humid); | |
postStr += "&field3="; | |
postStr += String(pressu); | |
postStr += "\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.println("BME280 Temperature: "); | |
Serial.print(temp1); | |
Serial.print(" degrees Fahrenheit, Humidity: "); | |
Serial.print(humid); | |
Serial.print(" %, & Pressure: "); | |
Serial.print(pressu); | |
Serial.println(" hPa sent to Thingspeak"); | |
Serial.println(""); | |
} | |
else { | |
Serial.println("Connection error"); | |
} | |
client.stop(); | |
} | |
void print_time() { | |
/* Get the current time and date from the chip */ | |
Time t = rtc.time(); | |
/* Name the day of the week */ | |
memset(day, 0, sizeof(day)); | |
switch (t.day) { | |
case 1: | |
strcpy(day, "Sun"); | |
break; | |
case 2: | |
strcpy(day, "Mon"); | |
break; | |
case 3: | |
strcpy(day, "Tue"); | |
break; | |
case 4: | |
strcpy(day, "Wed"); | |
break; | |
case 5: | |
strcpy(day, "Thu"); | |
break; | |
case 6: | |
strcpy(day, "Fri"); | |
break; | |
case 7: | |
strcpy(day, "Sat"); | |
break; | |
} | |
/* Format the time and date and insert into the temporary buffer */ | |
snprintf(buf, sizeof(buf), "%s %04d-%02d-%02d %02d:%02d:%02d", day, t.yr, t.mon, t.date, t.hr, t.min, t.sec); | |
/* Print the formatted string to serial so we can see the time */ | |
Serial.println(buf); | |
} | |
void print_sensors() { | |
Serial.print("Temperature(F) = "); | |
Serial.print(convert_F(bme.readTemperature())); | |
Serial.println(" *F"); | |
Serial.print("Pressure = "); | |
Serial.print(bme.readPressure() / 100.0F); | |
Serial.println(" hPa"); | |
Serial.print("Approx. Altitude = "); | |
Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA)); | |
Serial.println(" m"); | |
Serial.print("Humidity = "); | |
Serial.print(bme.readHumidity()); | |
Serial.println(" %"); | |
Serial.println(); | |
} | |
double convert_F (double c) { | |
double fahr; | |
fahr = (c * 9.0) / 5.0 + 32; | |
return fahr; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment