Go Down

Topic: il cancello si apre...grazie anche a voi (Read 25 times) previous topic - next topic

bl4d3

Aggiungo uno sketch per arduino uno, attiva e disattiva un uscita di arduino in base alla chiamata http usata.
attiva->http://yourip/?out=5&status=1
disattiva->http://yourip/?out=5&status=0
rappresentazione json delle uscite->http://yourip/?out=all

Code: [Select]

#include <SPI.h>
#include <Ethernet.h>

// define actions
#define action_none -1
#define action_out_all 0
#define action_mypin_up 1
#define action_mypin_down 2

// define network config
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xBE }; //physical mac address
byte ip[] = { 192, 168, 1, 20 };                  // ip in lan -> cambialo pure
byte gateway[] = { 192, 168, 1, 1 };                  // internet access via router
byte subnet[] = { 255, 255, 255, 0 };                   //subnet mask
Server server(80);                                      //server port
String readString = String(30); //string for fetching data from address

// arduino out
int pinOutRelay = 5;

// incoming GET command  
String r_pinOutRelay_down = "GET /?out=5&status=0 HTTP/1.1";
String r_pinOutRelay_up = "GET /?out=5&status=1 HTTP/1.1";
String r_out_all = "GET /?out=all HTTP/1.1";

// current action
int current_action;

void setup(){
 //start Ethernet
 Ethernet.begin(mac, ip, gateway, subnet);
 
 pinMode(pinOutRelay, OUTPUT);    
 digitalWrite(pinOutRelay, LOW);
 //enable serial datada print  
 Serial.begin(9600);
 current_action = -1;
}
void loop(){
 current_action = -1;
 // Create a client connection
 Client client = server.available();
   if (client) {
     while (client.connected()) {
      if (client.available()) {
       char c = client.read();
       //read char by char HTTP request
       if (readString.length() < 30)
       {
         //store characters to string
         readString = readString + c;
       }  
       //output chars to serial port
       //Serial.print(c);
       //if HTTP request has ended
       if (c == '\n') {
     
        Serial.print(readString);
        // ****************************************************
         if(readString.startsWith(r_pinOutRelay_up))
         {
         Serial.print("\n HIGH\n");
         current_action = action_mypin_up;
         }
         else if(readString.startsWith(r_pinOutRelay_down))
         {
          Serial.print("\n LOW\n");
          current_action = action_mypin_down;              
         }
         else if(readString.startsWith(r_out_all))
         {
           Serial.print("\n ALL\n");
           current_action = action_out_all;
         }
         else
         {
           Serial.print("\n None \n");  
           current_action = action_none;
         }
        // ****************************************************  
         // now output HTML data starting with standart header
         client.println("HTTP/1.1 200 OK");
         client.println("Content-Type: text/html");
         client.println();
        char buf[12];              
        switch(current_action)
        {
        case action_out_all:
          client.print("{\"ip\" : \"192.168.10.20\", \"devices\" : [{ \"type\" : \"gate\", \"name\" : \"caldaia\", \"out\" : \"");          
          client.print(pinOutRelay);
          client.print("\"}");            
          client.print("]}");                      
          break;
        case action_mypin_down:
          digitalWrite(pinOutRelay, LOW);
          client.print("{\"status\" : \"low\" , \"out\" : \"");
          client.print(pinOutRelay);
          client.print("\"}");                          
          break;          
        case action_mypin_up:
          digitalWrite(pinOutRelay, HIGH);
          client.print("{\"status\" : \"high\" , \"out\" : \"");
          client.print(pinOutRelay);
          client.print("\"}");                
          break;
        default:
          current_action = action_none;        
        }
       

        // ****************************************************            
       
         //clearing string for next read
         readString="";
         //stopping client
         client.stop();
       }
     }
   }
 }
}    

bl4d3

ho iscritto il mio progetto a MAKE un voto sarebbe gradito!

http://makezine.com/tagyourgreen/?id=90

grazie!

DrAma78



bl4d3

ho ricevuto un po' di email da varia gente che mi ha detto: "come posso scrivere il codice per arduino"? La riposta più ovvia era: "con la tastieria" ieri sera ho partorito questo

http://domotichome.heroku.com/masters

inserisci i device che vuoi collegare, la configurazione di rete e genera il codice.

E' tutto in beta, riportate eventuali bugs se qualcuno lo prova. Grazie!

Go Up