Chris Roberts
Published

WiFi Vocabulary Builder

Use this WiFi Vocabulary Builder to figure out flashy new words to work into every day conversation!

BeginnerFull instructions provided1,520
WiFi Vocabulary Builder

Things used in this project

Story

Read more

Code

randomWord.ino

C/C++
Used to run the randomWord project!
#include <SPI.h>
#include <WiFi.h>
#include <WiFiClient.h>

#include <Temboo.h>
#include "TembooAccount.h" // Contains Temboo account information

#if defined(ENERGIA) // LaunchPad MSP430, Stellaris and Tiva, Experimeter Board FR5739 specific
#include "Energia.h"
#else // error
#error Platform not defined
#endif

#include "SPI.h"
#include "Screen_HX8353E.h"
Screen_HX8353E myScreen;

WiFiClient client;

// The number of times to trigger the action if the condition is met
// We limit this so you won't use all of your Temboo calls while testing
int maxCalls = 50;

// The number of times this Choreo has been run so far in this sketch
int calls = 0;

//Define switch1 as pin 33, found on the datasheet
#define s1 33
//Define switch2 as pin 32
#define s2 32
//Initialize a button state variable, used to debounce pin 33 since it is shared with wifi uart
boolean lastButtonState = 1;

//Initialize strings to store the word in and the definition
String wordIn = "";
String definition = "";

//Initialization needed for the LCD screen
uint16_t x, y, x00, y00;

void setup() {
  Serial.begin(9600);
  //Initialize the onboard lcd screen
  myScreen.begin();
  x00 = 0;
  y00 = 0;
  myScreen.clear();

  int wifiStatus = WL_IDLE_STATUS;

  // Determine if the WiFi Shield is present
  Serial.print("\n\nShield:");
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("FAIL");

    // If there's no WiFi shield, stop here
    while(true);
  }

  Serial.println("OK");
  midPrint("WiFi Status...");

  // Try to connect to the local WiFi network using credentials provided in TembooAccount.h
  while(wifiStatus != WL_CONNECTED) {
    Serial.print("WiFi:");
    wifiStatus = WiFi.begin(WIFI_SSID, WPA_PASSWORD);

    if (wifiStatus == WL_CONNECTED) {
      midPrint("OK");
      Serial.println("OK");
    } 
    else {
      midPrint("Connecting...");
      Serial.println("FAIL");
    }
    delay(5000);
  }

  //WiFi.startSmartConfig(); //If user wants to have real time wifi configuration, include this line and use the TI Wifi Starter app available on the app store

  // Initialize pins
  pinMode(s1, INPUT_PULLUP);
  pinMode(s2, INPUT_PULLUP);


  //Print instructions to the LCD screen
  myScreen.clear();
  myScreen.gText(x00,0,"S1 to Retrieve");
  myScreen.gText(x00,10,"S2 for Definition");
  Serial.println("Setup complete.\n");
}

void loop() {
  int getWord = digitalRead(s1);
  int showDefinition = digitalRead(s2);

  //Check if user wants to get a new word, and debounce button
  if (getWord == 0 && getWord == lastButtonState) {
    if (calls < maxCalls) {
      definition = "";
      wordIn = "";
      midPrint("Getting Random Word!");
      wordIn = runRandomWord();
      if(wordIn.startsWith("TTP_CODE")||wordIn.startsWith("0")){
        wordWrap("Word Lookup Failed.  Please Try Again!");
      }
      else{
        Serial.println("Received Word = " + wordIn);
        definition = runGetSearchResult(wordIn);
        Serial.println(definition);
        midPrint(wordIn);
      }
      calls++;      
    } 
    else {
      wordWrap("Skipping to save Temboo calls. Adjust maxCalls as required.");
    }
  }

  //Check if user wants to show the definition
  else if(showDefinition == 0){
    if(definition == ""){//error if user has not looked up a word or if wolfram lookup fails
      wordWrap("No Definition Found. Try again!");

    }
    else if (definition.startsWith("TTP_CODE")){
      wordWrap("Definition Lookup Failed.  Please Try Again!");
    }
    else {
      wordWrap(definition);
    }    
  }
  lastButtonState = getWord;
  delay(50);
}


