Code: Select all
/*LIBRERIAS Y COMPONENTES*/
#include <Adafruit_SleepyDog.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include <TinyGPSPlus.h>
#include <SoftwareSerial.h>
#include <ESP8266WiFi.h>
#define CORTACORRIENTE D5 //
#define BLOQUEO D7 //
#define DESBLOQUEO D3 //
static const int RXPin = D1, TXPin = D2; // Ublox 6m GPS module to pins 12 and 13
static const uint32_t GPSBaud = 9600; // Ublox GPS default Baud Rate is 9600
TinyGPSPlus gps; // Create an Instance of the TinyGPS++ object called gps
SoftwareSerial ss(RXPin, TXPin);
const double HOME_LAT = ; // Enter Your Latitude and Longitude here
const double HOME_LNG = ;
/************************* CONEXION WIFI *********************************/
#ifndef STASSID
#define STASSID ""
#define STAPSK ""
const char *ssid = STASSID;
const char *pass = STAPSK;
/************************* ADAFRUIT CLAVES *********************************/
#define AIO_SERVER "io.adafruit.com"
#define AIO_SERVERPORT 1883 // use 8883 for SSL
#define AIO_USERNAME "" // USERNAME
#define AIO_KEY "" // LLAVE DE ADAFRUIT
#endif
/************ CONTROL DEL FAN ******************/
int boton = D4;
int motor = D0;
int status= true;
/************ SERVIDOR GLOBAL ******************/
// WIFICLIENT
WiFiClient client;
////CONEXION WEBSERVER
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
/****************************** BANNED A ENVIAR A ADAFRUIT ***************************************/
Adafruit_MQTT_Subscribe onoff1 = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/CORTACORRIENTE");
Adafruit_MQTT_Subscribe onoff2 = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/BLOQUEO");
Adafruit_MQTT_Subscribe onoff3 = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/DESBLOQUEO");
const char GPSLOC_FEED[] PROGMEM = AIO_USERNAME "/feeds/gps/csv";
Adafruit_MQTT_Publish gpsloc = Adafruit_MQTT_Publish(&mqtt, GPSLOC_FEED);
/*************************** CODIGO ************************************/
void MQTT_connect();
void setup() {
Serial.begin(115200);
ss.begin(GPSBaud);
delay(10);
pinMode(CORTACORRIENTE, OUTPUT);
pinMode(BLOQUEO, OUTPUT);
pinMode(DESBLOQUEO, OUTPUT);
pinMode(motor, OUTPUT);
pinMode(boton, INPUT_PULLUP); // con el transistor define la entrada de voltaje del boton
digitalWrite(CORTACORRIENTE,HIGH);
digitalWrite(BLOQUEO,HIGH);
digitalWrite(DESBLOQUEO,HIGH);
// Connect to WiFi access point.
Serial.begin(115200);
Serial.println();
Serial.println();
// We start by connecting to a WiFi network
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
///DEFINIR A RECIBIR O ENVIAR
mqtt.subscribe(&onoff1);
mqtt.subscribe(&onoff2);
mqtt.subscribe(&onoff3);
}
uint32_t x=0;
void loop() {
////**CONTROL DEL MOTOR(FAN)**/////
if (digitalRead(boton) == false) {
status = !status;
digitalWrite(motor, status);
}
{
while(digitalRead(boton) == false);
delay(5); // un delay pequeño
}
///////MQTT BANNED:
smartDelay(500);
MQTT_connect();
Adafruit_MQTT_Subscribe *subscription;
while ((subscription = mqtt.readSubscription(5000))) {
if (subscription == &onoff1){
Serial.print(F("Got: "));
Serial.println((char *)onoff1.lastread);
uint16_t state1 = atoi((char *)onoff1.lastread);
Serial.println(state1);
digitalWrite(CORTACORRIENTE,state1);
}
if (subscription == &onoff2){
Serial.print(F("Got: "));
Serial.println((char *)onoff2.lastread);
uint16_t state2 = atoi((char *)onoff2.lastread);
Serial.println(state2);
digitalWrite(BLOQUEO,state2);
}
if (subscription == &onoff3){
Serial.print(F("Got: "));
Serial.println((char *)onoff3.lastread);
uint16_t state3 = atoi((char *)onoff3.lastread);
Serial.println(state3);
digitalWrite(DESBLOQUEO,state3);
}
}
// ping the server to keep the mqtt connection alive
// NOT required if you are publishing once every KEEPALIVE seconds
/*
if(! mqtt.ping()) {
mqtt.disconnect();
}
*/
smartDelay(500);
float GPSlat = (gps.location.lat()); // variable to store latitude
float GPSlng = (gps.location.lng()); // variable to store longitude
float GPSalt = (gps.altitude.feet()); // variable to store altitude
float Distance_To_Home; // variable to store altitude
Distance_To_Home = (unsigned long)TinyGPSPlus::distanceBetween(gps.location.lat(),gps.location.lng(),HOME_LAT, HOME_LNG);
Serial.print(0,0);
Serial.print(F(" GPS Tracking"));
Serial.print("GPS Lat: ");
Serial.print(gps.location.lat(), 6); // Display latitude to 6 decimal points
Serial.print("GPS Lon: ");
Serial.print(gps.location.lng(), 6);
Serial.println("Distance: ");
Serial.println(Distance_To_Home);
char sendbuffer[200];
char *p= sendbuffer;
dtostrf(Distance_To_Home, 3, 4, p); // Convert Distance to Home to a String Variable and add it to the buffer
p += strlen(p);
p[0] = ','; p++;
dtostrf(GPSlat, 2, 6, p);
p += strlen(p);
p[0] = ','; p++;
dtostrf(GPSlng, 3, 6, p); // Convert GPSlng(longitude) to a String variable and add it to the buffer
p += strlen(p);
p[0] = ','; p++;
dtostrf(GPSalt, 2, 6, p); // Convert GPSalt(altimeter) to a String variable and add it to the buffer
p += strlen(p);
p[0] = 0;
Serial.print("Sending: "); Serial.print(sendbuffer);
if (! gpsloc.publish(15)) {
Serial.println(F("Failed"));
} else {
Serial.println(F("OK!"));
}
smartDelay(500);
}
// **************** Smart delay - used to feed TinyGPS ****************
static void smartDelay(unsigned long ms)
{
unsigned long start = millis();
do
{
while (ss.available())
gps.encode(ss.read());
} while (millis() - start < ms);
}
void MQTT_connect() {
int8_t ret;
// Stop if already connected.
if (mqtt.connected()) {
return;
}
Serial.print("Connecting to MQTT... ");
uint8_t retries = 3;
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(5000); // wait 5 seconds
retries--;
if (retries == 0) {
// basically die and wait for WDT to reset me
while (1);
}
}
Serial.println("MQTT Connected!");
}
Code: Select all
--------------- CUT HERE FOR EXCEPTION DECODER ---------------
Exception (3):
epc1=0x4000bf64 epc2=0x00000000 epc3=0x00000000 excvaddr=0x4023ec85 depc=0x00000000
>>>stack>>>
ctx: cont
sp: 3ffffd10 end: 3fffffc0 offset: 0190
3ffffea0: 40204268 3ffffed0 3ffeeb24 402044d8
3ffffeb0: 3fffff40 00000096 00000000 40206314
3ffffec0: 3ffe8b40 3ffeeb24 3ffeeb24 c29595cf
3ffffed0: 00000000 3ffee8a0 3ffee87c 40201a88
3ffffee0: 00000096 3ffe8b3e 3ffeeb24 40204274
3ffffef0: 3ffeea10 00000000 3ffee87c 40201b08
3fffff00: 4023ec85 3fffff40 3ffeeb24 402045ac
3fffff10: d972cd7d 3ffeeb24 00000000 40201c88
3fffff20: 3ffeea10 3ffeeb24 00000000 402014b8
3fffff30: d7342edc c0016469 00000000 3ffeebb0
3fffff40: 33363737 2e373032 30303030 2e30312c
3fffff50: 32353239 2d2c3537 372e3437 39353239
3fffff60: 39312c35 00322e36 e0000000 BANNED
3fffff70: 412ecded 0000002c 415d9d41 434431b9
3fffff80: 00000000 3ffeeae8 3ffeeb24 402010de
3fffff90: 00000040 00000000 feefeffe 3ffeebb0
3fffffa0: 3fffdad0 00000000 3ffeeb9c 4020517c
3fffffb0: feefeffe feefeffe 3ffe85d8 401011e1
<<<stack<<<
--------------- CUT HERE FOR EXCEPTION DECODER ---------------
ets Jan 8 2013,rst cause:2, boot mode:(3,6)
load 0x4010f000, len 3460, room 16
tail 4
chksum 0xcc
load 0x3fff20b8, len 40, room 4
tail 4
chksum 0xc9
csum 0xc9
v00049250
~ld