martes, 16 de octubre de 2012

Negamatik. x10 + Arduino (3) - Proccessing Server

En esta entrada escribiré el código en processing, que actuará como servidor el cual recibirá ordenes a traves de internet y se las transmitirá al arduino que tengamos conectado al puerto USB del ordenador, el cual a su vez estará conectado al módulo X10.

import processing.net.*;

String HTTP_GET_REQUEST = "GET /";
String HTTP_POST_REQUEST = "POST /";
String HTTP_HEADER = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
int NUM_MESN = 15;

Server s;
Client c;
String input;
String error="";

String [] mens = new String[NUM_MESN];
int num = 0;

void setup()
{
  size(450, 600);
  background(204);
  stroke(0);
  initMens();
  s = new Server(this, 6661); // start server on http-alt
}

void draw()
{
  background(204);
  stroke(0);
 
  // Receive data from client
  c = s.available();
  if (c != null) {
    input = c.readString();
    textSize(14);
    fill(0, 102, 153);
    text(input, 15, 60);

    String cab = "";
    if(input.indexOf("\n")>0){
      cab = input.substring(0, input.indexOf("\n"));
    }
   
    if (cab.indexOf(HTTP_GET_REQUEST) == 0) // starts with ...
    {
      c.write(HTTP_HEADER);  // answer that we're ok with the request and are gonna send html     
      // some html
      c.write("<html><head><title>Processing talkin'</title></head><body><h3>Your base are belong to us!");
      c.write("</h3></body></html>");     
      // close connection to client, otherwise it's gonna wait forever
      c.stop();
    }else if(cab.indexOf(HTTP_POST_REQUEST) == 0){
          Date d = new Date();
          long timestamp = d.getTime();
          String date = new java.text.SimpleDateFormat("HH:mm:ss dd-MM-yyyy").format(timestamp);

          c.write(HTTP_HEADER);

          String data = "";
          if(input.indexOf("STATUS")>0){
            data = input.substring(input.indexOf("STATUS"), input.length());
            data = data.substring(data.indexOf("=")+1, data.length());   
        setMens("ST: " + data + " | " + date);
            //envio arduino orden status
            c.write("OK");
         
          }else if(input.indexOf("ENCIENDE")>0){
            data = input.substring(input.indexOf("ENCIENDE"), input.length());
            data = data.substring(data.indexOf("=")+1, data.length());   
        setMens("ON:   " + data + " | " + date);
            //envio arduino orden encender + data
            c.write("OK");
          }else if(input.indexOf("APAGA")>0){
            data = input.substring(input.indexOf("APAGA"), input.length());
            data = data.substring(data.indexOf("=")+1, data.length());   
        setMens("OFF: " + data + " | " + date);
            //envio arduino orden apagar + data
            c.write("OK");
          }else{
            data = input;
            error="Ultimo error:\n" + data +  "\n| " + date;   
        setMens("ERR: POST  | " + date);
            c.write("RE");
          }
      // close connection to client, otherwise it's gonna wait forever
      c.stop();
    }
    else if (input.indexOf("message=") == 0) // starts with ...
    {
         println("aqui    nunca:" + input);
    }
    //delay(10000);
  }else{
    fill(#035BAD);
    text("waiting for you ... port 6661", 15, 30);
    printMens();
    printError();
    //println("waiting for you ... port 6661");
  }
}

void printMens(){
  for(int i = 0; i<NUM_MESN; i++){
      fill(#035BAD);
      text((i+1)+".- ", 15, 50+20*i);
     
      fill(#1B85E8);
      text(mens[i], 50, 50+20*i);
  }
}

void printError(){
  fill(160, 0, 0);
  text(error, 15, 380);
}

void initMens(){
  for(int i = 0; i<NUM_MESN; i++){
      mens[i]="";
  }
}

void setMens(String men){
  String [] tmp = mens;
 
  if(num==NUM_MESN){
     num = NUM_MESN-1;
     for(int i = 0; i<NUM_MESN-1; i++){
       mens[i]= tmp[i+1];
     }
  }
  mens[num] = men;
  num++;
}

No hay comentarios:

Publicar un comentario