Skip to content

Instantly share code, notes, and snippets.

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 honestcomrade/56cc1382231a806d7625abb4edd16314 to your computer and use it in GitHub Desktop.
Save honestcomrade/56cc1382231a806d7625abb4edd16314 to your computer and use it in GitHub Desktop.
/*
Simple HTTP get webclient test
*/
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <ESP8266WiFi.h>
#define BME_SCK 13
#define BME_MISO 12
#define BME_MOSI 11
#define BME_CS 10
#define SEALEVELPRESSURE_HPA (1013.25)
Adafruit_BME280 bme; // I2C
const char* ssid = "Casetta";
// const char* password = mypasword;
String host = "rtupdate.wunderground.com";
String path = "/weatherstation/updateweatherstation.php?";
String id = "ID=KCAMARTI34&PASSWORD=xmxlu778&dateutc=now&tempf=";
String params = "";
String rt = "&realtime=1&rtfreq=2.5&action=updateraw";
unsigned long delayTime;
void setup() {
Serial.begin(115200);
delay(100);
bool status;
// 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());
Serial.println("Mac Adress: ");
Serial.println(getMacAddress());
}
int value = 0;
void loop() {
WiFiClient client;
double fahr = convertF(bme.readTemperature());
params = String(fahr);
printValues();
client.print(String("GET ") + path + id + params + rt + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
Serial.print(String("GET ") + path + id + params + rt + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
// Read all the lines of the reply from server and print them to Serial
while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);
}
Serial.println();
Serial.println("closing connection");
delay(delayTime);
}
String getMacAddress() {
byte mac[6];
WiFi.macAddress(mac);
String cMac = "";
for (int i = 0; i < 6; ++i) {
if (mac[i]<0x10) {cMac += "0";}
cMac += String(mac[i],HEX);
if(i<5)
cMac += ":"; // put : or - if you want byte delimiters
}
cMac.toUpperCase();
return cMac;
}
void printValues() {
Serial.print("Temperature(C) = ");
Serial.print(bme.readTemperature());
Serial.println(" *C");
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 convertF (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