Created
January 5, 2019 02:20
-
-
Save yeffrimic/609b10b3a0708e21d20c7133bbe602de to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Base de proyectos | |
*/ | |
#include <ESP8266WiFi.h> | |
#include <PubSubClient.h> | |
#include <TinyGPS++.h> | |
#include <SoftwareSerial.h> | |
#define usuarioServidorMQTT "upbpnxzc" // usuario para acceder al broker | |
#define claveServidorMQTT "OSPLsP4Gq96a" // clave del usuario para acceder al broker | |
#define servidorMQTT "m11.cloudmqtt.com" | |
#define puertoMQTT 14224 | |
#define topicoActualizacion "/update" | |
#define topicoConexion "/conection" //topico que utilizara la opción de birth de mqtt | |
#define mensajeConexion "1" | |
#define mensajeDesconexion "0" | |
#define ledAlarma D3 | |
WiFiClient clienteWifi; | |
PubSubClient client(clienteWifi); | |
int inputPin = D2; // choose the input pin (for PIR sensor) | |
int pirState = LOW; // we start, assuming no motion detected | |
int val = 0; // variable for reading the pin status | |
const char* clienteMQTT = "HackerspaceX"; | |
static const int RXPin = D5, TXPin = D6; | |
static const uint32_t GPSBaud = 9600; | |
boolean estadoLed = true; | |
boolean parpadeoLed = false; | |
long contadorLed; | |
long contadorLed2; | |
int tiempoParpadeo = 500; | |
// The TinyGPS++ object | |
TinyGPSPlus gps; | |
SoftwareSerial ss(RXPin, TXPin); | |
void callback(char* topic, byte* payload, unsigned int length) { | |
// handle message arrived | |
Serial.println("mensaje entrante"); | |
Serial.println(topic); | |
if (String(topic) == "/alarmaOn") { | |
Serial.println("activando alarma"); | |
parpadeoLed = true; | |
} | |
if (String(topic) == "/alarmaOff") { | |
digitalWrite(ledAlarma,LOW); | |
Serial.println("desactivando alarma"); | |
parpadeoLed = false; | |
} | |
} | |
long lastReconnectAttempt = 0; | |
boolean reconnect() { | |
if (client.connect(clienteMQTT, usuarioServidorMQTT, claveServidorMQTT, topicoConexion, 2, 1, mensajeDesconexion)) { | |
client.subscribe(topicoActualizacion); | |
client.subscribe("/alarmaOn"); | |
client.subscribe("/alarmaOff"); | |
client.subscribe(topicoActualizacion); | |
client.publish(topicoConexion, mensajeConexion); | |
} | |
return client.connected(); | |
} | |
void setup() | |
{ | |
pinMode(inputPin, INPUT); // declare sensor as input | |
Serial.begin(115200); | |
ss.begin(GPSBaud); | |
delay(2000); | |
pinMode(ledAlarma, OUTPUT); | |
pinMode(LED_BUILTIN, OUTPUT); | |
digitalWrite(ledAlarma, HIGH); | |
delay(1000); | |
digitalWrite(ledAlarma, LOW); | |
delay(1000); | |
WiFi.begin("Hackerspace", "IOT123456"); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); | |
Serial.println("iniciando program " ); | |
client.setServer(servidorMQTT, puertoMQTT); | |
client.setCallback(callback); | |
lastReconnectAttempt = 0; | |
} | |
void loop() | |
{ | |
if (!client.connected()) { | |
long now = millis(); | |
if (now - lastReconnectAttempt > 5000) { | |
lastReconnectAttempt = now; | |
// Attempt to reconnect | |
if (reconnect()) { | |
lastReconnectAttempt = 0; | |
} | |
} | |
} else { | |
client.loop(); | |
while (ss.available() > 0) | |
if (gps.encode(ss.read())) | |
displayInfo(); | |
if (millis() > 5000 && gps.charsProcessed() < 10) | |
{ | |
Serial.println(F("GPS no detectado, revisar cableado.")); | |
while (true); | |
} | |
val = digitalRead(inputPin); // read input value | |
if (val == HIGH) { // check if the input is HIGH | |
digitalWrite(LED_BUILTIN, HIGH); // turn LED ON | |
if (pirState == LOW) { | |
// we have just turned on | |
Serial.println("Motion detected!"); | |
client.publish("/movimiento", "movimiento detectado "); | |
// We only want to print on the output change, not state | |
pirState = HIGH; | |
} | |
} else { | |
digitalWrite(LED_BUILTIN, LOW); // turn LED OFF | |
if (pirState == HIGH) { | |
// we have just turned of | |
Serial.println("Motion ended!"); | |
client.publish("/movimiento", "movimiento terminado"); | |
// We only want to print on the output change, not state | |
pirState = LOW; | |
} | |
} | |
} | |
} | |
void displayInfo() | |
{ | |
Serial.print(F("Localización: ")); | |
if (gps.location.isValid()) | |
{ | |
Serial.print(gps.location.lat(), 6); | |
Serial.print(F(",")); | |
Serial.println(gps.location.lng(), 6); | |
String localizacion = String(gps.location.lat(), 6) + ',' + String(gps.location.lng(), 6); | |
char dato[localizacion.length() + 1]; | |
localizacion.toCharArray(dato, localizacion.length()); | |
client.publish("/gps", dato); | |
} | |
else | |
{ | |
Serial.println(F("INVALIDO")); | |
client.publish("/gps", "sin señal"); | |
} | |
contadorLed = millis(); | |
if (contadorLed - contadorLed2 > tiempoParpadeo && parpadeoLed == true) { | |
contadorLed2 = contadorLed; | |
Serial.println(estadoLed); | |
digitalWrite(ledAlarma, estadoLed); | |
estadoLed = !estadoLed; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment