Witam serdecznie,
postaram się najkrócej jak mogę. Próbuję zrekonstruować projekt polegający na wysyłaniu danych odebranych z czujnika DHT11.
Plik .php obecny na hostingu:
<?php
date_default_timezone_set("America/Los_Angeles");
$TimeStamp = "The current time is " . date("h:i:sa");
file_put_contents('dataDisplayer.html', $TimeStamp, FILE_APPEND);
if( $_REQUEST["Humidity"] || $_REQUEST["Temperature_Cel"] ||
$_REQUEST["Temperature_Fehr"]
|| $_REQUEST["HeatIndex_Cel"] || $_REQUEST["HeatIndex_Fehr"] )
{
echo " The Humidity is: ". $_REQUEST['Humidity']. "%<br />";
echo " The Temperature is: ". $_REQUEST['Temperature_Cel']. " Celcius<br />";
echo " The Temperature is: ". $_REQUEST['Temperature_Fehr']. " Fehr<br />";
echo " The Heat Index: ". $_REQUEST['HeatIndex_Cel']. " Heat Idx Cel<br />";
echo " The Heat Index: ". $_REQUEST['HeatIndex_Fehr']. " Heat Idx Fehr<br />";
}
$var1 = $_REQUEST['Humidity'];
$var2 = $_REQUEST['Temperature_Cel'];
$var3 = $_REQUEST['Temperature_Fehr'];
$var4 = $_REQUEST['HeatIndex_Cel'];
$var5 = $_REQUEST['HeatIndex_Fehr'];
$WriteMyRequest=
"<p> The Humidity is : " . $var1 . "% </p>".
"<p>And the Temperature is : " . $var2 . " Celcius </p>".
"<p>And the Temperature is : " . $var3 . " Fehreinheit</p>".
"<p>And The Heat Index is : " . $var4 . " Heat Index Celcius </p>".
"<p>And The Heat Index is : " . $var5 . " Heat Index Fehrenheit </p><br/>";
file_put_contents('dataDisplayer.html', $WriteMyRequest, FILE_APPEND);
?>
Tak wygląda program wgrany do mikrokontrolera:
#include "ESP8266WiFi.h"
#include "DHT.h"
#define DHTPIN 2 // what digital pin we're connected to pin2 to D4 on esp board
// Uncomment whatever DHT sensor type you're using!
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT21 // DHT 21
//#define DHTTYPE DHT22 // DHT 22
DHT dht(DHTPIN,DHTTYPE);
const char server[] = "adres_serwera";
const char* MY_SSID = "nazwa_wifi";
const char* MY_PWD = "haslo_wifi";
WiFiClient client;
void setup()
{
Serial.begin(115200);
dht.begin();
Serial.print("Connecting to "+*MY_SSID);
WiFi.begin(MY_SSID, MY_PWD);
Serial.println("going into wl connect");
while (WiFi.status() != WL_CONNECTED) //not connected, ...waiting to connect
{
delay(1000);
Serial.print(".");
}
Serial.println("wl connected");
Serial.println("");
Serial.println("Credentials accepted! Connected to wifi\n ");
Serial.println("");
}
void loop() {
// Wait a few seconds between measurements.
delay(2000);
//prefer to use float, but package size or float conversion isnt working
//will revise in future with a string fuction or float conversion function
int Humidity = dht.readHumidity();
// Read temperature as Celsius (the default)
int Temperature_Cel = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
int Temperature_Fehr = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(Humidity) || isnan(Temperature_Cel) || isnan(Temperature_Fehr))
{
Serial.println("Failed to read from DHT sensor!");
return;
}
// Compute heat index in Fahrenheit (the default)
int HeatIndex_Fehr = dht.computeHeatIndex(Temperature_Fehr, Humidity);
// Compute heat index in Celsius (isFahreheit = false)
int HeatIndex_Cel = dht.computeHeatIndex(Temperature_Cel, Humidity, false);
Serial.print("Humidity: ");
Serial.print(Humidity);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(Temperature_Cel);
Serial.print(" *C ");
Serial.print(Temperature_Fehr);
Serial.print(" *F\t");
Serial.print("Heat index: ");
Serial.print(HeatIndex_Cel);
Serial.print(" *C ");
Serial.print(HeatIndex_Fehr);
Serial.println(" *F\n");
Serial.println("\nStarting connection to server...");
// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.println("connected to server");
WiFi.printDiag(Serial);
String data = "Humidity="
+ (String) Humidity
+ "&Temperature_Cel=" +(String) Temperature_Cel
+ "&Temperature_Fehr=" +(String) Temperature_Fehr
+ "&HeatIndex_Cel=" +(String) HeatIndex_Cel
+ "&HeatIndex_Fehr=" +(String) HeatIndex_Fehr;
client.println("POST plik.php HTTP/1.1"); //change this if using your Sub-domain
client.print("Host: adres_hosta\n"); //change this if using your Domain
client.println("User-Agent: ESP8266/1.0");
client.println("Connection: close");
client.println("Content-Type: application/x-www-form-urlencoded");
client.print("Content-Length: ");
client.print(data.length());
client.print("\n\n");
client.print(data);
client.stop();
Serial.println("\n");
Serial.println("My data string im POSTing looks like this: ");
Serial.println(data);
Serial.println("And it is this many bytes: ");
Serial.println(data.length());
delay(2000);
delay(5000);
}
}
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
I teraz tak - jeżeli ręcznie wykonam wysyłkę danych, tzn. wpiszę coś takiego w przeglądarce:
plik.php?Humidity=11&Temperature_Cel=22&Temperature_Fehr=33&HeatIndex_Cel=44&HeatIndex_Fehr=55
To te dane są normalnie dodane to pliku i wszystko elegancko działa. Tak samo sytuacja wygląda gdy podam adresy serwera i pliku .php człowieka, który stworzył ten projekt.
Niestety, z niewiadomych dla mnie przyczyn nie mogę wysłać danych pogodowych na moją stronę. Hotlink Protection w cPanelu wyłączony. Gdzie leży problem, czy hosting może blokować mi taki sposób wysyłki danych? Jeżeli tak, to czy jest jakiś sposób na ominięcie tego?
Z góry dzięki za pomoc,
Pozdrawiam