Skocz do zawartości

Telnet Arduino/Ethernet Shield wyłączanie i włączanie lampki


patryczek803

Pomocna odpowiedź

Witam. Staram się od paru dni kontrolować lampke on/off poprzez LAN poprzez telnet. Mam problem z kodem który umożliwi mi odczytanie Stringa z interfejsu arduino ktory został wyslany przez moj telefon przez Telnet. Kod był wczesniej napisany zebym mogl i wylaczac lampke poprzez arduino i tam wszystko jest okay, ale jak napisac kod ktory będzie brał Stringa z tego co juz sie tam znajduje a nie zostało wpisane w interfejsie arduino? Dodaje aktualny stan kodu:

#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
IPAddress ip(192, 168, 0, 177);
IPAddress myDns(192,168,0, 1);
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 255, 0);
// telnet defaults to port 23
EthernetServer server(23);
boolean gotAMessage = false; // whether or not you got a message from the client yet


int relayPin = 6;
String receivedData = ""; //Pusty ciąg odebranych danych

void setup() {
 // Open serial communications and wait for port to open:
 Serial.begin(9600);

 // start the Ethernet connection:
 Serial.println("Trying to get an IP address using DHCP");
 if (Ethernet.begin(mac) == 0) {
   Serial.println("Failed to configure Ethernet using DHCP");
   // initialize the Ethernet device not using DHCP:
   Ethernet.begin(mac, ip, myDns, gateway, subnet);
 }
 // print your local IP address:
 Serial.print("My IP address: ");
 ip = Ethernet.localIP();
 for (byte thisByte = 0; thisByte < 4; thisByte++) {
   // print the value of each byte of the IP address:
   Serial.print(ip[thisByte], DEC);
   Serial.print(".");
 }
 Serial.println();
 // start listening for clients
 server.begin();

 pinMode(relayPin, OUTPUT);
 Serial.println("Turn your lamp on or off");
 digitalWrite(relayPin, HIGH);
}
void loop() {
// wait for a new client:
 EthernetClient client = server.available();

 // when the client sends the first byte, say hello:
 if (client) {
   if (gotAMessage == false) { // or if(!gotAMessage)
     Serial.println("We have a new client");
     client.println("Hello, client!");
     gotAMessage = true;
   }

   // read the bytes incoming from the client:
   char thisChar = client.read();
   // echo the bytes back to the client:
   server.write(thisChar);
   // echo the bytes to the server as well:
   Serial.print(thisChar);
   Ethernet.maintain();

if(Serial.available() > 0) { 
   //If yes, read and save in variable dataReceived
   receivedData = Serial.readStringUntil('\n'); 

   if (receivedData == "on") { 
     digitalWrite(relayPin, LOW); //Turn on Relay/Turn on lamp
     Serial.println("Lamp is on");
   }
   else if (receivedData == "off") { //Turn off Relay,Turn off lamp
     digitalWrite(relayPin, HIGH);
     Serial.println("Lamp is off");
   }
   else{
     Serial.println("Bad Choice");
     Serial.println("You can choose: on or off");
   }
}

} 

[ Dodano: 18-01-2017, 16:58 ]

Temat do zamknięcia udało mi się w końcu dotrzeć do tego. Jednakże nie do końca rozumiem cały kod jestem początkujący. Wklejam kod i jeżeli ktoś mógłby wytłumaczyć te trudniejsze częśći byłbym wdzięczny.

#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.
// gateway and subnet are optional:
byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
IPAddress ip(192, 168, 0, 177);
IPAddress myDns(192,168,0, 1);
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 255, 0);

// telnet defaults to port 23
EthernetServer server(23);
boolean alreadyConnected = false; // whether or not you got a message from the client yet
int relay = 6;
String commandString; //Temporary store any command that ethernet client is sending to the server

void setup() {
 pinMode(relay, OUTPUT); // set the output
 digitalWrite(relay, HIGH);
   Ethernet.begin(mac, ip, myDns, gateway, subnet); // initialize the Ethernet device not using DHCP:
   server.begin(); // start listening for clients
 Serial.begin(9600); // Open serial communications and wait for port to open:
 // this check is only needed on the Leonardo:
 while (!Serial) {
   ; // wait for serial port to connect. Needed for native USB port only
 }
Serial.print("Chat server address:");
Serial.println(Ethernet.localIP());
}

void loop(){

 EthernetClient client = server.available(); // wait for a new client

 // when the client sends the first byte, say hello:
 if (client) {
   if (!alreadyConnected) {
     client.flush(); //clear out the input buffer
     commandString =""; //clear the commandString variable

     server.println("---> Please type your command and hit Return...");
     alreadyConnected = true;
   }
   while (client.available()){
     char newChar = client.read(); //read the bytes incoming from the client;

   if (newChar == 0x0D){ // IF a 0xOD is recevied - a carriage return. 
   server.print("Received this command: ");
   server.println(commandString);
   processCommand(commandString);
   } else{
     Serial.println(newChar);
     commandString += newChar;
   }
   }
 }
}


void processCommand(String command){
 server.print("Processing command ");
 server.println(command);


 if (command.indexOf("on") > -1){
   Serial.println("Lamp On command received");
   digitalWrite(relay, LOW);
   server.println("Lamp was turned on");
   commandString ="";
   return;
 }
 if (command.indexOf("off") > -1){
   Serial.println("Lamp off command received");
   digitalWrite(relay, HIGH);
   server.println("Lamp was turned off");
   commandString="";
   return;
 }
 commandString="";
 instructions();
}

void instructions(){
 server.println("I don't understand");
 server.println("Please use on of these commands:");
 server.println("* on, to turn on the lamp");
 server.println("* off, to turn off the lamp");
}
Link do komentarza
Share on other sites

Dołącz do dyskusji, napisz odpowiedź!

Jeśli masz już konto to zaloguj się teraz, aby opublikować wiadomość jako Ty. Możesz też napisać teraz i zarejestrować się później.
Uwaga: wgrywanie zdjęć i załączników dostępne jest po zalogowaniu!

Anonim
Dołącz do dyskusji! Kliknij i zacznij pisać...

×   Wklejony jako tekst z formatowaniem.   Przywróć formatowanie

  Dozwolonych jest tylko 75 emoji.

×   Twój link będzie automatycznie osadzony.   Wyświetlać jako link

×   Twoja poprzednia zawartość została przywrócona.   Wyczyść edytor

×   Nie możesz wkleić zdjęć bezpośrednio. Prześlij lub wstaw obrazy z adresu URL.

×
×
  • Utwórz nowe...

Ważne informacje

Ta strona używa ciasteczek (cookies), dzięki którym może działać lepiej. Więcej na ten temat znajdziesz w Polityce Prywatności.