Bonjour à toutes et à tous,
Avant de vous posez ma question, je tiens à préciser que je suis nouveau sur le forum.
J'ai lu les règles du forum mais malheureusement il est peut être possible que j'oublie quelques règles n'ayant pas l'habitude de poster des questions.
Si c'est le cas, je vous remercie grandement de me le faire remarquer afin de pouvoir corriger mes erreurs.
Merci beaucoup.
Je travail actuellement sur un projet personnel. Le but de celui-ci est de récupérer des informations sur les transports en commun. Pour cela, j'utilise une API qui me génère un fichier JSON.
Le site de l'API que j'utilise est celui-ci: http://transport.opendata.ch
La requête HTTP que j'utilise est la suivante: http://transport.opendata.ch/v1/stationboard?station=Cugy%20VD,%20Le%20Moulin&id=8570164&limit=2&transportations=bus&type=departure
J'utilise donc une carte nodeMCU que je programme avec l'IDE d'arduino.
Le code que j'utilise est le suivant (les adresses MAC et tous les autres paramètres réseau présents ici ont été changés):
#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
const char* ssid = "MyNetwork"; // Wifi SSID
const char* password = "MyPassword"; // Wifi Password
bool ssidFound = false; // Var that says if the wifi is found
void setup() {
// Set the speed of the serial and delay 10
Serial.begin(115200);
delay(10);
// Show the MAC address
Serial.println();
Serial.print("MAC Address: ");
Serial.println(WiFi.macAddress());
Serial.println();
// Call ScanWifi to test if the wifi SSID is found
ScanWifi();
// If the ssid is found continue the init.
if (ssidFound == true){
// Connect to WiFi network
// Print new line for "println" and no new line for "print"
// Print informations which says the nodeMCU try to connect to the WiFi
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
// Start the connection to the SSID wich is specified above
WiFi.begin(ssid, password);
// Do while the nodeMCU card is not connected to the WIFI:
// - Wait 500ms
// - Print "."
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
} // end While
//Check if the connection is init.
if(WiFi.status() == WL_CONNECTED){
//Display a message that say the connection is init.
Serial.println();
Serial.print("TEST 1: Connection ");
Serial.print(ssid);
Serial.println(" => Ok!");
}//end if
} //end if
} // end setup
void loop() {
// Display a message that say the function loop is started
Serial.println("TEST 2: The function loop is open");
//Start the HTTP Request
HTTPClient http;
http.begin("http://transport.opendata.ch/v1/stationboard?station=Cugy%20VD,%20Le%20Moulin&id=8570164&limit=2&transportations=bus&type=departure");
// Display a message that say the http.begin is ok
Serial.println("TEST 3: Request is ok");
//Init the httpCode
int httpCode = http.GET();
//Check the returning code
if (httpCode > 0) {
//Generated with https://arduinojson.org/v5/assistant/
const size_t bufferSize = JSON_ARRAY_SIZE(2) + JSON_ARRAY_SIZE(8) + JSON_ARRAY_SIZE(26) + JSON_OBJECT_SIZE(2) + 73*JSON_OBJECT_SIZE(3) + 109*JSON_OBJECT_SIZE(5) + 36*JSON_OBJECT_SIZE(10) + 2*JSON_OBJECT_SIZE(11) + 20080;
DynamicJsonBuffer jsonBuffer(bufferSize);
JsonObject& root = jsonBuffer.parseObject(http.getString());
//Set vars
JsonObject& stationboard1 = root["stationboard"][1];
JsonObject& stationboard1_stop = stationboard1["stop"];
const char* departure = stationboard1_stop["departure"];
Serial.println("TEST 4: departure==stationboard1_stop");
Serial.println(departure);
//Close the http object
http.end(); //Close connection
Serial.println("TEST 5: http.end => OK");
}//end if (httpCode > 0)
// Wait 10 min.
delay(600000);
}// end loop
// Name: ScanWifi
// Goal: Test if the wifi ssid is founded
// Param: none
// Return: none
void ScanWifi() {
// Get how many Wifi connection can be scaned
int numberOfNetworks = WiFi.scanNetworks();
// Display all scanned Wifi
for(int i=0; i<numberOfNetworks; i++){
// Print network name
Serial.print("Network name: ");
Serial.println(WiFi.SSID(i));
// If the SSID scaned is eq. SSId to connect display a message and set var "ssidFound" true
if (WiFi.SSID(i)==ssid){
Serial.print(ssid);
Serial.println(" NETWORK IS FOUND!!");
ssidFound = true;
}// end if
Serial.println("-----------------------");
}//end For
}//end ScanWifi
Le retour sur le moniteur serie est le suivant:
MAC Address: XX:XX:XX:XX:XX:XX
Network name: otherNetwork
-----------------------
Network name: otherNetwork
-----------------------
Network name: otherNetwork
-----------------------
Network name: otherNetwork
-----------------------
Network name: otherNetwork
-----------------------
Network name: MyNetwork
MyNetwork NETWORK IS FOUND!!
-----------------------
Network name: otherNetwork
-----------------------
Network name: MyNetwork
MyNetwork NETWORK IS FOUND!!
-----------------------
Connecting to MyNetwork
.......
TEST 1: Connection MyNetwork => Ok!
TEST 2: The function loop is open
TEST 3: Request is ok
TEST 4: departure==stationboard1_stop
TEST 5: http.end => OK
Mon problème est que entre le test 4 et le test 5 je devrai obtenir une date de type 2018-07-09T16:38:00+0200
Mais rien ne s'affiche.
Pour créer ce code, je me suis inspiré du tutoriel présent sur ce site: http://www.instructables.com/id/ESP8266-Parsing-JSON/
Des librairies suivantes:
- ArduinoJson.h
- ESP8266WiFi.h
- ESP8266HTTPClient.h
Et de l'assistant de ArduinoJson: Assistant | ArduinoJson 5
Je vous remercie d'avance pour votre aide. ![]()
