Get Data from ESP8266 via webserver or mqtt?

The easiest would be to add MQTT to the ESP and have that publish the sensor readings over MQTT. OH then subscribes through the MQTT binding. That approach is kind of the standard approach for ESPs integration with home automation hubs (MQTT is one of the main transport protocols used in this problem space).

But since you already have the ESP serving up a web page the HTTP binding is the next best choice.

When posting configs, please click the code tab and paste the full text you find there in code fences.

```
code goes here
```

Screen shots are not terribly useful and without code fences the text gets formatted in unhelpful ways. This works great for stuff like HTML too as it preserves the < >.

For this use case you wouldn’t use the command properties at all. Each Channel represents two way communication. Device → OH is handled with the “state” properties and OH → Device is handled by the “command” properties. You don’t have a case where OH is telling the ESP to do something (e.g. turn on a light) so you have no commands at all. You just want OH to periodically pull and parse the web page hosted by the ESP.

Pretty much all of the AI chatbots are worse than useless when it comes to OH configs.

The HTTP binding is one of the hardest to work with because it’s one of the lower level bindings.

At a high level:

  • ignore all the command stuff, you’re not going to be using them

  • in your Channel config the two most important fields will be:

    • the “State URL Extension” which I presume you don’t need here. This is where you would add the last part of the URL to get to the web page where the status data can be retrieved and it’s useful in cases where the URL for the state is different from the URL for commands, or the Thing needs to hit several URLs across multiple Channels.

    • the second filed is the “State Transformation”. This is where you put the stuff that parses and extracts the piece of data you need for that Channel. In this case I’d expect either REGEX (REGEX:.*temperature>(.*)</span>.*) or, assuming XHTML XPath could be used (this doesn’t look like XHTML but it’s hard to tell without code fences).

Transformations are add-ons that must be installed from the Add-on Store.

Here is a relatively simple HTTP example that pulls information from the National Weather Service:

UID: http:url:nws
label: National Weather Service Forecast
thingTypeUID: http:url
configuration:
  authMode: BASIC
  ignoreSSLErrors: false
  baseURL: https://api.weather.gov/gridpoints/{Station Name}/{gridpoints,gridpoints}/forecast
  delay: 0
  stateMethod: GET
  refresh: 6000
  commandMethod: GET
  contentType: application/json
  timeout: 3000
  bufferSize: 2048
channels:
  - id: Updated
    channelTypeUID: http:datetime
    label: Updated
    description: ""
    configuration:
      stateTransformation: JSONPATH:$.properties.updated

I’m just showing the one Channel. the full Thing can be found at NWS Weather API - #2 by rlkoshak. You can see that it;s all the same end point so all I really need to configure is the stateTransformation to extract that one piece of data from the data pulled down. Note that it’s JSON formatted so I use the JSONPATH transform to extract the DateTime when the forecast was last updated (in this case).

Another simple one that has both a command and state config on the Channel:

UID: http:url:adguard
label: AdGuard
thingTypeUID: http:url
configuration:
  headers:
    - ""
  ignoreSSLErrors: true
  stateMethod: GET
  refresh: 30
  commandMethod: POST
  timeout: 3000
  authMode: BASIC_PREEMPTIVE
  baseURL: https://{adguard}/control
  password: password
  delay: 0
  contentType: application/json
  bufferSize: 2048
  username: username
location: Global
channels:
  - id: protection_status
    channelTypeUID: http:switch
    label: Protection Status
    description: ""
    configuration:
      mode: READWRITE
      onValue: "true"
      commandTransformation: 'JINJA:{"protection_enabled": {{value}}}'
      offValue: "false"
      stateExtension: /status
      commandExtension: /dns_config
      stateTransformation: JSONPATH:$.protection_enabled

This interacts with the AdGuard API to keep track of whether ad blocking is enabled and to be able to turn ad blocking on and off through OH.

Note that except for the JINJA transform stuff in { } has been redacted and you would need to replace them with your values to have a working Thing.

1 Like