Looking for code to capture data going to Wifi

Do you have a question on how to do something.
Ask in here.
Post Reply
zveck
Posts: 12
Joined: Wed May 08, 2013 2:43 pm

Looking for code to capture data going to Wifi

Post by zveck »

I'm looking for some reliable code to capture and decode the data going to the wifi module. The reason I want this data is to drive a second arduino with a large touchscreen so I can display and see the data from more than 3 feet away. Yes I can use the portal, client, android app, but I just want to look over a see the data without doing anything but turning my head. When I bought the Reef Angel over a year ago, there was talk of a bigger screen. With all the cheep tablets out there, that idea was dropped it seems. I don't want to do it that way. I can see the data via usb serial monitor directly or via a bluetooth module tied to the tx line. I know what most of the data is. I just need a few lines of code to parse it into the variables it represents.
binder
Posts: 2871
Joined: Fri Mar 18, 2011 6:20 pm
Location: Illinois
Contact:

Re: Looking for code to capture data going to Wifi

Post by binder »

if you want to see what data is being sent, just pull it manually yourself to examine it or look at the code in the libraries.

Code: Select all

http://YOURIP:2000/r99
the xml data being sent is located in the RA_Wifi.cpp file. that contains all the data and should be pretty straight forward with understanding the xml data.


Sent from my iPad mini
zveck
Posts: 12
Joined: Wed May 08, 2013 2:43 pm

Re: Looking for code to capture data going to Wifi

Post by zveck »

As I said, I can see the data:

HTTP/1.1 200 OK
Server: ReefAngel
Cache-Control: no-store, no-cache, must-revalidate
Pragma: no-cache
Connection: close
Content-Type: text/xml
Content-Length: 602

<RA><ID>zveck</ID><T1>780</T1><T2>782</T2><T3>0</T3><PH>791</PH><R>244</R><RON>0</RON><ROFF>255</ROFF><R1>31</R1><RON1>0</RON1><ROFF1>255</ROFF1><R2>0</R2><RON2>0</RON2><ROFF2>255</ROFF2><R3>0</R3><RON3>0</RON3><ROFF3>255</ROFF3><R4>0</R4><RON4>0</RON4><ROFF4>255</ROFF4><R5>0</R5><RON5>0</RON5><ROFF5>255</ROFF5><R6>0</R6><RON6>0</RON6><ROFF6>255</ROFF6><R7>0</R7><RON7>0</RON7><ROFF7>255</ROFF7><R8>0</R8><RON8>0</RON8><ROFF8>255</ROFF8><ATOLOW>0</ATOLOW><ATOHIGH>0</ATOHIGH><EM>0</EM><EM1>0</EM1><REM>1</REM><AF>0</AF><SF>0</SF><PWMA>24</PWMA><PWMD>19</PWMD><PWMAO>255</PWMAO><PWMDO>255</PWMDO></RA>


I just need a few lines of Arduino code to turn that data into a string or strings. As you know there is a serial buffer limit of 64, I have expanded that to 256 but that's still not enough to capture the whole "packet". I am tapping into the RX line going into the WIFI module and running that into the RX line on a mega that will drive a
large touch screen. The code for the mega is done except for the capture the long packet part. I even have the parse bit done enough to pick what I want out of a string or strings.
User avatar
jsclownfish
Posts: 378
Joined: Mon Oct 24, 2011 7:52 pm
Location: Saint Louis

Re: Looking for code to capture data going to Wifi

Post by jsclownfish »

I spent some time on a LCD arduino/touchscreen and just sent the data over the I2C lines. It wasn't everything, but it was enough to control the outlets and view the data. As you said, I haven't found time to get back to it as the other options were easy enough and the system was pretty choppy at the time.
http://forum.reefangel.com/viewtopic.ph ... D&start=30
You can see the code I used there if it helps at all.
-Jon
zveck
Posts: 12
Joined: Wed May 08, 2013 2:43 pm

Re: Looking for code to capture data going to Wifi

Post by zveck »

Were you involved with the Jardino/Stilo thing? That's some of the basic hardware I have. I built it and decided short of the small screen, the reef angel was far better and hoped it would have the larger screen soon. Pissed at this point it never happened since I don't see all that well. The reason I decided to chase the serial buss is, the code is there being sent so I don't have to mess with the Reef Angel code at all. After I get the code capture, i'll look at the remote code coming in and duplicate that to send commands from the touch screen.
binder
Posts: 2871
Joined: Fri Mar 18, 2011 6:20 pm
Location: Illinois
Contact:

Re: Looking for code to capture data going to Wifi

Post by binder »

inside the ra_wifi.cpp file, there is code that reads data from the serial line and converts it into a string. that is exactly how we process the web page requests. I'm on my phone right now and can't give you more specific details on the function name.

Sent from my Moto X
binder
Posts: 2871
Joined: Fri Mar 18, 2011 6:20 pm
Location: Illinois
Contact:

Re: Looking for code to capture data going to Wifi

Post by binder »

processHTTP() - handles the reading from the serial line
pushbuffer(byte inStr) - handles copying the incoming data into a larger buffer to be processed and parsed

the main code you are interested is:

Code: Select all

