Advertisement
Guest User

espcode2

a guest
Jul 25th, 2022
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <SoftwareSerial.h>
  3. #include <ESP8266HTTPClient.h>
  4. #include <WiFiClient.h>
  5.  
  6. SoftwareSerial s(3,1);
  7. HTTPClient http;
  8. WiFiClient client;
  9. int httpCode;
  10. String payload;
  11.  
  12. // Replace with your network credentials
  13. const char* ssid = "RIFQA";
  14. const char* password = "09200011";
  15.  
  16. // Set web server port number to 80
  17. WiFiServer server(80);
  18.  
  19. // Variable to store the HTTP request
  20. String header;
  21.  
  22. // Auxiliar variables to store the current output state
  23. String output5State = "off";
  24. String output4State = "off";
  25.  
  26. // Assign output variables to GPIO pins
  27. const int output5 = 5;
  28. const int output4 = 4;
  29.  
  30. // Current time
  31. unsigned long currentTime = millis();
  32. // Previous time
  33. unsigned long previousTime = 0;
  34. // Define timeout time in milliseconds (example: 2000ms = 2s)
  35. const long timeoutTime = 2000;
  36.  
  37. // Set your Static IP address
  38. IPAddress local_IP(192, xxx, xxx, xxx);
  39. // Set your Gateway IP address
  40. IPAddress gateway(192, xxx, xxx, 1);
  41.  
  42. IPAddress subnet(255, 255, 255, 0);
  43. //IPAddress primaryDNS(8, 8, 8, 8); //optional
  44. //IPAddress secondaryDNS(8, 8, 4, 4); //optional
  45.  
  46. void setup()
  47. {
  48. s.begin(115200);
  49. // Initialize the output variables as outputs
  50. pinMode(output5, OUTPUT);
  51. pinMode(output4, OUTPUT);
  52. // Set outputs to LOW
  53. digitalWrite(output5, LOW);
  54. digitalWrite(output4, LOW);
  55.  
  56. // Configures static IP address
  57. if (!WiFi.config(local_IP, gateway, subnet)) {
  58. s.println("STA Failed to configure");
  59. }
  60.  
  61. // Connect to Wi-Fi network with SSID and password
  62. s.print("Connecting to ");
  63. s.println(ssid);
  64. WiFi.begin(ssid, password);
  65. while (WiFi.status() != WL_CONNECTED) {
  66. delay(500);
  67. s.print(".");
  68. }
  69. // Print local IP address and start web server
  70. s.println("");
  71. s.println("WiFi connected.");
  72. s.println("IP address: ");
  73. s.println(WiFi.localIP());
  74. server.begin();
  75. }
  76.  
  77. void loop()
  78. {
  79. if (WiFi.status() == WL_CONNECTED)
  80. {
  81. http.begin(client, "http://192.xxx.xxx.xxx:80/skripsi/cekktp.php?id=136410060212");
  82. httpCode = http.GET();
  83. if (httpCode > 0)
  84. {
  85. payload = http.getString();
  86. if(payload.equals("b")) s.print('b');
  87. else if(payload.equals("c")) s.print('c');
  88. }
  89. else
  90. {
  91. s.print('x');//connection refused / read timeout
  92. s.println(http.errorToString(httpCode));
  93. }
  94.  
  95. http.end(); //Close connection
  96. }
  97.  
  98. delay(1000);
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement