Process web server response

I’m looking for suggestions how to send a request to a web server (via URL), parse the webpage return string, and display information in a tile. The request will be sent to Arduino MKR1010 microcontrollers with environmental sensors connected.

To display information in a tile, you need a device/attribute.

Therefore my recommendation would be creating a custom driver, which makes use of httpGet or asynchttpGet Hubitat Documetation, parses the response, then pushes the desired data to an attribute using sendEvent

Thanks @dkilgore90. The driver is written and working very well, but I do have a series of questions. I’ll create a thread under coding (unless this thread can be moved…)

Done. Now under Developers/Coding Questions

Here’s a list of questions I developed while writing the driver. As an overview, the driver uses asynchttpGet to make a request to a webserver. The webserver runs on an Arduino MKR1010 and displays the results from the Arduino processing data from a connected BME280 sensor. I wrote the Arduino program in C++ and it creates a HTML webpage that formats the data as a table. The driver parses the response so HE can use the data in a tile.

  1. Is there a method to prototype code besides using the HE?
  2. I used Notepad++ with Java selected as the language to write the code, and then copy and paste to HE. Is there a better method? Version/reversion control is difficult with copying and pasting back and forth.
  3. My understanding is that Groovy is an interpreted version of Java. I assume the HE interpreter is single pass and, therefore, variable declaration order is important.
  4. For simplicity, the first version of the driver hardcoded the webserver’s IP address. This version works, but there would need to be a separate copy of the driver for each Arduino. To fix that issue, I created a second version that used command to request user input for the webserver's IP address. The variable referenced in command is not global. How should this issue be resolved? Also, are the variables referenced in command persistent after a hub reboot?
  5. I created a third version that uses preferences instead of command. Doing so works better in that input variables seem to be global. Is there a recommended preference – command or input?
  6. The attribute variables listed in the device’s page (“Current states”) are not in the order listed in the driver. And the order appears to randomly change. Is this normal?
  7. Can more than one attribute be used in a tile? It appears not, so I built a concatenated attribute comprised of the info I wanted to display.
  8. The def params used by the asynchttpGet handler is not global when defined in the program’s body. What’s the best method to handle this?

You would want to use an INPUT preference to store something like an IP Address. No need to create a custom command for that, as the value will not change once set.

Hmmm… never noticed the order changing, but it could be replayed to the order in which the Attributes are assigned their first values.

1 Like

It sounds like you’re enjoying writing your own solution/integration. If, however, you’d like a slightly more ready to run integration for Arduino microcontrollers with Hubitat, take a look at my HubDuino project. It is a set of Arduino libraries and example sketches, along with HubItat Drivers, that allow for a quick integration between the two.

Even if you don’t want to use HubDuino, feel free to take a look at my Parent and Child drivers for examples of using Groovy code.

Have fun!

2 Likes

I'll sometimes use a web console like https://www.google.com/url?sa=t&source=web&rct=j&url=https://groovyconsole.appspot.com/&ved=2ahUKEwjWr7jjhr_1AhUvCjQIHS1tAmwQFnoECAQQAQ&usg=AOvVaw0Fl3vkMn32sHQNIBcydWZ1 to test out some snippets that are pure groovy (don't use any hub-specific methods)

Personally I use Visual Studio Code for the actual editor - lots of folks (myself included) use GitHub for version control.

Didn't understand the entire question here, since I write code, but don't speak the whole developer lingo. But yes, must declare a variable before you can use it.

Have you declared them as attributes at the top of your driver? Otherwise they aren't fully persisted, just show up transiently. Usually I see them displayed in alphabetical order.

1 Like

That’s it – alphabetical order

Variables need to be declared before they are used. i=i+10 may be undefined unless int=i precedes it.

1 Like

Thanks @ogiewon. I’ve been looking at your project.

2 Likes