So I have this code which records the temperature and humidity using a DHT22 sensor. After it get's the values it then sends it to a webpage using PHP $_GET method, where that gets imported into a DB and emails (using PHP). But this only happens when I upload the code to my ESP8266; how can I know build in a loop function that it sends it every 30 mins or given time?
#include <ESP8266WiFi.h>
#include "DHT.h"
const char* ssid = "bbox2"; // SSID of local network
const char* password = "azerty"; // Password on network
WiFiClient client;
char servername[]="lucasdebelder.be"; // remote server we will connect to
String result;
float temperature;
float humidity;
#define DHTPIN 14
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
void setup() {
dht.begin();
delay(2000);
Serial.begin(115200);
Serial.println("Connecting");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
}
Serial.println("Connected");
delay(1000);
readSensor();
String temperatureString = String(temperature,1);
String humidityString = String(humidity,1);
sendDataToServer(temperatureString,humidityString);
}
void loop() {
}
void sendDataToServer(String temperature, String humidity)
{
if (client.connect(servername, 80)) { //starts client connection, checks for connection
Serial.println("connected");
client.println("GET /send_temperature.php?temperature="+temperature+"&humidity="+humidity+" HTTP/1.1"); //Send data
client.println("Host: lucasdebelder.be");
client.println("Connection: close"); //close 1.1 persistent connection
client.println(); //end of get request
}
else {
Serial.println("connection failed"); //error message if no client connect
Serial.println();
}
while(client.connected() && !client.available()) delay(1); //waits for data
while (client.connected() || client.available()) { //connected or data available
char c = client.read(); //gets byte from ethernet buffer
result = result+c;
}
client.stop(); //stop client
Serial.println(result);
}
void readSensor()
{
humidity = dht.readHumidity();
temperature = dht.readTemperature();
Serial.println("Temperature:");
Serial.println(temperature);
Serial.println("Humidity:");
Serial.println(humidity);
}