- Thu Sep 03, 2015 12:53 pm
#27728
I have done something similar.
ESP is reading a sensor, connecting to my wifi, connecting to Apache2 webserver on raspberry pi, transmit info, then shut down.
ESP sketch (transmit part):
Code: Select allvoid setup() {
Serial.begin(115200);
delay(10);
// 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());
}
void loop() {
// Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 8000;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
//temp variable is set with a function to check the temp of an DS18B20 sensor. Let me know if you need it
String data = "temp1=" + (String)temp;
// "temp1=" is what is going to be sent using GET to the apache server, see code in add.php
// We now create a URI for the request
String url = "GET /add.php?" + data + " HTTP/1.1";
client.println(url);
client.println("Host: 192.168.1.200");
client.println("Connection: close");
client.println();
delay(500);
// 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);
}
client.stop(); //Stopping client
ESP.deepSleep(60U*60*1000000); //U for unsigned
delay(1000); //for above sleep
}
Apache2 to receive data (using GET)
Code: Select all<?php
$date = new DateTime();
$date = $date->format("d-M-Y h:i:s ");
if(!empty($_GET["temp1"]))
{
$tempesp = "Temperature=";
$tempesp .= ($_GET["temp1"]);
file_put_contents("/media/USB/Temp.ESP", $date);
file_put_contents("/media/USB/Temp.ESP", $tempesp, FILE_APPEND);
}
echo $tempesp;
?>
I use this so that my server writes the value of temp1, along with a timestamp, to a text file, which I then read with RPI-Monitor to display nice graphs and such.
Hope this helps