(SOLVED) ESP8266 with Json by wifi

Thank you Rich,

But when I send this command:

Switch Light_map “Light_map” (FF_Bed, Lights) { http=">[*:POST:http://192.168.1.166:8800:MAP(placa1.map)]" }

I get this error:

2017-08-18 08:06:13.456 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve item ‘PlacaMarkLiga’ for widget org.eclipse.smarthome.model.sitemap.Switch
2017-08-18 08:06:13.456 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve item for widget org.eclipse.smarthome.model.sitemap.Switch
2017-08-18 08:06:13.457 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve item for widget org.eclipse.smarthome.model.sitemap.Switch
2017-08-18 08:06:13.457 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve item for widget org.eclipse.smarthome.model.sitemap.Switch
2017-08-18 08:06:13.457 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve item ‘PlacaMarkLiga’ for widget org.eclipse.smarthome.model.sitemap.Switch
2017-08-18 08:06:13.457 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve item ‘PlacaMarkLiga’ for widget org.eclipse.smarthome.model.sitemap.Switch
2017-08-18 08:06:13.458 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve item ‘PlacaMarkLiga’ for widget org.eclipse.smarthome.model.sitemap.Switch
2017-08-18 08:06:18.385 [WARN ] [org.apache.karaf.services.eventadmin] - EventAdmin: Exception during event dispatch [org.osgi.service.event.Event [topic=openhab/command/Light_map] {bridgemarker=true, item=Light_map, command=ON} | {org.osgi.service.cm.ManagedService, org.osgi.service.event.EventHandler}={event.topics=openhab/*, service.pid=org.openhab.http, component.name=org.openhab.binding.http, component.id=18, service.id=332, service.bundleid=220, service.scope=bundle} | Bundle(org.openhab.binding.http_1.10.0 [220])]
java.lang.IllegalArgumentException: Invalid uri ‘http://192.168.1.166:8800:MAP(placa1.map)’: invalid port number
at org.apache.commons.httpclient.HttpMethodBase.(HttpMethodBase.java:222)[58:org.apache.servicemix.bundles.commons-httpclient:3.1.0.7]
at org.apache.commons.httpclient.methods.ExpectContinueMethod.(ExpectContinueMethod.java:93)[58:org.apache.servicemix.bundles.commons-httpclient:3.1.0.7]
at org.apache.commons.httpclient.methods.EntityEnclosingMethod.(EntityEnclosingMethod.java:119)[58:org.apache.servicemix.bundles.commons-httpclient:3.1.0.7]
at org.apache.commons.httpclient.methods.PostMethod.(PostMethod.java:106)[58:org.apache.servicemix.bundles.commons-httpclient:3.1.0.7]
at org.openhab.io.net.http.HttpUtil.createHttpMethod(HttpUtil.java:318)[197:org.openhab.core.compat1x:2.1.0]
at org.openhab.io.net.http.HttpUtil.executeUrl(HttpUtil.java:167)[197:org.openhab.core.compat1x:2.1.0]
at org.openhab.io.net.http.HttpUtil.executeUrl(HttpUtil.java:130)[197:org.openhab.core.compat1x:2.1.0]
at org.openhab.binding.http.internal.HttpBinding.formatAndExecute(HttpBinding.java:285)[220:org.openhab.binding.http:1.10.0]
at org.openhab.binding.http.internal.HttpBinding.internalReceiveCommand(HttpBinding.java:130)[220:org.openhab.binding.http:1.10.0]
at org.openhab.core.binding.AbstractBinding.receiveCommand(AbstractBinding.java:97)[197:org.openhab.core.compat1x:2.1.0]
at org.openhab.core.events.AbstractEventSubscriber.handleEvent(AbstractEventSubscriber.java:45)[197:org.openhab.core.compat1x:2.1.0]
at org.apache.felix.eventadmin.impl.handler.EventHandlerProxy.sendEvent(EventHandlerProxy.java:415)[6:org.apache.karaf.services.eventadmin:4.0.8]
at org.apache.felix.eventadmin.impl.tasks.HandlerTask.run(HandlerTask.java:90)[6:org.apache.karaf.services.eventadmin:4.0.8]
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)[:1.8.0_112]
at java.util.concurrent.FutureTask.run(FutureTask.java:266)[:1.8.0_112]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)[:1.8.0_112]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)[:1.8.0_112]
at java.lang.Thread.run(Thread.java:745)[:1.8.0_112]

The port is open in the Windows firewall, but I still get this error.

Could help?

You have an item on your sitemap named PlacaMarkLiga.

This item, as far as the sitemap is concerned, does not exist.

Do you have an other with exactly this name? Case matters.

Are you seeing errors when the .sitemap or .items files load?

Have you opensed the files in Designer to see if it finds any syntax errors?

Thanks for the help Rich,

PlacaMarkLiga was just rubbish that he had forgotten to exclude. I already erased it.
There is no error at startup of openhab.
The error occurs when you call the LIght_map command:

2017-08-18 14:54:48.326 [WARN ] [org.apache.karaf.services.eventadmin] - EventAdmin: Exception during event dispatch [org.osgi.service.event.Event [topic=openhab/command/Light_map] {bridgemarker=true, item=Light_map, command=ON} | {org.osgi.service.cm.ManagedService, org.osgi.service.event.EventHandler}={event.topics=openhab/*, service.pid=org.openhab.http, component.name=org.openhab.binding.http, component.id=18, service.id=329, service.bundleid=220, service.scope=bundle} | Bundle(org.openhab.binding.http_1.10.0 [220])]
java.lang.IllegalArgumentException: Invalid uri ‘http://192.168.1.166:8800:MAP(placa1.map)’: invalid port number

The invalid port error appears for any port I use.

Any idea ?

There is something wrong with the syntax of the http binding config.

Compare it to the docs and see if you are missing a double quotes or something like that.

It is treating the :MAP part as part of the url.

Hello Rich,

Searching deeper, I noticed that the ideal connection would be TCP and not HTTP, because the device is on LAN. Is my thinking correct?
In this way, I changed the settings as follows:
Items:
Switch Light_Omnimark “OmniMark” (FF_Bed, Lights) {udp=">[ON:192.168.1.171:8800:‘MAP(omnimark.map)’)], >[OFF:192.168.1.171:8800:‘MAP(omnimark.map)’]"}

Map:ON="{“ObjType”: 0,“Id”: 1,“OnState”: 1,“InvertedState”: 0,“ToggleState”: 0,“Send2”: 0}"
OFF="{“ObjType”: 0,“Id”: 1,“OnState”: 0,“InvertedState”: 0,“ToggleState”: 0,“Send2”: 0}"
(The string must be sent in json format)

I configured the channel as follows:

But I get the following error message:
2017-08-19 10:17:17.674 [ERROR] [g.tcp.AbstractDatagramChannelBinding] - there is no channel that services [itemName=Light_Omnimark, command=OFF]
2017-08-19 10:17:19.797 [ERROR] [g.tcp.AbstractDatagramChannelBinding] - there is no channel that services [itemName=Light_Omnimark, command=ON]

What am I doing wrong? Sorry for the inconvenience, but I’m still starting on OpenHAB.

If your device uses HTTP commands like GET and PUT then ylou should be using the HTTP binding regardless of whether it is on your lan or not.

I have no experience with the TCP binding so can’t help there.

But you already to be trying to create a Network binding Thing which has nothing at all to do with the TCP/UDP binding. All the Network binding will tell you is whether the device at a given IP address responds to pings.

Furthermore, the TCP/UDP binding is a1.x version binding which doesn’t use Things anyway.

Hello Rich

Sorry, but because of my lack of knowledge in openHab, I’m getting confused. I will try to explain the operation of my devices:

I have some devices that work in internal network. They must connect to the server (Raspberry) where openHAB is installed. Each client device connects to the server on a different port (8800, 8801, etc).

What I need to do: send a simple string {“ObjType”: 0, “Id”: 1, “OnState”: 1, “InvertedState”: 0, “ToggleState”: 0, “Send2”: 0} to the port of the device to be controlled

Is HTTP connection the best way to do this? I have read the openHAB documentation, but I did not understand how to do this.

Could you show me the way?

You are missing a whole lot of details.

What protocol do the devices expect? Answer that and you will have your answer as to what binding to use.

You first must understand how your devices talk. OH will not help you answer this question.

There is a world of difference between plain old UDP, TCP, HTTP, MQTT and any of the hundreds of protocols computers use to talk to each other. You have provided no information to narrow it down.

Excuse me, Rich,

I really forgot these important details.

The devices receive and send a simple JSON string (eg receive Json with command: {“ObjType”: 0, “Id”: 1, “OnState”: 1, “InvertedState”: 0, “ToggleState”: 0, "Send2 ": 0} and returns the state with the same JSON string, informing its current state.
The problem is that each device is connected to a different port, which is defined in the configuration of each device.
The server will be a Raspberry PI3, with OpenHAB. The devices will connect directly to the server through the wi-fi. All on a single internal network.

Did I forget something?

Again, very grateful for your help.

I don’t understand anything about your setup :frowning:

This seems contradicting. For sure both sides can be both server and client, but it is not how this usually works.

Are you sure that your ESP8266 code is doing what it supposed to do (switch the relay) and if so, how did you test this? Using e.g. curl commands or postman or some other tool?

Can you show us the command that you use? That will make it easier for us to see if we can select the proper binding in openHAB to control your device.

Hello Marcel,

True, I explained incorrectly at first. ESP 8266 is a client. And it works correctly. When I use a TCP socket server I have here, just send the JSON command “{” ObjType “: 0,” Id “: 1,” OnState “: 1,” InvertedState “: 0,” ToggleState “: 0,” Send2 “: 0}”, the relay lights up. When I send the JSON command “{” ObjType “: 1,” Id “: 1,” Op “: 1,” Send2 “: 0}”, the ESP returns the relay state.
The ESP is connected to a port set during configuration.

The ESP is already configured to read the string and perform the function I chose (on, off, toogle, etc).

With the socket server, the ESP works perfectly. But I can not get openHAB to send this command to the port.

Thanks for any help.

I suggest you to use some common supported protocol such as http/rest (or simple query) where the esp node is the server

You own the node code so i think that it’s better to modifiy the node to be “connected” to OH easily
and avoid to stress OH for very personal solution. do you agree :slight_smile: ?
Greetings

1 Like

Hello Fulvio,

I do not know if this would be the best alternative, especially if I intend to use twenty ESP or more.
I believe the solution would be to connect the ESP in the OH as a thing. I’ve noticed that there is setup for IP and port when adding a new thing.
Could this path be viable?

Grateful

Ok, I think this could work with some modifications to your original plan.

Disclaimer: I have never worked with the TCP & UDP Binding before. This is all based on this and this. I did not have time to test this tonight. However, I’m hopeful that this will work :slight_smile:

First make sure that you have the TCP & UDP Binding installed.

Next configure the binding using the service/tcp.cfg configuration file:

port=8800
addressmask=true

Port 8800 is the port that openHAB will listen on for incoming connections from your ESP8266 clients. So this is a deviation from your original plan where each ESP8266 would be connecting to a different port. openHAB will be able to differentiate between the different ESP8266 clients by their ip address.

The addressmask parameter is required to be able to use * for the port number in the item definition.

You’re item definition will look like this:

Switch Light_Omnimark         “OmniMark”       (FF_Bed, Lights) {tcp="<[ON:123.123.123.123:*:‘MAP(omnimark.map)’)], <[OFF:123.123.123.123:*:‘MAP(omnimark.map)’]"}

Replace 123.123.123.123 by the ip address of your ESP8266.

Cross fingers, and hope that it works :slight_smile:

Yes

How? There are literally hundreds of ways this can happen. This is what we really need to know.

Wrong. If you have this many nodes it is even more important that you use something higher level like HTTP or MQTT.

There are better ways to handle addressing each device uniquely in the higher level protocols, not to mention built in with checking, guaranteed delivery, etc.

Hello Marcel

After your tips, I believe we’re almost there !!!

But now TCP tries to communicate with all ports. And when I activate the switch, I get the log: there is no channel that services [itemName = Light_Omnimark, command = ON]

Any idea?

2017-08-22 22: 25: 07.364 [INFO] [ing.tcp.AbstractSocketChannelBinding] - Received connection request from /192.168.1.166:25108
2017-08-22 22: 25: 07.365 [INFO] [ing.tcp.AbstractSocketChannelBinding] - Disconnecting the unallowed remote end /192.168.1.166:25108
2017-08-22 22: 25: 07.382 [ERROR] [ing.tcp.AbstractSocketChannelBinding] - there is no channel that services [itemName = Light_Omnimark, command = ON]
2017-08-22 22: 25: 07.616 [ERROR] [ing.tcp.AbstractSocketChannelBinding] - We do not accept outgoing connections for Items that do use address masks
2017-08-22 22: 25: 07.616 [ERROR] [ing.tcp.AbstractSocketChannelBinding] - We do not accept outgoing connections for Items that do use address masks
2017-08-22 22: 25: 07.616 [INFO] [ing.tcp.AbstractSocketChannelBinding] - Received connection request from /192.168.1.166:14962
2017-08-22 22: 25: 07.616 [INFO] [ing.tcp.AbstractSocketChannelBinding] - Disconnecting the unallowed remote end /192.168.1.166:14962
2017-08-22 22: 25: 07.866 [ERROR] [ing.tcp.AbstractSocketChannelBinding] - We do not accept outgoing connections for Items that do use address masks
2017-08-22 22: 25: 07.866 [ERROR] [ing.tcp.AbstractSocketChannelBinding] - We do not accept outgoing connections for Items that do use address masks
2017-08-22 22: 25: 08.116 [ERROR] [ing.tcp.AbstractSocketChannelBinding] - We do not accept outgoing connections for Items that do use address masks
2017-08-22 22: 25: 08.116 [ERROR] [ing.tcp.AbstractSocketChannelBinding] - We do not accept outgoing connections for Items that do use address masks

Grateful

There seems to be an issue with your item definition because openHAB is not allowing the connection from 192.168.1.166.

2017-08-22 22: 25: 07.364 [INFO] [ing.tcp.AbstractSocketChannelBinding] - Received connection request from /192.168.1.166:25108
2017-08-22 22: 25: 07.365 [INFO] [ing.tcp.AbstractSocketChannelBinding] - Disconnecting the unallowed remote end /192.168.1.166:25108

This is not correct. It should look like this:

2017-08-23 20:29:21.184 [INFO ] [ing.tcp.AbstractSocketChannelBinding] - Received connection request from /192.168.0.7:50102
2017-08-23 20:29:21.188 [INFO ] [ing.tcp.AbstractSocketChannelBinding] - 192.168.0.7:* is an allowed masked remote end. The channel will now be configured

Here I’m connecting successfully using telnet from my laptop to my openHAB instance running on a Raspberry Pi.

There seem to be a number of problems with single and double quotes in your item definition. These are not of the correct type.
When editing files, use either the openHAB Designer or Visual Studio Code with the openHAB extension (or notepad, notepad++, nano or vi if you must).
Don’t use Write, Wordpad, Word or any other such kind of program because the tend to ‘beautify’ your text by replacing single and double quotes by more fancy but incompatible versions.

The same holds true for your map file. Here you can spot the incorrect double quotes as well:

ON="{“ObjType”: 0,“Id”: 1,“OnState”: 1,“InvertedState”: 0,“ToggleState”: 0,“Send2”: 0}“
OFF=”{“ObjType”: 0,“Id”: 1,“OnState”: 0,“InvertedState”: 0,“ToggleState”: 0,“Send2”: 0}"

Only the first double qoute is the correct one. The others are all the fancy type.

So tonight I’ve been doing some testing with the following item defintion. Note that 192.168.0.7 is the ip address of my laptop.

Switch Light_Omnimark         "OmniMark"       (FF_Bed, Lights) {tcp="<[ON:192.168.0.7:*:MAP(omnimark.map)], <[OFF:192.168.0.7:*:MAP(omnimark.map)]"}

And this omnimark.map file:

{\"ObjType\"\:\ 0,\"Id\"\:\ 1,\"OnState\"\:\ 1,\"InvertedState\"\:\ 0,\"ToggleState\"\:\ 0,\"Send2\"\:\ 0}\r\n=ON
{\"ObjType\"\:\ 0,\"Id\"\:\ 1,\"OnState\"\:\ 0,\"InvertedState\"\:\ 0,\"ToggleState\"\:\ 0,\"Send2\"\:\ 0}\r\n=OFF
ON={"ObjType": 0,"Id": 1,"OnState": 1,"InvertedState": 0,"ToggleState": 0,"Send2": 0}
OFF={"ObjType": 0,"Id": 1,"OnState": 0,"InvertedState": 0,"ToggleState": 0,"Send2": 0}

Using telnet I can connect to openHAB on port 8800. When I send the “ON” JSON string then in Basic UI the switch switches to the ON position (after refreshing the page). When I send the “OFF” JSON string then in Basic UI the switch switches to the OFF position (again, after refreshing the page).

When I use Basic UI to send the ON and OFF commands then for some reason I do not see the result in my telnet session. So at first I thought that this didn’t work. However, when tracing with Wireshark I can see that my laptop receives the JSON string from openHAB, so it is working.

In your omnimark.map file you may need to play with the \r\n a bit. May be just a \n will suffice. That all depends on your ESP8266 code. When using telnet I need to append the JSON in the mapping file with \r\n because telnet appends those characters when I press the enter key.

That all said, I would strongly suggest that you take the advice Rich gave and use MQTT instead.

Good luck!

Hello Marcel,

Eureka !!!

It worked! It was the damn quotes! Now everything is working perfectly.

Thank you very much. And sorry for messing with your sleep with the tests!

1 Like

Hi Nilton

I have started replacing nearly all my own ESP8266 code (both LUA and Arduino) because I have found ESPEASY does almost everything that I need. It is well documented.

For this post, you can switch an output pin high or low via http

However, as I already run an MQTT Broker on the same Raspberry PI as OH2, I simply create the OH2 switches and use the default MQTT pub/sub commands in OH2 to control any output on any ESP8266 device I have on the network by name. No need to know the IP address of the ESP.

For example, my OH2 .items file might contain something like the following to control my 8 channel relay board
Switch RELAY_BOARD_CH1 {mqtt=">[mqttserver:ESP8266-RELAYS/gpio/05:command:ON:1],>[mqttserver:ESP8266-RELAYS/gpio/05:command:OFF:0]"}
Switch RELAY_BOARD_CH2 {mqtt=">[mqttserver:ESP8266-RELAYS/gpio/04:command:ON:1],>[mqttserver:ESP8266-RELAYS/gpio/04:command:OFF:0]"}
Switch RELAY_BOARD_CH3 {mqtt=">[mqttserver:ESP8266-RELAYS/gpio/00:command:ON:1],>[mqttserver:ESP8266-RELAYS/gpio/00:command:OFF:0]"}
Switch RELAY_BOARD_CH4 {mqtt=">[mqttserver:ESP8266-RELAYS/gpio/02:command:ON:1],>[mqttserver:ESP8266-RELAYS/gpio/02:command:OFF:0]"}
Switch RELAY_BOARD_CH5 {mqtt=">[mqttserver:ESP8266-RELAYS/gpio/16:command:ON:1],>[mqttserver:ESP8266-RELAYS/gpio/16:command:OFF:0]"}
Switch RELAY_BOARD_CH6 {mqtt=">[mqttserver:ESP8266-RELAYS/gpio/14:command:ON:1],>[mqttserver:ESP8266-RELAYS/gpio/14:command:OFF:0]"}
Switch RELAY_BOARD_CH7 {mqtt=">[mqttserver:ESP8266-RELAYS/gpio/12:command:ON:1],>[mqttserver:ESP8266-RELAYS/gpio/12:command:OFF:0]"}
Switch RELAY_BOARD_CH8 {mqtt=">[mqttserver:ESP8266-RELAYS/gpio/13:command:ON:1],>[mqttserver:ESP8266-RELAYS/gpio/13:command:OFF:0]"}

Where ESP8266-RELAYS is the system name of my ESP controlling the relay board
There is almost nothing to configure to have ESPEASY work with OH2 - after configuring the WiFi network details, all you really need to do is add an OpenHAB MQTT controller.

It works wonderfully. I have also used IFTTT and my Google Home to make a call directly to an ESP that I have exposed (for testing purposes only) to the internet, so that IFTTT can access it.

Have fun.

Cheers MG

1 Like

Wonderful Mark,

I’m going to study the code.

Thank you very much.