Адаптировал и скомпилировал Arduino Code 2 под Arduino 1.0.
В новой версии появилось много новых функций.(Библиотека Ethernet).
Хотелось бы посмотреть как автор управляет Arduinoй через сайт и проверить данный скетч.
PS. Библиотеки все новые, кроме того, потребовалась модификация WString.h иWString.cpp
Код: Выделить всё
// Universal Arduino client-server for MajorDoMo (http://smartliving.ru)
// v 0.2
//#include <Client.h>
//#include <BSeries.h>
#include <DallasTemperature.h>
//#include <NewOneWire.h>
#include <OneWire.h>
//#include <S20Series.h>
//#include <StratBase.h>
#include <Ethernet.h>
#include <SPI.h>
//#include <stdio.h>
//#include "WString.h"
#define ONE_WIRE_BUS 2
#define MAX_COMMAND_LEN (10)
#define MAX_PARAMETER_LEN (10)
#define COMMAND_TABLE_SIZE (4)
#define TO_UPPER(x) (((x >= 'a') && (x <= 'z')) ? ((x) - ('a' - 'A')) : (x))
OneWire oneWire(ONE_WIRE_BUS);
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0x00, 0x2A, 0xF6, 0x13, 0x68, 0xFC };
byte ip[] = { 192,168,0,72 };
byte rserver[] = {192,168,0,17};
char serverName[] = "www.google.com";
//IPAddress rserver;
//String commandReceived(10);
String parametersReceived(10);
String url = String(25);
int maxLength=25;
byte isdata=0;
char temp[6];
int old_temperature1=0;
int old_temperature2=0;
int old_garage=0;
// 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 = server.available();
// tammat
//
EthernetClient rclient = server.available();
int ledPin = 13;
int webRequest = false; // is command requested by HTTP?
DallasTemperature tempSensor(&oneWire);
//DallasTemperature tempSensor;
char gCommandBuffer[MAX_COMMAND_LEN + 1];
char gParamBuffer[MAX_PARAMETER_LEN + 1];
long gParamValue;
typedef struct {
char const *name;
void (*function)(void);
} command_t;
// COMMON FUNCTIONS BEGIN
void printLine(const char line[]) {
if (webRequest) {
if (webClient) {
if (webClient.connected()) {
webClient.println(line);
webClient.println("<br/>");
}
}
} else {
Serial.println(line);
}
}
void sendHTTPRequest(const char data[]) {
char buf[150];
sprintf(buf, "GET %s HTTP/1.0", data);
if (rclient.connect(rserver,80)) {
Serial.println(buf);
Serial.println("Sending data...");
rclient.println(buf);
rclient.println("Host: 192.168.0.17");
rclient.println();
delay(2000);
isdata=0;
Serial.println("Done.");
rclient.stop();
} else {
Serial.println("HTTP connection failed");
}
}
// COMMON FUNCTIONS END
// -------------------------------------------------------------------------
// COMMANDS BEGIN
command_t const gCommandTable[COMMAND_TABLE_SIZE] = {
{"LED", commandsLed, },
// {"TEMP", commandsTemp, },
{NULL, NULL }
};
void commandsLed(void) {
printLine("LED command received.");
sendHTTPRequest("/objects/?object=ThisComputer&op=m&m=StartUp&");
if (gParamValue >=0 && gParamValue <= 255) {
analogWrite(ledPin, gParamValue);
}
else {
printLine("wrong parameter value");
}
}
/*void commandsTemp(void) {
printLine("TEMP command received.");
switch(tempSensor.isValid())
{
case 1:
printLine("Invalid CRC");
tempSensor.reset();
return;
case 2:
printLine("Invalid device");
tempSensor.reset();
return;
}
}
/*
// COMMANDS END
// -------------------------------------------------------------------------
// PROCESS COMMANDS BEGIN
/**********************************************************************
*
* Function: cliBuildCommand
*
* Description: Put received characters into the command buffer or the
* parameter buffer. Once a complete command is received
* return true.
*
* Notes:
*
* Returns: true if a command is complete, otherwise false.
*
**********************************************************************/
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;
/* Don't store any new line characters or spaces. */
if ((nextChar == '\n') || (nextChar == ' ') || (nextChar == '\t') || (nextChar == '\r'))
return false;
/* The completed command has been received. Replace the final carriage
* return character with a NULL character to help with processing the
* command. */
//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) {
/* Convert the incoming character to upper case. This matches the case
* of commands in the command table. Then store the received character
* in the command buffer. */
gCommandBuffer[idx] = TO_UPPER(nextChar);
idx++;
/* If the command is too long, reset the index and process
* the current command buffer. */
if (idx > MAX_COMMAND_LEN) {
idx = 0;
return true;
}
}
if (state == PARAM) {
/* Store the received character in the parameter buffer. */
gParamBuffer[idx2] = nextChar;
idx2++;
/* If the command is too long, reset the index and process
* the current parameter buffer. */
if (idx > MAX_PARAMETER_LEN) {
idx2 = 0;
return true;
}
}
return false;
}
/**********************************************************************
*
* Function: cliProcessCommand
*
* Description: Look up the command in the command table. If the
* command is found, call the command's function. If the
* command is not found, output an error message.
*
* Notes:
*
* Returns: None.
*
**********************************************************************/
void cliProcessCommand(void)
{
int bCommandFound = false;
int idx;
/* Convert the parameter to an integer value.
* If the parameter is empty, gParamValue becomes 0. */
gParamValue = strtol(gParamBuffer, NULL, 0);
/* Search for the command in the command table until it is found or
* the end of the table is reached. If the command is found, break
* out of the loop. */
for (idx = 0; gCommandTable[idx].name != NULL; idx++) {
if (strcmp(gCommandTable[idx].name, gCommandBuffer) == 0) {
bCommandFound = true;
break;
}
}
/* If the command was found, call the command function. Otherwise,
* output an error message. */
if (bCommandFound == true) {
if (!webRequest) {
printLine("");
}
(*gCommandTable[idx].function)();
}
else {
if (!webRequest) {
printLine("");
}
printLine("Command not found.");
}
}
// PROCESS COMMANDS END
// -------------------------------------------------------------------------
void setup() {
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
Serial.begin(115200);
//tempSensor.begin(2);
pinMode(ledPin, OUTPUT); // sets the pin as output
Serial.println("Arduino command processing example");
Serial.print('>');
}
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) {
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
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.println("Web request:");
//Serial.println(url.toCharArray());
if (url.indexOf("?")>=0) {
String commandReceived;
String parametersReceived;
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="";
}
// commandReceived=commandReceived.toUpperCase();
// parametersReceived=parametersReceived.toUpperCase();
// Serial.println("Web command received:");
// Serial.println(commandReceived.toCharArray());
//tammat toCharArray
strcpy (gCommandBuffer, commandReceived.toCharArray());
//strcpy (gCommandBuffer, commandReceived);
if (parametersReceived.length()>0) {
// Serial.println("Parameters received:");
// Serial.println(parametersReceived.toCharArray());
strcpy (gParamBuffer, parametersReceived.toCharArray());
}
} 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();
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();
}
}