json - Arduino EthernetClient cannot connect to Web API -


i trying arduino ethernet connecting client asp.net web api on azure. supposed post data api persisted in mssql db.

because not working choose go basic ethernetclient examples , them working. got arduino firing get request webpage , got html data response. no problems there. if try fetch json data web api getting errors.

first thought might web api caused problem found public test api called jsonplaceholder.typicode.com sends out dummy json data. did not work either. below code working right now:

#include <spi.h> #include <ethernet.h>  byte mac[] = { 0x90, 0xa2, 0xda, 0x00, 0x69, 0xe6 }; byte ip[] = { 192, 168, 0, 20 }; char server[] = "http://jsonplaceholder.typicode.com";  ethernetclient client;  void setup() {   serial.begin(9600);   if (ethernet.begin(mac) == 0) {     serial.println("failed configure ethernet using dhcp");     ethernet.begin(mac, ip);   }   delay(1000);   serial.println("connecting...");   serial.println(client.connect(server, 80)); // returns -5.   if (client.connect(server, 80)) {     serial.println("connected");     client.println("get http://jsonplaceholder.typicode.com/posts/1 http/1.1");     client.println("host: jsonplaceholder.typicode.com");     client.println("connection: close");     client.println("content-type: application/json");     client.println();   }   else {     serial.println("connection failed");   } }  void loop() {   if (client.available()) {     char c = client.read();     serial.print(c);   }    if (!client.connected()) {     serial.println();     serial.println("disconnecting.");     client.stop();      while (true);   } } 

when arduino tries connect server following output on serial monitor:

coconnecting... -5  connected  disconnecting. 

the if (client.connect(server, 80)) statement returns -5 should not possible. according www.arduino.cc/en/reference/clientconnect possible return values supposed follows:

  • success 1
  • timed_out -1
  • invalid_server -2
  • truncated -3
  • invalid_response -4

so see -5 should not possible return value. oh , way. when connecting webpage in working example , fetching html data same line returns 0 should not possible value well. enters if statement anyway. have believed should return 1 enter??

can please shed light on why cannot connect web api's?? mean there shouldn't difference? in advance.

with of user in arduino usergroup on facebook, got issue solved. running same issue in future, here explanation of solution.

the problem seems lie in dns resolving when passing hostname. ethernetclient.connect() calls dnsclient.gethostbyname() resolve url. reason doesn't work out well. lack of ability in c/c++ prevents me figure out why. in end ethernetclient.connect() returns -5 both unhandled in ethernetclient library nor documented. why arduino never connects web api.

in further experiments found if pass servers ip address instead ethernetclient.connect() connects. in http header can pass url request , host header field. following code shows working solution:

#include <spi.h> #include <ethernet.h>  byte mac[] = { 0x90, 0xa2, 0xda, 0x00, 0x69, 0xe6 }; byte ip[] = { 192, 168, 0, 20 }; byte serverip[] = {54, 243, 62, 12}; // server ip address running tracert on server hostname.  ethernetclient client;  void setup() {   serial.begin(9600);   if (ethernet.begin(mac) == 0) {     serial.println("failed configure ethernet using dhcp");     ethernet.begin(mac, ip);   }   delay(1000);   serial.println("connecting...");   if (client.connect(serverip, 80)) { // remember pass ip address server since dns resolution apparently doesn't work.     serial.println("connected");     client.println("get http://jsonplaceholder.typicode.com/posts/1 http/1.1");     client.println("host: jsonplaceholder.typicode.com");     client.println("connection: close");     client.println("content-type: application/json");     client.println();   }   else {     serial.println("connection failed");   } }  void loop() {   if (client.available()) {     char c = client.read();     serial.print(c);   }    if (!client.connected()) {     serial.println();     serial.println("disconnecting.");     client.stop();      while (true);   } } 

i find out why dns resolving doesn't work. if here relevant libraries:

https://github.com/arduino/arduino/blob/master/libraries/ethernet/src/ethernetclient.cpp

https://github.com/arduino/arduino/blob/master/libraries/ethernet/src/dns.cpp


Comments

Popular posts from this blog

Django REST Framework perform_create: You cannot call `.save()` after accessing `serializer.data` -

Why does Go error when trying to marshal this JSON? -