Hello all!
Thank you for taking the time to process my question. I have been fooling around off and on with Arduino for a couple of years now and am familiar with the basics. The board that I have and that I'm using to prototype this project is an Arduino Due. The project is a battery management system with an Internet connection, so the Arduino will be processing HTTP requests and extracting information from them. For instance, at one point, the Arduino will need to extract a complete IP address, subnet mask, default gateway, and HTTP port number from the HTTP request. Here is the code that I've come up with so far to do that, with an example HTTP request array:
char extractedIPOctet1[4];
int iPOctet1;
int iPOctet2StartingIndex;
char extractedIPOctet2[4];
int iPOctet2;
int iPOctet3StartingIndex;
char extractedIPOctet3[4];
int iPOctet3;
int iPOctet4StartingIndex;
char extractedIPOctet4[4];
int iPOctet4;
int subnetOctet1StartingIndex;
char extractedSubnetOctet1[4];
int subnetOctet1;
int subnetOctet2StartingIndex;
char extractedSubnetOctet2[4];
int subnetOctet2;
int subnetOctet3StartingIndex;
char extractedSubnetOctet3[4];
int subnetOctet3;
int subnetOctet4StartingIndex;
char extractedSubnetOctet4[4];
int subnetOctet4;
int gatewayOctet1StartingIndex;
char extractedGatewayOctet1[4];
int gatewayOctet1;
int gatewayOctet2StartingIndex;
char extractedGatewayOctet2[4];
int gatewayOctet2;
int gatewayOctet3StartingIndex;
char extractedGatewayOctet3[4];
int gatewayOctet3;
int gatewayOctet4StartingIndex;
char extractedGatewayOctet4[4];
int gatewayOctet4;
int hTTPPortStartingIndex;
char extractedHTTPPort[6];
int hTTPPort;
void setup() {
Serial.begin(9600);
}
void loop() {
char httpRequest[] = "iPAddressOctet1=192&iPAddressOctet2=168&iPAddressOctet3=0&iPAddressOctet4=220&"
"subnetMaskOctet1=255&subnetMaskOctet2=255&subnetMaskOctet3=255&subnetMaskOctet4=0&defaultGatewayOctet1=192&"
"defaultGatewayOctet2=168&defaultGatewayOctet3=0&defaultGatewayOctet4=1&hTTPPort=80";
// see if the Next button on the network setup page was clicked
if (httpRequest[0] == 'i') {
int networkIndex = 0;
for (byte x = 16; x >= 16; x++) {
if (httpRequest[x] == '&') {
iPOctet2StartingIndex = x + 17;
break;
}
extractedIPOctet1[networkIndex] = httpRequest[x];
networkIndex++;
}
iPOctet1 = atoi(extractedIPOctet1);
Serial.println("Extracted IP address octet 1:");
Serial.println(iPOctet1);
delay(1000);
networkIndex = 0;
for (byte x = iPOctet2StartingIndex; x >= iPOctet2StartingIndex; x++) {
if (httpRequest[x] == '&') {
iPOctet3StartingIndex = x + 17;
break;
}
extractedIPOctet2[networkIndex] = httpRequest[x];
networkIndex++;
}
iPOctet2 = atoi(extractedIPOctet2);
Serial.println("Extracted IP address octet 2:");
Serial.println(iPOctet2);
delay(1000);
networkIndex = 0;
for (byte x = iPOctet3StartingIndex; x >= iPOctet3StartingIndex; x++) {
if (httpRequest[x] == '&') {
iPOctet4StartingIndex = x + 17;
break;
}
extractedIPOctet3[networkIndex] = httpRequest[x];
networkIndex++;
}
iPOctet3 = atoi(extractedIPOctet3);
Serial.println("Extracted IP address octet 3:");
Serial.println(iPOctet3);
delay(1000);
networkIndex = 0;
for (byte x = iPOctet4StartingIndex; x >= iPOctet4StartingIndex; x++) {
if (httpRequest[x] == '&') {
subnetOctet1StartingIndex = x + 18;
break;
}
extractedIPOctet4[networkIndex] = httpRequest[x];
networkIndex++;
}
iPOctet4 = atoi(extractedIPOctet4);
Serial.println("Extracted IP address octet 4:");
Serial.println(iPOctet4);
delay(1000);
networkIndex = 0;
for (byte x = subnetOctet1StartingIndex; x >= subnetOctet1StartingIndex; x++) {
if (httpRequest[x] == '&') {
subnetOctet2StartingIndex = x + 18;
break;
}
extractedSubnetOctet1[networkIndex] = httpRequest[x];
networkIndex++;
}
subnetOctet1 = atoi(extractedSubnetOctet1);
Serial.println("Extracted subnet mask octet 1:");
Serial.println(subnetOctet1);
delay(1000);
networkIndex = 0;
for (byte x = subnetOctet2StartingIndex; x >= subnetOctet2StartingIndex; x++) {
if (httpRequest[x] == '&') {
subnetOctet3StartingIndex = x + 18;
break;
}
if (httpRequest[x] == '&') break;
extractedSubnetOctet2[networkIndex] = httpRequest[x];
networkIndex++;
}
subnetOctet2 = atoi(extractedSubnetOctet2);
Serial.println("Extracted subnet mask octet 2:");
Serial.println(subnetOctet2);
delay(1000);
networkIndex = 0;
for (byte x = subnetOctet3StartingIndex; x >= subnetOctet3StartingIndex; x++) {
if (httpRequest[x] == '&') {
subnetOctet4StartingIndex = x + 18;
break;
}
extractedSubnetOctet3[networkIndex] = httpRequest[x];
networkIndex++;
}
subnetOctet3 = atoi(extractedSubnetOctet3);
Serial.println("Extracted subnet mask octet 3:");
Serial.println(subnetOctet3);
delay(1000);
networkIndex = 0;
for (byte x = subnetOctet4StartingIndex; x >= subnetOctet4StartingIndex; x++) {
if (httpRequest[x] == '&') {
gatewayOctet1StartingIndex = x + 22;
break;
}
extractedSubnetOctet4[networkIndex] = httpRequest[x];
networkIndex++;
}
subnetOctet4 = atoi(extractedSubnetOctet4);
Serial.println("Extracted subnet mask octet 4:");
Serial.println(subnetOctet4);
delay(1000);
networkIndex = 0;
for (byte x = gatewayOctet1StartingIndex; x >= gatewayOctet1StartingIndex; x++) {
if (httpRequest[x] == '&') {
gatewayOctet2StartingIndex = x + 22;
break;
}
extractedGatewayOctet1[networkIndex] = httpRequest[x];
networkIndex++;
}
gatewayOctet1 = atoi(extractedGatewayOctet1);
Serial.println("Extracted default gateway octet 1:");
Serial.println(gatewayOctet1);
delay(1000);
networkIndex = 0;
for (byte x = gatewayOctet2StartingIndex; x >= gatewayOctet2StartingIndex; x++) {
if (httpRequest[x] == '&') {
gatewayOctet3StartingIndex = x + 22;
break;
}
extractedGatewayOctet2[networkIndex] = httpRequest[x];
networkIndex++;
}
gatewayOctet2 = atoi(extractedGatewayOctet2);
Serial.println("Extracted default gateway octet 2:");
Serial.println(gatewayOctet2);
delay(1000);
networkIndex = 0;
for (byte x = gatewayOctet3StartingIndex; x >= gatewayOctet3StartingIndex; x++) {
if (httpRequest[x] == '&') {
gatewayOctet4StartingIndex = x + 22;
break;
}
extractedGatewayOctet3[networkIndex] = httpRequest[x];
networkIndex++;
}
gatewayOctet3 = atoi(extractedGatewayOctet3);
Serial.println("Extracted default gateway octet 3:");
Serial.println(gatewayOctet3);
delay(1000);
networkIndex = 0;
for (byte x = gatewayOctet4StartingIndex; x >= gatewayOctet4StartingIndex; x++) {
if (httpRequest[x] == '&') {
hTTPPortStartingIndex = x + 10;
break;
}
extractedGatewayOctet4[networkIndex] = httpRequest[x];
networkIndex++;
}
gatewayOctet4 = atoi(extractedGatewayOctet4);
Serial.println("Extracted default gateway octet 4:");
Serial.println(gatewayOctet4);
delay(1000);
networkIndex = 0;
for (byte x = hTTPPortStartingIndex; x >= hTTPPortStartingIndex; x++) {
if (httpRequest[x] == NULL) break;
extractedHTTPPort[networkIndex] = httpRequest[x];
networkIndex++;
}
hTTPPort = atoi(extractedHTTPPort);
Serial.println("Extracted HTTP port number:");
Serial.println(hTTPPort);
delay(1000);
}
}
The serial print commands and delays are in there for debugging. When viewed in the serial monitor, this code is able to extract the IP address octets, subnet mask octets, and default gateway octets. However, when it gets to the HTTP port, the code extracts a 0 when it should be 80 (see the HTTP request). Would anyone happen to know why this may be? Perhaps something having to do with it being at the end of the char array? It's probably obvious to you all, but I'm at a loss. Maybe I'm doing this completely wrong!
Let me know if you need any other information. Thank you for your time!