Guest User

Untitled

a guest
Jan 16th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. #include "WiFiEsp.h"
  2.  
  3. // Emulate Serial1 on pins 6/7 if not present
  4. #ifndef HAVE_HWSERIAL1
  5. #include "SoftwareSerial.h"
  6. SoftwareSerial Serial1(2,3); // RX, TX
  7. #endif
  8.  
  9. char ssid[] = "RPi"; // your network SSID (name)
  10. char pass[] = "raspberry"; // your network password
  11. int status = WL_IDLE_STATUS; // the Wifi radio's status
  12.  
  13. char server[] = "192.168.50.1";
  14.  
  15. unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
  16. const unsigned long postingInterval = 10000L; // delay between updates, in milliseconds
  17. int testLED = 12;
  18. // Initialize the Ethernet client object
  19. WiFiEspClient client;
  20.  
  21. void setup()
  22. {
  23. pinMode(testLED, OUTPUT);
  24. // initialize serial for debugging
  25. Serial.begin(9600);
  26. // initialize serial for ESP module
  27. Serial1.begin(9600);
  28. // initialize ESP module
  29. WiFi.init(&Serial1);
  30.  
  31. // check for the presence of the shield
  32. if (WiFi.status() == WL_NO_SHIELD) {
  33. Serial.println("WiFi shield not present");
  34. // don't continue
  35. while (true);
  36. }
  37.  
  38. // attempt to connect to WiFi network
  39. while ( status != WL_CONNECTED) {
  40. Serial.print("Attempting to connect to WPA SSID: ");
  41. Serial.println(ssid);
  42. // Connect to WPA/WPA2 network
  43. status = WiFi.begin(ssid, pass);
  44. }
  45.  
  46. Serial.println("You're connected to the network");
  47.  
  48. //printWifiStatus();
  49. }
  50.  
  51. void loop()
  52. {
  53. // if there's incoming data from the net connection send it out the serial port
  54. // this is for debugging purposes only
  55. while (client.available()) {
  56. char c = client.read();
  57. //code to run relay
  58. if(c == '#')
  59. {
  60. while(client.available() == 0) { } // Do nothing while we wait for the next value
  61.  
  62. // reading the second charactor
  63. c = client.read();
  64. if(c == '1'){
  65. Serial.println("On");
  66. digitalWrite(testLED, HIGH);
  67. delay(600000);
  68.  
  69. }
  70. else if(c == '0'){
  71. Serial.println("Off");
  72. digitalWrite(testLED, LOW);
  73. delay(600000);
  74.  
  75.  
  76. } //end of C cheking if-else
  77.  
  78.  
  79. } //end of first charactor # if
  80. //end of replay code
  81.  
  82.  
  83. }
  84.  
  85. // if 10 seconds have passed since your last connection,
  86. // then connect again and send data
  87. if (millis() - lastConnectionTime > postingInterval) {
  88. httpRequest();
  89. }
  90. }
  91.  
  92. // this method makes a HTTP connection to the server
  93. void httpRequest()
  94. {
  95. Serial.println();
  96.  
  97. // close any connection before send a new request
  98. // this will free the socket on the WiFi shield
  99. client.stop();
  100.  
  101. // if there's a successful connection
  102. if (client.connect(server, 80)) {
  103. Serial.println("Connecting...");
  104.  
  105. // send the HTTP PUT request
  106. client.println(F("GET /simple.txt HTTP/1.1"));
  107. client.println(F("Host: localhost"));
  108. client.println("Connection: close");
  109. client.println();
  110. // note the time that the connection was made
  111. lastConnectionTime = millis();
  112. }
  113. else {
  114.  
  115. WiFi.begin(ssid, pass);
  116. Serial.println("Reconnecting again");
  117. }
  118. }
Add Comment
Please, Sign In to add comment