I am using arduino nano with esp8266 to connect to my wifi network.
I wanted to access an XML file based on particular id.
I have created a web service on my PC. Using this web service as reference to one method employee details. I need to pass the employee ID as a parameter to that method, that will return an XML file like below.
<employee>
<empID>1001</empID>
<emp_name>ramesh.d</emp_name>
<DOJ>29-12-2009</DOJ>
<salary>10000</salary>
</employee>
From this XML file I need to store the details into my variables like employeeId, Name, DOJ, Salary.
I am using the following code to do this.
#include <string.h>
#include <SPI.h>;
#include <SoftwareSerial.h>
#define DST_IP "192.168.1.102"
#define DEBUG true
SoftwareSerial esp8266(6, 7);
// Define Constants
// Max string length may have to be adjusted depending on data to be extracted
#define MAX_STRING_LEN 20
// Setup vars
char tagStr[MAX_STRING_LEN] = "";
char dataStr[MAX_STRING_LEN] = "";
char tmpStr[MAX_STRING_LEN] = "";
char endTag[3] = {'<', '/', '\0'};
int len;
// DECLARED VARIABLES HERE FOR GLOBAL USE
char ClbSts[MAX_STRING_LEN] = "\0";
char calDate[MAX_STRING_LEN] = "\0";
char nxtCAL[MAX_STRING_LEN] = "\0";
// Flags to differentiate XML tags from document elements (ie. data)
boolean tagFlag = false;
boolean dataFlag = false;
String emp_ID = "1001";
void setup()
{
Serial.begin(9600);
esp8266.begin(9600); // your esp's baud rate might be different
//lcd.begin(20, 4);
sendData("AT+RST\r\n",5000,DEBUG); // reset module
Serial.println();
sendData("AT+CWMODE=3\r\n",2000,DEBUG); // configure as access point
Serial.println();
sendData("AT+CWLAP\r\n",10000,DEBUG);
Serial.println();
String joinAP = "AT+CWJAP=\"INTRO\",\"123458\"";
joinAP +="\r\n";
sendData(joinAP,20000,DEBUG);
Serial.println();
delay(1000);
}
void loop() {
delay(1000);
Serial.println();
String cmd = "AT+CIPSTART=\"TCP\",\""; //make this command: AT+CPISTART="TCP","192.168.1.102",80
cmd += DST_IP;
cmd += "\",80";
cmd += "\r\n";
sendData(cmd,8000,DEBUG);
Serial.println();
String method ="GET /employee/emp.asmx/empdetails?empcode=";
method += emp_ID;
method +="HTTP/1.1\r\n";
method += "Host: 192.168.1.102\r\n\r\n";
String cipSend = "AT+CIPSEND=";
cipSend +=method.length();
cipSend +="\r\n";
sendData(cipSend,2000,DEBUG);
//Send our http GET request
sendData(method,30000,DEBUG);
readTags();
sendData("AT+CIPCLOSE",5000,DEBUG);
Serial.println();
}
// Process each char from web
void readTags() {
Serial.println("serialEvent Started");
// Read a char
char inChar = esp8266.read();
Serial.print(".");
if (inChar == '<') {
addChar(inChar, tmpStr);
tagFlag = true;
dataFlag = false;
} else if (inChar == '>') {
addChar(inChar, tmpStr);
if (tagFlag) {
strncpy(tagStr, tmpStr, strlen(tmpStr)+1);
}
// Clear tmp
clearStr(tmpStr);
tagFlag = false;
dataFlag = true;
} else if (inChar != 10) {
if (tagFlag) {
// Add tag char to string
addChar(inChar, tmpStr);
// Check for </XML> end tag, ignore it
if ( tagFlag && strcmp(tmpStr, endTag) == 0 ) {
clearStr(tmpStr);
tagFlag = false;
dataFlag = false;
}
}
if (dataFlag) {
// Add data char to string
addChar(inChar, dataStr);
}
}
// If a LF, process the line
if (inChar == 10 ) {
Serial.print("tagStr: ");
Serial.println(tagStr);
Serial.print("dataStr: ");
Serial.println(dataStr);
// Find specific tags and print data
if (matchTag("<empID>")) {
//Serial.println("dataStr Length:");
//Serial.print(strlen(dataStr));
// Serial.println("NAME: ");
// Serial.print(dataStr[0]);
strncpy(employeeId,dataStr,20);
ClbSts[21] = '\0';
}
if (matchTag("<emp_name>")) {
//Serial.print(" Flavor Name: ");
//strncpy(flavor_name,dataStr,strlen(dataStr)+1);
//Serial.print(flavor_name);
}
if (matchTag("<DOJ>")) {
//Serial.print(" Volume to Mix: ");
//strncpy(mix_volume,dataStr,strlen(dataStr)+1);
//Serial.print(mix_volume);
//Serial.print(" ");
}
// Clear all strings
clearStr(tmpStr);
clearStr(tagStr);
clearStr(dataStr);
// Clear Flags
tagFlag = false;
dataFlag = false;
}
}
/////////////////////
// Other Functions //
/////////////////////
// Function to clear a string
void clearStr (char* str) {
int len = strlen(str);
for (int c = 0; c < len; c++) {
str[c] = 0;
}
}
//Function to add a char to a string and check its length
void addChar (char ch, char* str) {
char *tagMsg = "<TRUNCATED_TAG>";
char *dataMsg = "-TRUNCATED_DATA-";
// Check the max size of the string to make sure it doesn't grow too
// big. If string is beyond MAX_STRING_LEN assume it is unimportant
// and replace it with a warning message.
if (strlen(str) > MAX_STRING_LEN - 2) {
if (tagFlag) {
clearStr(tagStr);
strcpy(tagStr,tagMsg);
}
if (dataFlag) {
clearStr(dataStr);
strcpy(dataStr,dataMsg);
}
// Clear the temp buffer and flags to stop current processing
clearStr(tmpStr);
tagFlag = false;
dataFlag = false;
} else {
// Add char to string
str[strlen(str)] = ch;
}
}
// Function to check the current tag for a specific string
boolean matchTag (char* searchTag) {
if ( strcmp(tagStr, searchTag) == 0 ) {
return true;
} else {
return false;
}
}
String sendData(String command, const int timeout, boolean debug)
{
String response = "";
esp8266.print(command); // send the read character to the esp8266
long int time = millis();
while( (time+timeout) > millis())
{
while(esp8266.available())
{
// The esp has data so display its output to the serial window
char c = esp8266.read(); // read the next character.
response+=c;
}
}
if(debug)
{
Serial.print(response);
}
return response;
}
This code responding like
+IPD,192:<?xml version="1.0" encoding="utf-8"?>
OK
OK
Unlink
What I have to do?
how could I read the XML nodes?
Is this code correct?
Suggest me corrections if any.
i am poor in english. If anybody can't understand my question. kindly reply me.