Advertisement
Guest User

Untitled

a guest
Apr 15th, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <OneWire.h>
  3. #include <DallasTemperature.h>
  4. //#include <WiFiEspClient.h>
  5.  
  6.  
  7. /********************************************************************/
  8. // Data wire is plugged into pin 2 on the Arduino
  9. #define ONE_WIRE_BUS D4
  10.  
  11. /********************************************************************/
  12. // Setup a oneWire instance to communicate with any OneWire devices
  13. // (not just Maxim/Dallas temperature ICs)
  14. OneWire oneWire(ONE_WIRE_BUS);
  15.  
  16. /********************************************************************/
  17. // Pass our oneWire reference to Dallas Temperature.
  18. DallasTemperature sensors(&oneWire);
  19.  
  20. // Init library
  21. WiFiClient client;
  22. //dht DHT;
  23.  
  24. // Wifi config, ssid = wifi name & password = wifi password
  25. const char* ssid = "ATkudnisdb0TAbEdfhrsdhg7i2";
  26. const char* password = "2pkpmp3niagfdhgsdfhg%";
  27. // Get data host
  28. const char* host = "172.168.2.143"; // Your domain remember don't add slash at the end or http it will not work
  29. const char* streamId = "collectdata.php";
  30.  
  31. // Sensor values
  32. //int humidityValue = 0;
  33. int tempValue = 0;
  34.  
  35. void sendRequest(){
  36. Serial.print("connecting to ");
  37. Serial.println(host);
  38.  
  39. // Use WiFiClient class to create TCP connections
  40. WiFiClient client;
  41. const int httpPort = 80;
  42. if (!client.connect(host, httpPort)) {
  43. Serial.println("connection failed");
  44.  
  45. }
  46. // We now create a URI for the request
  47. String url = "/php/";
  48. url += streamId;
  49. url += "?temp=";
  50. url += tempValue;
  51.  
  52. Serial.print("Requesting URL: ");
  53. Serial.println(url);
  54.  
  55. // This will send the request to the server
  56. client.print("POST " + url + " HTTP/1.1rn" +
  57. "Host: " + host + "rn" +
  58. "Connection: closernrn");
  59.  
  60.  
  61. delay(1000);
  62.  
  63. Serial.println();
  64. Serial.println("closing connection");
  65.  
  66. delay(2500);
  67. }
  68.  
  69. void setup() {
  70. sensors.begin();
  71. Serial.begin(9600);
  72.  
  73. Serial.println();
  74. Serial.print("Connecting to ");
  75. Serial.println(ssid);
  76.  
  77. WiFi.begin(ssid, password);
  78.  
  79. while (WiFi.status() != WL_CONNECTED) {
  80. delay(500);
  81. Serial.print(".");
  82. }
  83. Serial.println("");
  84. Serial.println("WiFi connected");
  85. Serial.println("IP address: ");
  86. Serial.println(WiFi.localIP());
  87. //digitalWrite(blue,HIGH);
  88. }
  89.  
  90. void loop() {
  91. Serial.print(" Requesting temperatures...");
  92. sensors.requestTemperatures();
  93. tempValue = sensors.getTempCByIndex(0);
  94. Serial.print(tempValue);// Send the command to get temperature readings
  95.  
  96. sendRequest();
  97. }
  98.  
  99. PHP CODE:
  100. <?php
  101. $servername = “localhost”;
  102. $username = “esp8266”;
  103. $password = “Tutorial”;
  104. $dbname = “esp8266”;
  105.  
  106. $mood = $_GET[‘mood’];
  107. $conn = mysql_connect(“localhost”,”esp8266”,”Tutorial”);
  108. if(!$conn)
  109. {
  110. die(‘Could not connect: ’ . mysql_error());
  111. }
  112. $datenow = date(‘Y-m-d’);
  113. $sql = “INSERT INTO `JSDataTable`(`logdate`,`mood`) VALUES (”$datenow”,”$mood”)”;
  114. $result = mysql_query($sql);
  115. if(!result)
  116. {
  117. die(‘Invalid query: ‘ . mysql_error());
  118. }
  119. echo “<h1>The data has been sent!</h1>”;
  120. mysql_close($conn);
  121. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement