Skip to content

Instantly share code, notes, and snippets.

@greggjaskiewicz
Created September 22, 2020 18:41
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 greggjaskiewicz/63afbd45c25bb912fdb81b07c936101d to your computer and use it in GitHub Desktop.
Save greggjaskiewicz/63afbd45c25bb912fdb81b07c936101d to your computer and use it in GitHub Desktop.
measure temp and upload
#include <Wire.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <limits>
#include <ArduinoJson.h>
#include <ESP8266HTTPClient.h>
#include <SPI.h>
#include <Adafruit_BME280.h>
#ifndef STASSID
#define STASSID “xxx”
#define STAPSK “xx”x
#endif
const char* ssid = STASSID;
const char* password = STAPSK;
ESP8266WebServer server(80);
int D0 = 16;
int D1 = 5;
int D2 = 4;
int D3 = 0;
int D4 = 2;
int D5 = 14;
int D6 = 12;
int D7 = 13;
int D8 = 15;
int RX = 3;
Adafruit_BME280 *bme;
static float lastTemperature = std::numeric_limits<float>::infinity();
void setup() {
Serial.begin(9600);
Wire.begin(D2, D1);
bme = new Adafruit_BME280();
bool status = bme->begin(0x76, &Wire);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
if (status == false) {
Serial.print("SensorID was: 0x");
Serial.println(bme->sensorID(), 16);
while (1) {
yield();
}
}
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(400);
}
String status1 = String("IP:" + WiFi.localIP().toString() + " ");
Serial.println(status1);
if (MDNS.begin("esp8266_temp")) {
Serial.println("connected ");
} else {
Serial.println("MDNS failed ");
delay(2000);
}
server.on("/", handleRoot);
server.begin();
}
void handleRoot() {
String tempString = String(lastTemperature, 3);
String d = String("It's " + tempString + " C here!");
if (lastTemperature == std::numeric_limits<float>::infinity()) {
d = String("no measurements done yet");
}
// digitalWrite(led, 1);
server.send(200, "text/plain", d);
// digitalWrite(led, 0);
}
// the loop function runs over and over again forever
void loop() {
// ESP.deepSleep(200e6); // 20e6 is 20 microseconds
delay(15000); // Pause for 15 seconds
float temp = bme->readTemperature();
if (isnan(temp) == false) {
lastTemperature = temp;
String tempString = String(temp, 2);
String d = String(tempString + " 'C ");
Serial.println(d);
// send HTTP request
String requestURL = urlWithLastTemperature(temp);
HTTPClient http;
// Your Domain name with URL path or IP address with path
http.begin(requestURL.c_str());
// Send HTTP GET request
int httpResponseCode = http.GET();
if (httpResponseCode != 200) {
Serial.println("response code: "+httpResponseCode);
}
} else {
Serial.println("temp fail! ");
}
server.handleClient();
MDNS.update();
}
String urlWithLastTemperature(float lastTemp) {
String url = String("http://foobar/insert_temp.php?temp=" + String(lastTemp, 2));
return url;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment