collapse

* Links de Robótica

* Posts Recentes

Ajuda com TRIAC por senso
[17 de Novembro de 2017, 18:00]


TV LG White Screen por almamater
[15 de Novembro de 2017, 08:37]


Pergunta sobre prototipagem ( Pesquisa ) por luisjustin
[14 de Novembro de 2017, 23:22]


Medir Agua que está no Poço por Njay
[14 de Novembro de 2017, 13:28]


Amplificador audio por beirao
[12 de Novembro de 2017, 23:43]


Power_OK Enermax por edeweld
[12 de Novembro de 2017, 17:15]


Driver motor por beirao
[11 de Novembro de 2017, 17:40]


Preciso de uma fonte de corrente constante ? por samc
[09 de Novembro de 2017, 14:18]


Pressoestato 0-10V por artur36
[07 de Novembro de 2017, 23:22]


Ideias para construir um quadrúpede simples por Njay
[07 de Novembro de 2017, 18:39]

Autor Tópico: App Inventor Automação com Android e Arduino EthernetShield  (Lida 4756 vezes)

0 Membros e 1 Visitante estão a ver este tópico.

Offline r16

  • Mini Robot
  • *
  • Mensagens: 337
App Inventor Automação com Android e Arduino EthernetShield
« em: 27 de Dezembro de 2012, 00:29 »
Olá Pessoal.

Pesquisando um pouco na net encontrei este projecto:

---------------------------------CÓDIGO ARDUINO-------------------------------------


Código: [Seleccione]
/*
Web Server
A simple web server that switches LED's on and off, based on input from an
Android App Inventor application
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
* LED outputs attached to pins 5 and 6
created 13 Oct 2010
by Rogier van den Berg / rogiervandenberg.nl
*/
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192,168,0, 201 };
//Settings for the two LED's
int ledPin = 5;
int ledState = LOW;
int powerLedPin = 6;
int powerLedState = LOW;
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
Server server(80);
void setup()
{
//Set the LED pins as output
pinMode(ledPin, OUTPUT);
pinMode(powerLedPin, OUTPUT);
//For debugging, set the Serial Output
Serial.begin(9600);
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
//Turn one of the LED's on, to know it is ready to go!
digitalWrite(powerLedPin, HIGH);
}
void loop()
{
//Make sure requests are taken care of
handleIncomingInstruction();
//Make sure the power led stays on, when nothing happens.
if(powerLedState == LOW)
switchPowerLed();
}
void handleIncomingInstruction()
{
// listen for incoming clients
Client client = server.available();
if (client) {
// an http request ends with a blank line
boolean newLine = true;
String line = "";
while (client.connected() && client.available()) {
char c = client.read();
//Serial.print(c);
switchPowerLed();
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && newLine) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
}
if (c == '\n') {
// you're starting a new line
newLine = true;
evaluateLine(line);
line = "";
}
else if (c != '\r') {
// you've gotten a character on the current line
newLine = false;
line += c;
}
}
evaluateLine(line);
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
}
}
void evaluateLine(String line)
{
if (line.startsWith("tag", 0)) {
String instruction = line.substring(4, line.length());
Serial.println(instruction);
if (instruction == "TestOpdracht")
switchLed();
}
}
void switchLed()
{
// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
Serial.println("We switchen de LED!");
}
void switchPowerLed()
{
// if the LED is off turn it on and vice-versa:
if (powerLedState == LOW)
powerLedState = HIGH;
else
powerLedState = LOW;
// set the LED with the ledState of the variable:
digitalWrite(powerLedPin, powerLedState);
}

------------------------------------------------ FIM ------------------------------------------
Código para app inventor para o abrir basta no app inventor fazer:
My projectos-->More Actions-->Upload Source



https://docs.google.com/open?id=13G80qipX40PaMmMDtX2ft6X5kSFTltopZMag8SrWX-ezovhWmcegBli5Xs0L


---------------------- VIDEO DE DEMONSTRAÇÃO----------------------------------




Ora bem o que este projecto faz é basicamente atraves de um Android com um programa apenas com um botao faz ligar e desligar o led.
Agora eu gostaria da vossa ajuda para o seguinte:
Eu pretendo que ao ligar o Led esse botão fique com a cor VERDE e ao desligar o led fique com a cor Vermelha.
E pretendia também o seguinte: se o led tiver acesso e o botão verde, eu gostaria que ao desligar o equipamento android e voltar a ligar o programa, que ele veja o estado do led.. se ele tiver acesso aparece então a cor verde se tiver desligado, aparece vermelho.
Alguem me pode enviar o projecto já modificado com essa funcoes?
Ficaria muito grato.

Offline r16

  • Mini Robot
  • *
  • Mensagens: 337
