ok here the code
/* Web_Parms_1.pde - very simple Webduino example of parameter passing and parsing */
/* can set pins to INPUT OUTPUT can read and write pins
*
* how to use, examples:
* curl -d "SET=D1,INPUT" http://192.168.x.y/parsed.html
* curl -d "SET=D1,OUTPUT" http://192.168.x.y/parsed.html
* curl -d "READ=D1,1" http://192.168.x.y/parsed.html
* curl -d "WRITE=D1,1" http://192.168.x.y/parsed.html
*
* curl -d "SET=A1,INPUT" http://192.168.x.y/parsed.html
* curl -d "SET=A1,OUTPUT" http://192.168.x.y/parsed.html
* curl -d "READ=A1,1" http://192.168.x.y/parsed.html
* curl -d "WRITE=A1,55" http://192.168.x.y/parsed.html
*
* curl -d "WRITE=A1,55&READ=A2,1&WRITE=D2,0" http://192.168.x.y/parsed.html
* as many & as you like..
* returns eg:
* {"1":{"cmd":"WRITE","type":"D,"pin":"D1","value":"0","status":"OK"},{"2":{"cmd":"READ","type":"A,"pin":"A1","value":"55","status":"OK"}}
*
* This invokes parsedCmd, which displays the "raw" parameter string,
* but also uses the "nexyURLparam" routine to parse out the individual
* parameters, and display them.
*/
#define WEBDUINO_FAIL_MESSAGE "<h1>Request Failed</h1>"
//#include "avr/pgmspace.h" // new include
#include "WebServer/WebServer.h"
#define VERSION_STRING "0.1"
// ROM-based messages used by the application
// These are needed to avoid having the strings use up our limited
// amount of RAM.
// not used anymore, should be deleted:
/*
P(Page_start) = "<html><head><title>Web_Parms_1 Version " VERSION_STRING "</title></head><body>\n";
P(Page_end) = "</body></html>";
P(Get_head) = "<h1>GET from ";
P(Post_head) = "<h1>POST to ";
P(Default_head) = "unidentified URL requested.</h1><br>\n";
P(Raw_head) = "raw.html requested.</h1><br>\n";
P(Parsed_head) = "parsed.html requested.</h1><br>\n";
P(Good_tail_begin) = "URL tail = '";
P(Bad_tail_begin) = "INCOMPLETE URL tail = '";
P(Tail_end) = "'<br>\n";
P(Parsed_tail_begin) = "URL parameters:<br>\n";
P(Parsed_item_separator) = " = '";
P(Params_end) = "End of parameters<br>\n";
P(Post_params_begin) = "Parameters sent by POST:<br>\n";
P(Line_break) = "<br>\n";
*/
P(Unknown_head) = "<h1>UNKNOWN request for ";
extern char* itoa(int a, char* buffer, unsigned char radix);
// variables used in code
char modeOUTPUT[8] = "OUTPUT";
char modeINPUT[8] = "INPUT";
char commandRead[8] = "READ";
char commandWrite[8] = "WRITE";
char commandSet[8] = "SET";
bool printLineDebug = false;
int valRead = 0;
char valReadChar[10] ;
char pinNum[3] = "0";
int pinNumber = 0;
int pinValueInt = 0;
char pinName[3];
char pinValue[8];
char theString[100];
char theCountChar[8];
char failedCMD[16] ;
bool isDigital = false;
char isOK[3] ="OK";
int loopCounter = 0;
char test[10] = " ";
int resetCounter = 0;
char buff1[10];
/* This creates an instance of the webserver. By specifying a prefix
* of "", all pages will be at the root of the server. */
#define PREFIX ""
WebServer webserver(PREFIX, 80);
#define NAMELEN 64
#define VALUELEN 64
void parsedCmd(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete){
// Serial.println("starting parse ");
URLPARAM_RESULT rc;
char name[NAMELEN];
char value[VALUELEN];
resetCounter=0; // tell loop, we are still alive
if (type != WebServer::POST ){
server.printP(Unknown_head);
return;
}
int theCount =0;
server.print("{");
/// loop over & parameters
while (server.readPOSTparam(name, NAMELEN, value, VALUELEN)){
theCount +=1;
strcpy(failedCMD,isOK);
itoa(theCount,theCountChar,10);
if (theCount >1) strcpy(theString,",\"");
else strcpy(theString,"\"");
if (printLineDebug) Serial.println(theCountChar);
strcat(theString,theCountChar);
strcat(theString,"\":{");
char *theComma = strchr(value,',');
if (theComma == NULL) {
if (printLineDebug) Serial.println("bad input");
strcat(theString,"\"badinput\":\"");
strcat(theString,name);
strcat(theString,"=");
strcat(theString,value);
strcat(theString,"\"}");
}
else {
strcat(theString,"\"cmd\":\"");
strcat(theString,name);
strcat(theString,"\"");
strcat(theString,",\"type\":\"");
if (value[0] == 'D') {
isDigital = true;
strcat(theString,"D");
}
else {
isDigital = false;
strcat(theString,"A");
}
strncpy(pinNum,value+1,(int)(theComma-value-1));
if (printLineDebug) Serial.println("pinName");
if (printLineDebug) Serial.println(pinName);
if (printLineDebug) Serial.println(pinNum);
pinNumber = atoi(pinNum);
if (pinNumber< 0 || pinNumber >7) {
strcat(theString,",\"badinput\":\"");
strcat(theString,value);
strcat(theString,"\"}");
}
else{
// get function = name and value = second part after ","
strncpy(pinName,value,2);
strcpy(pinValue,theComma+1);
pinValueInt = atoi(pinValue);
strcat(theString,",\"pin\":\"");
strcat(theString,pinName);
strcat(theString,"\"");
// read from pins
if (strcmp( name, commandRead ) == 0 ){
if (isDigital){
if (PIN_MAP[pinNumber].pin_mode == INPUT){
valRead = digitalRead(pinNumber) ;
}
else{
strcpy(failedCMD,"NotReadMode");
valRead=-1;
}
}
else{
if (PIN_MAP[pinNumber+10].pin_mode == INPUT){
valRead = analogRead(pinNumber+10);
}
else{
strcpy(failedCMD,"NotReadMode");
valRead=-1;
}
}
itoa(valRead,valReadChar,10);
Serial.println("valReadChar");
Serial.println(valReadChar);
strcat(theString,",\"value\":\"");
strcat(theString,valReadChar);
strcat(theString,"\"");
}
// write to pins
if (strcmp( name, commandWrite ) == 0 ){
if (pinValueInt<0) pinValueInt=0;
if (isDigital) {
if (PIN_MAP[pinNumber].pin_mode == OUTPUT) {
if (pinValueInt>1) pinValueInt=1;
digitalWrite(pinNumber, pinValueInt);
}
else strcpy(failedCMD,"NotWriteMode");
}
else{
if (PIN_MAP[pinNumber+10].pin_mode == OUTPUT) {
if (pinValueInt>255) pinValueInt=255;
analogWrite(pinNumber+10, pinValueInt);
}
else strcpy(failedCMD,"NotWriteMode");
}
itoa(pinValueInt,valReadChar,10);
strcat(theString,",\"value\":\"");
strcat(theString,valReadChar);
strcat(theString,"\"");
}
// set pin functions
if (strcmp( name, commandSet ) == 0 ){
strcat(theString,",\"value\":\"");
strcat(theString,pinValue);
strcat(theString,"\"");
if (strcmp(pinValue,modeINPUT)==0){
if (isDigital) pinMode(pinNumber, INPUT);
else pinMode(pinNumber+10, INPUT);
}
if (strcmp(pinValue,modeOUTPUT)==0){
if (isDigital) pinMode(pinNumber, OUTPUT);
else pinMode(pinNumber+10, OUTPUT);
}
}
}
}
strcat(theString,",\"status\":\"");
if ( strcmp( failedCMD, isOK ) != 0) {
strcat(theString,"error-");
strcat(theString,failedCMD);
}
else strcat(theString,"OK");
strcat(theString,"\"}");
Serial.println(theString);
server.print(theString);
}
server.print("}");
}
void setup()
{
webserver.addCommand("parsed.html", &parsedCmd);
for (int i = 0; i < 8; i++) pinMode(i, INPUT) ;
for (int i = 10; i < 18; i++) pinMode(i, INPUT) ;
/* start the webserver */
webserver.begin();
Serial.begin(9600);
Serial.println("starting");
loopCounter =0;
}
void loop()
{
char buff[64];
int len = 64;
/* process incoming connections one at a time forever */
// Serial.println("loop");
delay(1);
webserver.processConnection(buff, &len);
resetCounter +=1;
if (resetCounter > 3000){ // this is about 18 seconds
resetCounter =0;
strcpy(theString,"reset: ");
itoa( millis(), buff1,10);
strcat(theString,buff1);
strcpy(theString,"(mSec) after start");
Serial.println(theString);
delay (100);
NVIC_SystemReset(); // this resets the system
}
loopCounter+=1;
if ( loopCounter > 300) { // log heatbeat into serial connecting for testing only
strcpy(theString,"Loop ");
itoa( millis(), buff1,10);
strcat(theString,buff1);
strcat(theString,"(mSec), " );
strcat(theString,", resetCount= " );
itoa( resetCounter, buff1,10);
strcat(theString,buff1);
Serial.println(theString);
loopCounter=0;
}
}
>