Mega code:
Code: Select all
// constants won't change. They're used here to set pin numbers:
const int button1Pin = 22; // the number of the pushbutton pin
const int button2Pin = 24; // the number of the pushbutton pin
const int button3Pin = 26; // the number of the pushbutton pin
const int button4Pin = 28; // the number of the pushbutton pin
const int button5Pin = 30; // the number of the pushbutton pin
const int button6Pin = 32; // the number of the pushbutton pin
const int led1RPin = 2; // the number of the LED pin
const int led2GPin = 3; // the number of the LED pin
const int led3BPin = 5; // the number of the LED pin
const int led4RPin = 6; // the number of the LED pin
const int led4GPin = 7; // the number of the LED pin
const int led5RPin = 8; // the number of the LED pin
const int led5BPin = 9; // the number of the LED pin
const int led5GPin = 44; // the number of the LED pin
const int led6BPin = 45; // the number of the LED pin
const int led6RPin = 46; // the number of the LED pin
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network.
// gateway and subnet are optional:
byte mac[] = {
0xA8, 0x61, 0x0A, 0xAE, 0x64, 0x52 };
IPAddress ip(10, 200, 1, 199);
IPAddress myDns(10, 100, 10, 10);
IPAddress gateway(10, 200, 1, 1);
IPAddress subnet(255, 255, 255, 0);
// telnet defaults to port 23
EthernetServer server(1234);
bool alreadyConnected = false; // whether or not the client was connected previously
String commandStr;
// variables will change:
int button1State = 0; // variable for reading the pushbutton status
int button2State = 0; // variable for reading the pushbutton status
int button3State = 0; // variable for reading the pushbutton status
int button4State = 0; // variable for reading the pushbutton status
int button5State = 0; // variable for reading the pushbutton status
int button6State = 0; // variable for reading the pushbutton status
int lastbutton1State = 0;
int lastbutton2State = 0;
int lastbutton3State = 0;
int lastbutton4State = 0;
int lastbutton5State = 0;
int lastbutton6State = 0;
void setup() {
// initialize the LED pin as an output:
pinMode(led1RPin, OUTPUT);
pinMode(led2GPin, OUTPUT);
pinMode(led3BPin, OUTPUT);
pinMode(led4RPin, OUTPUT);
pinMode(led4GPin, OUTPUT);
pinMode(led5RPin, OUTPUT);
pinMode(led5GPin, OUTPUT);
pinMode(led5BPin, OUTPUT);
pinMode(led6RPin, OUTPUT);
pinMode(led6BPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(button1Pin, INPUT);
pinMode(button2Pin, INPUT);
pinMode(button3Pin, INPUT);
pinMode(button4Pin, INPUT);
pinMode(button5Pin, INPUT);
pinMode(button6Pin, INPUT);
// 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
// initialize the ethernet device
Ethernet.begin(mac, ip, myDns, gateway, subnet);
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
}
}
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
}
// start listening for clients
server.begin();
Serial.print("Server address:");
Serial.println(Ethernet.localIP());
}
void loop() {
ButtonState();
TCPRead();
}
void ButtonState(){
// read the state of the pushbutton value:
button1State = digitalRead(button1Pin);
button2State = digitalRead(button2Pin);
button3State = digitalRead(button3Pin);
button4State = digitalRead(button4Pin);
button5State = digitalRead(button5Pin);
button6State = digitalRead(button6Pin);
// wait for a new client:
EthernetClient client = server.available();
// when the client sends the first byte, say hello:
if (client) {
if (!alreadyConnected) {
// clear out the input buffer:
client.flush();
Serial.println("We have a new client");
client.println("Hello, client!");
alreadyConnected = true;
}
}
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (button1State != lastbutton1State) {
if (button1State == HIGH) {
// turn LED on:
//digitalWrite(led1Pin, HIGH);
server.write("Button1_High\r\n");
Serial.write("Button1_High\r\n");
} else {
// turn LED off:
//digitalWrite(led1Pin, LOW);
server.write("Button1_Low\r\n");
Serial.write("Button1_Low\r\n");
}
lastbutton1State = button1State;
}
if (button2State != lastbutton2State) {
if (button2State == HIGH) {
// turn LED on:
//digitalWrite(led2Pin, HIGH);
server.println("Button2_High\r\n");
Serial.write("Button2_High\r\n");
} else {
// turn LED off:
//digitalWrite(led2Pin, LOW);
server.println("Button2_Low\r\n");
Serial.write("Button2_Low\r\n");
}
lastbutton2State = button2State;
}
if (button3State != lastbutton3State) {
if (button3State == HIGH) {
// turn LED on:
//digitalWrite(led3Pin, HIGH);
server.println("Button3_High\r\n");
Serial.write("Button3_High\r\n");
} else {
// turn LED off:
//digitalWrite(led3Pin, LOW);
server.println("Button3_Low\r\n");
Serial.write("Button3_Low\r\n");
}
lastbutton3State = button3State;
}
if (button4State != lastbutton4State) {
if (button4State == HIGH) {
// turn LED on:
//digitalWrite(led4Pin, HIGH);
server.println("Button4_High\r\n");
Serial.write("Button4_High\r\n");
} else {
// turn LED off:
//digitalWrite(led4Pin, LOW);
server.println("Button4_Low\r\n");
Serial.write("Button4_Low\r\n");
}
lastbutton4State = button4State;
}
if (button5State != lastbutton5State) {
if (button5State == HIGH) {
// turn LED on:
//digitalWrite(led5Pin, HIGH);
server.println("Button5_High\r\n");
Serial.write("Button5_High\r\n");
} else {
// turn LED off:
//digitalWrite(led5Pin, LOW);
server.println("Button5_Low\r\n");
Serial.write("Button5_Low\r\n");
}
lastbutton5State = button5State;
}
if (button6State != lastbutton6State) {
if (button6State == HIGH) {
// turn LED on:
//digitalWrite(led6Pin, HIGH);
server.println("Button6_High\r\n");
Serial.write("Button6_High\r\n");
} else {
// turn LED off:
//digitalWrite(led6Pin, LOW);
server.println("Button6_Low\r\n");
Serial.write("Button6_Low\r\n");
}
lastbutton6State = button6State;
}
}
void TCPRead(){
EthernetClient client = server.available();
if (client.available()) {
// Read char until linefeed
char c = client.read();
if (c != '\n') {
// Add received char to string variable
commandStr += c;
//Serial.println("Command: " + commandStr);
} else {
// Print received command to serial monitor
Serial.println("Command: " + commandStr);
// Process the received command
processCommand(commandStr);
// Clear variable for receive next command
commandStr = "";
}
}
}
void processCommand(String cmd) {
if (cmd == "LED1_HIGH") {
// Turn on LED
analogWrite(led1RPin, 0);
} else if (cmd == "LED1_LOW") {
// Turn off LED
analogWrite(led1RPin, 230);
} else if (cmd == "LED2_HIGH") {
// Turn on LED
analogWrite(led2GPin, 0);
} else if (cmd == "LED2_LOW") {
// Turn off LED
analogWrite(led2GPin, 240);
} else if (cmd == "LED3_HIGH") {
// Turn on LED
analogWrite(led3BPin, 0);
} else if (cmd == "LED3_LOW") {
// Turn off LED
analogWrite(led3BPin, 230);
} else if (cmd == "LED4_HIGH") {
// Turn on LED
analogWrite(led4RPin, 0);
analogWrite(led4GPin, 0);
} else if (cmd == "LED4_LOW") {
// Turn off LED
analogWrite(led4RPin, 235);
analogWrite(led4GPin, 235);
} else if (cmd == "LED5_HIGH") {
// Turn on LED
analogWrite(led5RPin, 0);
analogWrite(led5GPin, 0);
analogWrite(led5BPin, 0);
} else if (cmd == "LED5_LOW") {
// Turn off LED
analogWrite(led5RPin, 230);
analogWrite(led5GPin, 230);
analogWrite(led5BPin, 230);
} else if (cmd == "LED6_HIGH") {
// Turn on LED
analogWrite(led6RPin, 0);
analogWrite(led6BPin, 0);
} else if (cmd == "LED6_LOW") {
// Turn off LED
analogWrite(led6RPin, 240);
analogWrite(led6BPin, 240);
}
// end of script
}