💡 Vendo Nightlight Control
Remotely control nightlight (LED lighting) on multiple JuanFi vending machines from your MikroTik router using an automated API script. This guide demonstrates how to loop through multiple vendo devices, authenticate via API key, and toggle nightlights on/off based on time of day or manual trigger. Perfect for managing ambient lighting in 24/7 convenience stores, reducing power consumption at night, or creating scheduled lighting scenes.
What this does:
- Targets multiple vendo IP addresses (e.g., 10.1.1.2, 10.1.2.2, 10.1.3.2)
- Authenticates with API key header (
X-TOKEN) - Sends HTTP POST request to toggle nightlight
- Logs success/failure for each vendo
- Handles offline devices gracefully
Prerequisites
- ✅ MikroTik RouterOS device with HTTP client capability
- ✅ One or more JuanFi vendo devices on network (with nightlight feature)
- ✅ Vendo devices have assigned static IP addresses
- ✅ API credentials from vendo (IP address + API key)
- ✅ Network connectivity between MikroTik and all vendos
- ✅ Access to RouterOS console (SSH, WebFig, or WinBox)
- ✅ RouterOS v6.48+ for reliable HTTP POST operations
Vendo compatibility:
- Works with JuanFi v2+ and compatible vending systems
- Requires vendo firmware that supports
/admin/api/toggerNightLightendpoint - Check with your vendo provider for API documentation
Configuration Steps
Option A: Terminal Configuration
-
Access the terminal via SSH, console, or WebFig terminal
ssh admin@your-router-ip -
Create a script for nightlight control:
/system script add name="vendo-nightlight-control" source={
:local vendos {"10.1.1.2"; "10.1.2.2"; "10.1.3.2";};
:local apiKey "pqa92jwxrk";
:local state 0;
:foreach vendo in=$vendos do={
/do {
:log warning "============<[[ Setting Nightlight $vendo ]]>=============";
/tool fetch http-method=post http-header-field="X-TOKEN: $apiKey" \
url="http://$vendo/admin/api/toggerNightLight?toggle=$state"
} on-error={
:log error "============<[[ ERROR! $vendo OFFLINE... ]]>=============";
}
}
}tipCustomize the script:
- Add/remove vendo IPs in
vendosarray - Replace
pqa92jwxrkwith your actual API key - Change
state 0to1to turn on nightlights
- Add/remove vendo IPs in
-
Schedule the script to run (daily at night - turn off nightlights):
/system scheduler add name="nightlight-off-schedule" on-event="vendo-nightlight-control" \
start-time=20:00:00 interval=1d comment="Turn off vendo nightlights at 8 PM" -
Create second script for daytime (turn nightlights back on):
/system script add name="vendo-nightlight-on" source={
:local vendos {"10.1.1.2"; "10.1.2.2"; "10.1.3.2";};
:local apiKey "pqa92jwxrk";
:local state 1;
:foreach vendo in=$vendos do={
/do {
:log warning "============<[[ Setting Nightlight $vendo ]]>=============";
/tool fetch http-method=post http-header-field="X-TOKEN: $apiKey" \
url="http://$vendo/admin/api/toggerNightLight?toggle=$state"
} on-error={
:log error "============<[[ ERROR! $vendo OFFLINE... ]]>=============";
}
}
} -
Schedule daytime activation (turn on nightlights at morning):
/system scheduler add name="nightlight-on-schedule" on-event="vendo-nightlight-on" \
start-time=06:00:00 interval=1d comment="Turn on vendo nightlights at 6 AM" -
Verify scripts were created:
/system script printShould show
vendo-nightlight-controlandvendo-nightlight-on.
Option B: WebFig Configuration
-
Navigate to System > Scripts:
- Click + to add new script
- Name:
vendo-nightlight-control - Paste the script from Option A step 2
- Click OK
-
Add second script for day mode:
- Click + again
- Name:
vendo-nightlight-on - Change
state 0tostate 1in source - Click OK
-
Schedule nighttime script:
- Navigate to System > Scheduler
- Click +
- Name:
nightlight-off-schedule - On Event:
vendo-nightlight-control - Start Time:
20:00:00 - Interval:
1d - Click OK
-
Schedule daytime script:
- Click + again
- Name:
nightlight-on-schedule - On Event:
vendo-nightlight-on - Start Time:
06:00:00 - Interval:
1d - Click OK
Understanding the Script
Array of Vendos
:local vendos {"10.1.1.2"; "10.1.2.2"; "10.1.3.2";};
Syntax breakdown:
{...}- Array/list container"10.1.1.2"- First vendo IP;- Separator between array elements- Final
;after last element required
To add more vendos:
:local vendos {"10.1.1.2"; "10.1.2.2"; "10.1.3.2"; "10.1.4.2"; "10.1.5.2";};
HTTP Request Components
| Component | Purpose |
|---|---|
http-method=post | Use HTTP POST (not GET) |
http-header-field="X-TOKEN: $apiKey" | Authentication header |
url="http://$vendo/..." | Target URL with vendo IP |
toggle=$state | Query parameter (0=off, 1=on) |
/tool fetch | RouterOS HTTP client command |
Error Handling
/do {
# Try to execute command
} on-error={
# If error occurs (vendo offline, wrong API key, etc.)
:log error "ERROR! $vendo OFFLINE..."
}
Gracefully handles:
- Vendo offline/unreachable
- Timeout (vendo slow to respond)
- API key invalid
- Wrong endpoint
Verification
-
Verify scripts exist:
/system script printShould show both
vendo-nightlight-controlandvendo-nightlight-on. -
Test script manually (turn off nightlights):
/system script run vendo-nightlight-control -
Check logs for execution:
/log print where topics~"system"Should show entries like:
"============<[[ Setting Nightlight 10.1.1.2 ]]>============="
"============<[[ ERROR! 10.1.1.2 OFFLINE... ]]>=============" -
Test with vendo online:
- Ensure at least one vendo is powered on
- Run:
/system script run vendo-nightlight-control - Check physical vendo: nightlight should turn off
- Check logs: Should see success message (no error)
-
Verify scheduler is active:
/system scheduler printShould show both nightlight schedules enabled.
-
Monitor scheduled execution:
- Wait for scheduled time (or manually run for testing)
- Check logs:
/log print - Physical vendos should respond
-
Test API authentication:
# From any device with HTTP client
curl -H "X-TOKEN: pqa92jwxrk" \
"http://10.1.1.2/admin/api/toggerNightLight?toggle=0"Should return success JSON (consult vendo API docs for response format).
Troubleshooting
| Issue | Cause | Solution |
|---|---|---|
| All vendos show OFFLINE error | Network unreachable or wrong IPs | Verify IPs with /ip arp print; ping vendos: ping 10.1.1.2 |
| Script runs but no effect | API key invalid or endpoint wrong | Test with curl (see verification step 7); check vendo firmware version |
| "Unknown identifier" error | Variable name or syntax error | Verify: array syntax {...}, string quotes "...", semicolons exact |
| Scheduler doesn't run at scheduled time | Time not synchronized or scheduler disabled | Check: /system clock print; verify scheduler is disabled=no |
| Timeout waiting for vendo response | Vendo slow or network congestion | Increase timeout: add http-timeout=5s to fetch command |
| Some vendos work, some offline | Specific vendo offline or firewall blocking | Check individual vendo: ping 10.1.X.X; verify no firewall rules |
| API key rejected | Typo in API key or key expired | Verify exact key from vendo provider; contact support for key rotation |
| Script loops forever | Infinite loop or missing exit condition | Verify array syntax; check on-error is catching exceptions |
Advanced Options
Add more vendos dynamically:
:local vendos {"10.1.1.2"; "10.1.2.2"; "10.1.3.2"; "10.1.4.2"; "10.1.5.2"; "10.1.6.2";};
Stagger requests to avoid overload:
:foreach vendo in=$vendos do={
:delay 500ms; # Wait 500ms between requests
/do {
:log warning "Setting Nightlight $vendo";
/tool fetch http-method=post http-header-field="X-TOKEN: $apiKey" \
url="http://$vendo/admin/api/toggerNightLight?toggle=$state"
} on-error={
:log error "ERROR! $vendo OFFLINE...";
}
}
Toggle based on sunset/sunrise time:
:local currentHour [/system clock get hour];
:local state 0;
# Turn off nightlights between 8 PM and 6 AM
:if ($currentHour >= 20 || $currentHour < 6) do={
:set state 0; # OFF at night
} else={
:set state 1; # ON during day
}
# Then loop through vendos...
:foreach vendo in=$vendos do={ ... }
Retry failed vendos (add resilience):
:local vendos {"10.1.1.2"; "10.1.2.2"; "10.1.3.2";};
:local apiKey "pqa92jwxrk";
:local state 0;
:local retries 3;
:foreach vendo in=$vendos do={
:local success 0;
:for attempt from=1 to=$retries do={
:if ($success = 0) do={
/do {
/tool fetch http-method=post http-header-field="X-TOKEN: $apiKey" \
url="http://$vendo/admin/api/toggerNightLight?toggle=$state"
:set success 1;
:log warning "Vendo $vendo - Success on attempt $attempt";
} on-error={
:log error "Vendo $vendo - Attempt $attempt failed";
:delay 1s;
}
}
}
}
Send email notification on errors:
:local vendos {"10.1.1.2"; "10.1.2.2"; "10.1.3.2";};
:local apiKey "pqa92jwxrk";
:local state 0;
:local failedVendos "";
:foreach vendo in=$vendos do={
/do {
/tool fetch http-method=post http-header-field="X-TOKEN: $apiKey" \
url="http://$vendo/admin/api/toggerNightLight?toggle=$state"
} on-error={
:set failedVendos "$failedVendos $vendo,";
}
}
# If any failed, send email alert
:if ($failedVendos != "") do={
/tool e-mail send to="admin@example.com" subject="Vendo Nightlight Failed: $failedVendos" \
body="The following vendos did not respond: $failedVendos"
}
Log all transactions to file for audit:
:local vendos {"10.1.1.2"; "10.1.2.2"; "10.1.3.2";};
:local apiKey "pqa92jwxrk";
:local state 0;
:local timestamp [/system clock get date];
/file print file="nightlight-log" >> "$timestamp: Starting nightlight control\n";
:foreach vendo in=$vendos do={
/do {
/tool fetch http-method=post http-header-field="X-TOKEN: $apiKey" \
url="http://$vendo/admin/api/toggerNightLight?toggle=$state"
/file print file="nightlight-log" >> "$timestamp: $vendo - SUCCESS\n";
} on-error={
/file print file="nightlight-log" >> "$timestamp: $vendo - FAILED\n";
}
}
Monitor and alert via Telegram (see NetWatch Telegram guide):
:local failCount 0;
:foreach vendo in=$vendos do={
/do {
/tool fetch http-method=post http-header-field="X-TOKEN: $apiKey" \
url="http://$vendo/admin/api/toggerNightLight?toggle=$state"
} on-error={
:set failCount ($failCount + 1);
}
}
:if ($failCount > 0) do={
:local botApi "123456789:ABCDefGHIJKlmNOpqrsTUvwxYZ";
:local chatId "-123456789";
:local message "🚨 $failCount vendos offline during nightlight control";
/tool fetch url="https://api.telegram.org/bot$botApi/sendMessage?chat_id=$chatId&text=$message" keep-result=no;
}
Integration with JuanFi System
Combine with time-of-use (TOU) billing:
- Nightlights OFF during high-rate hours (peak pricing)
- Nightlights ON during low-rate hours (save power cost)
Coordinate with customer notification:
# Before turning off nightlights, send SMS/popup to customers
:log info "Announcing nightlight change to customers...";
# Call customer notification API here
:delay 5s;
# Then execute nightlight toggle
/system script run vendo-nightlight-control
Monitor vendo energy usage:
Use nightlight state as part of overall power consumption tracking.
JuanFi System Best Practices
-
Keep API keys secure:
- Store in
/system script environmentif possible - Never commit API keys to version control
- Rotate keys periodically
- Store in
-
Test before deploying:
- Run script on one vendo first
- Verify nightlight actually changes state
- Check for unintended side effects
-
Schedule during low-usage times:
- Avoid peak business hours
- Batch all API calls together (not spread throughout day)
-
Monitor for failures:
- Review logs daily:
/log print | grep -i nightlight - Set up alerts for repeated failures
- Document failed vendos for maintenance
- Review logs daily:
-
Document your settings:
- Record which vendo IPs are in the array
- Keep API key secure but documented for emergency access
- Note scheduled times for on/off cycles
Related Configurations
- Telegram alerts: See NetWatch Telegram Alerts
- Email notifications: See Send Logs to Email
- Scheduling tasks: See Guest Bandwidth DHCP On-Up
- Vendo management: Consult JuanFi System documentation
Completion
✅ Vendo nightlight control is now automated!
Next steps:
- Test script with one vendo manually
- Set up two schedules (morning on, evening off)
- Monitor logs for 7 days to verify reliability
- Adjust timing based on actual business hours
- Add email alerts for failed vendos
- Back up configuration:
/system backup save - Document nightlight schedule for staff
- Consider adding seasonal adjustments (longer days in summer)