safwan092

Untitled

Feb 5th, 2023
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1.  
  2. #include <WiFi.h>
  3. #include <HTTPClient.h>
  4.  
  5. // Replace with your network credentials
  6. const char* ssid = "network";
  7. const char* password = "123456789";
  8.  
  9. // Update HOST URL here
  10.  
  11. #define HOST "safetyhelmetiot.000webhostapp.com" // Enter HOST URL without "http:// " and "/" at the end of URL
  12. // Declare global variables which will be uploaded to server
  13.  
  14. int humidity = 0;
  15. int temp = 0;
  16. int gas = 0;
  17. int pressure = 0;
  18. int hr = 0;
  19. int o_r = 0;
  20. int gpslat = 0;
  21. int gpslon = 0;
  22.  
  23. String sendhumidity, sendtemp, sendgas, sendpressure, sendhr, sendo_r, sendgpslat, sendgpslon, postData;
  24.  
  25.  
  26. void setup() {
  27. Serial.begin(115200);
  28.  
  29. WiFi.begin(ssid, password);
  30. Serial.println("Connecting");
  31. while (WiFi.status() != WL_CONNECTED) {
  32. delay(500);
  33. Serial.print(".");
  34. }
  35. Serial.println("");
  36. Serial.print("Connected to WiFi network with IP Address: ");
  37. Serial.println(WiFi.localIP());
  38. }
  39.  
  40. void loop() {
  41. HTTPClient http; // http object of clas HTTPClient
  42.  
  43. // Convert integer variables to string
  44. sendhumidity = String(100);
  45. sendtemp = String(20);
  46. sendgas = String(1503);
  47. sendpressure = String(1224);
  48. sendhr = String(85);
  49. sendo_r = String(96);
  50. sendgpslat = String("7.12654");
  51. sendgpslon = String("8.12356");
  52.  
  53. postData = "sendhumidity=" + sendhumidity + "&sendtemp=" + sendtemp + "&sendgas=" + sendgas + "&sendpressure=" + sendpressure + "&sendhr=" + sendhr + "&sendo_r=" + sendo_r + "&sendgpslat=" + sendgpslat + "&sendgpslon=" + sendgpslon;
  54.  
  55.  
  56. http.begin("http://safetyhelmetiot.000webhostapp.com/dbwrite.php"); // Connect to host where MySQL databse is hosted
  57. http.addHeader("Content-Type", "application/x-www-form-urlencoded"); //Specify content-type header
  58.  
  59.  
  60.  
  61. int httpCode = http.POST(postData); // Send POST request to php file and store server response code in variable named httpCode
  62.  
  63. // if connection eatablished then do this
  64. if (httpCode == 200) {
  65. Serial.println("Values uploaded successfully."); Serial.println(httpCode);
  66. String webpage = http.getString(); // Get html webpage output and store it in a string
  67. Serial.println(webpage + "\n");
  68. }
  69.  
  70. // if failed to connect then return and restart
  71.  
  72. else {
  73. Serial.println(httpCode);
  74. Serial.println("Failed to upload values. \n");
  75. http.end();
  76. return;
  77. }
  78.  
  79. delay(3000);
  80.  
  81. }
Add Comment
Please, Sign In to add comment