Reputation: 11
I'm trying to check the number of unread Mails with an Arduino+Ethernet Shield, sending two IMAP-requests. With client.read(server_answer), I store it into a char. When I send it to serial with Serial.print(server_answer), I get the following:
* OK IMAP server ready H migmx111 92345
0 OK LOGIN completed
* STATUS INBOX (UNSEEN 1)
0 OK STATUS completed
* STATUS INBOX (MESSAGES 1917)
0 OK STATUS completed
* BYE Server logging out
0 OK LOGOUT completed
Now my question: How can I extract the two numbers (total count of mails and unread mails, in the example 1 unread and 1917 total count)? How can I get them in two different strings? I want to display the numbers with some some text ("You have [number] new mails!") on a LCD.
If it helps, here's interesting part of my code:
void loop()
{
updateClient();
checkAvail();
}
void updateClient()
{
if ((millis() - updateTimer) > 10000)
{
Ethernet.begin(mac, ip);
// Serial.println("connecting...");
delay(1000);
if (client.connect())
{
//Serial.println("connected");
client.println("0 login myusername mypasswd");
client.println("0 STATUS INBOX (UNSEEN)");
client.println("0 STATUS INBOX (MESSAGES)");
client.println("0 logout");
clientConnected = true;
}
else
{
Serial.println("connection failed");
}
updateTimer = millis();
}
}
void checkAvail()
{
if (clientConnected)
{
if (client.available())
{
server_answer = client.read();
Serial.print(server_answer);
}
if (!client.connected())
{
Serial.println();
// Serial.println("disconnecting.");
client.stop();
clientConnected = false;
}
}
}
Upvotes: 1
Views: 1761
Reputation: 6187
Without writing your code for you, you need to break the incoming data up into chunks using strtok_r(). Looking at your code above calling strtok_r() with '(' as a delimiter then again with a space as a delimiter and then again with ')' should get you to the begining of your first number. From there atoi() will convert it to an interger. Repeating the process should get you to the second value as well.
Take a crack at this and post your code if you have any more problems.
Upvotes: 0