//runRandomWord runs the random word choreo to retrieve a random word from wordnik
String runRandomWord() {
  //place holder used to store word as returned from wordnik
  String temp = "";
  //output used to store parsed word from the temp variable
  String output = "";

  TembooChoreo RandomWordChoreo(client);

  // Set Temboo account credentials
  RandomWordChoreo.setAccountName(TEMBOO_ACCOUNT);
  RandomWordChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME);
  RandomWordChoreo.setAppKey(TEMBOO_APP_KEY);

  // Set profile to use for execution
  RandomWordChoreo.setProfile("XXXXXXX");                //Enter your Wordnik Profile Name here!

  // Set Choreo inputs
  String HasDefinitionValue = "true";
  RandomWordChoreo.addInput("HasDefinition", HasDefinitionValue);
  String MinDictionariesValue = "5";
  RandomWordChoreo.addInput("MinDictionaries", MinDictionariesValue);


  // Identify the Choreo to run
  RandomWordChoreo.setChoreo("/Library/Wordnik/Words/RandomWord");

  // Run the Choreo
  unsigned int returnCode = RandomWordChoreo.run();
  Serial.println("random word returnCode = " + String(returnCode));

  // Read and print the error message
  while (RandomWordChoreo.available()) {
    char c = RandomWordChoreo.read();
    temp += c;
    //Used to parse the wordnik return, deletes the sring every time a semi colon is encountered since word desired follos the last semicolon
    if(c==':'){ 
      temp = "";
    }
  }
  RandomWordChoreo.close();
  //Further parse the random word out and store the value in the variable output so that only the word remains
  char* s = &temp[1];
  while(*s != '"' && *s != '\n'){
    output += *s;
    s++;
  }
  //return only the word
  return(output);
}

//GetSearchResult sends the random word to wolfram alpha and receives back the first definition
String runGetSearchResult(String input) {
  String temp = "";
  String output = "";
  TembooChoreo GetSearchResultChoreo(client);

  // Invoke the Temboo client
  GetSearchResultChoreo.begin();

  // Set Temboo account credentials
  GetSearchResultChoreo.setAccountName(TEMBOO_ACCOUNT);
  GetSearchResultChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME);
  GetSearchResultChoreo.setAppKey(TEMBOO_APP_KEY);

  // Set Choreo inputs
  String InputValue = "define "+input;
  GetSearchResultChoreo.addInput("Input", InputValue);
  String AppIDValue = "XXXXXXXX";                       //Enter your Wolfram AppID Here!
  GetSearchResultChoreo.addInput("AppID", AppIDValue);

  // Identify the Choreo to run
  GetSearchResultChoreo.setChoreo("/Library/WolframAlpha/GetSearchResult");

  // Run the Choreo; when results are available, print them to serial
  unsigned int returnCode = GetSearchResultChoreo.run();
  Serial.println("definition returnCode = " + String(returnCode));

  //Stores full return value in a temp variable
  while(GetSearchResultChoreo.available()) {
    char d = GetSearchResultChoreo.read();
    temp += d;
  }
  GetSearchResultChoreo.close();
  //finds index of the word which indicates the definition follows on the next line
  int i = temp.indexOf("Result");
  //Finds the index of the line containing the definition and stores it in 's'
  int s = temp.indexOf('\n',i)+2;
  //Starting at the index 's', write the entire line to the variable output
  while(temp[s] != '\n'){
    output += temp[s];
    s++;
  }
  //Trim away any unwanted spaces and characters
  output.trim();
  //Return the output containing just the definition as a string
  return(output);
}

//Used to print a string in the middle of the screen
void midPrint(String input){
  myScreen.clear();
  myScreen.gText((myScreen.screenSizeX()-input.length()*myScreen.fontSizeX())/2,myScreen.screenSizeY()/2,input);
}

//Used to wordWrap a string on the LCD, maxChars is the number of characters to write per line
void wordWrap(String input){  
  String line = "";
  int s = 0;
  int maxChars = 20;
  y = 0;
  boolean lastLine = false;
  boolean wrap;
  myScreen.clear();
  while(lastLine != true){
    wrap = false;
    line = "";
    for(int i = s;i<s+maxChars;i++){
      if (input[i] == '\0'){
        lastLine = true;
      }
      else if (input[i] == ' '){
        wrap = true;
      }
    }
    if(lastLine == true){
      while(input[s] != '\0'){
        line += input[s];
        s++;
      }
    }
    else if (wrap == true){
      for(int i = s;i<input.lastIndexOf(" ",s+maxChars);i++){
        line += input[i];
      }
      s = input.lastIndexOf(" ",s+maxChars)+1;
    }
    else{
      for (int i = s; i<s+maxChars;i++){
        line += input[i];
      }
      line += '-';
      s += maxChars;
    }
    myScreen.gText(x00,y,line);
    y += 10;
  }
}

Credits

Chris Roberts

Chris Roberts

8 projects • 18 followers
I am an applications engineer with Texas Instruments working with the Launchpad by trade, and a maker by passion!

Comments

Add projectSign up / Login