Hi all
Im a noob with blynk, I got my Esp8266 messuare temprature and humidity with a DHT22 sensor, and the Blynk app shows the values from the sensor. I thought I would extend the feature a bit, I want that the ESP8266 allso sends out the data from the sensor to a apache server with some php script.
So I modified the working ESP8266 code to add the simple webclient script. I works as I want, except from that when the ESP8266, disconnects from the webserver, it allso disconnects the connection to cloud.blynk.com server, how could I prevent it from allso disconnect from the blynk server
This is the code I use
/*
* Simple HTTP get webclient test
*/
#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
char auth[] = "xxxxxxx";
#include <DHT.h>
#define DHTPIN 2 //pin gpio 12 in sensor
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
#include <SimpleTimer.h>
SimpleTimer timer;
const char* ssid = "xxxxx";
const char* password = "xxxx";
const char* host = "10.0.0.63"; // raspberry pi
void sendUptime()
{
float h = dht.readHumidity();
float t = dht.readTemperature();
Blynk.virtualWrite(10, t); // virtual pin
Blynk.virtualWrite(11, h); // virtual pin
}
void setup() {
Serial.begin(9600);
delay(100);
Serial.println("DHT22 WIFI Client!");
Blynk.begin(auth, "xxxxx", "xxxx");
dht.begin();
timer.setInterval(10000, sendUptime);//1000=1s
// We start by connecting to a WiFi network
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());
}
int value = 0;
void loop() {
Blynk.run();
delay(60000);
++value;
float h = dht.readHumidity();
float t = dht.readTemperature();
// check if returns are valid, if they are NaN (not a number)
// then something went wrong!
if (isnan(t) || isnan(h)) {
Serial.println("Failed to read from DHT");
} else {
Serial.print("Luft fuktighet: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Tempratur: ");
Serial.print(t);
Serial.println(" *C");
delay(10000); // pauser 10 sekunder
}
timer.run();
Serial.print("connecting to ");
Serial.println(host);
// Use WiFiClient class to create TCP connections
WiFiClient client1;
const int httpPort = 80;
if (!client1.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
// We now create a URI for the request
String url = "/testwifi/index.php";
Serial.print("Requesting URL: ");
Serial.println(url);
// This will send the request to the server
client1.print(String("GET ") + url + "?temp=" + t + "&fukt=" + h + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
delay(500);
// Read all the lines of the reply from server and print them to Serial
while(client1.available()){
String line = client1.readStringUntil('\r');
Serial.print(line);
}
Serial.println();
Serial.println("closing connection");
}