Re: App Inventor Automação com Android e Arduino EthernetShield
« Responder #1 em: 27 de Dezembro de 2012, 22:12 »
Olá já fiz alguns progressos.
Agora peço ajuda de novo aqui no codigo:

Código: [Seleccione]
/*
 Web  Server

 A simple web server that switches LED's on and off, based on input from an
 Android App Inventor application

 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13
 * LED outputs attached to pins 5 and 6

 created 13 Oct 2010
 by Rogier van den Berg / rogiervandenberg.nl

 */

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

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192,168,0, 201 };

//Settings for the two LED's
int ledPin = 5;
int ledState = LOW;
int powerLedPin = 6;
int powerLedState = LOW;

// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
Server server(80);

void setup()
{
  //Set the LED pins as output
  pinMode(ledPin, OUTPUT);
  pinMode(powerLedPin, OUTPUT);

  //For debugging, set the Serial Output
  Serial.begin(9600);

  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();

  //Turn one of the LED's on, to know it is ready to go!
  digitalWrite(powerLedPin, HIGH);
}

void loop()
{
  //Make sure requests are taken care of
  handleIncomingInstruction();

  //Make sure the power led stays on, when nothing happens.
  if(powerLedState == LOW)
    switchPowerLed();
}

void handleIncomingInstruction()
{
  // listen for incoming clients
  Client client = server.available();
  if (client) {
    // an http request ends with a blank line
    boolean newLine = true;
    String line = "";

    while (client.connected() && client.available()) {


        char c = client.read();
        //Serial.print(c);
        switchPowerLed();


        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && newLine) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
        }
        if (c == '\n') {
          // you're starting a new line
          newLine = true;
          evaluateLine(line);
          line = "";
        }
        else if (c != '\r') {
          // you've gotten a character on the current line
          newLine = false;
          line += c;
        }

    }

    evaluateLine(line);

     // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();   
  }
}

void evaluateLine(String line)
{
  if (line.startsWith("tag", 0)) {
    String instruction = line.substring(4, line.length());
    Serial.println(instruction);
    if (instruction == "TestOpdracht")
      switchLed();
  }
 }

 void switchLed()
 {
   // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

      // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
    Serial.println("We switchen de LED!");
 }

 void switchPowerLed()
 {
      // if the LED is off turn it on and vice-versa:
    if (powerLedState == LOW)
      powerLedState = HIGH;
    else
      powerLedState = LOW;

      // set the LED with the ledState of the variable:
    digitalWrite(powerLedPin, powerLedState);
 }


dentro do  void switchLed()
eu queria ter um :
client.println("<font color='green' size='5'>ON");
para quando tivesse HIGH
e um
client.println("<font color='green' size='5'>OFF");
para quando tivesse LOW.

Alguem me pode ajudar nessa modificacao no codigo, ja tentei mas nao tive sucesso.

Obrigado

Offline tarquinio

  • Mini Robot
  • *
  • Mensagens: 529
Re: App Inventor Automação com Android e Arduino EthernetShield
« Responder #2 em: 28 de Dezembro de 2012, 01:17 »
Não basta fazeres algo deste género?

 void switchLed()
 {
   // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
   {
      ledState = HIGH;
     client.println("<font color='green' size='5'>ON");    }
   else
   {
      ledState = LOW;
     client.println("<font color='green' size='5'>ON");    }
   }
      // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
    Serial.println("We switchen de LED!");
 }

Depois tens é de definir o client como variável global, ou passar como parametro para a função.

troca  Client client = server.available();
por   client = server.available();

E coloca fora da função:

Client client;

Não será a solução mais bonita, mas acho que já deveria funcionar... :P

Offline tarquinio

  • Mini Robot
  • *
  • Mensagens: 529
Re: App Inventor Automação com Android e Arduino EthernetShield
« Responder #3 em: 28 de Dezembro de 2012, 02:04 »
Ok tenta isto então:

Código: [Seleccione]
/*
 Web  Server

 A simple web server that switches LED's on and off, based on input from an
 Android App Inventor application

 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13
 * LED outputs attached to pins 5 and 6

 created 13 Oct 2010
 by Rogier van den Berg / rogiervandenberg.nl

 */

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

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192,168,0, 201 };

//Settings for the two LED's
int ledPin = 5;
int ledState = LOW;
int powerLedPin = 6;
int powerLedState = LOW;

// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);

void setup()
{
  //Set the LED pins as output
  pinMode(ledPin, OUTPUT);
  pinMode(powerLedPin, OUTPUT);

  //For debugging, set the Serial Output
  Serial.begin(9600);

  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();

  //Turn one of the LED's on, to know it is ready to go!
  digitalWrite(powerLedPin, HIGH);
}

void loop()
{
  //Make sure requests are taken care of
  handleIncomingInstruction();

  //Make sure the power led stays on, when nothing happens.
  if(powerLedState == LOW)
    switchPowerLed();
}

