[Ovmsdev] Simple TCP client

Soko ovms at soko.cc
Tue Aug 25 23:06:17 HKT 2020


Hi guys,

@Craig & Derek: Thanks for your input here! I didn't realize the MQTT 
server working on a local LAN basis as well. I definitely don't want 
anything in the internet. Although it means having another service 
running somewhere as middle man between my HA and OVMS.

Michael's command is what I was looking for. Just tested 
/http://192.168.4.1/api/execute?type=js&output=text&command=JSON.print(OvmsMetrics.GetValues(""))/ 
successfully. Don't even need the apikey as I don't have a PWD set as 
OVMS has no AccessPoint started (OVMS is just in my local WiFi with no 
internet connection).

So if I go down the polling route this is what I'm going to do. Pushing 
would still be better though as I don't want my HA (Arduino) to freeze 
for a couple of seconds when polling the car and its not booked into my 
WiFi. Running a TCP server on my HA and having OVMS push only when its 
in my WiFi is the more elegant way... but also the more complex.

thanks guys

Soko

On 24.08.2020 22:39, Michael Balzer wrote:
> Soko,
>
> the webserver REST API provides a simple command execution endpoint 
> you can use to execute Javascript code as well.
>
> See:
> - 
> https://docs.openvehicles.com/en/latest/components/ovms_webserver/docs/index.html#authorization
>
> The scripting system has a simple metrics getter and JSON encoder.
>
> See:
> - 
> https://docs.openvehicles.com/en/latest/userguide/scripting.html#ovmsmetrics
> - https://docs.openvehicles.com/en/latest/userguide/scripting.html#json
>
> Example executing a shell command:
>
> - 
> http://myovms.local/api/execute?apikey=mypassword&output=text&command=stat
>
> Example getting a single metric value:
>
> - 
> http://myovms.local/api/execute?apikey=mypassword&type=js&output=text&command=print(OvmsMetrics.Value("v.b.soc"))
>
> Examples getting multiple metrics JSON encoded:
>
> - 
> http://myovms.local/api/execute?apikey=mypassword&type=js&output=text&command=JSON.print(OvmsMetrics.GetValues("m.net."))
> - 
> http://myovms.local/api/execute?apikey=mypassword&type=js&output=text&command=JSON.print(OvmsMetrics.GetValues(["v.c.charging","v.b.soc"]))
>
> (URI encode as needed)
>
> Also see the scripting documentation and the plugin examples on how to 
> add custom modules providing custom commands.
>
> Of course you can also write data to files and poll those via HTTP. 
> That's one of the reasons the webserver provides access to the file 
> system. Write files in C++ as usual, see scripting object VFS for 
> script based file I/O.
>
> Regards,
> Michael
>
>
> Am 24.08.20 um 18:01 schrieb Soko:
>>
>> Hi again,
>>
>> Before I decide how to do this I would like to explore the other way: 
>> My home automation is polling the data from OVMS.
>>
>> I have had a look to the HTTP API. But it seems this is an API for 
>> the server on the internet and not for OVMS itself, correct?
>>
>> Next option: My home automation does a simple HTTP-GET to OVMS and 
>> reads data like this.
>> Is there a way to read metrics like this?
>> I have found the config->webserver site. I could point it to an own 
>> dir like /store/data. Now I could write/update one file there from my 
>> vehicle-module called "current" with all the data I need. Then simply 
>> to a http-get to 192.168.4.1/current and I get the data of the file. 
>> Is it easy - and most important: allowed - to write/update into a 
>> file in OVMS.
>>
>> thanks heaps for the input
>>
>> Soko
>>
>> On 24.08.2020 01:46, Mark Webb-Johnson wrote:
>>> The vehicle module is definitely the wrong place to do this.
>>>
>>> If you really want to implement in C++, I suggest you look at the 
>>> ovms_server_v3 module. You could use that as a template for what you 
>>> need. It should periodically read from the metrics, and send what 
>>> you need to your home automation.
>>>
>>> You should also consider a javascript plugin to do this. That would 
>>> probably be simpler and easier to update (without firmware changes). 
>>> If it is a common home automation system, you could write it as a 
>>> plugin and share for others with the same system.
>>>
>>> Regards, Mark
>>>
>>>> On 24 Aug 2020, at 12:00 AM, Soko <ovms at soko.cc 
>>>> <mailto:ovms at soko.cc>> wrote:
>>>>
>>>> Hi Mark,
>>>>
>>>> I have my own home automation running at home. I want to push the 
>>>> current values (SoC, odometer, ...) once my car is in the driveway 
>>>> and OVMS is booked into my WiFi. My home automation has a TCP 
>>>> server running were I process the data further.
>>>>
>>>> I don't want to the OVMS Server functionality as I'm not a Linux 
>>>> guy, don't want the data in the internet and no SIM use on the OVMS.
>>>>
>>>> So does the canlog_tcpclient.cpp take care of the concurrency etc. 
>>>> correctly? I will do the same in my code. Or should I start a 
>>>> separate task from the vehicle task for every push?
>>>>
>>>> thx
>>>> Soko
>>>>
>>>> On 23 August 2020 16:37:46 CEST, Mark Webb-Johnson 
>>>> <mark at webb-johnson.net <mailto:mark at webb-johnson.net>> wrote:
>>>>
>>>>     What are you trying to do?
>>>>
>>>>      From a design point of view, it seems strange to have an outgoing tcp connection from a vehicle module. Those modules need to be very responsive and realtime.
>>>>
>>>>     Mongoose is the correct approach. But take care, because most of the work is done in the network (not vehicle) task, so you need to deal with mutex and other concurrency issues.
>>>>
>>>>     Regards, Mark
>>>>
>>>>         On 23 Aug 2020, at 7:54 PM, Soko <ovms at soko.cc
>>>>         <mailto:ovms at soko.cc>> wrote: Hey everyone, I'm trying to
>>>>         send a simple tcp msg to a remote tcp-server. I've looked
>>>>         into the FreeRTOS example
>>>>         (esp-idf\examples\protocols\sockets\tcp_client\) which
>>>>         works (kinda) but OVMS behaves weirdly when I do it like
>>>>         that. Thomas pointed me to mongoose... So is this the way
>>>>         to go? I would take canlog_tcpclient.cpp as a model...
>>>>         Ohh... and is it OK if I send my tcp msg in the vehcile
>>>>         task? Mongoose should take care of unforeseen errors so the
>>>>         task shouldn't block, right? thx Soko
>>>>         ------------------------------------------------------------------------
>>>>         OvmsDev mailing list OvmsDev at lists.openvehicles.com
>>>>         <mailto:OvmsDev at lists.openvehicles.com>
>>>>         http://lists.openvehicles.com/mailman/listinfo/ovmsdev 
>>>>
>>>>     ------------------------------------------------------------------------
>>>>     OvmsDev mailing list
>>>>     OvmsDev at lists.openvehicles.com  <mailto:OvmsDev at lists.openvehicles.com>
>>>>     http://lists.openvehicles.com/mailman/listinfo/ovmsdev
>>>>
>>>> _______________________________________________
>>>> OvmsDev mailing list
>>>> OvmsDev at lists.openvehicles.com <mailto:OvmsDev at lists.openvehicles.com>
>>>> http://lists.openvehicles.com/mailman/listinfo/ovmsdev
>>>
>>>
>>> _______________________________________________
>>> OvmsDev mailing list
>>> OvmsDev at lists.openvehicles.com
>>> http://lists.openvehicles.com/mailman/listinfo/ovmsdev
>>
>> _______________________________________________
>> OvmsDev mailing list
>> OvmsDev at lists.openvehicles.com
>> http://lists.openvehicles.com/mailman/listinfo/ovmsdev
>
> -- 
> Michael Balzer * Helkenberger Weg 9 * D-58256 Ennepetal
> Fon 02333 / 833 5735 * Handy 0176 / 206 989 26
>
> _______________________________________________
> OvmsDev mailing list
> OvmsDev at lists.openvehicles.com
> http://lists.openvehicles.com/mailman/listinfo/ovmsdev
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openvehicles.com/pipermail/ovmsdev/attachments/20200825/085b3305/attachment.htm>


More information about the OvmsDev mailing list