211 lines
5.8 KiB
C++
Executable File
211 lines
5.8 KiB
C++
Executable File
/*
|
|
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;
|
|
}
|
|
}
|