OpenWRT as Pi replacement for sustainability project

Hello,

I am working on a sustainability project and want to use an old router as a development board instead of Arduino/raspberry Pi. In order to make this work I need access to as many GPIO pins and functions as possible.

current setup:
WDR4300 with extra USB mod (https://openwrt.org/toh/tp-link/tl-wdr4300_v1#hardware_modifications)
I have 16MB flash mod ( Upgrade TP-link_TL-WDR4300 from 8M to 16M flash - #24 by gergepalfi )

I am having issues using the GPIO pins that are already mapped as LEDs under /sys/class/leds/
I have rmmod gpio_button_hotplug and rmmod ledtrig_usbport but I still can’t write to the LED value files. I can only write to /sys/class/gpio/tp-link:power:usb1/value to control power on both usb ports

I was able to run gpio_test.sh to read and write the 4 JTAG pins

GPIO Name comments
0 JTAG-9
1 JTAG-3
2 JTAG-5
3 JTAG-7
4 unknown
5 SPI-CS DO NOT USE!
6 SPI-CLK DO NOT USE!
7 SPI-MOSI DO NOT USE!
8 SPI-MISO DO NOT USE!
9 UART0_SIN DO NOT USE!
10 UART0_SOUT DO NOT USE!
11 LED_USB1
12 LED_USB2
13 LED_2.4GHz
14 LED_SYSTEM
15 LED_QSS
16 WPS_BUTTON
17 WIFI_SW
18 LNA0 DO NOT USE!
19 LNA1 DO NOT USE!
20 unknown
21 PW_USB2
22 PW_USB1

Goals

I would like to get the following basic functions working in order of importance

function comment
GPIO read/write basic IO
GPIO PWM LED brightness or motor driver control
I2C on custom pins hoping to use displays or sensors
encoder for rotary knobs

PWM??

I see that the LEDs have a max_brightness and brightness option, even if I can’t write to it. I assume that this is some pwm functionality. It would be nice to get pwm to work

I2C, encoder and other packages

I see there are packages for all kinds of hardware like GPIO expander chips, buzzer, encoder, I2C and more. I would like to get as much documentation as possible about these in case I can use them.

Suggestions welcome

I am not sure how all of this is even possible, I know that it will be a lot harder than Raspberri Pi or Arduino Projects, but that is what this whole thing is about, to see how far I can push the limits and repurpose a router.

Python
I have installed python on an internal USB storage on the modded USB port. I consider this to be the worst option, but if it happens to work somehow then I will take it.

Shell scripts
So far I had success using simple sh scripts to toggle and test pins, but I think it may not be possible for more complex tasks

Compiling C/C++ for MIPS
I think this is the best option, but also the hardest, I have very minimal experience setting up and using cross compilers

Device tree modification
The device tree is already modified for the 16M flash mod, so I could change other things if needed.

If you know of any interesting documentation or mods for using GPIO in weird ways like this please share the links.

2 Likes

yep, if ur careless you can knock power off CPU

Not sure if this is helpful but there is this discussion:

thanks @antonk
I discovered a small mistake on my part. In fact I can write LED states. That space before the redirect is very important, because otherwise the LED is not set, so this solves my first problem.

root@OpenWrt:/mnt/usb/circulating_stuff# echo 0>/sys/class/leds/green:system/brightness 

root@OpenWrt:/mnt/usb/circulating_stuff# echo 0 >/sys/class/leds/green:system/brightness 

However it also shatters my hopes for pwm, the brightness file is always 1 or 0. Even if I write a pwm value of 200 or 20 to it it resets to 1, so I guess no pwm.

Probably you could change the value between 0 and 1 in quick succession and you got PWM. This won't be very efficient but at least possible.

It might be helpful to know what the device is (or will be) doing from a functional perspective.

Are you using routing/firewall functions on the current router for the application?

The idea at the moment is to use it as some kind of appliance with timer screen, buttons, knobs and switches. Think IOT washing machine, microwave, or stove/oven that has multiple inputs and elements to control.

To answer your networking question, no, it will only be used as a client device.

Think of PI ZERO - all gpios on the pcb, costs peanuts and will suffice for years with OpenWrt.

1 Like

Have you considered using an Arduino device, with firmata firmware, and an I/O expansion board? Or perhaps an inexpensive ESP32 board, perhaps even standalone?

2 Likes

To my understanding, the idea is to save old devices from the landfill. Clearly the OP wants to hear suggestions for a new device :wink:

1 Like

That is correct, this is an educational project to provide options to reduce e-waste and repurpose old devices. That means ESP, Arduino, Teensy, Raspberry Pi, Orange Pi, STM32 are out of the question because it makes no sense to promote buying new even if it would make development a whole lot easier.
That is the reason I chose routers and OpenWRT. OpenWRT already gives new life to these devices long after the manufacturer has abandoned firmware updates. However it all has limits, think 4/32 devices (I am also working on a linksys160N which is close to 20 years old now) . Why do they have to go to landfill if they can be used with Wifi connectivity and GPIO for IOT purposes?

2 Likes

Once upon a time, this might have made sense - today, a esp32-c3 (e.g. with tasmota or esphome) goes for under 2 EUR… Plenty of GPIOs, several dedicated (multiplexed) hardware buses (I²C, SPI, serial, …), 3.3 V (or 5 V via USB/ internal voltage regulator), significantly under 1 A and quite a ready-made addon ecosystem; ethernet- or PoE ports are possible as well. The tl-wdr4300 is still an okay'ish router (8 MB flash is becoming a challenge, especially if you want to add more complex IoT stuff) despite its 802.11n/ wifi4 WLAN, but it won't excel for your intended use case.

Doesn't it make sense to avoid manufacturing a new device when an old one (designated for the landfill) could do its job? Regardless of the cost of a new device (even negative cost, let alone $2 positive cost)?

In this particular case (and today, when esp32 availability is not an issue), I don't think so, because of the (ongoing) integration cost. No, I'm not saying you need to bill your own hobby time here, that's free or just brings 'joy'. But I am considering the efforts to isolate these disjunct GPIO sources, what it takes to talk to them. Yes, a lot of respect to those who shoe-horned an sd card on the original wrt-54g and wrote bit-banging spi drivers for that - or who desoldered the tuner from bt848 cards to gain a dozen GPIOs on x86, but that was a different era and there were no better alternatives. While this approach is risky (might not survive your soldering attack), 'messy' (rats' nest of tiny wires and hot glue), requires long term commitment (rebasing the patches to a vibrant code base like OpenWrt, think ar71xx --> ath79, or just kernel bumps and the effect on your dts patches (files/ --> (files-6.6/ && files-6.12/ --> files/, given that you can't integrate your custom patches upstream, you're not standing on the shoulders of giants, but 'fighting' against them (and a router needs timely updates, a esp32 less so)), doesn't integrate 'nicely' (compared to tasmota/ esphome (or even your self-written} web guis, mqtt integration, matter, ...), isn't 'isolated' (if you fry your esp32, just take a new (or better, esp8266, esp32, esp32-c3, esp32-c6, all nicely abstracted away via arduino or IDF, if you fry your router, you don't have internet and need to redo your work for a different model) one), rarely is placed conveniently (router sits at a different place than your shutters or whatever you want to control (and you could use the router's USB ports for powering the esp, if that is not an issue) and you can easily replicate the esp (need a second one upstairs, an experimental one on your desk, etc.). Here availability, pricing (router vs esp), reproducability (in the sense of duplicating and migrating to bigger models) and the wide scope of APIs (both exposed hardware interfaces and software integration (mqtt et al)) just favours the esp and 'negates' the cost. It's self-contained (you don't break your router, your router upgrades don't break your project) and in a way more dependable. (And yes I realize that the maze of arduino libraries and the lack of auditing isn't all golden either). If all you have is a hammer, not everything makes a good nail.

I still have a couple of 250GB HDDs (not a typo) running on my main computer… I agree about not throwing away useable hardware.

Sometimes it's not (only) about the money / time needed to retrofit old hardware, but also about the new hardware required (USB connectors, memory chips, …).

Anyway, OP has their requirements, so no new hardware.

1 Like

The energy consumption during use could be relevant, in case these devices would be always-on. For anything that is only occasionally powered on when it is actually needed, it does not matter that much. Even some 20 to 30 years old PC clone could be fine, no matter if you got much more performance-per-watt out of modern hardware. For example, I have some USB HDDs that I use for backing up my computers. Once a week or two, I’d plug in the disk and run rsync. That is sufficient for my use case and certainly more energy efficient than having an always-on NAS.

why not use it to provide wifi for an arduino or an iot sensor etc... I use most my old 4mb units for this purpose. I also do some preprocessing of data on the 703n's,. before sending it to the main unit via wifi.

the typical plumbing

sensors -->     arduino --> serial comms-->     tp-link 703n ---> preprocess -->wif-->     web-socket server (main unit)

i prefer to repurpose over recycle too :wink:

EDIT
I should mention that i use a 17cent level shifter, to drop the arduino 5v uart to play nice with the 703n's 3.3v uart

also, if the unit is monitor mode capable, you can use it for communicate and or controlling esp32's via esp-now ... I've done this as well, no wires or mods necessary ..just monitor mode and the esp-now-openwrt library

3 Likes

I made some progress with setting up the SDK and compiling some basic helloworld and openwrt packages.
my current goal is to try this

if anybody knows of other low level GPIO related packages/source code please let me know
it may be useful as reference.

At the moment I am limited by the speed of shell scripts, so I can't even emulate PWM by quick switching as suggested by @antonk

I was already able to attach simple buttons, switches, beepers and relays. Even a motor driver, but without PWM I can't control speed

Not sure how much I can do, but I will try looking into PWM, or even servo control if I can accurately time duty cycles.

well nothing is as simple as download and compile

/home/gerge/Downloads/wrt_hack/openwrt-sdk-ath79-generic_gcc-14.3.0_musl.Linux-x86_64/build_dir/target-mips_24kc_musl/linux-ath79_generic/i2c-gpiod-custom/i2c-gpiod-custom.c:52:10: fatal error: asm-generic/gpio.h: No such file or directory
   52 | #include <asm-generic/gpio.h>
      |          ^~~~~~~~~~~~~~~~~~~~
compilation terminated.

too tired to troubleshoot now, will try tomorrow

Those old routers are rather power-hungry compared to any of the modern alternatives (ESP32, Raspberry Pi Zero, …) and any environmental gain from not manufacturing a new device will be quickly dwarfed by the power consumption if this is destined to be an always-on device.

That said, if you need a few ethernet ports anyway, choosing an old router can make a lot of sense.