Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <WiFiClient.h>
- #include <ESP8266WebServer.h>
- #include <ESP8266HTTPClient.h>
- /* Desired credentials. */
- const char *ssid = "ZONG-E5573";
- const char *password = "42546791";
- //Web/Server address to read/write from
- const char *host = "192.168.8.100"; //website or IP address of server
- void setup() {
- delay(1000);
- Serial.begin(115200);
- WiFi.mode(WIFI_OFF); //Prevents reconnection issue (taking too long to connect)
- delay(1000);
- WiFi.mode(WIFI_STA); //This line hides the viewing of ESP as wifi hotspot
- WiFi.begin(ssid, password); //Connect to your WiFi router
- Serial.println("");
- Serial.print("Connecting");
- // Wait for connection
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- //If connection successful show IP address in serial monitor
- Serial.println("");
- Serial.print("Connected to ");
- Serial.println(ssid);
- Serial.print("IP address: ");
- Serial.println(WiFi.localIP()); //IP address assigned to your ESP
- }
- //=======================================================================
- // Main Program Loop
- //=======================================================================
- void loop() {
- HTTPClient http; //Declare object of class HTTPClient
- String ADCData, station, postData;
- int adcvalue=random(10,40); //Random integer value
- ADCData = String(adcvalue); //String to interger conversion
- station = "A";
- //Post Data
- postData = "status=" + ADCData + "&station=" + station ;
- http.begin("http://192.168.8.100/fypdemo/postdemo.php"); //Specify request destination
- http.addHeader("Content-Type", "application/x-www-form-urlencoded"); //Specify content-type header
- int httpCode = http.POST(postData); //Send the request
- String payload = http.getString(); //Get the response payload
- Serial.println(httpCode); //Print HTTP return code
- Serial.println(payload); //Print request response payload
- Serial.println(postData);
- http.end(); //Close connection
- delay(5000); //Post Data at every 5 seconds
- }
- <?php
- //Creates new record as per request
- //Connect to database
- $servername = "localhost";
- $username = "root";
- $password = "";
- $dbname = "espdemo";
- // Create connection
- $conn = new mysqli($servername, $username, $password, $dbname);
- // Check connection
- if ($conn->connect_error) {
- die("Database Connection failed: " . $conn->connect_error);
- }
- //Get current date and time
- date_default_timezone_set('Asia/Kolkata');
- $d = date("Y-m-d");
- //echo " Date:".$d."<BR>";
- $t = date("H:i:s");
- if(!empty($_POST['status']) && !empty($_POST['station']))
- {
- $status = $_POST['status'];
- $station = $_POST['station'];
- $sql = "INSERT INTO logs (station, status, Date, Time)
- VALUES ('".$station."', '".$status."', '".$d."', '".$t."')";
- if ($conn->query($sql) === TRUE) {
- echo "OK";
- } else {
- echo "Error: " . $sql . "<br>" . $conn->error;
- }
- }
- $conn->close();
- ?>
Add Comment
Please, Sign In to add comment