Hello,
Im trying to convert data being posted from a server to an ethernet board into integer values.
This is what I get in the serial monitor...
Ready
Client request: POST data:
firste:-1seconde:-1en:-1
Sending response
Returning Information to server...done
Client request: POST data:
firste:-1seconde:-1en:-1a=255&b=111&c=C
Sending response
Returning Information to server...done
"a=255&b=111&c=C" is what i am attempting to convert into two interger variables 255 and 111,
the code on the board is...
#include <SD.h>
#include <SPI.h>
#include <Ethernet.h>
#define maxLength 64
byte mac[] = {0x1c, 0x26, 0x0c, 0x98, 0x22, 0x85 };
byte ip[] = {192, 168, 1 , 20 };
byte gateway[] = {192, 168, 1, 1 };
byte subnet[] = {255, 255, 255, 0 };
EthernetServer server(80);
String inString = String(maxLength);
char luxbuf[6];
const int photocellPin = A0;
int photocellReading;
const float Res=10.0;
const int Pin9 = 9;
const int Pin8 = 8;
const int Pin7 = 7;
int lux = 0;
int val;
int lightlux = 0;
int lightvol = 0;
void setup()
{
Serial.begin(9600);
Ethernet.begin(mac, ip, gateway, subnet);
pinMode(Pin9, OUTPUT);
pinMode(Pin8, OUTPUT);
pinMode(Pin7, OUTPUT);
pinMode(photocellPin, INPUT);
delay(2000);
server.begin();
Serial.println("Ready");
}
void loop()
{
int bufLength;
EthernetClient client = server.available();
if(client) {
boolean currentLineIsBlank = true;
boolean currentLineIsGet = true;
int tCount = 0;
char tBuf[64];
int a,b;
char *pch;
Serial.print("Client request: ");
while (client.connected()) {
while(client.available()) {
char c = client.read();
if (c == '\n' && currentLineIsBlank) {
if(tCount < 63)
{
tBuf[tCount] = c;
tCount++;
tBuf[tCount] = 0;
}
// retrieve the POST data
Serial.print("POST data: ");
Serial.print(tBuf);
inString = String(tBuf);
int firste = inString.indexOf('=');
Serial.print("firste:");
Serial.print(firste);
int seconde = inString.indexOf('=', firste);
Serial.print("seconde:");
Serial.print(seconde);
int en = inString.indexOf("C", firste);
Serial.print("en:");
Serial.print(en);
if (firste > -1) {
bufLength = (seconde-5);
if(bufLength > 6){
bufLength = 6;
}
inString.substring((firste+1), (seconde-1)).toCharArray(luxbuf, bufLength);
lightlux = atoi(luxbuf);
Serial.print("lightlux:");
Serial.print(lightlux);
lightvol = 51*(lightlux/1333);
Serial.print("lightvol:");
Serial.print(lightvol);
bufLength = (en - (seconde + 1));
if(bufLength > 6){
bufLength = 6;
}
inString.substring((seconde+1), (en-1)).toCharArray(luxbuf, bufLength);
val = atoi(luxbuf);
Serial.print("val:");
Serial.print(val);
}
while(client.available()) Serial.write(client.read());
Serial.println();
Serial.println("Sending response");
Serial.print("Returning Information to server...");
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
photocellReading = analogRead(photocellPin);
float Vout=photocellReading*0.0048828125;
lux=500/(Res*((5-Vout)/Vout));
client.print("Lux Value is : ");
client.print(lux);
client.print(" Current Voltage is: ");
client.print(lightvol);
client.print(" END");
client.println("
");
client.stop();
}
else if (c == '\n') {
currentLineIsBlank = true;
currentLineIsGet = false;
}
else if (c != '\r') {
currentLineIsBlank = false;
}
}
}
if (val != 0) {
Serial.print("adjusting lights...");
while (lightlux < lux){
lightvol = lightvol + .1;
adjustlight();
}
while (lightlux > lux){
lightvol = lightvol - .1;
adjustlight();
}
}
Serial.println("done");
}
}
void adjustlight() {
Serial.print("adjust light function starting with val: ");
Serial.print(val);
if (val == '000') { analogWrite(Pin9, LOW);analogWrite(Pin8, LOW);analogWrite(Pin7, LOW);}
if (val == '111') { analogWrite(Pin9, lightvol);analogWrite(Pin8, lightvol);analogWrite(Pin7, lightvol);}
if (val == '100') { analogWrite(Pin9, lightvol);analogWrite(Pin8, LOW);analogWrite(Pin7, LOW);}
if (val == '010') { analogWrite(Pin9, LOW);analogWrite(Pin8, lightvol);analogWrite(Pin7, LOW);}
if (val == '001') { analogWrite(Pin9, LOW);analogWrite(Pin8, LOW);analogWrite(Pin7, lightvol);}
if (val == '110') { analogWrite(Pin9, lightvol);analogWrite(Pin8, lightvol);analogWrite(Pin7, LOW);}
if (val == '011') { analogWrite(Pin9, LOW);analogWrite(Pin8, lightvol);analogWrite(Pin7, lightvol);}
photocellReading = analogRead(photocellPin);
float Vout=photocellReading*0.0048828125;
lux=500/(Res*((5-Vout)/Vout));
}
ive tried a few different things, like moving inString to where the buffer takes in charachters, but everyone returns int firste = -1
I dont care how the values are converted, just want to do it.
Any and all help is appreciated.