Číslo z txt súboru do premennej

Sekcia určená pre Arduino nadšencov

Moderátor: Moderátori

martinius96

Číslo z txt súboru do premennej

Príspevok od používateľa martinius96 » 28 Jan 2017, 19:05

Ahoj, rieším jeden problém mam Uno a k tomu Ethernet shield a ovládam relátka na základe hodnoty v txt súbore, ktorú viem meniť cez PHP script.
PHP script: http://arduino.php5.sk/lumino/getcontents.php
.TXT súbor: http://arduino.php5.sk/lumino/readme.txt
Hodnotu získam cez tento script. Avšak v char c, ktorý je client.read tak je to celý záznam vrátane HTTP hlavičky a to nechcem, vedeli by ste mi to upraviť, aby som dokázal pracovať s tou 0 alebo 1? Ďakujem za pomoc.
Kód:

Kód: Vybrať všetko

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

// 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() {
  // 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 /lumino/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() {
  // 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 the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();

    // do nothing forevermore:
    while (true);
  }
}

maskrtnik01
Ultimate člen
Ultimate člen
Príspevky: 2568
Dátum registrácie: 20 Júl 2010, 00:00
Bydlisko: okolie KE
Vek: 27

Re: Číslo z txt súboru do premennej

Príspevok od používateľa maskrtnik01 » 28 Jan 2017, 19:31

Dobrý večer,

Nuž keď to riešite takto cez samostatný http server, ten Vám vždy odošle úplnú http response. Takže buď si nájdite nejaký wrapper okolo protokolu http.
Alebo pokiaľ hodnoty nepotrebujete meniť zvonku(=z internetu) a stačí Vám zmena zvnútra Vašej LAN siete(počítač, mobil ak ste doma,...), tak by som Vám doporučil skúsiť http server v Arduine http://startingelectronics.org/tutorial ... r-tutorial.
0

martinius96

Re: Číslo z txt súboru do premennej

Príspevok od používateľa martinius96 » 28 Jan 2017, 20:11

Práve takto to nechcem. Chcem to cez Webclient.

Používateľov profilový obrázok
roboulbricht
Stály člen
Stály člen
Príspevky: 156
Dátum registrácie: 07 Jan 2015, 12:01
Bydlisko: Banská Bystrica
Vek: 54
Kontaktovať používateľa:

Re: Číslo z txt súboru do premennej

Príspevok od používateľa roboulbricht » 28 Jan 2017, 22:15

HTTP odpoveď ti príde v takomto tvare.

Kód: Vybrať všetko

http http://arduino.php5.sk/lumino/readme.txt
HTTP/1.1 200 OK
Accept-Ranges: bytes
Connection: Keep-Alive
Content-Length: 1
Content-Type: text/plain
Date: Sat, 28 Jan 2017 21:00:49 GMT
ETag: "1-5472b7ac25dd8"
Keep-Alive: timeout=2, max=100
Last-Modified: Sat, 28 Jan 2017 18:09:12 GMT
Server: Apache/2.4.25 (Debian) mod_auth_tkt/2.1.0 OpenSSL/1.0.2j mpm-itk/2.4.7-04 PHP/5.6.29-0+deb8u1

0
Čiže si musíš sledovať načítané znaky a keď sa objaví prázdny riadok, môžeš začať ˇhľadať '1' alebo '0'. HTTP má na konci každého riadku znak CR, čo je v desiatkovej sústave 13. Čiže hľadáš na konci v znakoch odpoveď, ktorá by mala byť asi takto.

Kód: Vybrať všetko

\r - posledný znak na konci hlavičky
\r - prázdny riadok
0 - tvoja hodnota
0

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