Advertisement
safwan092

Untitled

Feb 5th, 2023
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 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 "iotdashboard2792021.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 parking1 = 0;
  15. int parking2 = 0;
  16. int parking3 = 0;
  17. int parking4 = 0;
  18. int parking5 = 0;
  19. int parking6 = 0;
  20.  
  21.  
  22. String sendparking1, sendparking2, sendparking3, sendparking4, sendparking5, sendparking6, sendled1, sendled2, sendled3, sendled4, sendled5, sendled6, sendtemp, sendhum, sendpanic, postData;
  23.  
  24.  
  25. void setup() {
  26. Serial.begin(115200);
  27.  
  28. WiFi.begin(ssid, password);
  29. Serial.println("Connecting");
  30. while (WiFi.status() != WL_CONNECTED) {
  31. delay(500);
  32. Serial.print(".");
  33. }
  34. Serial.println("");
  35. Serial.print("Connected to WiFi network with IP Address: ");
  36. Serial.println(WiFi.localIP());
  37. }
  38.  
  39. void loop() {
  40. HTTPClient http; // http object of clas HTTPClient
  41.  
  42. parking1 = digitalRead(5); // D1 = 5
  43. parking2 = digitalRead(4); // D2 = 4
  44. parking3 = digitalRead(14); // D5 = 14
  45. parking4 = digitalRead(12); // D6 = 12
  46. parking5 = digitalRead(13); // D7 = 13
  47. parking6 = digitalRead(16); // D0 = 16
  48.  
  49. if (parking1 == 0) {
  50. parking1 = 2;
  51. }
  52. if (parking2 == 0) {
  53. parking2 = 2;
  54. }
  55. if (parking3 == 0) {
  56. parking3 = 2;
  57. }
  58. if (parking4 == 0) {
  59. parking4 = 2;
  60. }
  61. if (parking5 == 0) {
  62. parking5 = 2;
  63. }
  64. if (parking6 == 0) {
  65. parking6 = 2;
  66. }
  67.  
  68.  
  69. // Convert integer variables to string
  70. sendparking1 = String(parking1);
  71. sendparking2 = String(parking2);
  72. sendparking3 = String(parking3);
  73. sendparking4 = String(parking4);
  74. sendparking5 = String(parking5);
  75. sendparking6 = String(parking6);
  76.  
  77.  
  78. postData = "sendparking1=" + sendparking1 + "&sendparking2=" + sendparking2 + "&sendparking3=" + sendparking3 + "&sendparking4=" + sendparking4 + "&sendparking5=" + sendparking5 + "&sendparking6=" + sendparking6;
  79.  
  80.  
  81. http.begin("http://iotdashboard2792021.000webhostapp.com/dbwrite.php"); // Connect to host where MySQL databse is hosted
  82. http.addHeader("Content-Type", "application/x-www-form-urlencoded"); //Specify content-type header
  83.  
  84.  
  85.  
  86. int httpCode = http.POST(postData); // Send POST request to php file and store server response code in variable named httpCode
  87. //Serial.println("Values are, sp1 = " + sendparking1 + " and sp2 = "+sendparking2 + " and sp3 = "+sendparking3 + " and sp4 = "+sendparking4 + " and sp5 = "+sendparking5 + " and sp6 = "+sendparking6 );
  88.  
  89.  
  90. // if connection eatablished then do this
  91. if (httpCode == 200) {
  92. Serial.println("Values uploaded successfully."); Serial.println(httpCode);
  93. String webpage = http.getString(); // Get html webpage output and store it in a string
  94. Serial.println(webpage + "\n");
  95. }
  96.  
  97. // if failed to connect then return and restart
  98.  
  99. else {
  100. Serial.println(httpCode);
  101. Serial.println("Failed to upload values. \n");
  102. http.end();
  103. return;
  104. }
  105.  
  106.  
  107. delay(3000);
  108. delay(3000);
  109.  
  110. }
  111.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement