report the current weather

master v0.0.5
Kenneth Barbour 2020-01-24 10:49:23 -05:00
parent 33cc34f155
commit 63a8bce948
5 changed files with 50 additions and 1 deletions

View File

@ -16,5 +16,8 @@ upload_speed = 921600
build_flags =
!bin/version.sh
extra_scripts = pre:bin/firmware_rename.py
lib_deps = https://github.com/kenbarbour/HttpServer
lib_deps =
https://github.com/kenbarbour/HttpServer
Adafruit Unified Sensor@^1.1.1
Adafruit BME280 Library@^2.0.1

11
src/bme280.cpp 100644
View File

@ -0,0 +1,11 @@
#include "bme280.h"
#include <Arduino.h>
Adafruit_BME280 BME;
void bme280_init()
{
if (!BME.begin(0x76)) {
Serial.println("Could not find a valid BME280 sensor");
}
}

13
src/bme280.h 100644
View File

@ -0,0 +1,13 @@
#ifndef _UWEATHER_BME280_H_
#define _UWEATHER_BME280_H_
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
extern Adafruit_BME280 BME;
void bme280_init();
#endif /** _UWEATHER_BME280_H_ include guard */

View File

@ -4,6 +4,7 @@
#include "version.h"
#include "web.h"
#include "update.h"
#include "bme280.h"
#ifndef SERIAL_BAUD
#define SERIAL_BAUD 74880
@ -47,6 +48,7 @@ void setup()
Serial.println(WiFi.localIP());
connected = true;
}
const char* softApSSID = unique_softap_ssid();
WiFi.softAP(softApSSID);
Serial.printf_P( PSTR("Created Soft AP for setup: %s\r\n"), softApSSID);
//TODO: turn off SoftAP later if connected
@ -61,6 +63,9 @@ void setup()
//uweather_web_shutdown();
}
//
bme280_init();
uweather_update_init();
}

View File

@ -1,4 +1,5 @@
#include "web.h"
#include "bme280.h"
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <FS.h>
@ -66,10 +67,25 @@ void handle_setup(HttpRequest& req, HttpResponse& res)
void handle_firmware(HttpRequest& req, HttpResponse& res)
{
if (req.method[0] != 'G') { // Methods not implemented
res.code = 501;
return;
}
res.headers.set("Content-Type", "text/html");
serve_static("/w/firmware.html", res);
}
void handle_weather_current(HttpRequest& req, HttpResponse& res)
{
char buff[3][10] = {};
dtostrf(BME.readTemperature(), 2, 1, buff[0]);
dtostrf(BME.readPressure(), 2, 1, buff[1]);
dtostrf(BME.readHumidity(), 2, 1, buff[2]);
Serial.printf("temp= %s, pres= %s, hum=%s\r\n", buff[0], buff[1], buff[2]);
res.headers.set("Content-Type", "application/json");
content.printf("{\"time\": null, \"temperature\": %s, \"pressure\": %s, \"humidity\": %s}", buff[0], buff[1], buff[2]);
}
// Handle WiFi Scan
void handle_wifi_scan(HttpRequest& req, HttpResponse& res)
{
@ -163,6 +179,7 @@ Route routes[] = {
{ GET, "/wifi/scan", handle_wifi_scan},
{ GET | POST | DELETE, "/wifi", handle_wifi_state},
{ GET | POST, "/firmware", handle_firmware},
{ GET, "/weather/current", handle_weather_current},
{ GET, "/#", handle_asset}
};