Go Down

Topic: Replacing Processing internally within Arduino (Read 748 times) previous topic - next topic

evanschrs

Feb 20, 2013, 06:04 pm Last Edit: Feb 21, 2013, 01:08 am by Coding Badly Reason: 1
Hi,

I currently have a working model which displays data from a webpage on a LED matrix, this uses an Arduino Mega connected to an LED Matrix and a computer, via Serial Processing on the computer gets the data online then sends to the arduino which displays it on the LED matrix.

This works great.

However my next mission is to get it to be self contained, I am able to get the data from the webpage using just the arduino (with wifi shield) and display it on the Matrix, however when I try to get the data online again it breaks the matrix.

I am looking for any other ways to get a very small amount of data from a webpage than the one I am using which is defiantly not working. See below for my code:


Code: [Select]
/*
*Light Plinth

Circuit additions:
* WiFi shield
* AdaFruit 32x32 LED Matrix

Created 20 Feb 2013
By Chris Evans; Wonder Room
*/

// LED Matrix Libraries.
#include <Adafruit_GFX.h>   // Core graphics library
#include <RGBmatrixPanel.h> // Hardware-specific library
// Wifi Library
#include <WiFi.h>
#include <SPI.h>
////////////////////////////////////////////////////////////////////////
// Matrix Pin Layout:
//#define CLK 10
//#define OE  11
//#define LAT 12
#define CLK 10
#define OE  11
#define LAT 12
#define A   A8
#define B   A9
#define C   A10
#define D   A11
RGBmatrixPanel matrix(A, B, C, D, CLK, LAT, OE, false);
////////////////////////////////////////////////////////////////////////
//Variable Set Up:
String pageValue;
String tempValue;
int num1 = 3;


//Wifi Set up //////////////////////////////////////////////////////////
     /* Home Set Up:
     char ssid[] = "_";     //  your network SSID (name)
     char pass[] = "thisistheshit";  // your network password
     */
char ssid[] = "3MobileWiFi-3c77";     //  your network SSID (name)
char pass[] = "01269109";  // your network password
int status = WL_IDLE_STATUS;     // the Wifi radio's status

WiFiClient client;

// Configure Website Reading ///////////////////////////////////////////
char inString[32]; // string for incoming serial data
int stringPos = 0; // string index counter
boolean startRead = false; // is reading?

byte server[] = {
 173,254,28,46 }; //ip Address of the server you will connect to

//The location to go to on the server
//make sure to keep HTTP/1.0 at the end, this is telling it what type of file it is
String weblocal = "/~evansch1/arduinoadmin/fbconn.php?uid=wonderroomarduino HTTP/1.0"; // Arduinos Unique ID Goes here!


void setup() {
 //Initialize serial and wait for port to open:
 Serial.begin(9600);
 while (!Serial) {
   ; // wait for serial port to connect. Needed for Leonardo only
 }

 // check for the presence of the shield:
 if (WiFi.status() == WL_NO_SHIELD) {
   Serial.println("WiFi shield not present");
   // don't continue:
   while(true);
 }

 // attempt to connect to Wifi network:
 while ( status != WL_CONNECTED) {
   Serial.print("Attempting to connect to WPA SSID: ");
   Serial.println(ssid);
   // Connect to WPA/WPA2 network:    
   status = WiFi.begin(ssid, pass);

   // wait 10 seconds for connection:
   delay(10000);
 }
 Serial.print("You're connected to the network");
 tempValue = connectAndRead();
 Serial.println("tempValue = " + tempValue);
 matrix.begin();
 loading();
}

void loop() {
  matrix.fillScreen(matrix.Color333(0, 0, 0));
  matrix.setCursor(7, 4);    // start in the middle
  matrix.setTextSize(1);     // size 1 == 8 pixels high
  matrix.setTextWrap(true);
  matrix.setTextColor(matrix.Color333(7,7,7));
  matrix.println(tempValue);
  drawHand();
  delay(10000);
  matrix.fillScreen(matrix.Color333(0, 0, 0));
  tempValue = connectAndRead();
}


// Get Data From FB //////////////////////////////////////////////////////
String connectAndRead(){
 //connect to the server

 Serial.println("reading page...");

 //port 80 is typical of a www page
 if (client.connect(server, 80)) {
   Serial.println("page found.");
   client.print("GET ");
   client.println(weblocal);
   client.println();

   //Connected - Read the page
   return readPage(); //go and read the output

 }
 else {
   return "reading failed";
 }
} // End connectAndRead //

String readPage(){
 //read the page, and capture & return everything between '[' and ']'
 stringPos = 0;
 memset( &inString, 0, 32 ); //clear inString memory

 while(true){

   if (client.available()) {
     char c = client.read();

     if (c == '[' ) { //'[' is our begining character
       startRead = true; //Ready to start reading the part
     }
     else if(startRead){

       if(c != ']'){ //']' is our ending character
         inString[stringPos] = c;
         stringPos ++;
       }
       else{
         //got what we need here! We can disconnect now
         startRead = false;
         client.stop();
         client.flush();
         Serial.println("disconnecting.");
         return inString;
       }
     }
   }
 }
} // End readPage //



Moderator edit: [code] [/code] tags added.

PaulS

Quote
however when I try to get the data online again it breaks the matrix.

What does this mean?

Code: [Select]
String pageValue;
String tempValue;

Stop this!

Code: [Select]
String weblocal = "/~evansch1/arduinoadmin/fbconn.php?uid=wonderroomarduino HTTP/1.0"; // Arduinos Unique
And this.

Use char arrays and pointers, instead.

Code: [Select]
String readPage(){
          return inString;
        }
      }
    }
  }
} // End readPage //

This is REALLY ridiculous - taking a global char array containing the data and wrapping it in a String object just so that the function can have something to return.

evanschrs

Hi, thanks for the reply,

Apologies, when the Arduino connects online the Matrix basically loses its current display and flickers random lines of lights on and off all over.

Thanks I will change to char arrays, and look into pointers.


Sorry can you elaborate on this, I am quite new to coding Arduino so am not totally sure what you mean:

"This is REALLY ridiculous - taking a global char array containing the data and wrapping it in a String object just so that the function can have something to return."

PaulS

Code: [Select]
#define CLK 10
#define OE  11
#define LAT 12
#define A   A8
#define B   A9
#define C   A10
#define D   A11
RGBmatrixPanel matrix(A, B, C, D, CLK, LAT, OE, false);

You have your matrix connected to the SPI pins that the Arduino uses to communicate with the Ethernet shield.

Quote
Sorry can you elaborate on this, I am quite new to coding Arduino so am not totally sure what you mean:

"This is REALLY ridiculous - taking a global char array containing the data and wrapping it in a String object just so that the function can have something to return."

You have a function, readPage() that collects data into a global array, inString. Does that function need to return anything, when the data is already in a global place? No, it does not. But, you have it wrap the data in an instance of a class that is known to have problems.

That function is only called by connectAndRead(), which doesn't do anything with the data that was read. It simply returns the object that was created to wrap the global data.

The connectAndRead() function is called in two places. Each of them simply prints the data that is returned somewhere (to the serial port or to the matrix). In both of those cases, the data has to be extracted from the wrapper, so the overhead of passing global data wrapped in a leaky mess is completely unnecessary.

Go Up