Here is the full code as requested.
The sBuffer is being filled previously in the code on Arduino 1 (around line 190) and right before I send it over serial, it is printed to the SerialMonitor over USB. This is how I know its terminated properly due to the fact that I dont get garbage in the Serial Monitor.
The serial sending is at the bottom of Arduino 1 code and in a function called sendMessgeToSlave.
Arduino 1
/*
Web Server
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
* SD on 4
* connect ground to ground
Pin 2 connected to Pin 3, Pin 3 connected to pin 2 on other arduino
*/
#define DEBUG
#include <SD.h>
#include <SPI.h>
#include <Ethernet.h>
#include <SoftwareSerial.h>
#define resetRelay 7
#define zcPin 9
#define dataPin 8
//MAC ADDRESS CHANGED FOR ONLINE POSTING
byte mac[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
IPAddress ip(192,168,1, 62);
EthernetServer server(80);
//vars to parse http request
//HTTP request Buffer
int _readRequestSize = 50;
char readRequest[50] = "";
int requestPos = 0;
int validPage = 0;
//default URL
char baseURL[10] = "index.htm";
//Serial Buffer
int _sBufferSize=50;
char sBuffer[50];
int realSBufferSize=0;
int sendCommand=0;
//generic loop var
int i = 0;
//parsing variables
int spaceIndex = -1;
int slashIndex = -1;
int dotIndex=-1;
int lockSlash= 0;
int lockDot=0;
int lockSpace=0;
//mode for reset var.
int mode =0;
SoftwareSerial ss(2,3);
void setup()
{
Serial.begin(9600);
ss.begin(9600);
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
//Start SD
pinMode(10, OUTPUT);
SD.begin(4);
//relay Reset
pinMode(resetRelay,OUTPUT);
digitalWrite(7,LOW);
Serial.print("init Done");
}
void loop()
{
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
Serial.println("Client Connected\nReading Request");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
//start to copy request into buffer
if(requestPos < _readRequestSize)
{
readRequest[requestPos] = c;
requestPos++;
}
if (c == '\n' && currentLineIsBlank) {
//start response to client
//reset counter
requestPos=0;
Serial.println("Begin Request Parser");
//readRequest Parser
for(i=4;i<_readRequestSize;i++)
{
if(readRequest[i]=='/'&&!lockSlash)
{
slashIndex=i;
lockSlash=1;
}
else if(readRequest[i]=='.'&&!lockDot)
{
dotIndex=i;
lockDot=1;
}
else if(readRequest[i]==' '&&!lockSpace)
{
//space index
spaceIndex=i;
lockSpace=1;
break;
}
}//end for
#ifdef DEBUG
Serial.println("Slash : Dot : Space");
Serial.print(slashIndex);
Serial.print(" : ");
Serial.print(dotIndex);
Serial.print(" : ");
Serial.println(spaceIndex);
#endif
//requestPos = urlLength
if(dotIndex==-1)
{
Serial.println("NO URL -> Pushing Index");
//no url given
requestPos=10;
}
else
{
requestPos = (dotIndex+3)-(slashIndex);
}
//create url charArray
char url[requestPos];
if(dotIndex==-1)
{
for(i=0;i<requestPos;i++)
{
url[i] = baseURL[i];
}
}
else
{
for(i=0;i<requestPos;i++)
{
url[i] = readRequest[i+slashIndex+1];
}
}
//add null term
url[requestPos]=NULL;
#ifdef DEBUG
Serial.print("\nURL: ");
Serial.println(url);
#endif
if((dotIndex != -1)&&(spaceIndex != (dotIndex+4)))
{
Serial.println("Grabbing Env Vars");
//grab rest of message
requestPos = (spaceIndex) - (dotIndex+4);
//flag to send
Serial.println("Setting sendCommand");
sendCommand=1;
//copy over buffer
for(i=0;i<requestPos;i++)
{
sBuffer[i] = readRequest[i+dotIndex+4];
}
realSBufferSize=requestPos;
//add null termination
sBuffer[requestPos]=NULL;
}//end if dotIndex
//parsing for URL
requestPos=0;
String URL = String(url);
URL.trim();
if(URL.equals("resetServer.htm"))
{
URL = String("index.htm");
mode=-1;
}
Serial.println("Checking for file");
char file[URL.length()+1];
URL.toCharArray(file,URL.length()+1);
Serial.print(file);
//parse here
File doc = SD.open(file);
if(doc)
{
// send a standard http response header
Serial.println("File Found");
client.println("HTTP/1.1 200 OK\nContent-Type: text/html\n");
validPage=1;
while (doc.available())
{
client.print((char)doc.read());
}
doc.close();
if(validPage && sendCommand) sendMessageToSlave();
}
else
{
Serial.println("File not found");
client.println("HTTP/1.1 404 Not Found");
validPage=0;
}
//exit client loop
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:
#ifdef DEBUG
Serial.println("\nClient disconnected");
#endif
client.stop();
resetGlobalVars();
}//end client
//reset
if(mode==-1)
{
mode=0;
digitalWrite(resetRelay,HIGH);
}
}
void resetGlobalVars()
{
Serial.print("Resetting Golbal Vars");
for(i=0;i<_readRequestSize;i++)
{
readRequest[i]=NULL;
}
for(i=0;i<_sBufferSize;i++)
{
sBuffer[i]=NULL;
}
spaceIndex=slashIndex=dotIndex=-1;
lockSlash=lockDot=lockSpace=0;
requestPos=validPage=sendCommand=0;
}
void sendMessageToSlave()
{
//implement soft serial to other device
Serial.println("Send Message To Slave!");
Serial.print("sBuffer:\n\t");
Serial.println(sBuffer);
//See aprrox Line 193 for termination
for(i=0;i<50;i++)
{
ss.print(sBuffer[i]);
ss.write(sBuffer[i]);
}
}
Arduino 2
// include the X10 library files:
#include <x10.h>
#include <x10constants.h>
#include <SoftwareSerial.h>
#define zcPin 9 // the zero crossing detect pin
#define dataPin 8 // the X10 data out pin
#define repeatTimes 3 // how many times each X10 message should repeat
x10 myHouse = x10(zcPin, dataPin);
SoftwareSerial ss(2,3);
void setup() {
// begin serial:
Serial.begin(9600);
ss.begin(9600);
//set internal pullup
pinMode(zcPin, INPUT);
digitalWrite(zcPin, HIGH);
// Turn off all lights:
myHouse.write(A, ALL_UNITS_OFF,repeatTimes);
}
void loop()
{
Serial.println("loop");
if(ss.available())
{
Serial.print("Message Avail");
Serial.print(ss.read());
Serial.write(ss.read());
}
}
An example of what the first arduino gets is
"http://192.168.1.62/lights.htm?house=A&dev=02&ctrl=0" --from web browser
It then extracts the url and checks for special cases and to see if the file exists on sd.. if it does it will return it to the client....
then the rest of the input containing "?house=A&dev=02&ctrl=0" is then parsed out and stored in sBuffer. I then calculate the length of the string and add in the null terminator being sBuffer[end] = NULL; This is around line 193 on arduino 1.
**The other thing that I may be causing the issue, but I cannot seem to find if it actually is, and I dont believe it is, is the var requestPos. I use this var to store different things at different times as a work around to help with the flash storage issue I am having. But like I said, I could not find anything wrong with it.
I do know that more or less I will actually be getting ascii encoding over this line and reading on arduino 2. I have managed this before. My issue is that the Message Avail is never actually printed, thus the second arduino is not getting the message. So this means, to me at least, that I am not getting softSerial to work properly.
Thanks