dear friends i finally managed to upload my weather data to thingspeak and wu.
i am using wemod d1 mini. it is working very well.
problem is with my code. i can continuously upload data to thingspeak account.
but i can only upload data tu WU when i power wemos board. then it wont upload any new data even it is in my loop section.
could you please help me to find my fault?
*today i changed its way of uploading data to WU by using rapidfire function.
thank you in advance
here is code
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <stdint.h>
#include "SparkFunBME280.h"
#include "Wire.h"
#include "SPI.h"
const char* host = "rtupdate.wunderground.com";
const char* WUID = "IANKARA21";
const char* WUPASS = "xxxxxxxx";
//---------------------WEATHER VAR-----------------
float windspeed_eu;
float windgust_eu;
float winddir_eu;
float tempout_eu;
float dewpout_eu;
float humidity_eu;
float baro_eu;
float rain1h_eu;
float rain24h_eu;
//US
String windspeed;
String windgust;
String winddir;
String tempout;
String dewpout;
String humidity;
String baro;
String rain1h;
String rain24h;
float tempf;
float baromin;
//Global sensor object
BME280 mySensorA;
String apiKey = "xxxxxxxxxxxxxxx"; //fill in the api key from thingspeak
const char* ssid = "TTNET_ZyXEL_AFR3"; //fill in your wifi name
const char* password = "xxxxxxxxxxxxxxx"; //fill in your wifi password
const char* server = "api.thingspeak.com";
WiFiClient client;
#define OLED_RESET LED_BUILTIN //4
Adafruit_SSD1306 display(OLED_RESET);
#if (SSD1306_LCDHEIGHT != 32)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
void setup()
{
//***Set up sensor 'A'******************************//
//commInterface can be I2C_MODE or SPI_MODE
mySensorA.settings.commInterface = I2C_MODE;
mySensorA.settings.I2CAddress = 0x76;
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;
//***Initialize the things**************************//
Serial.begin(57600);
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);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.println();
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
// Clear the buffer.
display.clearDisplay();
display.display();
//variables for weather underground
tempf = mySensorA.readTempF();
humidity = mySensorA.readFloatHumidity();
baromin = (((mySensorA.readFloatPressure()) / 3386.375258) + 3);
//vvvvvvvvvvvvvvvvvvvvvvvvvvvvv
}
void loop()
{
//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.print(", ");
Serial.println(" degrees C");
Serial.print("Temperature: ");
Serial.print(mySensorA.readTempF(), 2);
Serial.print(", ");
Serial.println(" degrees F");
Serial.print("Pressure: ");
Serial.print(mySensorA.readFloatPressure(), 2);
Serial.print(", ");
Serial.println(" Pa");
Serial.print("Altitude: ");
Serial.print(mySensorA.readFloatAltitudeMeters(), 2);
Serial.print(", ");
Serial.println("m");
Serial.print("Altitude: ");
Serial.print(mySensorA.readFloatAltitudeFeet(), 2);
Serial.print(", ");
Serial.println("ft");
Serial.print("%RH: ");
Serial.print(mySensorA.readFloatHumidity(), 2);
Serial.print(", ");
Serial.println(" %");
Serial.print(baromin, 2);
Serial.print(", ");
Serial.println(" inhg");
Serial.println();
// oled screen settings
display.setTextSize(2);
display.setTextColor(WHITE);
// Clear the buffer.
display.clearDisplay(); display.setCursor(0, 0);
display.print("T: ");
display.print(mySensorA.readTempC());
display.print(" C");
display.print("P: ");
display.print((mySensorA.readFloatPressure()) / 100);
display.print(" mb");
display.display();
//--------------------------thingspeak-------------------------
if (client.connect(server, 80)) { // "184.106.153.149" or api.thingspeak.com
String postStr = apiKey;
postStr += "&field1=";
postStr += String(mySensorA.readTempC());
postStr += "&field2=";
postStr += String(mySensorA.readFloatPressure() / 100);
postStr += "&field3=";
postStr += String(mySensorA.readFloatHumidity());
postStr += "&field4=";
postStr += String();
postStr += "&field5=";
postStr += String();
postStr += "&field6=";
postStr += String();
postStr += "&field7=";
postStr += String();
postStr += "&field8=";
postStr += String();
postStr += "\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n";
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("\n\n\n\n\n\n\n\n");
client.print(postStr);
client.stop();
// weather underground ................ THIS ONLY UPLOADS WHEN POWERED UP THE BOARD
Serial.println("Send to WU Sensor Values");
Serial.print("connecting to ");
Serial.println(host);
// Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
// Ccreate a URI
String url = "/weatherstation/updateweatherstation.php?ID=";
url += WUID;
url += "&PASSWORD=";
url += WUPASS;
url += "&dateutc=now&winddir=";
url += winddir;
url += "&windspeedmph=";
url += windspeed;
url += "&windgustmph=";
url += windgust;
url += "&tempf=";
url += tempf;
url += "&dewptf=";
url += dewpout;
url += "&humidity=";
url += humidity;
url += "&baromin=";
url += baromin;
url += "&rainin=";
url += rain1h;
url += "&dailyrainin=";
url += rain24h;
url += "&weather=&clouds=&softwaretype=Arduino-ESP8266&action=updateraw&realtime=1&rtfreq=3.5";
Serial.print("Requesting URL: ");
Serial.println(url);
// Send the request to the server
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
delay(10);
// Read all and print
while (client.available()) {
String line = client.readStringUntil('\r');
Serial.print(line);
}
Serial.println();
Serial.println("closing connection");
client.flush(); //***
client.stop(); //***
//wifi_set_sleep_type(NONE_SLEEP_T);
//wifi_set_sleep_type(MODEM_SLEEP_T);
//wifi_set_sleep_type(LIGHT_SLEEP_T);
// thingspeak needs minimum 15 sec delay between updates
delay(15000);
}
}