Library for creating a server

I am trying to make a simple server on a dedicated IP port, that simply allows an Arduino or Raspberry Pi communicate with the router. I was wondering if there is a library already in the OpenWRT SDK that I could use to do this. If not, what library would be recommended to do create a server on an OpenWRT router? I'd prefer the library to be written in C++, but I would also be just as fine if the library is written in C.

The OpenWrt SDK for version 18 uses musl libc. You should be able to see more by merely downloading the SDK file.

1 Like

That is the "right" answer to a very generic question.

You basically asked "What is the library for reading from files", but in terms of TCP/IP.

First, let me say that "mere mortals" shouldn't be writing servers. There are far too many things that can be done wrong in terms of security and robustness (and have been done wrong, over the years). Better is to use a robust server and its API than try to write your own. (With that in mind, I don't consider uhttpd to be secure or robust, certainly not for an Internet-facing application, and run nginx for any non-internal HTTP / websockets applications.)

Second, running a server of any sort on a device that you rely on for Internet connectivity and security of your entire network, including all attached computers and devices, isn't a great idea. Unless you are interacting with the core systems on the router and, for some strange reason, keyed ssh access, or secure, authenticated access to another, existing control mechanism isn't sufficient, I find it hard to justify running anything on the router that isn't directly and absolutely required for its functions of routing and firewall.

Following through on that, with a Raspberry Pi Zero W available for US$10, or something like the inexpensive GL.iNet routers available for under US$20 if you need an Ethernet port, I'd recommend that over using your existing router.

Now, onto some more helpful suggestions.

What are you using for transport? TCP/IP talks about packets of data, not what is in those packets. Rather than reinvent the wheel, I'd stick with something well established and appropriate. Two I commonly use are HTTP-S and MQTT over TLS. Questions to ask yourself include:

  • How large are the payloads?
  • Is guaranteed delivery required?
  • How often is data sent? Received (and is it even a two-way conversation)?

For MQTT, I find the mosquitto server to work well for me. I run it on a server inside of my firewall, not on my router. MQTT clients can either publish or subscribe to messages, allowing for "two-way" communication through two, one-way channels. Given that you said "Arduino", this may be the kind of thing you're considering. The "paho" MQTT library seems reliable to me for enabling clients. It is available as C and C++, as I recall. There are also bindings or utility libraries for common scripting languages, such as Python.

What data are you going to pass? How are you going to pass it? Many times a standard format such as JSON can make initial programming and later modifications a lot easier. The "ArduinoJson" library works well for embedded devices and, despite its name, runs quite well on every platform I've tried, including macOS, FreeBSD, and Linux-based OSes. I've used it on AVR as well as ARM embedded controllers.

2 Likes

Thank you so much Jeff. MQTT is exactly what I am looking for. Thanks for the advice on not using the router, I will definatly take that into consideration.

I found the HiveMQ tutorials and other information very valuable when I was starting to explore MQTT. What they write about is not specific to their products. A couple jump-in points are

https://www.hivemq.com/mqtt-essentials/

https://www.hivemq.com/blog/how-to-get-started-with-mqtt/

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.