Prečítanie súboru z internetu + vykonanie akcie

Sekcia určená pre Arduino nadšencov

Moderátor: Moderátori

martinius96

Prečítanie súboru z internetu + vykonanie akcie

Príspevok od používateľa martinius96 » 15 Apr 2017, 16:15

Ahoj, mám Uno s W5100 Ethernet shieldom a chcem z internetu zo svojej stránky stiahnuť .txt súbor, v ktorom je 0 alebo 1. Podľa toho mi má zopnúť relé. Stiahnutie mi ide, ale už neviem ako mám vytvoriť podmienku if pre obsah súboru, nakoľko char c je všetko, čo mi vráti Uno vrátane HTTP hlavičky atď. Mohol by mi niekto prosím poupraviť kód a prípadne aj odtestovať?
Hodnota sa mení tu: http://arduino.php5.sk/rele.php
Txt. súbor: http://arduino.php5.sk/readme.txt
Toto je môj kód, s ktorým som to skúšal.

Kód: Vybrať všetko

/*
  Web client

 This sketch connects to a website (http://www.google.com)
 using an Arduino Wiznet Ethernet shield.

 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13

 created 18 Dec 2009
 by David A. Mellis
 modified 9 Apr 2012
 by Tom Igoe, based on work by Adrian McEwen

 */

#include <SPI.h>
#include <Ethernet.h>
int led = 9;
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(74,125,232,128);  // numeric IP for Google (no DNS)
char server[] = "www.arduino.php5.sk";    // name address for Google (using DNS)

// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 1, 254);

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

void setup() {
  pinMode(led, OUTPUT);
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // try to congifure using IP address instead of DHCP:
    Ethernet.begin(mac, ip);
  }
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");

  // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {
    Serial.println("connected");
    // Make a HTTP request:
    client.println("GET /readme.txt HTTP/1.1");
    client.println("Host: www.arduino.php5.sk");
    client.println("Connection: close");
    client.println();
  } else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}

void loop() {
  digitalWrite(led, HIGH);
  // if there are incoming bytes available
  // from the server, read them and print them:
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
    if(c=1){ digitalWrite(led, HIGH);
      }else{digitalWrite(led, LOW);}
    
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();

    // do nothing forevermore:
    while (true);
  }
}
Vrátená response:

Kód: Vybrať všetko

connecting...
connected
HTTP/1.1 200 OK
Date: Sat, 15 Apr 2017 13:59:55 GMT
Server: Apache/2.4.25 (Debian) mod_auth_tkt/2.1.0 mod_fastcgi/mod_fastcgi-SNAP-0910052141 OpenSSL/1.0.2k mpm-itk/2.4.7-04 PHP/5.6.30-0+deb8u1
Last-Modified: Sat, 15 Apr 2017 13:38:22 GMT
ETag: "1-54d34abf6d50b"
Accept-Ranges: bytes
Content-Length: 1
Connection: close
Content-Type: text/plain

1
disconnecting.
Za každú odpoveď ďakujem, ak by ste mi poupravili kód, bol by som vám ešte vďačnejší.

peterple
Ultimate člen
Ultimate člen
Príspevky: 2328
Dátum registrácie: 25 Jún 2013, 21:06
Bydlisko: Krajné
Vek: 57
Kontaktovať používateľa:

Re: Prečítanie súboru z internetu + vykonanie akcie

Príspevok od používateľa peterple » 15 Apr 2017, 16:57

Kód robiť za teba nebudem. Ale môžem ťa trochu nasmerovať. Char c, je premenná v ktorej máš vždy len jeden znak. že sa ti to zdá ako celá odpoveď je len vďaka tomu že sa to client.read() opakuje pretože tá funkcia loop sa volá zas a znova.
Takže by si potreboval nejako prísť na to že už skončil http header.

To vieš podľa toho že ti prídu po sebe dva znaky CR LF, teda niečo takéhoto "\r\n\r\n".
Skús teraz niečo vymyslieť sám. Ako by sa to dalo v tom loop zistiť.
0
Ukáž múdremu chybu a on sa ti poďakuje. Ukáž chybu hlupákovi a on sa urazí.

Používateľov profilový obrázok
mirki
Stály člen
Stály člen
Príspevky: 323
Dátum registrácie: 08 Okt 2007, 00:00
Bydlisko: Banská Bystrica
Vek: 33

Re: Prečítanie súboru z internetu + vykonanie akcie

Príspevok od používateľa mirki » 15 Apr 2017, 18:23

Máš tam chybu.
Skús preštudovať: https://www.arduino.cc/en/reference/if
Snáď nájdeš čo tým myslím. :wink:
0

Používateľov profilový obrázok
djwiktor
Ultimate člen
Ultimate člen
Príspevky: 1624
Dátum registrácie: 01 Júl 2010, 00:00
Bydlisko: Šaľa
Vek: 36
Kontaktovať používateľa:

Re: Prečítanie súboru z internetu + vykonanie akcie

Príspevok od používateľa djwiktor » 15 Apr 2017, 19:28

no jo, if :) snad to chlapcisko pochopi a najde a opravi :)
0
Tlacim na 3D tlaciarni Felix 3.0 dual
Stormchasers.sk

Napísať odpoveď
  • Podobné témy
    Odpovedí
    Zobrazení
    Posledný príspevok