Такой же простой и короткий надо писать, возьму то что под рукой -- это веб-сервер с поддержкой нескольких команд. По коду в принципе можно разобраться как оно устроено и под себя переделать.
Код: Выделить всё
// Universal Arduino controller for MajorDoMo (http://smartliving.ru)
// v 0.1
#include <Ethernet.h>
#include <SPI.h>
#include <VirtualWire.h>
#include <EasyTransferVirtualWire.h>
#include <EEPROM.h> //Needed to access the eeprom read write functions
#define MAX_COMMAND_LEN (10)
#define MAX_PARAMETER_LEN (30)
#define COMMAND_TABLE_SIZE (10)
#define TO_UPPER(x) (((x >= 'a') && (x <= 'z')) ? ((x) - ('a' - 'A')) : (x))
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0xDA, 0x7D, 0x5E, 0xDF, 0xCE, 0xED };
//byte ip[] = { 192,168,0,75 };
IPAddress ip(192,168,0,75);
//byte pingAddr [] = {192, 168, 0, 1};
//SOCKET pingSocket = 0;
String url = "";
int maxLength=25;
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
EthernetClient webClient;
#define PIN_LED A3
#define PIN_LED_T A3
#define PIN_RF A0
#define RF_SEND_COUNTER 3
int webRequest = false; // is command requested by HTTP?
int startPin=2;
int endPin=9; // 10,11,12,13 used by ethernet shield,
//create object
EasyTransferVirtualWire ET;
struct SEND_DATA_STRUCTURE{
//put your variable definitions here for the data you want to send
//THIS MUST BE EXACTLY THE SAME ON THE OTHER ARDUINO
//Struct can'e be bigger then 26 bytes for VirtualWire version
unsigned int device_id;
unsigned int destination_id;
unsigned int packet_id;
byte command;
int data;
};
unsigned int unique_device_id = 0;
//give a name to the group of data
SEND_DATA_STRUCTURE mydata;
char gCommandBuffer[MAX_COMMAND_LEN + 1];
char gParamBuffer[MAX_PARAMETER_LEN + 1];
String commandReceived;
String parametersReceived;
long gParamValue;
typedef struct {
char const *name;
void (*function)(void);
} command_t;
command_t const gCommandTable[COMMAND_TABLE_SIZE] = {
{"HELP", commandsHelp,},
{"LED", commandsLed, },
{"ON", commandsOn, },
{"OFF", commandsOff, },
{"STATE", commandsState, },
{"CLICK", commandsClick, },
{"LCLICK", commandsLClick, },
{"RCLICK", commandsRClick, },
{"RF", commandsRF, },
{NULL, NULL }
};
//This function will write a 2 byte integer to the eeprom at the specified address and address + 1
void EEPROMWriteInt(int p_address, unsigned int p_value)
{
byte lowByte = ((p_value >> 0) & 0xFF);
byte highByte = ((p_value >> 8) & 0xFF);
EEPROM.write(p_address, lowByte);
EEPROM.write(p_address + 1, highByte);
}
//This function will read a 2 byte integer from the eeprom at the specified address and address + 1
unsigned int EEPROMReadInt(int p_address)
{
byte lowByte = EEPROM.read(p_address);
byte highByte = EEPROM.read(p_address + 1);
return ((lowByte << 0) & 0xFF) + ((highByte << 8) & 0xFF00);
}
void blinking(int count) {
for(int i=0;i<count;i++) {
digitalWrite(PIN_LED, HIGH);
delay(200);
digitalWrite(PIN_LED, LOW);
delay(200);
}
}
void sendRFData() {
Serial.print("Transmitting packets ... ");
for(int i=0;i<RF_SEND_COUNTER;i++) {
if (i>0) {
delay(200);
}
digitalWrite(PIN_LED_T, HIGH);
ET.sendData();
digitalWrite(PIN_LED_T, LOW);
}
Serial.println("DONE");
}
void printLine(const char line[]) {
if (webRequest) {
if (webClient) {
if (webClient.connected()) {
webClient.println(line);
webClient.println("<br/>");
}
}
} else {
Serial.println(line);
}
}
int cliBuildCommand(char nextChar) {
static uint8_t idx = 0; //index for command buffer
static uint8_t idx2 = 0; //index for parameter buffer
enum { COMMAND, PARAM };
static uint8_t state = COMMAND;
if ((nextChar == '\n') || (nextChar == ' ') || (nextChar == '\t') || (nextChar == '\r'))
return false;
//if (nextChar == '\r') {
if (nextChar == ';') {
gCommandBuffer[idx] = '\0';
gParamBuffer[idx2] = '\0';
idx = 0;
idx2 = 0;
state = COMMAND;
return true;
}
if (nextChar == ',') {
state = PARAM;
return false;
}
if (state == COMMAND) {
gCommandBuffer[idx] = TO_UPPER(nextChar);
idx++;
if (idx > MAX_COMMAND_LEN) {
idx = 0;
return true;
}
}
if (state == PARAM) {
gParamBuffer[idx2] = nextChar;
idx2++;
if (idx > MAX_PARAMETER_LEN) {
idx2 = 0;
return true;
}
}
return false;
}
void cliProcessCommand(void)
{
int bCommandFound = false;
int idx;
gParamValue = strtol(gParamBuffer, NULL, 0);
for (idx = 0; gCommandTable[idx].name != NULL; idx++) {
if (strcmp(gCommandTable[idx].name, gCommandBuffer) == 0) {
bCommandFound = true;
break;
}
}
if (bCommandFound == true) {
if (!webRequest) {
printLine("");
}
(*gCommandTable[idx].function)();
}
else {
if (!webRequest) {
printLine("");
}
printLine("Command not found:");
printLine(gCommandBuffer);
}
}
void commandsLed(void) {
printLine("LED command received.");
if (gParamValue >=0 && gParamValue <= 255) {
analogWrite(PIN_LED, gParamValue);
}
else {
printLine("wrong parameter value");
}
}
void commandsOn(void) {
printLine("ON command received.");
if (gParamValue>=startPin && gParamValue<=endPin) {
printLine("Turning ON");
digitalWrite(gParamValue,LOW);
} else {
printLine("Incorrect PIN");
}
}
void commandsOff(void) {
printLine("OFF command received.");
if (gParamValue>=startPin && gParamValue<=endPin) {
printLine("Turning OFF");
digitalWrite(gParamValue,HIGH);
} else {
printLine("Incorrect PIN");
}
}
void commandsClick(void) {
printLine("CLICK command received.");
if (gParamValue>=startPin && gParamValue<=endPin) {
printLine("Clicking");
digitalWrite(gParamValue,LOW);
delay(1000);
digitalWrite(gParamValue,HIGH);
} else {
printLine("Incorrect PIN");
}
}
void commandsLClick(void) {
printLine("LCLICK command received.");
digitalWrite(6,LOW);
if (gParamValue==1) {
digitalWrite(9,LOW);
delay(2000);
digitalWrite(9,HIGH);
}
if (gParamValue==2) {
digitalWrite(8,LOW);
delay(2000);
digitalWrite(8,HIGH);
}
if (gParamValue==3) {
digitalWrite(7,LOW);
delay(2000);
digitalWrite(7,HIGH);
}
digitalWrite(6,HIGH);
}
void commandsRClick(void) {
printLine("RCLICK command received.");
digitalWrite(5,LOW);
if (gParamValue==1) {
digitalWrite(9,LOW);
delay(2000);
digitalWrite(9,HIGH);
}
if (gParamValue==2) {
digitalWrite(8,LOW);
delay(2000);
digitalWrite(8,HIGH);
}
if (gParamValue==3) {
digitalWrite(7,LOW);
delay(2000);
digitalWrite(7,HIGH);
}
digitalWrite(5,HIGH);
}
void commandsRF(void) {
printLine("RF command received");
printLine(gParamBuffer);
//gParamBuffer
//to-command-data
//parametersReceived
String paramString;
unsigned int destination_id=0;
byte command=0;
int data=0;
int splitPosition;
splitPosition=parametersReceived.indexOf('-');
if(splitPosition != -1) {
paramString=parametersReceived.substring(0,splitPosition);
destination_id=paramString.toInt();
parametersReceived=parametersReceived.substring(splitPosition+1,parametersReceived.length());
splitPosition=parametersReceived.indexOf('-');
if(splitPosition != -1) {
paramString=parametersReceived.substring(0,splitPosition);
command=paramString.toInt();
parametersReceived=parametersReceived.substring(splitPosition+1,parametersReceived.length());
data=parametersReceived.toInt();
}
}
mydata.packet_id = random(65535);
mydata.command = command;
mydata.data = data;
mydata.destination_id = destination_id;
sendRFData();
//gparam
}
void commandsState(void) {
for(int i=startPin;i<=endPin;i++) {
int st=digitalRead(i);
char my_st[5];
itoa(st,my_st,10);
// printLine("PIN"+i);
printLine(my_st);
}
}
void commandsHelp(void) {
int idx;
for (idx = 0; gCommandTable[idx].name != NULL; idx++) {
printLine(gCommandTable[idx].name);
}
}
void setup() {
pinMode(PIN_LED_T, OUTPUT);
pinMode(PIN_LED, OUTPUT); // sets the pin as output
for (int thisPin = startPin; thisPin <=endPin; thisPin++) {
pinMode(thisPin, OUTPUT);
digitalWrite(thisPin,HIGH);
}
delay(200);
Serial.begin(9600);
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
Serial.println("Arduino command processing example");
Serial.print('>');
ET.begin(details(mydata));
vw_set_tx_pin(PIN_RF);
vw_set_rx_pin(A1);
vw_set_ptt_pin(A2);
//vw_set_ptt_inverted(true);
vw_setup(2000); // Bits per sec
randomSeed(analogRead(0));
// Device ID
Serial.print("Getting Device ID... ");
unique_device_id=EEPROMReadInt(0);
//unique_device_id=0;
if (unique_device_id<10000 || unique_device_id>60000) {
Serial.print("N/A, updating... ");
unique_device_id=random(10000, 60000);
EEPROMWriteInt(0, unique_device_id);
}
Serial.println(unique_device_id);
mydata.device_id = unique_device_id;
mydata.destination_id = 0;
}
void loop() {
char rcvChar;
int bCommandReady = false;
if (Serial.available() > 0) {
rcvChar = Serial.read();
Serial.print(rcvChar);
bCommandReady = cliBuildCommand(rcvChar);
}
if (bCommandReady == true) {
webRequest = false;
bCommandReady = false;
cliProcessCommand();
Serial.print('>');
}
// web server
// listen for incoming clients
EthernetClient client = server.available();
webClient = client;
//webClient = server.available();
if (client) {
Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
//Serial.write(c);
if (url.length() < maxLength) {
url+=(c);
}
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
Serial.print("HTTP request: ");
Serial.println(url);
if (url.indexOf("?")>=0) {
int PosB=url.indexOf("?")+1;
int PosE=url.indexOf("HTTP");
if (url.indexOf(",")>=0) {
// command has parameters
int PosP=url.indexOf(",");
commandReceived=url.substring(PosB,PosP);
parametersReceived=url.substring(PosP+1,PosE-1);
} else {
// command does not have parameters
commandReceived=url.substring(PosB,PosE-1);
parametersReceived="";
}
//Serial.print("Command: ");
//Serial.println(commandReceived);
//Serial.print("Parameter: ");
//Serial.println(parametersReceived);
commandReceived.toUpperCase();
parametersReceived.toUpperCase();
//strcpy (gCommandBuffer, commandReceived.toCharArray());
commandReceived.toCharArray(gCommandBuffer,commandReceived.length()+1);
if (parametersReceived.length()>0) {
//strcpy (gParamBuffer, parametersReceived.toCharArray());
parametersReceived.toCharArray(gParamBuffer,parametersReceived.length()+1);
}
} else {
//no command defined, showing help string
strcpy (gCommandBuffer, "HELP\0");
}
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html><head><title>Arduino</title></head><body>");
webRequest = true;
bCommandReady = false;
cliProcessCommand();
client.println("</body><html>");
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
url = "";
client.stop();
Serial.println("client disonnected");
}
}