Communicating over WiFi from an ESP8266 Arduino sketch to a Device Handler running on Hub


#1

Hello,

I am trying, and failing, to communicate from an Arduino sketch (ARD) to a custom Device Handler (DH) on a LAN-connected SmartThings Hub (V2). Specifically, the response is HTTP 202 Accepted, with no other visible side-effect. The events list on the Hub does not show this call. Similarly, the events list on the device does not show this call. My goal is to get the payload in the “parse()” method of the device handler. Following are all the troubleshooting steps I’ve already tried. I would appreciate any suggestions about what might be going wrong, or at least how to further troubleshoot the issue.

  • The Arduino board and the Hub are on the same network. The Arduino board is on the WiFi network, the Hub is connected directly via Ethernet to the same Wifi router.
  • In the opposite direction, the DH can talk over HTTP to the ARD, seen as a custom device (using new physicalgraph.device.HubAction(…))
  • The DH sends the “callback” URL and PORT, via an HTTP GET, to the ARD. I confirmed the values are properly received on the ARD side.
  • The ARD is able to make GET requests over WiFi and fetch data (e.g. the google.com homepage)
  • The ARD is able to properly connect to the URL and PORT sent by the DH, and can send a payload (see code below). It gets a 202 response. If I try other URLs or PORTs, I get a connection error.
  • If I use Fiddler on my laptop to make a POST request to the same IP/PORT, I at least see the corresponding event (with the correct base64-encoded payload) in the Hub’s list of events. Why it’s not in the DH’s event list (i.e. why it doesn’t get routed to the DH, and, eventually, to the DH’s “parse()” method) is a whole different question. I’m trying to take things one at a time…
  • The ARD code that I’m using is as follows, based on HTTPClient from ESP8266HTTPClient.h:

HTTPClient httpClient;
if (httpClient.begin(IP_FOR_ST, atoi(PORT_FOR_ST), “/”))
{
String testString = String("{“status”:“test”}");
httpClient.addHeader(“Content-Type”, “text”);
httpClient.addHeader(“Content-Length”, String(testString.length()));
int httpCode = httpClient.POST(testString);
String response = httpClient.getString();
httpClient.end();

char message_text[600];
itoa(httpCode, message_text, 10);
DEBUG(LOG_NO_FILTER, message_text);
DEBUG(LOG_NO_FILTER,payload.c_str());

DEBUG(LOG_NO_FILTER, "---StatusToST: SUCCESS---");

}

  • The ARD code that tests a GET on google.com is included below. It gets back an HTTP 301 with a full HTML body:

HTTPClient httpClient;

if (httpClient.begin(“172.217.14.228” /Google/, 80, “/”))
{
httpClient.GET();
DEBUG(LOG_NO_FILTER, httpClient.getString().c_str());
}

As I mentioned above, any ideas, questions, or suggestions are welcome. Thanks!

Cheers,
~alex


#2

One additional, slightly odd observation… I tried setting the MAC address of my laptop as the Device Network ID of the device, thinking that would help me better simulate traffic from the Arduino to the Hub. When I did that, POSTs from Fiddler on my laptop to the hub no longer showed up in the Hub’s event list. They also continued to not show up in the event list of the device. As soon as I switched the Device Network ID to no longer be the laptop’s MAC address, the events started showing up again in response to the Fiddler messages. Not sure what to make of this, except to note that means that the Hub is indeed processing messages at that IP:PORT endpoint…

I also added a “configure” command in the device type handler. It makes the call to explicitly set the callback IP and PORT on the Arduino. When the Arduino replies - as a result of running the simulator against the “physical device”, I can see an exception thrown by the parse() method of the handler. The exception is not important - the code in the parse method is not ready yet - but the point is that the parse method at least got called…


#3

Figured it out… The system was working almost as intended (the messages were being sent) - I just didn’t know how to look for the results…

It was a combination of my having misunderstood what an "event’ was in this case and an error in my “parse()” function. Parse was getting called, but, because of the error, it wasn’t generating any events. As soon as I had a dummy “sendEvent” as the first line in my parse(), I started seeing the event getting generated.

Moral of the story: figuring out your debugging tools is pretty useful… :wink: