Guest User

Untitled

a guest
Nov 12th, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <WiFiClient.h>
  3. #include <ESP8266WebServer.h>
  4. #include <ESP8266HTTPClient.h>
  5.  
  6. /* Desired credentials. */
  7. const char *ssid = "ZONG-E5573";
  8. const char *password = "42546791";
  9.  
  10. //Web/Server address to read/write from
  11. const char *host = "192.168.8.100"; //website or IP address of server
  12. void setup() {
  13. delay(1000);
  14. Serial.begin(115200);
  15. WiFi.mode(WIFI_OFF); //Prevents reconnection issue (taking too long to connect)
  16. delay(1000);
  17. WiFi.mode(WIFI_STA); //This line hides the viewing of ESP as wifi hotspot
  18.  
  19. WiFi.begin(ssid, password); //Connect to your WiFi router
  20. Serial.println("");
  21.  
  22. Serial.print("Connecting");
  23. // Wait for connection
  24. while (WiFi.status() != WL_CONNECTED) {
  25. delay(500);
  26. Serial.print(".");
  27. }
  28.  
  29. //If connection successful show IP address in serial monitor
  30. Serial.println("");
  31. Serial.print("Connected to ");
  32. Serial.println(ssid);
  33. Serial.print("IP address: ");
  34. Serial.println(WiFi.localIP()); //IP address assigned to your ESP
  35. }
  36.  
  37. //=======================================================================
  38. // Main Program Loop
  39. //=======================================================================
  40. void loop() {
  41. HTTPClient http; //Declare object of class HTTPClient
  42.  
  43. String ADCData, station, postData;
  44. int adcvalue=random(10,40); //Random integer value
  45. ADCData = String(adcvalue); //String to interger conversion
  46. station = "A";
  47.  
  48. //Post Data
  49. postData = "status=" + ADCData + "&station=" + station ;
  50.  
  51. http.begin("http://192.168.8.100/fypdemo/postdemo.php"); //Specify request destination
  52. http.addHeader("Content-Type", "application/x-www-form-urlencoded"); //Specify content-type header
  53.  
  54. int httpCode = http.POST(postData); //Send the request
  55. String payload = http.getString(); //Get the response payload
  56.  
  57. Serial.println(httpCode); //Print HTTP return code
  58. Serial.println(payload); //Print request response payload
  59. Serial.println(postData);
  60.  
  61. http.end(); //Close connection
  62.  
  63. delay(5000); //Post Data at every 5 seconds
  64. }
  65.  
  66. <?php
  67. //Creates new record as per request
  68. //Connect to database
  69. $servername = "localhost";
  70. $username = "root";
  71. $password = "";
  72. $dbname = "espdemo";
  73.  
  74. // Create connection
  75. $conn = new mysqli($servername, $username, $password, $dbname);
  76. // Check connection
  77. if ($conn->connect_error) {
  78. die("Database Connection failed: " . $conn->connect_error);
  79. }
  80.  
  81. //Get current date and time
  82. date_default_timezone_set('Asia/Kolkata');
  83. $d = date("Y-m-d");
  84. //echo " Date:".$d."<BR>";
  85. $t = date("H:i:s");
  86.  
  87. if(!empty($_POST['status']) && !empty($_POST['station']))
  88. {
  89. $status = $_POST['status'];
  90. $station = $_POST['station'];
  91.  
  92. $sql = "INSERT INTO logs (station, status, Date, Time)
  93.  
  94. VALUES ('".$station."', '".$status."', '".$d."', '".$t."')";
  95.  
  96. if ($conn->query($sql) === TRUE) {
  97. echo "OK";
  98. } else {
  99. echo "Error: " . $sql . "<br>" . $conn->error;
  100. }
  101. }
  102. $conn->close();
  103. ?>
Add Comment
Please, Sign In to add comment