Advertisement
Guest User

Untitled

a guest
May 10th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <PubSubClient.h>
  3.  
  4. // Update these with values suitable for your network.
  5.  
  6. const char* ssid = "wifi";
  7. const char* password = "wifipassword";
  8. const char* mqtt_server = "broker";
  9. const char* mqtt_password = "pass";
  10. const char* mqtt_user = "user";
  11. const int mqtt_port = 1883;
  12.  
  13.  
  14. int ledPin = D3; // choose the pin for the LED
  15. int inputPin = D2; // choose the input pin (for PIR sensor)
  16. int pirState = LOW; // we start, assuming no motion detected
  17. int val = 0;
  18.  
  19. WiFiClient espClient;
  20. PubSubClient client(espClient);
  21. long lastMsg = 0;
  22. char msg[50];
  23. int value = 0;
  24.  
  25. void setup_wifi() {
  26.  
  27. delay(10);
  28. // We start by connecting to a WiFi network
  29. Serial.println();
  30. Serial.print("Connecting to ");
  31. Serial.println(ssid);
  32.  
  33. WiFi.begin(ssid, password);
  34.  
  35. while (WiFi.status() != WL_CONNECTED) {
  36. delay(500);
  37. Serial.print(".");
  38. }
  39.  
  40. randomSeed(micros());
  41.  
  42. Serial.println("");
  43. Serial.println("WiFi connected");
  44. Serial.println("IP address: ");
  45. Serial.println(WiFi.localIP());
  46. }
  47.  
  48. void callback(char* topic, byte* payload, unsigned int length) {
  49. Serial.print("Message arrived [");
  50. Serial.print(topic);
  51. Serial.print("] ");
  52. for (int i = 0; i < length; i++) {
  53. Serial.print((char)payload[i]);
  54. }
  55. Serial.println();
  56.  
  57. // Switch on the LED if an 1 was received as first character
  58. if ((char)payload[0] == '1') {
  59. digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level
  60. // but actually the LED is on; this is because
  61. // it is active low on the ESP-01)
  62. } else {
  63. digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH
  64. }
  65.  
  66. }
  67.  
  68. void reconnect() {
  69. // Loop until we're reconnected
  70. while (!client.connected()) {
  71. Serial.print("Attempting MQTT connection...");
  72. // Create a random client ID
  73. String clientId = "ESP8266Client-";
  74. // Attempt to connect
  75. if (client.connect("prueba", mqtt_user, mqtt_password)) {
  76. Serial.println("connected");
  77. // Once connected, publish an announcement...
  78. client.publish("outTopic", "hello world");
  79. // ... and resubscribe
  80. client.subscribe("inTopic");
  81. } else {
  82. Serial.print("failed, rc=");
  83. Serial.print(client.state());
  84. Serial.println(" try again in 5 seconds");
  85. // Wait 5 seconds before retrying
  86. delay(5000);
  87. }
  88. }
  89. }
  90.  
  91. void setup() {
  92. pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output
  93. Serial.begin(115200);
  94. setup_wifi();
  95. client.setServer(mqtt_server, mqtt_port);
  96. client.setCallback(callback);
  97.  
  98. pinMode(ledPin, OUTPUT); // declare LED as output
  99. pinMode(inputPin, INPUT);
  100. }
  101.  
  102. void loop() {
  103.  
  104. if (!client.connected()) {
  105. reconnect();
  106. }
  107. client.loop();
  108.  
  109. val = digitalRead(inputPin); // read input value
  110. if (val == HIGH) {
  111. if (pirState == LOW) { // check if the input is HIGH
  112. digitalWrite(ledPin, HIGH); // turn LED ON
  113. delay(100);
  114. digitalWrite(ledPin, LOW);
  115. // we have just turned on
  116. Serial.println("Motion detected!");
  117. // We only want to print on the output change, not state
  118. pirState = HIGH;
  119. }
  120. } else {
  121. digitalWrite(ledPin, LOW); // turn LED OFF
  122. if (pirState == HIGH) {
  123. // we have just turned of
  124. //Serial.println("Motion ended!");
  125. // We only want to print on the output change, not state
  126. pirState = LOW;
  127. client.publish("/alerta", "intruso detectado");
  128. }
  129. }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement