Guest User

Untitled

a guest
Nov 22nd, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <UIPEthernet.h>
  3. #include <PubSubClient.h>
  4. #include <Pir.h>
  5. #include <avr/wdt.h>
  6.  
  7. // Update these with values suitable for your network.
  8. String devices[] = {"PR1"};
  9. String boardName = "BA01";
  10. String boardsChannel = "wrks_boards";
  11. String sensorsChannel = "wrks_sensors/BA01";
  12.  
  13. EthernetClient ethClient;
  14. PubSubClient client(ethClient);
  15. int pr1Pin = 2; // choose the input pin (for PIR sensor)
  16. int pr1State = LOW; // we start, assuming no motion detected
  17. int pr1Val = 0; // variable for reading the pin status
  18.  
  19. void setup()
  20. {
  21. // BEGIN Ethernet client declaration
  22. // Change MAC Address
  23. byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
  24. // MQTT Server IP Address
  25. IPAddress server(192, 168, 1, 181);
  26. Serial.print("Ethernet Begin");
  27. Serial.begin(9600);
  28. Ethernet.begin(mac);
  29. Serial.print("Board IP Address: ");
  30. Serial.println(Ethernet.localIP());
  31. client.setServer(server, 1883);
  32. // END Ethernet client declaration
  33.  
  34. pinMode(pr1Pin, INPUT); // declare PIR sensor as input
  35.  
  36. // Allow the hardware to sort itself out
  37. delay(2000);
  38. }
  39.  
  40. void loop()
  41. {
  42. g5 = digitalRead(pr1Pin); // read input value
  43. if (pr1Val == HIGH) { // check if the input is HIGH
  44. if (pr1State == LOW) {
  45. // we have just turned on
  46. Serial.println("Motion detected!");
  47. // We only want to print on the output change, not state
  48. pr1State = HIGH;
  49. }
  50. } else {
  51. digitalWrite(ledPin, LOW); // turn LED OFF
  52. if (pirState == HIGH){
  53. // we have just turned of
  54. Serial.println("Motion ended!");
  55. // We only want to print on the output change, not state
  56. pirState = LOW;
  57. }
  58. }
  59. }
Add Comment
Please, Sign In to add comment