- Impressum / Datenschutz / ...
- ESP8266 spezifisch
- Codebesprechung
- ESP Taupunkt Steuerung BME280 & Wunderground
ESP Taupunkt Steuerung BME280 & Wunderground
- Cyrix
- Autor
- Offline
- New Member
Weniger
Mehr
- Beiträge: 2
- Dank erhalten: 0
21 Jul 2017 22:39 #222
von Cyrix
Cyrix erstellte das Thema ESP Taupunkt Steuerung BME280 & Wunderground
Hallo Community
seit unzähligen Stunden zermatere ich mir jetzt den Kopf wo genau das Problem an meinem Programm ist.
Generell soll es sich mit dem WLAN verbinden und dort dann das aktuelle Wetter von Wunderground parsen.
Darüber hinaus fragt er einen BME280 im Innenbereich ab und soll die aktuellen werte auf einem OLED Display anzeigen.
soweit scheint auch alles zu funktionieren nur ich bekomme während des Loops keine veränderte Anzeige auf dem OLED hin. es ist so als ob es einfriert.
Es steckt natürlich noch ganz schön in den Kinderschuhen und ist auch ziemlich zusammen geklöppelt aber bevor das OLED nicht funktionert lohnt sich weitermachen auch nicht wirklich.
Code:
#include <ESP8266WiFi.h>
#include <stdint.h>
#include "SparkFunBME280.h"
#include "Wire.h"
#ifndef min
#define min(x,y) (((x)<(y))?(x):(y))
#endif
#ifndef max
#define max(x,y) (((x)>(y))?(x):(y))
#endif
#include <ArduinoJson.h>
#include <SPI.h>
//#include <Adafruit_GFX.h>
//#include <Adafruit_PCD8544.h>
BME280 mySensorA;
#include <SPI.h>
#include <Wire.h>
#include <ACROBOTIC_SSD1306.h>
static const char SSID[] = "";
static const char PASSWORD[] = "";
// Use your own API key by signing up for a free developer account.
// www.wunderground.com/weather/api/
#define WU_API_KEY ""
// Country and city
#define WU_LOCATION "Germany/Holzwickede"
// 5 minutes between update checks. The free developer account has a limit
// on the number of calls so don't go wild.
#define DELAY_NORMAL (5*60*1000)
// 20 minute delay between updates after an error
#define DELAY_ERROR (20*60*1000)
#define WUNDERGROUND "api.wunderground.com"
// HTTP request
const char WUNDERGROUND_REQ[] =
"GET /api/" WU_API_KEY "/conditions/q/" WU_LOCATION ".json HTTP/1.1\r\n"
"User-Agent: ESP8266/0.1\r\n"
"Accept: */*\r\n"
"Host: " WUNDERGROUND "\r\n"
"Connection: close\r\n"
"\r\n";
void setup()
{
Serial.begin(9600);
//DISPLAY START *****************************************************************************
Wire.begin();
oled.init(); // Initialze SSD1306 OLED display
oled.clearDisplay(); // Clear screen
oled.setTextXY(0,0); // Set cursor position, start of line 0
oled.putString("Keller");
oled.setTextXY(1,0); // Set cursor position, start of line 1
oled.putString("Taupunkt");
oled.setTextXY(2,0); // Set cursor position, start of line 2
oled.putString("Steuerung");
// ******************************************************************************************
//***Set up sensor 'A'******************************//
//commInterface can be I2C_MODE or SPI_MODE
mySensorA.settings.commInterface = I2C_MODE;
mySensorA.settings.I2CAddress = 0x77;
mySensorA.settings.runMode = 3; // 3, Normal mode
mySensorA.settings.tStandby = 0; // 0, 0.5ms
mySensorA.settings.filter = 0; // 0, filter off
//tempOverSample can be:
// 0, skipped
// 1 through 5, oversampling *1, *2, *4, *8, *16 respectively
mySensorA.settings.tempOverSample = 1;
//pressOverSample can be:
// 0, skipped
// 1 through 5, oversampling *1, *2, *4, *8, *16 respectively
mySensorA.settings.pressOverSample = 1;
//humidOverSample can be:
// 0, skipped
// 1 through 5, oversampling *1, *2, *4, *8, *16 respectively
mySensorA.settings.humidOverSample = 1;
Serial.print("Program Started\n");
Serial.println("Starting BME280s... result of .begin():");
delay(10); //Make sure sensor had enough time to turn on. BME280 requires 2ms to start up.
//Calling .begin() causes the settings to be loaded
Serial.print("Sensor A: 0x");
Serial.println(mySensorA.begin(), HEX);
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print(F("Connecting to "));
Serial.println(SSID);
WiFi.begin(SSID, PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(F("."));
}
Serial.println();
Serial.println(F("WiFi connected"));
Serial.println(F("IP address: "));
Serial.println(WiFi.localIP());
}
// ***********************************************************************************
static char respBuf[4096];
bool showWeather(char *json);
void loop()
{
oled.clearDisplay();
delay (1000);
oled.setTextXY(0,0);
oled.putString("Test1");
oled.setTextXY(1,0);
oled.putString("Test2");
oled.setTextXY(2,0);
oled.putString("Test3");
oled.clearDisplay();
Serial.print("SChleife");
float h_innen = mySensorA.readFloatHumidity();
// Read temperature as Celsius
float t_innen = mySensorA.readTempC();
// set text color / Textfarbe setzen
// set text cursor position / Textstartposition einstellen
//Start with temperature, as that data is needed for accurate compensation.
//Reading the temperature updates the compensators of the other functions
//in the background.
Serial.print("Temperature: ");
Serial.print(mySensorA.readTempC(), 2);
Serial.println(" degrees C");
Serial.print("Pressure: ");
Serial.print(mySensorA.readFloatPressure(), 2);
Serial.println(" Pa");
Serial.print("Altitude: ");
Serial.print(mySensorA.readFloatAltitudeMeters(), 2);
Serial.println("m");
Serial.print("%RH: ");
Serial.print(mySensorA.readFloatHumidity(), 2);
Serial.println(" %");
// TODO check for disconnect from AP
// Open socket to WU server port 80
Serial.print(F("Connecting to "));
Serial.println(WUNDERGROUND);
// Use WiFiClient class to create TCP connections
WiFiClient httpclient;
const int httpPort = 80;
if (!httpclient.connect(WUNDERGROUND, httpPort)) {
Serial.println(F("connection failed"));
delay(DELAY_ERROR);
return;
}
// This will send the http request to the server
Serial.print(WUNDERGROUND_REQ);
httpclient.print(WUNDERGROUND_REQ);
httpclient.flush();
// Collect http response headers and content from Weather Underground
// HTTP headers are discarded.
// The content is formatted in JSON and is left in respBuf.
int respLen = 0;
bool skip_headers = true;
while (httpclient.connected() || httpclient.available()) {
if (skip_headers) {
String aLine = httpclient.readStringUntil('\n');
//Serial.println(aLine);
// Blank line denotes end of headers
if (aLine.length() <= 1) {
skip_headers = false;
}
}
else {
int bytesIn;
bytesIn = httpclient.read((uint8_t *)&respBuf[respLen], sizeof(respBuf) - respLen);
Serial.print(F("bytesIn ")); Serial.println(bytesIn);
if (bytesIn > 0) {
respLen += bytesIn;
if (respLen > sizeof(respBuf)) respLen = sizeof(respBuf);
}
else if (bytesIn < 0) {
Serial.print(F("read error "));
Serial.println(bytesIn);
}
}
delay(1);
}
httpclient.stop();
if (respLen >= sizeof(respBuf)) {
Serial.print(F("respBuf overflow "));
Serial.println(respLen);
delay(DELAY_ERROR);
return;
}
// Terminate the C string
respBuf[respLen++] = '\0';
Serial.print(F("respLen "));
Serial.println(respLen);
//Serial.println(respBuf);
if (showWeather(respBuf)) {
delay(DELAY_NORMAL);
}
else {
delay(DELAY_ERROR);
}
}
bool showWeather(char *json)
{
StaticJsonBuffer<3*1024> jsonBuffer;
// Skip characters until first '{' found
// Ignore chunked length, if present
char *jsonstart = strchr(json, '{');
//Serial.print(F("jsonstart ")); Serial.println(jsonstart);
if (jsonstart == NULL) {
Serial.println(F("JSON data missing"));
return false;
}
json = jsonstart;
// Parse JSON
JsonObject& root = jsonBuffer.parseObject(json);
if (!root.success()) {
Serial.println(F("jsonBuffer.parseObject() failed"));
return false;
}
// Extract weather info from parsed JSON
JsonObject& current = root["current_observation"];
const float temp_f = current["temp_f"];
Serial.print(temp_f, 1); Serial.print(F(" F, "));
const float temp_c = current["temp_c"];
Serial.print(temp_c, 1); Serial.print(F(" C, "));
const char *humi = current[F("relative_humidity")];
Serial.print(humi); Serial.println(F(" RH"));
const char *weather = current["weather"];
Serial.println(weather);
const char *pressure_mb = current["pressure_mb"];
Serial.println(pressure_mb);
const char *observation_time = current["observation_time_rfc822"];
Serial.println(observation_time);
delay (100);
//Date/time string looks like this
//Wed, 27 Jun 2012 17:27:14 -0700
//012345678901234567890
// 1 2
// LCD has 14 characters per line so show date on one line and time on the
// next. And shorten the date so it fits.
char date[14+1];
const char *time;
//Wed, 27 Jun 12
memcpy(date, observation_time, 12);
memcpy(&date[12], &observation_time[14], 2);
date[14] = '\0';
//17:27:14 -0700
time = &observation_time[17];
return true;
}
seit unzähligen Stunden zermatere ich mir jetzt den Kopf wo genau das Problem an meinem Programm ist.
Generell soll es sich mit dem WLAN verbinden und dort dann das aktuelle Wetter von Wunderground parsen.
Darüber hinaus fragt er einen BME280 im Innenbereich ab und soll die aktuellen werte auf einem OLED Display anzeigen.
soweit scheint auch alles zu funktionieren nur ich bekomme während des Loops keine veränderte Anzeige auf dem OLED hin. es ist so als ob es einfriert.
Es steckt natürlich noch ganz schön in den Kinderschuhen und ist auch ziemlich zusammen geklöppelt aber bevor das OLED nicht funktionert lohnt sich weitermachen auch nicht wirklich.
Code:
Warnung: Spoiler!
[ Zum Anzeigen klicken ]
[ Zum Verstecken klicken ]
#include <ESP8266WiFi.h>
#include <stdint.h>
#include "SparkFunBME280.h"
#include "Wire.h"
#ifndef min
#define min(x,y) (((x)<(y))?(x):(y))
#endif
#ifndef max
#define max(x,y) (((x)>(y))?(x):(y))
#endif
#include <ArduinoJson.h>
#include <SPI.h>
//#include <Adafruit_GFX.h>
//#include <Adafruit_PCD8544.h>
BME280 mySensorA;
#include <SPI.h>
#include <Wire.h>
#include <ACROBOTIC_SSD1306.h>
static const char SSID[] = "";
static const char PASSWORD[] = "";
// Use your own API key by signing up for a free developer account.
// www.wunderground.com/weather/api/
#define WU_API_KEY ""
// Country and city
#define WU_LOCATION "Germany/Holzwickede"
// 5 minutes between update checks. The free developer account has a limit
// on the number of calls so don't go wild.
#define DELAY_NORMAL (5*60*1000)
// 20 minute delay between updates after an error
#define DELAY_ERROR (20*60*1000)
#define WUNDERGROUND "api.wunderground.com"
// HTTP request
const char WUNDERGROUND_REQ[] =
"GET /api/" WU_API_KEY "/conditions/q/" WU_LOCATION ".json HTTP/1.1\r\n"
"User-Agent: ESP8266/0.1\r\n"
"Accept: */*\r\n"
"Host: " WUNDERGROUND "\r\n"
"Connection: close\r\n"
"\r\n";
void setup()
{
Serial.begin(9600);
//DISPLAY START *****************************************************************************
Wire.begin();
oled.init(); // Initialze SSD1306 OLED display
oled.clearDisplay(); // Clear screen
oled.setTextXY(0,0); // Set cursor position, start of line 0
oled.putString("Keller");
oled.setTextXY(1,0); // Set cursor position, start of line 1
oled.putString("Taupunkt");
oled.setTextXY(2,0); // Set cursor position, start of line 2
oled.putString("Steuerung");
// ******************************************************************************************
//***Set up sensor 'A'******************************//
//commInterface can be I2C_MODE or SPI_MODE
mySensorA.settings.commInterface = I2C_MODE;
mySensorA.settings.I2CAddress = 0x77;
mySensorA.settings.runMode = 3; // 3, Normal mode
mySensorA.settings.tStandby = 0; // 0, 0.5ms
mySensorA.settings.filter = 0; // 0, filter off
//tempOverSample can be:
// 0, skipped
// 1 through 5, oversampling *1, *2, *4, *8, *16 respectively
mySensorA.settings.tempOverSample = 1;
//pressOverSample can be:
// 0, skipped
// 1 through 5, oversampling *1, *2, *4, *8, *16 respectively
mySensorA.settings.pressOverSample = 1;
//humidOverSample can be:
// 0, skipped
// 1 through 5, oversampling *1, *2, *4, *8, *16 respectively
mySensorA.settings.humidOverSample = 1;
Serial.print("Program Started\n");
Serial.println("Starting BME280s... result of .begin():");
delay(10); //Make sure sensor had enough time to turn on. BME280 requires 2ms to start up.
//Calling .begin() causes the settings to be loaded
Serial.print("Sensor A: 0x");
Serial.println(mySensorA.begin(), HEX);
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print(F("Connecting to "));
Serial.println(SSID);
WiFi.begin(SSID, PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(F("."));
}
Serial.println();
Serial.println(F("WiFi connected"));
Serial.println(F("IP address: "));
Serial.println(WiFi.localIP());
}
// ***********************************************************************************
static char respBuf[4096];
bool showWeather(char *json);
void loop()
{
oled.clearDisplay();
delay (1000);
oled.setTextXY(0,0);
oled.putString("Test1");
oled.setTextXY(1,0);
oled.putString("Test2");
oled.setTextXY(2,0);
oled.putString("Test3");
oled.clearDisplay();
Serial.print("SChleife");
float h_innen = mySensorA.readFloatHumidity();
// Read temperature as Celsius
float t_innen = mySensorA.readTempC();
// set text color / Textfarbe setzen
// set text cursor position / Textstartposition einstellen
//Start with temperature, as that data is needed for accurate compensation.
//Reading the temperature updates the compensators of the other functions
//in the background.
Serial.print("Temperature: ");
Serial.print(mySensorA.readTempC(), 2);
Serial.println(" degrees C");
Serial.print("Pressure: ");
Serial.print(mySensorA.readFloatPressure(), 2);
Serial.println(" Pa");
Serial.print("Altitude: ");
Serial.print(mySensorA.readFloatAltitudeMeters(), 2);
Serial.println("m");
Serial.print("%RH: ");
Serial.print(mySensorA.readFloatHumidity(), 2);
Serial.println(" %");
// TODO check for disconnect from AP
// Open socket to WU server port 80
Serial.print(F("Connecting to "));
Serial.println(WUNDERGROUND);
// Use WiFiClient class to create TCP connections
WiFiClient httpclient;
const int httpPort = 80;
if (!httpclient.connect(WUNDERGROUND, httpPort)) {
Serial.println(F("connection failed"));
delay(DELAY_ERROR);
return;
}
// This will send the http request to the server
Serial.print(WUNDERGROUND_REQ);
httpclient.print(WUNDERGROUND_REQ);
httpclient.flush();
// Collect http response headers and content from Weather Underground
// HTTP headers are discarded.
// The content is formatted in JSON and is left in respBuf.
int respLen = 0;
bool skip_headers = true;
while (httpclient.connected() || httpclient.available()) {
if (skip_headers) {
String aLine = httpclient.readStringUntil('\n');
//Serial.println(aLine);
// Blank line denotes end of headers
if (aLine.length() <= 1) {
skip_headers = false;
}
}
else {
int bytesIn;
bytesIn = httpclient.read((uint8_t *)&respBuf[respLen], sizeof(respBuf) - respLen);
Serial.print(F("bytesIn ")); Serial.println(bytesIn);
if (bytesIn > 0) {
respLen += bytesIn;
if (respLen > sizeof(respBuf)) respLen = sizeof(respBuf);
}
else if (bytesIn < 0) {
Serial.print(F("read error "));
Serial.println(bytesIn);
}
}
delay(1);
}
httpclient.stop();
if (respLen >= sizeof(respBuf)) {
Serial.print(F("respBuf overflow "));
Serial.println(respLen);
delay(DELAY_ERROR);
return;
}
// Terminate the C string
respBuf[respLen++] = '\0';
Serial.print(F("respLen "));
Serial.println(respLen);
//Serial.println(respBuf);
if (showWeather(respBuf)) {
delay(DELAY_NORMAL);
}
else {
delay(DELAY_ERROR);
}
}
bool showWeather(char *json)
{
StaticJsonBuffer<3*1024> jsonBuffer;
// Skip characters until first '{' found
// Ignore chunked length, if present
char *jsonstart = strchr(json, '{');
//Serial.print(F("jsonstart ")); Serial.println(jsonstart);
if (jsonstart == NULL) {
Serial.println(F("JSON data missing"));
return false;
}
json = jsonstart;
// Parse JSON
JsonObject& root = jsonBuffer.parseObject(json);
if (!root.success()) {
Serial.println(F("jsonBuffer.parseObject() failed"));
return false;
}
// Extract weather info from parsed JSON
JsonObject& current = root["current_observation"];
const float temp_f = current["temp_f"];
Serial.print(temp_f, 1); Serial.print(F(" F, "));
const float temp_c = current["temp_c"];
Serial.print(temp_c, 1); Serial.print(F(" C, "));
const char *humi = current[F("relative_humidity")];
Serial.print(humi); Serial.println(F(" RH"));
const char *weather = current["weather"];
Serial.println(weather);
const char *pressure_mb = current["pressure_mb"];
Serial.println(pressure_mb);
const char *observation_time = current["observation_time_rfc822"];
Serial.println(observation_time);
delay (100);
//Date/time string looks like this
//Wed, 27 Jun 2012 17:27:14 -0700
//012345678901234567890
// 1 2
// LCD has 14 characters per line so show date on one line and time on the
// next. And shorten the date so it fits.
char date[14+1];
const char *time;
//Wed, 27 Jun 12
memcpy(date, observation_time, 12);
memcpy(&date[12], &observation_time[14], 2);
date[14] = '\0';
//17:27:14 -0700
time = &observation_time[17];
return true;
}
Bitte Anmelden oder Registrieren um der Konversation beizutreten.
- gruenezone
- Offline
- Junior Member
Weniger
Mehr
- Beiträge: 30
- Karma: 1
- Dank erhalten: 2
26 Jul 2017 06:25 #225
von gruenezone
ESP Taupunkt Steuerung BME280 & Wunderground
Hi,
isoliere doch erst einmal den OLED-Code vom Rest und bringe das OLED zum laufen.
Hast Du das OLED mit SPI oder I2C in Betrieb? (Jumper)?
isoliere doch erst einmal den OLED-Code vom Rest und bringe das OLED zum laufen.
Hast Du das OLED mit SPI oder I2C in Betrieb? (Jumper)?
Bitte Anmelden oder Registrieren um der Konversation beizutreten.
- Cyrix
- Autor
- Offline
- New Member
Weniger
Mehr
- Beiträge: 2
- Dank erhalten: 0
26 Jul 2017 09:15 #227
von Cyrix
ESP Taupunkt Steuerung BME280 & Wunderground
Das oled läuft wenn ich es ohne das parsen laufen lasse ohne Probleme. Erst wenn der Part mit dem wunderground parsen dazu kommt scheint es "einzufrieren"
Selbst der serielle Text der mit der Display Änderung gedruckt wird erscheint.
Das Display wird per i2c betrieben und die pullups sind auch drin.
Selbst der serielle Text der mit der Display Änderung gedruckt wird erscheint.
Das Display wird per i2c betrieben und die pullups sind auch drin.
Bitte Anmelden oder Registrieren um der Konversation beizutreten.
- Aktuelle Seite:
- Startseite
- Impressum / Datenschutz / ...
- ESP8266 spezifisch
- Codebesprechung
- ESP Taupunkt Steuerung BME280 & Wunderground