Last active
September 13, 2018 22:57
-
-
Save KEVINALBO13/504de06d12a2e68c23f4d9a471c75d37 to your computer and use it in GitHub Desktop.
CODIGO BASE DE PROYECTO 13/9/2018
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
#include <Servo.h> | |
#include <WiFi.h> | |
#include <PubSubClient.h> | |
const char* ssid = "ALSW"; | |
const char* password = "2526-4897"; | |
const char* mqtt_server = "test.mosquitto.org"; | |
int mqtt_port = 1883; | |
WiFiClient espClient; | |
PubSubClient client(espClient); | |
static const int PinServoVidrio = 4; | |
static const int PinServoPlastico = 17; | |
static const int PinServoLata = 16; | |
static const int PinDireccion = 18; | |
static const int PinPulso = 23; | |
int Estado = 0; // 0 Nada - 1 Plastico -... | |
#define Plastico 1 | |
#define Vidrio 2 | |
#define Lata 3 | |
#define Nada 0 | |
#define PosicionDescanzo 10 | |
#define PosicionActivo 60 | |
Servo ServoVidrio; | |
Servo ServoPlastico; | |
Servo ServoLata; | |
void setup() { | |
Serial.begin(115200); | |
ServoVidrio.attach(PinServoVidrio); | |
ServoPlastico.attach(PinServoPlastico); | |
ServoLata.attach(PinServoLata); | |
pinMode(PinDireccion, OUTPUT); | |
digitalWrite(PinDireccion, 0); | |
pinMode(PinPulso, OUTPUT); | |
digitalWrite(PinPulso, 0); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); | |
client.setServer(mqtt_server, 1883); | |
client.setCallback(callback); | |
} | |
void loop () { | |
if (!client.connected()) { | |
reconnect(); | |
} | |
client.loop(); | |
MoverMotorPaso() ; | |
switch (Estado) { | |
case Nada : | |
ServoVidrio.write(PosicionDescanzo); | |
ServoPlastico.write(PosicionDescanzo); | |
ServoLata.write(PosicionDescanzo); | |
Estado = 5; | |
break; | |
case Vidrio: | |
ServoVidrio.write(PosicionActivo); | |
ServoPlastico.write(PosicionDescanzo); | |
ServoLata.write(PosicionDescanzo); | |
Estado = 5; | |
break; | |
case Plastico: | |
ServoVidrio.write(PosicionDescanzo); | |
ServoPlastico.write(PosicionActivo); | |
ServoLata.write(PosicionDescanzo); | |
Estado = 5; | |
break; | |
case Lata: | |
ServoVidrio.write(PosicionDescanzo); | |
ServoPlastico.write(PosicionDescanzo); | |
ServoLata.write(PosicionActivo); | |
Estado = 5; | |
break; | |
} | |
} | |
void MoverMotorPaso() { | |
digitalWrite(PinPulso, 0); | |
delay(1); | |
digitalWrite(PinPulso, 1); | |
delay(1); | |
} | |
void reconnect() { | |
while (!client.connected()) { | |
Serial.print("Intentando Conectar con MQTT..."); | |
if (client.connect("ESP32Client")) { | |
Serial.println ("connected"); | |
client.subscribe("itx/entrada"); | |
} else { | |
Serial.print("Error, rc="); | |
Serial.print(client.state()); | |
Serial.println(", Intentando de nuevo 5 segundos"); | |
delay(5000); | |
} | |
} | |
} | |
void callback(char* topic, byte* message, unsigned int length) { | |
Serial.print ("message arrived on topic: "); | |
Serial.print(topic); | |
Serial.print(" . message : "); | |
String messageTMP; | |
for (int i = 0; i < length; i++) { | |
Serial.print((char)message[i]); | |
messageTMP += (char) message [i]; | |
} | |
Serial.println(); | |
if (String(topic) == "itx/entrada") { | |
if (messageTMP == "vidrio") { | |
Estado = Vidrio; | |
} | |
if (messageTMP == "plastico") { | |
Estado = Plastico; | |
} | |
if (messageTMP == "lata") { | |
Estado = Lata; | |
} | |
if (messageTMP == "nada") { | |
Estado = Nada; | |
} | |
Serial.print("Mensaje: "); | |
Serial.println(messageTMP); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
esta todo malo chavala