Problem combining RN-XV WiFly Module and Nokia 5110 LCD to display tweets.

Hi!

I’m building a project that displays a random tweet from a server to a small LCD. I’m using an Arduino Uno (although I would like to use a Pro Mini eventually) with a RN-XV WiFly Module https://www.sparkfun.com/products/10822 and a Nokia 5110 LCD http://www.seeedstudio.com/depot/nokia-5110-lcd-module-p-1566.html.

I’ve written a simple PHP script that returns a tweet in this format:

!STARTOFTWEET!This is just a simple test tweet!ENDOFTWEET!

The !STARTOFTWEET! and !ENDOFTWEET! have been added to easily seperate the tweet from the HTTP header information that is returned by the Wifly.

I’ve been able to talk to the server and store the tweet in a String without any problems. I’ve then added a bit of code to format the tweet for use with the LCD to avoid words being cut off, etc. This all works fine if I output the result to the Serial monitor.

The LCD part is giving me headaches, however.

For some reason, I’m unable to display the tweet on the LCD. If I uncomment the code that writes to the LCD, the String containing the tweet only contains a small part of the actual tweet. By uncommenting the LCD part, I’m apparently disrupting the serial data transfer between the wifly and the arduino, causing the data to be corrupt. But I don’t understand how. It works fine if I just output the re-formatted tweet to the serial monitor.

// Initialize Wifly. Based on WiflyHQ library examples.

#include <WiFlyHQ.h>
#include <SoftwareSerial.h>
SoftwareSerial wifiSerial(9,10);

WiFly wifly;

/* Change these to match your WiFi network */

const char mySSID[] = "mySSID";
const char myPassword[] = "myPassword";

const char site[] = "mysite.com";

void terminal();

// Initialize LCD. Based on http://playground.arduino.cc/Code/PCD8544

#define PIN_SCE   7
#define PIN_RESET 6
#define PIN_DC    5
#define PIN_SDIN  4
#define PIN_SCLK  3

#define LCD_C     LOW
#define LCD_D     HIGH

#define LCD_X     84
#define LCD_Y     48

static const byte ASCII[][5] =
{
 
// I've left this part out; because it exceeded the forum's maximum number of characters and isn't really important.
// The missing code can be found here: http://playground.arduino.cc/Code/PCD8544

}

void LcdString(char *characters)
{
  
  while (*characters) {
    LcdCharacter(*characters++);
  } 
}

void LcdWrite(byte dc, byte data)
{
  digitalWrite(PIN_DC, dc);
  digitalWrite(PIN_SCE, LOW);
  shiftOut(PIN_SDIN, PIN_SCLK, MSBFIRST, data);
  digitalWrite(PIN_SCE, HIGH);
}



// Inititialize variables

String line = "";
String myTweet = "";
int lastSpace = 0;
int stringLength = 0;
int lineCounter = 0;  

int firstTime = 0;
String myResult = "";
int parseString = 0;

void setup(void)
{
  
    LcdInitialise();
    LcdClear();
  
    //char buf[32];

    Serial.begin(115200);
    Serial.println("Starting");

    wifiSerial.begin(9600);
    if (!wifly.begin(&wifiSerial, &Serial)) {
        Serial.println("Failed to start wifly");
	terminal();
    }

    /* Join wifi network if not already associated */
    if (!wifly.isAssociated()) {
	/* Setup the WiFly to connect to a wifi network */
        
	Serial.println("Joining network");
	wifly.setSSID(mySSID);
	wifly.setPassphrase(myPassword);
	wifly.enableDHCP();

	if (wifly.join()) {
	    Serial.println("Joined wifi network");
	} else {
	    Serial.println("Failed to join wifi network");
	    terminal();
	}
    } else {
        Serial.println("Already joined network");
    }

    //terminal();

    wifly.setDeviceID("Wifly-WebClient");
 
    if (wifly.isConnected()) {
        Serial.println("Old connection active. Closing");
	wifly.close();
    }

    if (wifly.open(site, 80)) {
        Serial.print("Connected to ");
	Serial.println(site);

        
    } else {
        Serial.println("Failed to connect");

    }
}

void loop() {

 
 while (wifly.available() > 0) {
   
     char ch = wifly.read();
     Serial.write(ch);

      if (parseString > 0) {
      myResult = myResult + ch;
      }
	if (ch == '!') {
            parseString = 1;
	}

  } 
  
  if (Serial.available() > 0) {
    
      wifly.write(Serial.read());
  }

  
  if (firstTime < 1) {  
  wifly.println("GET /myfile.php HTTP/1.1");
  wifly.println("Host: mysite.com");
  wifly.println();
  
  firstTime = 1;
  }
  
  if (myResult.length() > 10) {
    
  int startoftweet = myResult.indexOf('!STARTOFTWEET!');
  int endoftweet = myResult.lastIndexOf('!ENDOFTWEET!');
  
  startoftweet = startoftweet + 1;
  endoftweet = endoftweet - 11;
  
  myTweet = myResult.substring(startoftweet,endoftweet);


while (myTweet.length() > 0) {
  
  line = myTweet.substring(0,13);

    if (line.substring(12,13) != "") {
  
      lastSpace = line.lastIndexOf(' '); 
      
      
      if (lastSpace < 1) {
        lastSpace = 12;  
      }
      
      line = line.substring(0,lastSpace);
  
    } 
      
  
 if (lineCounter < 6) {
    
   gotoXY(0,lineCounter);
   lineCounter++;
 } else {
 
   // Clear the LCD, reset counter  
   LcdClear();
  lineCounter = 0;
  delay(5000);
 }
  
  myTweet = myTweet.substring(line.length(), myTweet.length()); 

  if (line.substring(0,1) == " ") {
    line = line.substring(1,line.length());  
  } 
  
   // Display the line on the LCD
  
  char TempTweet[line.length() + 1];
  line.toCharArray(TempTweet, line.length() + 1);  

  Serial.println(TempTweet);
  
  // If I comment this one out, it will show the tweet in the serial monitor. If I don't, the tweet is reduced to a '!'	
  LcdString(TempTweet);

}

  
  delay(5000);
 
  }
}


/* Connect the WiFly serial to the serial monitor. */
void terminal()
{
    while (1) {
	if (wifly.available() > 0) {
	    Serial.write(wifly.read());
	}


	if (Serial.available() > 0) {
	    wifly.write(Serial.read());
	}
    }
}

Additional info:

WiflyHQ Library: GitHub - harlequin-tech/WiFlyHQ: WiFly RN-XV Arduino Library
LCD Sample code: http://playground.arduino.cc/Code/PCD8544

Thanks very much for your help.