Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 12291d46ce | |||
| c846467feb | |||
| 8785c81248 |
106
ESP32-pair/ben_cuelight.py
Normal file
106
ESP32-pair/ben_cuelight.py
Normal file
@@ -0,0 +1,106 @@
|
||||
# upload with `mpremote ben_cuelight.py :main.py` when connected with serial
|
||||
|
||||
import espnow
|
||||
from machine import Pin, Timer
|
||||
import network
|
||||
import time
|
||||
import ubinascii
|
||||
|
||||
# io0 = Pin(0, Pin.IN)
|
||||
led = Pin(23, Pin.OUT)
|
||||
led.value(1)
|
||||
# turn off after 5s
|
||||
Timer(0).init(mode=Timer.ONE_SHOT, period=5000, callback=lambda _: led.value(0))
|
||||
|
||||
print("Ben Cuelight Starting...")
|
||||
|
||||
# ESPNow https://docs.micropython.org/en/latest/library/espnow.html
|
||||
sta = network.WLAN(network.WLAN.IF_STA)
|
||||
sta.active(True)
|
||||
|
||||
def nice_mac(bys): return ubinascii.hexlify(bys, ":").decode()
|
||||
|
||||
my_mac = sta.config("mac")
|
||||
|
||||
MOS_X4_MAC = b"\x00\x70\x07\x7C\xDB\xC8"
|
||||
ETH_01_MAC = b"\x14\x08\x08\x9E\xA1\x94"
|
||||
|
||||
print("MAC Address", nice_mac(my_mac))
|
||||
|
||||
e = espnow.ESPNow()
|
||||
e.active(True)
|
||||
print("ESPNow active")
|
||||
|
||||
|
||||
def wait_change(pin):
|
||||
# wait for pin to change value
|
||||
# it needs to be stable for a continuous 20ms
|
||||
cur_value = pin.value()
|
||||
active = 0
|
||||
while active < 20:
|
||||
if pin.value() != cur_value:
|
||||
active += 1
|
||||
else:
|
||||
active = 0
|
||||
time.sleep(0.001)
|
||||
|
||||
|
||||
def mos_x4():
|
||||
print("I am: MOS_X4")
|
||||
|
||||
mos1 = Pin(16, Pin.OUT) # flash
|
||||
mos2 = Pin(17, Pin.OUT)
|
||||
mos3 = Pin(26, Pin.OUT)
|
||||
mos4 = Pin(27, Pin.OUT) # mute-state
|
||||
|
||||
flash = mos1
|
||||
mute = mos4
|
||||
|
||||
mute.value(0)
|
||||
flash.value(0)
|
||||
|
||||
timer = Timer(1)
|
||||
|
||||
while True:
|
||||
host, msg = e.recv()
|
||||
if msg and host == ETH_01_MAC:
|
||||
if msg[0] == 1:
|
||||
# on
|
||||
print("on")
|
||||
mute.value(1)
|
||||
timer.init(mode=Timer.PERIODIC, period=1000, callback=lambda _: flash.toggle())
|
||||
elif msg[0] == 0:
|
||||
# off
|
||||
print("off")
|
||||
mute.value(0)
|
||||
flash.value(0)
|
||||
timer.deinit()
|
||||
else:
|
||||
# ????
|
||||
print("??", msg, msg[0])
|
||||
...
|
||||
|
||||
def eth_01():
|
||||
print("I am: ETH_01")
|
||||
|
||||
peer = MOS_X4_MAC
|
||||
e.add_peer(peer)
|
||||
|
||||
button = Pin(12, Pin.IN, Pin.PULL_UP)
|
||||
|
||||
remote_state_on = False
|
||||
|
||||
while True:
|
||||
wait_change(button)
|
||||
if button.value() == 0:
|
||||
remote_state_on = not remote_state_on
|
||||
print("Setting remote to:", "on" if remote_state_on else "off")
|
||||
e.send(peer, bytes([1 if remote_state_on else 0]))
|
||||
|
||||
|
||||
if my_mac == MOS_X4_MAC:
|
||||
mos_x4()
|
||||
elif my_mac == ETH_01_MAC:
|
||||
eth_01()
|
||||
else:
|
||||
print("Unknown device!!!", my_mac)
|
||||
@@ -1,39 +0,0 @@
|
||||
const char Data_PressVirtKey1OnBench_P[] PROGMEM = "<?xml version=\"1.0\"?>\
|
||||
<methodCall>\
|
||||
<methodName>PressKeyEx</methodName>\
|
||||
<params>\
|
||||
<param><value><string>C0000000001</string></value></param>\
|
||||
<param><value><i4>64</i4></value></param>\
|
||||
<param><value><i4>46</i4></value></param>\
|
||||
<param><value><boolean>0</boolean></value></param>\
|
||||
<param><value><int>2</int></value></param>\
|
||||
<param><value><int>0</int></value></param>\
|
||||
<param><value><int>1</int></value></param>\
|
||||
<param><value><boolean>1</boolean></value></param>\
|
||||
<param><value><boolean>1</boolean></value></param>\
|
||||
<param><value><int>1</int></value></param>\
|
||||
</params>\
|
||||
</methodCall>\
|
||||
";
|
||||
|
||||
const char Data_ReleaseVirtKey1OnBench_P[] PROGMEM = "<?xml version=\"1.0\"?>\
|
||||
<methodCall>\
|
||||
<methodName>PressKeyEx</methodName>\
|
||||
<params>\
|
||||
<param><value><string>C0000000002</string></value></param>\
|
||||
<param><value><i4>64</i4></value></param>\
|
||||
<param><value><i4>46</i4></value></param>\
|
||||
<param><value><boolean>0</boolean></value></param>\
|
||||
<param><value><int>2</int></value></param>\
|
||||
<param><value><int>0</int></value></param>\
|
||||
<param><value><int>1</int></value></param>\
|
||||
<param><value><boolean>1</boolean></value></param>\
|
||||
<param><value><boolean>0</boolean></value></param>\
|
||||
<param><value><int>1</int></value></param>\
|
||||
</params>\
|
||||
</methodCall>\
|
||||
";
|
||||
|
||||
__FlashStringHelper* Data_PressVirtKey1OnBench = (__FlashStringHelper*)Data_PressVirtKey1OnBench_P;
|
||||
__FlashStringHelper* Data_ReleaseVirtKey1OnBench = (__FlashStringHelper*)Data_ReleaseVirtKey1OnBench_P;
|
||||
|
||||
@@ -1,210 +0,0 @@
|
||||
/*
|
||||
DHCP-based IP printer
|
||||
|
||||
This sketch uses the DHCP extensions to the Ethernet library
|
||||
to get an IP address via DHCP and print the address obtained.
|
||||
using an Arduino WIZnet Ethernet shield.
|
||||
|
||||
Circuit:
|
||||
Ethernet shield attached to pins 10, 11, 12, 13
|
||||
|
||||
created 12 April 2011
|
||||
modified 9 Apr 2012
|
||||
by Tom Igoe
|
||||
modified 02 Sept 2015
|
||||
by Arturo Guadalupi
|
||||
|
||||
*/
|
||||
|
||||
#include <SPI.h>
|
||||
#include <Ethernet.h>
|
||||
#include "RRCS.h"
|
||||
|
||||
// Enter a MAC address for your controller below.
|
||||
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
|
||||
byte mac[] = {
|
||||
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
|
||||
};
|
||||
|
||||
constexpr int PIN_BTN_MUTE = A2;
|
||||
constexpr int PIN_LED_RED = A0;
|
||||
|
||||
//const uint8_t RRCSServerIP[4] = {10,75,120,147}; // TRUNKNAV VM
|
||||
//const uint8_t RRCSServerIP[4] = {192,168,192,247}; // Ben's laptop testing
|
||||
const uint8_t RRCSServerIP[4] = {192,168,192,241}; // Ben's windows VM
|
||||
const uint16_t RRCSServerPort = 8193;
|
||||
const uint16_t BlinkMs = 1000;
|
||||
|
||||
EthernetClient client;
|
||||
|
||||
void ToggleVirtualKey() {
|
||||
if (client.connect(RRCSServerIP, RRCSServerPort)) {
|
||||
Serial.println("Connected.");
|
||||
|
||||
client.println("POST / HTTP/1.1");
|
||||
client.print("Host: ");
|
||||
client.print(RRCSServerIP[0]); client.print(".");
|
||||
client.print(RRCSServerIP[1]); client.print(".");
|
||||
client.print(RRCSServerIP[2]); client.print(".");
|
||||
client.println(RRCSServerIP[3]);
|
||||
client.println("Content-Type: text/xml"); // Or other content type like application/json
|
||||
client.println("User-Agent: Ben's Silly Box");
|
||||
client.println("Connection: close");
|
||||
client.print("Content-Length: ");
|
||||
client.println(strlen(Data_PressVirtKey1OnBench_P));
|
||||
client.println();
|
||||
client.println(Data_PressVirtKey1OnBench);
|
||||
|
||||
client.println(); // End of request
|
||||
|
||||
while(client.available()) {
|
||||
Serial.write((char)client.read());
|
||||
}
|
||||
delay(50);
|
||||
client.stop();
|
||||
Serial.println();
|
||||
client.connect(RRCSServerIP, RRCSServerPort);
|
||||
//delay(20);
|
||||
|
||||
client.println("POST / HTTP/1.1");
|
||||
client.print("Host: ");
|
||||
client.print(RRCSServerIP[0]); client.print(".");
|
||||
client.print(RRCSServerIP[1]); client.print(".");
|
||||
client.print(RRCSServerIP[2]); client.print(".");
|
||||
client.println(RRCSServerIP[3]);
|
||||
client.println("Content-Type: text/xml"); // Or other content type like application/json
|
||||
client.println("User-Agent: Ben's Silly Box");
|
||||
client.println("Connection: close");
|
||||
client.print("Content-Length: ");
|
||||
client.println(strlen(Data_ReleaseVirtKey1OnBench_P));
|
||||
client.println();
|
||||
client.println(Data_ReleaseVirtKey1OnBench);
|
||||
|
||||
client.println(); // End of request
|
||||
|
||||
while(client.available()) {
|
||||
Serial.write((char)client.read());
|
||||
}
|
||||
client.stop();
|
||||
Serial.println();
|
||||
|
||||
Serial.println("Sent.");
|
||||
} else {
|
||||
Serial.println("Couldn't reach RRCS Server!");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
bool MuteState = false;
|
||||
bool EthNoLink = true;
|
||||
|
||||
void setup() {
|
||||
|
||||
pinMode (PIN_BTN_MUTE, INPUT_PULLUP);
|
||||
// Blink once on startup
|
||||
pinMode(PIN_LED_RED, OUTPUT);
|
||||
digitalWrite(PIN_LED_RED, HIGH);
|
||||
delay(100);
|
||||
digitalWrite(PIN_LED_RED, LOW);
|
||||
|
||||
// You can use Ethernet.init(pin) to configure the CS pin
|
||||
Ethernet.init(10); // Most Arduino shields
|
||||
//Ethernet.init(5); // MKR ETH Shield
|
||||
//Ethernet.init(0); // Teensy 2.0
|
||||
//Ethernet.init(20); // Teensy++ 2.0
|
||||
//Ethernet.init(15); // ESP8266 with Adafruit FeatherWing Ethernet
|
||||
//Ethernet.init(33); // ESP32 with Adafruit FeatherWing Ethernet
|
||||
|
||||
// Open serial communications and wait for port to open:
|
||||
Serial.begin(115200);
|
||||
while (!Serial) {
|
||||
; // wait for serial port to connect. Needed for native USB port only
|
||||
}
|
||||
|
||||
// start the Ethernet connection:
|
||||
Serial.println("Initialize Ethernet with DHCP:");
|
||||
if (Ethernet.begin(mac) == 0) {
|
||||
Serial.println("Failed to configure Ethernet using DHCP");
|
||||
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
|
||||
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
|
||||
} else if (Ethernet.linkStatus() == LinkOFF) {
|
||||
Serial.println("Ethernet cable is not connected.");
|
||||
}
|
||||
Serial.println("Failed Eth setup. Retrying later.");
|
||||
} else {
|
||||
EthNoLink = false;
|
||||
}
|
||||
// print your local IP address:
|
||||
Serial.print("My IP address: ");
|
||||
Serial.println(Ethernet.localIP());
|
||||
}
|
||||
|
||||
void loop() {
|
||||
digitalWrite(PIN_LED_RED, MuteState && ((millis() / BlinkMs) % 2));
|
||||
|
||||
|
||||
if (digitalRead(PIN_BTN_MUTE) == LOW) {
|
||||
// Mute action.
|
||||
MuteState = !MuteState;
|
||||
|
||||
Serial.println(MuteState ? "Muted." : "Unmuted.");
|
||||
|
||||
// Lazy debounce is best debounce
|
||||
delay(50);
|
||||
while (digitalRead(PIN_BTN_MUTE) == LOW) delay(100);
|
||||
|
||||
ToggleVirtualKey();
|
||||
}
|
||||
|
||||
if (EthNoLink) {
|
||||
Serial.println("Retrying Eth Setup...");
|
||||
if (Ethernet.begin(mac) == 0) {
|
||||
Serial.println("Failed to configure Ethernet using DHCP");
|
||||
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
|
||||
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
|
||||
} else if (Ethernet.linkStatus() == LinkOFF) {
|
||||
Serial.println("Ethernet cable is not connected.");
|
||||
}
|
||||
Serial.println("Failed Eth Setup.");
|
||||
} else {
|
||||
EthNoLink = false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
switch (Ethernet.maintain()) {
|
||||
case 1:
|
||||
//renewed fail
|
||||
Serial.println("Error: renewed fail");
|
||||
break;
|
||||
|
||||
case 2:
|
||||
//renewed success
|
||||
Serial.println("Renewed success");
|
||||
//print your local IP address:
|
||||
Serial.print("My IP address: ");
|
||||
Serial.println(Ethernet.localIP());
|
||||
digitalWrite(PIN_LED_RED, HIGH);
|
||||
break;
|
||||
|
||||
case 3:
|
||||
//rebind fail
|
||||
Serial.println("Error: rebind fail");
|
||||
break;
|
||||
|
||||
case 4:
|
||||
//rebind success
|
||||
Serial.println("Rebind success");
|
||||
//print your local IP address:
|
||||
Serial.print("My IP address: ");
|
||||
Serial.println(Ethernet.localIP());
|
||||
break;
|
||||
|
||||
default:
|
||||
//nothing happened
|
||||
break;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user