|
Post by randall on Oct 5, 2013 17:55:56 GMT -8
Just starting to play with Raw Mode and I was wondering if anybody has an example server.
Randy
|
|
|
Post by randall on Oct 5, 2013 17:56:51 GMT -8
Oh and I mean for an Arduino too.
Randy
|
|
|
Post by jeremy on Oct 6, 2013 8:16:07 GMT -8
Hi Randall,
I just started working on the raw mode server for my Arduino Mega 2650. I downloaded the milocreek arduino server and modified the main file like this:
// Filename ArduinoRAWServer.ino // Version 1.0 08/25/13 //
//#define WEBDUINO_SERIAL_DEBUGGING 1
#include "SPI.h" #include "Ethernet.h" #include "WebServer.h"
#include "avr/pgmspace.h"
#include "Config.h"
long messageCount;
static uint8_t mac[] = LOCALMAC;
static uint8_t ip[] = LOCALIP;
char *md5str;
char ST1Text[40]; // used in ST-1 Send text control
#include "MD5.h"
#include <stdarg.h> void p(char *fmt, ... ){ char tmp[128]; // resulting string limited to 128 chars va_list args; va_start (args, fmt ); vsnprintf(tmp, 128, fmt, args); va_end (args); Serial.print(tmp); }
#define MAX_STRING_LEN 100
char* subStr (char* input_string, char *separator, int segment_number) { char *act, *sub, *ptr; static char copy[MAX_STRING_LEN]; int i; strcpy(copy, input_string); for (i = 1, act = copy; i <= segment_number; i++, act = NULL) { sub = strtok_r(act, separator, &ptr); if (sub == NULL) break; } return sub; }
// no-cost stream operator as described at // http://sundial.org/arduino/?page_id=119 template<class T> inline Print &operator <<(Print &obj, T arg) { obj.print(arg); return obj; }
#define PREFIX ""
WebServer webserver(PREFIX, WEB_SERVER_PORT);
// commands are functions that get called by the webserver framework // they can read any posted data from client, and they output to server
void rawCmd(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete) {
#ifdef DEBUG Serial.println("------------------"); #endif
char myBuffer[1024];
server.httpSuccess("application/text"); if (type == WebServer::HEAD) return; int myChar; int count; myChar = server.read(); count = 0; while (myChar > 0) { myBuffer[count] = (char) myChar; myChar = server.read(); count++;
} myBuffer[count] = '\0';
delay(25);
messageCount++; char returnRaw[200] = "";
char *ObjectID = subStr(myBuffer, ",", 1); char *ObjectType = subStr(myBuffer, ",", 2);
char *ObjectName = subStr(myBuffer, ",", 3);
char *ObjectServerID = subStr(myBuffer, ",", 4);
char *ObjectFlags = subStr(myBuffer, ",", 5);
char *ObjectAction = subStr(myBuffer, ",", 6);
char *Password = subStr(myBuffer, ",", 7); #ifdef DEBUG Serial.println(myBuffer); Serial.println("-----------"); Serial.println(ObjectID); Serial.println(ObjectType); Serial.println(ObjectName); Serial.println(ObjectServerID); Serial.println(ObjectFlags); Serial.println(ObjectAction); Serial.println(Password); Serial.println("-----------"); #endif
// respond to only a server SS-1 present request
if ((strcmp(ObjectType, "256") == 0) && (strcmp(ObjectServerID, "SS-1") == 0)) { strcat(returnRaw,ObjectID); strcat(returnRaw, ","); strcat(returnRaw,ObjectType); strcat(returnRaw, ","); strcat(returnRaw,ObjectServerID); strcat(returnRaw, ","); strcat(returnRaw,ObjectFlags); strcat(returnRaw, ","); strcat(returnRaw, "2"); strcat(returnRaw, ","); strcat(returnRaw,Password);
}
#ifdef DEBUG Serial.print("returnRaw ="); Serial.println(returnRaw); #endif
server << returnRaw;
}
void setup() { Serial.begin(9600); // set up Serial library at 9600 bps Serial.print(F("ArduinoRAWServer ")); Serial.print(VERSIONNUMBER); Serial.println(F(" 08/23/13")); Serial.print(F("Compiled at:")); Serial.print (F(__TIME__)); Serial.print(F(" ")); Serial.println(F(__DATE__)); p("ip address = %i.%i.%i.%i\n", ip[0], ip[1], ip[2], ip[3]); p("port number = %i\n", WEB_SERVER_PORT); p("mac address = %#X %#X %#X %#X %#X %#X\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
Ethernet.begin(mac, ip); webserver.begin();
webserver.setDefaultCommand(&rawCmd); webserver.addCommand("raw", &rawCmd); messageCount=0; unsigned char* hash=MD5::make_hash(PASSWORD); //generate the digest (hex encoding) of our hash md5str = MD5::make_digest(hash, 16);
}
void loop() { // process incoming connections one at a time forever webserver.processConnection(); //Serial.println(freeMemory());
// if you wanted to do other work based on a connecton, it would go here }
my config.h file looks like this:
// Filename: Config.h // Version 1.5 08/06/13 BP MiloCreek
// your local function include file // ---------- // Note: This program is designed for you to supply your own functions in a file called Local.h // Out of the box, the program uses LocalExample.h included in ExecuteJSONCommand.h // // If you change LocalExample.h and update the software, LocalExample.h WILL BE OVERWRITTEN! // // Change #define LOCALFILE "LocalExample.h" to #define LOCALFILE "Local.h" to #include "Local.h" in ExecuteJSONCommand.h // ---------- #define LOCALFILE "LocalExample.h"
//define if you want debug on, otherwise #undef #define DEBUG
//define if you want to only debug local files, otherwise #undef #undef LOCALDEBUG
//WEB_SERVER_PORT is the port that the Arduino Webserver will be responding to requests from the RasPiConnect/ArduinoConnect App #define WEB_SERVER_PORT 9650
//LOCALIP is the address of your device. If you poke a hole through your firewall and expose it to the Internet, insert that address here //Usually, the port in the URL (i.e. 9600) will match the WEB_SERVER_PORT above but can be remapped in most routers/firewalls //Note that the address for your SystemURL in the RasPiConnectApp/ArduinoConnectApp should be of the form: http://192.168.1.120:9600/arduino (see manual for more information)
#define LOCALIP { 192, 168, 1, 177 }
// LOCALMAC must be a unique value on your network. Don't have two Arduinos as the same MAC on a network #define LOCALMAC { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }
//USERNAME is the username that you have entered in the RasPiConnect App. It must match and is case sensitive #define USERNAME "RasPiConnect"
//PASSWORD is the password that you have entered in the RasPiConnect App. It must match and is case sensitive #define PASSWORD "RasPiConnectPassword"
// set to True if you have an I2C bus set up and has an AdaFruit BMP085 and two BlinkM modules (addresses 0xC and 0xB) False if not #define I2CDEMO FALSE
//ARDUINOCONNECTSERVER Version Number. Do not change! #define VERSIONNUMBER "1.6"
This is working for me.
Jeremy
|
|