Lecture de page

Le but est de récupérer le contenu d’une page web. Ici nous prendrons l’exemple d’une page contenant un objet JSON.

1°) Sous TELNET :

Les commandes saisies au clavier sont en  vert gras et en italique xxx.xxx.com indique l’url du site web

silvain@silvain-Amilo-M1425:~$ telnet xxxx.xxx.com 80
Trying xxx.xxx.xxx.xxx…
Connected to xxxx.xxx.com.
Escape character is ‘^]’.
GET /page.php HTTP/1.1
host: xxxx.xxx.com.com

Appuyez sur Enter (pour générer un Line Feed <LF> caractère)

HTTP/1.1 200 OK
Date: Wed, 28 May 2014 08:05:34 GMT
Server: ATS/4.2.1
Cache-Control: max-age=0
Expires: Wed, 28 May 2014 08:05:34 GMT
Vary: Accept-Encoding
Content-Type: text/html; charset: UTF-8
Age: 0
Transfer-Encoding: chunked
Connection: keep-alive

45
{« datefete »: »28\/5″, »nbfete »:1, »tabfete »:[{« fete »: »Germain(de P.) »}]}
0
Connection closed by foreign host.
silvain@silvain-Amilo-M1425:~$

On remarque que la requête d’interrogation de la page est un GET /nom_de_la_page suivi de HTTP/1.1

2°) traduction du telnet sous Arduino :

a) Lecture de la page entière :

Le code :

 #include <Ethernet.h>  
 #include <SPI.h>  
 /*** Configuration réseau ***/  
 byte mac[] = { 0x00, 0xAD, 0xBE, 0xEF, 0xFE, 0x50 }; //-- Addresse MAC à adapter             
 EthernetServer serveurHTTP(80);            //-- objet serveur utilisant le port 80 = port HTTP  
 //-- Configurat TCP IP à adapter --  
 IPAddress ip(192,168,0,50);    
 IPAddress my_dns(192, 168, 0, 254);  
 IPAddress gateway(192, 168, 0, 254);  
 IPAddress subnet(255, 255, 255, 0);  
 //------------------------------  
 EthernetClient client;  
 //--------------------------------  
 void setup()  
 {Serial.begin(9600);  
  //---- initialise la connection ----  
  delay(1000);   
  Ethernet.begin(mac, ip, my_dns, gateway, subnet);  
  delay(1000);   
  Serial.println("connecting...");  
  //--- test client.connect(sURL, 80);  
  if (client.connect("andropebble.byethost33.com", 80)) {  
   Serial.println("connected");  
   delay(300);  
   client.println("GET /fete.php HTTP/1.1");  
   client.println("host: andropebble.byethost33.com");  
   client.println();  
  } else {  
   Serial.println("connection failed");  
  }  
 }  
 void loop()  
 {//-- Lecture de la page  
  if (client.available()) {  
   char c = client.read();  
   Serial.print(c);  
  }  
  //-- Si plus de caractère disponible  
  if (!client.connected()) {  
   Serial.println();  
   Serial.println("disconnecting.");  
   client.stop();  
   //-- Boucle infinie...  
   for(;;)  
    ;  
  }  
 }  

Le résultat :

 Test Reading Web Page:  
 connecting...  
 connected  
 HTTP/1.1 200 OK  
 Date: Wed, 28 May 2014 14:59:04 GMT  
 Server: ATS/4.2.1  
 Cache-Control: max-age=0  
 Expires: Wed, 28 May 2014 14:59:04 GMT  
 Vary: Accept-Encoding  
 Content-Type: text/html; charset: UTF-8  
 Age: 0  
 Transfer-Encoding: chunked  
 Connection: keep-alive  
 45  
 {"datefete":"28\/5","nbfete":1,"tabfete":[{"fete":"Germain(de P.)"}]}  
 0  
 disconnecting.  

b) Lecture unique de l’objet JSON :

 #include <Ethernet.h>  
 #include <SPI.h>  
 /*** Configuration réseau ***/  
 byte mac[] = { 0x00, 0xAD, 0xBE, 0xEF, 0xFE, 0x50 }; //-- Addresse MAC à adapter  
 IPAddress ip(192,168,0,50);              //-- Adresse IP à adapter  
 EthernetServer serveurHTTP(80);            //-- objet serveur utilisant le port 80 = port HTTP  
 IPAddress my_dns(192, 168, 0, 254);  
 IPAddress gateway(192, 168, 0, 254);  
 IPAddress subnet(255, 255, 255, 0);  
 String currentLine = "";      // string to hold the text from server  
 String Json = "";         // string to hold the Json Object  
 boolean readingJson = false;    // if you're currently reading the Json Object  
 char serverName[] = "andropebble.byethost33.com";  
 String sHost="Host: andropebble.byethost33.com";  
 String sPage="fete.php";  
 EthernetClient client;  
 void setup()  
 {currentLine.reserve(256);  
  Json.reserve(256);  
  Serial.begin(9600);  
  //---- initialise le serveur ----  
  delay(1000);   
  Ethernet.begin(mac, ip, my_dns, gateway, subnet);  
  delay(1000);   
  Serial.println("connecting...");  
  //--- test client.connect(sURL, 80);  
  if (client.connect(serverName, 80)) {  
   Serial.println("connected");  
   delay(300);  
   client.println("GET /"+sPage+" HTTP/1.1");  
   client.println(sHost);  
   client.println();  
  } else {  
   Serial.println("connection failed");  
  }  
 }  
 void loop()  
 {if (client.available()) {  
   char inChar = client.read();  
   //Serial.print(inChar);  
   //-- Ajout du byte lu à la fin de la ligne  
   currentLine += inChar;   
   //-- Effacer la ligne si un retour chariot est présent  
   if (inChar == '\n') {  
     currentLine = "";  
    }   
   //-- Si la ligne commence par '{'  
   //-- alors l'objet JSON commence  
   if (( inChar=='{')&&!readingJson) {  
     readingJson = true;   
     Json = "";  
    }  
   //-- Si on est dans l'objet JSON alors ajout des car reçus  
    if (readingJson) {  
     if (inChar != '\n') {  
      Json += inChar;  
     }   
     else {  
      //-- En fin de ligne finilisation du JSON  
      readingJson = false;  
      //Serial.println();  
      Serial.print("Json object = ");  
      Serial.println(Json);    
      //-- Fin de lecture du JSON, déconnexion  
      client.stop();   
      Serial.println("Disconnecting...");    
     }}  
    }  
 }  

Résultat :

 connecting...  
 connected  
 Json object = {"datefete":"28\/5","nbfete":1,"tabfete":[{"fete":"Germain(de P.)"}]}  

c) Parsing de l’objet JSON :

 

Laisser un commentaire

Ce site utilise Akismet pour réduire les indésirables. En savoir plus sur la façon dont les données de vos commentaires sont traitées.

Articles récents
Commentaires récents
fatima dans Bienvenue !
AdminDroid dans Bienvenue !
fatima dans Bienvenue !
Archives
Catégories