Created
June 4, 2018 23:39
-
-
Save martinius96/b3d770455d6c6c5fb76a03dbce3f24c7 to your computer and use it in GitHub Desktop.
Parser for Arduino and Ethernet shield W5100 for parse variable from .txt file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//https://arduino.php5.sk | |
//Autor: Martin Chlebovec (martinius96) | |
#include <SPI.h> | |
#include <Ethernet.h> | |
int led = 6; | |
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //MAC | |
char serverName[] = "www.arduino.php5.sk"; // webserver | |
IPAddress ip(192, 168, 2, 40); | |
EthernetClient client; | |
String readString; | |
int x=0; //number of rows | |
char lf=10; //line feed character | |
void setup(){ | |
pinMode(led, OUTPUT); | |
if (Ethernet.begin(mac) == 0) { | |
Serial.println("Failed to configure Ethernet using DHCP"); | |
Ethernet.begin(mac, ip); | |
} | |
Serial.begin(9600); | |
} | |
void loop(){ | |
if (client.connect(serverName, 80)) { //starts client connection, checks for connection | |
Serial.println("connected"); | |
client.println("GET /PHP_cz/preklady.txt HTTP/1.1"); //download text | |
client.println("Host: www.arduino.php5.sk"); | |
client.println("Connection: close"); //close 1.1 persistent connection | |
client.println(); //end of get request | |
} | |
else { | |
Serial.println("Connection unsucessful"); | |
Serial.println(); | |
} | |
while(client.connected() && !client.available()) delay(1); //wait for datas | |
while (client.connected() || client.available()) { | |
char c = client.read(); //read buffer | |
Serial.print(c); //full http header | |
if (c==lf) x=(x+1); //++ for our variable row | |
else if (x==12) readString += c; //our variable (12 can be changed to number of row in your server response, each server give you another amounth of rows in response) | |
} | |
Serial.print("Variable: "); | |
Serial.print(readString); | |
readString = (""); //delete string | |
x=0; //set 0 for number of rows | |
client.stop(); //end connection | |
delay(5000); //wait 5 seconds | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment