Hackster is hosting Hackster Holidays, Ep. 7: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 7 on Friday!
Stephen Borsay
Published © GPL3+

Send ESP8266 Data to Your Webpage - no AT Commands!

This project continues my series of transmitting data about the web. This time its sending sensor data to your own webpage!

IntermediateFull instructions provided2 hours57,068
Send ESP8266 Data to Your Webpage - no AT Commands!

Things used in this project

Hardware components

Adafruit Feather HUZZAH with ESP8266 WiFi
Adafruit Feather HUZZAH with ESP8266 WiFi
any 8266 will do
×1
Jumper wires (generic)
Jumper wires (generic)
×1
DHT11 Temperature & Humidity Sensor (4 pins)
DHT11 Temperature & Humidity Sensor (4 pins)
×1
USB-A to Micro-USB Cable
USB-A to Micro-USB Cable
×1

Software apps and online services

Arduino IDE
Arduino IDE
NodeMCU firmware
NodeMCU firmware

Story

Read more

Schematics

Circuit

esp8266 with wires

Code

dataCollector.php

PHP
PHP code for accepting our environmental variables through $_Request(POSTand GET) over the web and our PHP files creates an HTML fill with our data called dataDisplayer.html
<?php

/*
  ESP8266: send data to your Domain(or mine)
  Embedded-iot.net/dht11/dataCollector.php

  Uses POST command to send DHT data to a designated website
  The circuit:
  * DHT
  * Post to Domain

   Stephen Borsay
   Embedded-iot.net
   www.udemy.com/all-about-arduino-wireless
   https://www.hackster.io/detox
   https://github.com/sborsay/Arduino_Wireless
*/


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);


?>

ESP8266_toYourDomain

Arduino
This is the Arduino code to send a post of DHT sensor data to your .php site to display on your webpage.
/*
  ESP8266: send data to your Domain (or mine: Embedded-iot.net/dht11/dataCollector.php)(

  Uses POST command to send DHT data to a designated website
  The circuit:
  * DHT
  * Post to Domain

   Stephen Borsay
   Homepage: Embedded-iot.net
   www.udemy.com/all-about-arduino-wireless
   https://www.hackster.io/detox
   https://github.com/sborsay/Arduino_Wireless
*/

#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[] = "embedded-iot.net"; 

const char* MY_SSID = "YOUR_SSID_WiFi";
const char* MY_PWD =  "YOUR_WiFi_PASSWORD";

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;

     //change URL below if using your Sub-Domain
     client.println("POST /dht11/dataCollector.php HTTP/1.1"); 
     //change URL below if using your Domain
     client.print("Host: embedded-iot.net\n");                 
     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);
    } 

}


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");
}

Github Repository of Arduino Wireless projects

Look for: ESP8266_toYourDomain.ino ESP8266_toPHP.php

Credits

Stephen Borsay
11 projects • 73 followers
Computer Engineer: Embedded Systems and IoT. AWS IoT Hero www.udemy.com/user/stv/ device2cloud.net

Comments