204 lines
4.2 KiB
C++
204 lines
4.2 KiB
C++
// configuracion de red y osc
|
|
#include <SPI.h>
|
|
#include <Ethernet.h>
|
|
#include <EthernetUdp.h>
|
|
#include <OSCBundle.h>
|
|
// Cambiar la MAC para cada uno de los arduinos IMPORTANTE!
|
|
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0F, 0x5A, 0xB1 }; // 01
|
|
IPAddress ip(192, 168, 1, 107);
|
|
int serverPort = 10007; //puerto de escucha
|
|
int destPort = 9007; //puerto de envio
|
|
EthernetUDP Udp;
|
|
|
|
// Motor con encoder
|
|
#define pulso 5
|
|
#define dir 6
|
|
long int count = 0;
|
|
int detener = 0;
|
|
int solenoide = 0;
|
|
int grados = 0;
|
|
int tempus = 0;
|
|
int relay = 4;
|
|
int reles = 0;
|
|
const int tope = A0;
|
|
int lock = 1023;
|
|
int movimiento = 0;
|
|
int giro = 0;
|
|
int newcero = 1;
|
|
int estado = 0;
|
|
|
|
// sensor temperatura
|
|
#include <dht.h>
|
|
dht DHT;
|
|
#define DHT11_PIN 7
|
|
|
|
void pulse()
|
|
{
|
|
if(digitalRead(3) == HIGH)
|
|
count++;
|
|
else
|
|
count--;
|
|
}
|
|
|
|
void encendido(OSCMessage &msg, int addrOffset){
|
|
int value = msg.getFloat(0);
|
|
estado = value;
|
|
}
|
|
|
|
void rele(OSCMessage &msg, int addrOffset){
|
|
solenoide = msg.getFloat(0);
|
|
digitalWrite(relay, solenoide);
|
|
reles = solenoide;
|
|
}
|
|
|
|
void velocidad(OSCMessage &msg, int addrOffset ){
|
|
movimiento = msg.getFloat(0);
|
|
analogWrite(pulso,movimiento);
|
|
}
|
|
|
|
void direccion(OSCMessage &msg, int addrOffset ){
|
|
int sentido = msg.getFloat(0);
|
|
giro = sentido;
|
|
digitalWrite(dir,giro); // set DIR pin 1 or 0
|
|
}
|
|
|
|
void temper(OSCMessage &msg, int addrOffset ){
|
|
int value = msg.getFloat(0);
|
|
tempus = value;
|
|
}
|
|
|
|
void nuevocero(OSCMessage &msg, int addrOffset ){
|
|
int value = msg.getFloat(0);
|
|
newcero = value;
|
|
if (newcero == 0){
|
|
}
|
|
else {
|
|
count = 0;
|
|
}
|
|
}
|
|
void inductor(){
|
|
lock = analogRead(tope);
|
|
if (lock == 1023)
|
|
{
|
|
detener = 0;
|
|
|
|
}
|
|
else
|
|
{
|
|
detener = 1;
|
|
count = 0;
|
|
}
|
|
}
|
|
|
|
void parada(){
|
|
analogWrite(pulso,0);
|
|
}
|
|
|
|
void reseteo(){
|
|
detener = 0;
|
|
count = 0;
|
|
}
|
|
|
|
void OSCMsgReceive(){
|
|
OSCMessage msgIN;
|
|
int size;
|
|
if((size = Udp.parsePacket())>0){
|
|
while(size--)
|
|
msgIN.fill(Udp.read());
|
|
if(!msgIN.hasError()){
|
|
msgIN.route("/rele",rele);
|
|
msgIN.route("/speed",velocidad);
|
|
msgIN.route("/direccion",direccion);
|
|
msgIN.route("/temperatura",temper);
|
|
msgIN.route("/nuevocero",nuevocero);
|
|
msgIN.route("/estado",encendido);
|
|
//delay(10);
|
|
}
|
|
}
|
|
}
|
|
|
|
void medirtemp(){
|
|
OSCMessage msgOut("/temperatura");
|
|
DHT.read11(DHT11_PIN);
|
|
grados = int(DHT.temperature);
|
|
msgOut.add(grados);
|
|
Udp.beginPacket(Udp.remoteIP(), destPort);
|
|
msgOut.send(Udp);
|
|
Udp.endPacket();
|
|
msgOut.empty();
|
|
tempus = 0;
|
|
delay(1500);
|
|
}
|
|
|
|
void varios(){
|
|
//declare the bundle
|
|
OSCBundle bndl;
|
|
|
|
//BOSCBundle's add' returns the OSCMessage so the message's 'add' can be composed together
|
|
bndl.add("/posicion").add(-count/10);
|
|
bndl.add("/sentido").add(giro);
|
|
bndl.add("/parada").add(detener);
|
|
bndl.add("/freno").add(lock);
|
|
bndl.add("/ventilador").add(reles);
|
|
bndl.add("/estado").add(estado);
|
|
|
|
Udp.beginPacket(Udp.remoteIP(), destPort);
|
|
bndl.send(Udp); // send the bytes to the SLIP stream
|
|
Udp.endPacket(); // mark the end of the OSC Packet
|
|
bndl.empty(); // empty the bundle to free room for a new one
|
|
}
|
|
|
|
void setup(){
|
|
Serial.begin(9600);
|
|
delay(1500);
|
|
Serial.println("ARRANCANDO");
|
|
Ethernet.begin(mac, ip);
|
|
if (Ethernet.begin(mac) == 0) {
|
|
Serial.println("Failed to configure Ethernet using DHCP");
|
|
while(true);
|
|
}
|
|
// print your local IP address:
|
|
Serial.print("OSC Start! in this IP: ");
|
|
Serial.println(ip);
|
|
Ethernet.begin(mac, ip);
|
|
Udp.begin(serverPort);
|
|
delay(1000);
|
|
pinMode(pulso,OUTPUT);
|
|
pinMode(dir,OUTPUT);
|
|
pinMode(2, INPUT_PULLUP);
|
|
pinMode(3, INPUT_PULLUP);
|
|
pinMode(relay, OUTPUT);
|
|
attachInterrupt(0, pulse, FALLING);
|
|
varios();
|
|
Serial.println("FIN");
|
|
|
|
}
|
|
void loop(){
|
|
do{
|
|
if (detener == 0){
|
|
OSCMsgReceive();
|
|
inductor();
|
|
varios();
|
|
}
|
|
if (detener == 1){
|
|
inductor();
|
|
OSCMsgReceive();
|
|
parada();
|
|
digitalWrite(dir,0); // set DIR pin 1 or 0
|
|
analogWrite(pulso,32);
|
|
delay(5000);
|
|
analogWrite(pulso,0);
|
|
reseteo();
|
|
varios();
|
|
}
|
|
}while (tempus == 0);
|
|
|
|
if (tempus == 1){
|
|
OSCMsgReceive();
|
|
inductor();
|
|
medirtemp();
|
|
varios();
|
|
|
|
}
|
|
}
|