void handleIncomingInstruction()
{
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    // an http request ends with a blank line
    boolean newLine = true;
    String line = "";

    while (client.connected() && client.available()) {


        char c = client.read();
        //Serial.print(c);
        switchPowerLed();


        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && newLine)
        {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          client.println("<body>");
          if (ledState == LOW)
  {
      client.println("<font color='green' size='5'>OFF");   
          }
  else
  {
     client.println("<font color='green' size='5'>ON");   
          }
          client.println("</body>");
        }
        if (c == '\n') {
          // you're starting a new line
          newLine = true;
          evaluateLine(line);
          line = "";
        }
        else if (c != '\r') {
          // you've gotten a character on the current line
          newLine = false;
          line += c;
        }

    }

    evaluateLine(line);

     // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();   
  }
}

void evaluateLine(String line)
{
  if (line.startsWith("tag", 0)) {
    String instruction = line.substring(4, line.length());
    Serial.println(instruction);
    if (instruction == "TestOpdracht")
      switchLed();
  }
 }

 void switchLed()
 {
   // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

      // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
    Serial.println("We switchen de LED!");
 }

 void switchPowerLed()
 {
      // if the LED is off turn it on and vice-versa:
    if (powerLedState == LOW)
      powerLedState = HIGH;
    else
      powerLedState = LOW;

      // set the LED with the ledState of the variable:
    digitalWrite(powerLedPin, powerLedState);
 }


Offline r16

  • Mini Robot
  • *
  • Mensagens: 337
Re: App Inventor Automação com Android e Arduino EthernetShield
« Responder #4 em: 28 de Dezembro de 2012, 14:06 »
obrigado tarquinio com algumas modificacoes consegui

Código: [Seleccione]
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 75 }; // ip in lan
byte gateway[] = { 192, 168, 1, 254 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(6969);
//Settings for the two LED's
int ledPin = 5;
int ledState = LOW;
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):

void setup()
{
//Set the LED pins as output
pinMode(ledPin, OUTPUT);

//For debugging, set the Serial Output
Serial.begin(9600);
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
//Turn one of the LED's on, to know it is ready to go!

}
void loop()
{
//Make sure requests are taken care of
handleIncomingInstruction();
//Make sure the power led stays on, when nothing happens.

}
void handleIncomingInstruction()
{
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
// an http request ends with a blank line
boolean newLine = true;
String line = "";
while (client.connected() && client.available()) {
char c = client.read();
//Serial.print(c);

// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && newLine) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.println("<body>");
client.println("<hr />");
client.print("<font size='5'>LED status: ");
if (ledState == LOW)
{
client.println("<font color='green' size='5'>0");
}
else
{
client.println("<font color='green' size='5'>1");
}
// auto reload webpage every 5 second
client.println("<META HTTP-EQUIV=REFRESH CONTENT=5 URL=>");
client.println("</body>");
}
if (c == '\n') {
// you're starting a new line
newLine = true;
evaluateLine(line);
line = "";
}
else if (c != '\r') {
// you've gotten a character on the current line
newLine = false;
line += c;
}
}
evaluateLine(line);
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
}
}
void evaluateLine(String line)
{
if (line.startsWith("tag", 0)) {
String instruction = line.substring(4, line.length());
Serial.println(instruction);
if (instruction == "ligar")
ligar();
if (instruction == "desligar")
desligar();
}
}
void ligar()
{
// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);

}
void desligar()
{
// if the LED is off turn it on and vice-versa:
if (ledState == HIGH)
ledState = LOW;
else
ledState = HIGH;
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);

}


No entanto não tou a conseguir que o app inventor leia os seguintes valores:
if (ledState == LOW)
{
client.println("<font color='green' size='5'>0");
}
else
{
client.println("<font color='green' size='5'>1");
a minha estrutura esta assim:


http://postimage.org/image/l4m490qn7
ou seja o meu botao STATUS LED nao fica verde quando esta 1 nem vermelho quando esta zero.
no entanto quando eu acesso ao meu http://192.168.1.75:6969 aparece lá o estado 0 e 1
alguempode ajudar?

Offline tarquinio

  • Mini Robot
  • *
  • Mensagens: 529
Re: App Inventor Automação com Android e Arduino EthernetShield
« Responder #5 em: 28 de Dezembro de 2012, 14:52 »
Pois isso agora já deve ter a ver com a forma que a App está a ler os valores... Tens de ver exactamente o que é que é preciso ser enviado para a aplicação reconhecer os dados...

Offline r16

  • Mini Robot
  • *
  • Mensagens: 337
Re: App Inventor Automação com Android e Arduino EthernetShield
« Responder #6 em: 28 de Dezembro de 2012, 15:11 »
Eu tenho a estrutura do programa assim

http://postimage.org/image/l4m490qn7