bIncoming = true;
timeout = millis();
while ( bIncoming ) {
	// verify the timeout hasn't expired and we don't wait indefinitely for data
	if ( millis()-timeout > 100 ) {
		bIncoming = false;
	}
	
	// check if there is data available to be read from the serial port
	if (WIFI_SERIAL.available() > 0 ) {
		// .read() reads the available data from the serial port and passes it into the funciton
		// pushbuffer then copies the incoming data into a preallocated buffer and then
		// is processed or attempted to be processed.
		// this pattern is repeated until all the data is read from the serial line
		pushbuffer(WIFI_SERIAL.read());
		timeout=millis();
	}
}
WIFI_SERIAL is defined as either Serial or Serial1 based on the controller and what serial port you are reading the data from.

the static buffer that the data is read into is a "static char m_pushback[32];"
there could definitely be improvements with the reading and processing of the data, but that at least gives you a starting point.

everything should be pretty straightforward to follow. all the code i listed above is located in either RA_Wifi.cpp or RA_Wifi.h.
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: Looking for code to capture data going to Wifi

Post by lnevo »

There is code in the development libraries for touchscreen.
zveck
Posts: 12
Joined: Wed May 08, 2013 2:43 pm

Re: Looking for code to capture data going to Wifi

Post by zveck »

I'm not sure it's all that clear what i'm asking for so ill post the code I have that collects and parses the first 254 pieces of the string. With this I can parse up thru R3 data. The problem seems to be the next 254 pieces and beyond. the serial buffer limit seems to bite me in the butt. This code does not go into the Reef Angel, it is for a Mega 2560 that drives a touch panel.
20130113_124531_resized.jpg
20130113_124531_resized.jpg (172.04 KiB) Viewed 5489 times
20140514_155850_resized.jpg
20140514_155850_resized.jpg (148.35 KiB) Viewed 5489 times

Code: Select all

////start
char packet1[256]; // Allocate some space for the string
char inChar=-1; // Where to store the character read
byte indx = 0; // Index into array; where to store the character
char buf[4];

void setup()
{
  Serial.begin(57600);
  delay(1000);
}

void loop() {

  while (Serial.available() > 0) {  // Don't read unless you know there is data
    inChar = Serial.read(); // Read a character
    if(inChar == 'z') { goto decode1;}//eliminate spam header HTTP/1.1 200 OK Server: ReefAngel Cache-Control: no-store, no-cache, must-revalidate Pragma: no-cache Connection: close Content-Type: text/xml Content-Length: 602 
                // I get away with this because the z is the only z in the packet and is near the start of the valid data I want to collect <RA><ID>zveck</ID><T1>780</T1.......
      continue; 
      }
decode1:
   while (Serial.available() > 0) {   
     inChar = Serial.read(); // Read a character
       packet1[indx] = inChar; // Store it
        indx++; // Increment where to write next
         if(indx == 254) {
           packet1[indx] = '\0'; // Null terminate the string
           for (int i=0;i<254;i++) {
           Serial.print(packet1[i]);
           }
           indx = 0;
         }
   }
  
  delay(1000);

  /// parse 
  int a, b, length;
  a = (findString(packet1, "<T1>", 0));  // search for a value starting from beginning of packet
  b = (findString(packet1, "<", a));    // find the next '<' so we can extract the data
  length = b-a-1;  // calculate length of data
  strncpy(buf, packet1+a+1, length);  // copy data to our buffer
  buf[length] = '\0';  // add null to indicate end of string
  Serial.print("T1= ");
  Serial.println(buf);
 
  a = (findString(packet1, "<T2>", 0));  // search for a value starting from beginning of packet
  b = (findString(packet1, "<", a));    // find the next '<' so we can extract the data
  length = b-a-1;  // calculate length of data
  strncpy(buf, packet1+a+1, length);  // copy data to our buffer
  buf[length] = '\0';  // add null to indicate end of string
  Serial.print("T2= ");
  Serial.println(buf);
 
  a = (findString(packet1, "<T3>", 0));  // search for a value starting from beginning of packet
  b = (findString(packet1, "<", a));    // find the next '<' so we can extract the data
  length = b-a-1;  // calculate length of data
  strncpy(buf, packet1+a+1, length);  // copy data to our buffer
  buf[length] = '\0';  // add null to indicate end of string
  Serial.print("T3= ");
  Serial.println(buf);
   
  // int value = atoi(buf);  convert value to integer if you want to
  //  delay(1000);
  a = (findString(packet1, "<PH>", b));  // search for a value starting from last location
  b = (findString(packet1, "<", a));
  length = b-a-1;
  strncpy(buf, packet1+a+1, length);
  buf[length] = '\0';
  Serial.print("PH= ");
  Serial.println(buf); 
zveck
Posts: 12
Joined: Wed May 08, 2013 2:43 pm

Re: Looking for code to capture data going to Wifi

Post by zveck »

Well I gave up on the serial idea and went to ethernet to get the data via http://YOURIP:2000/r99 and used the TextFinder library to strip out the data and turn it into useful integers to be displayed on a larger screen.
zveck
Posts: 12
Joined: Wed May 08, 2013 2:43 pm

Re: Looking for code to capture data going to Wifi

Post by zveck »

lnevo wrote:There is code in the development libraries for touchscreen.
I'd sure like to see more info on how to use the code.
Post Reply