Last active
March 29, 2023 15:12
-
-
Save alphaville/bcd58379243ab7c49a00d91968ad5f3c to your computer and use it in GitHub Desktop.
This file contains 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 <Arduino.h> | |
#include <Wire.h> | |
#include "Adafruit_BME680.h" | |
#include "WiFi.h" | |
#include "WebServer.h" | |
/* CHANGE THESE: */ | |
#define NETWORK_SSID "YOUR_WIFI_SSID_HERE" | |
#define NETWORK_PASSWD "YOUR_WIFI_PASSWORD_HERE" | |
#define DEVICE_NAME "YellowDragon" | |
Adafruit_BME680 bme; | |
WebServer server(80); | |
float temperature; | |
void setup_serial() { | |
Serial.begin(115200); | |
while (!Serial) | |
; | |
} | |
void setup_bme() { | |
if (!bme.begin()) { | |
Serial.println(F("Could not find a valid BME680 sensor, check wiring!")); | |
while (1) | |
; | |
} | |
} | |
void handle_json() { | |
String resp = "{\n"; | |
resp = resp + " \"temperature\": " + temperature + "\n"; | |
resp = resp + "}"; | |
server.send(200, "text/json", resp); | |
} | |
void print_wifi_info() { | |
Serial.println(" "); | |
Serial.print("Local IP: "); | |
Serial.println(WiFi.localIP()); | |
Serial.print("Subnet Mask: "); | |
Serial.println(WiFi.subnetMask()); | |
Serial.print("Gateway IP: "); | |
Serial.println(WiFi.gatewayIP()); | |
Serial.print("DNS 1: "); | |
Serial.println(WiFi.dnsIP(0)); | |
Serial.print("DNS 2: "); | |
Serial.println(WiFi.dnsIP(1)); | |
Serial.print("MAC Address:"); | |
Serial.println(WiFi.macAddress()); | |
} | |
void setup_wifi() { | |
IPAddress local_IP(192, 168, 8, 55); /* < CHANGE THIS TO 192.168.0.[YOUR_ID] */ | |
IPAddress gateway(192, 168, 8, 1); | |
IPAddress subnet(255, 255, 255, 0); | |
WiFi.mode(WIFI_STA); | |
WiFi.disconnect(); | |
WiFi.config(local_IP, gateway, subnet, INADDR_NONE, INADDR_NONE); | |
String hostname = DEVICE_NAME; | |
WiFi.setHostname(hostname.c_str()); | |
delay(100); | |
WiFi.begin(NETWORK_SSID, NETWORK_PASSWD); | |
Serial.print("Connecting to WiFi .."); | |
while (WiFi.status() != WL_CONNECTED) { | |
Serial.print('.'); | |
delay(1000); | |
} | |
} | |
void setup_http_server() { | |
server.on("/", handle_json); | |
server.begin(); | |
Serial.println("HTTP server started"); | |
} | |
void setup() { | |
setup_serial(); | |
setup_bme(); | |
setup_wifi(); | |
print_wifi_info(); | |
setup_http_server(); | |
} | |
void get_bme_reading() { | |
if (bme.beginReading() == 0 || !bme.endReading()) { | |
Serial.println(F("Could not read BME :(")); | |
} | |
temperature = bme.temperature; | |
} | |
void loop() { | |
server.handleClient(); | |
get_bme_reading(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment