ToolBox IoT

 

EEC 172 Open end lab : ToolBox IoT

Intro

The objective of this lab was to trigger a AWS rule to send an email to subscribers and perform actions based on the content of the email. The email content depends on the user selection using the the remote sensor and the OLED screen as the interface. The remote control sensor takes input and decodes the signal to a number value which are the selections for the actions. We have 5 different actions. We use the OLED as the interface for the user. In the main menu, the user can choose between the 5 actions:

(1) Add a song to a Spotify playlist

(2) Add an event to your phone calendar

(3) Open an issue to a Github (this project’s repository)

(4) Play the Connect 4 Game

(5) Posts on Facebook whether you win or computer wins: (a. Computer vs player: uses the alphabeta algorithm with heuristic score improvement b.Player vs player)

(6) Order a pizza from Domino’s to your current location

IMG_4668

We used the IFTTT app in order to have the app do most of the actions for us. For example, when the user chooses to add a song to his Spotify playlist, he would select that from the menu followed by the name of the song when prompted. This will trigger the AWS to send an email with that information with a “#spotify’ hashtag in the email. The email notification that you receive will trigger the app IFTTT to perform an action. The action in this case will be to look up the song name on Spotify to your playlist. The playlist is specified by the configurations on the IFTTT app the user has to download in the actions. The user specifies their email to receive the notification on the IFTTT as well as making 5 IFTTT actions that trigger the AWS to perform the actions. For more information on how IFTTT works, visit https://ifttt.com/

How the ToolBox works:

The devices needed in this project are Single launchpad IR receiver and a remote control OLED AWS Account Phone & Facebook Account

Background

IFTTT

If This Then That, also known as IFTTT, is a free web-based service to create chains of simple conditional-statements, called applets. An applet is triggered by changes that occur within other web services such as Gmail, Facebook, Telegram, Instagram, or Pinterest. (from wiki)

Minmax algorithm

Minimax (sometimes MinMax or MM) is a decision rule used in decision theory, game theory, statistics and philosophy for minimizing the possible loss for a worst case (maximum loss) scenario. When dealing with gains, it is referred to as “maximin”—to maximize the minimum gain. Originally formulated for two-player zero-sum game theory, covering both the cases where players take alternate moves and those where they make simultaneous moves, it has also been extended to more complex games and to general decision-making in the presence of uncertainty. (from wiki)

Alpha–beta pruning

A search algorithm that seeks to decrease the number of nodes that are evaluated by the minimax algorithm in its search tree. It is an adversarial search algorithm used commonly for machine playing of two-player games (Tic-tac-toe, Chess, Go, etc.). It stops completely evaluating a move when at least one possibility has been found that proves the move to be worse than a previously examined move. Such moves need not be evaluated further. When applied to a standard minimax tree, it returns the same move as minimax would, but prunes away branches that cannot possibly influence the final decision. (from wiki)

The algorithm maintains two values, alpha and beta, which represent the minimum score that the maximizing player is assured of and the maximum score that the minimizing player is assured of respectively. Initially alpha is negative infinity and beta is positive infinity, i.e. both players start with their worst possible score. Whenever the maximum score that the minimizing player(beta) is assured of becomes less than the minimum score that the maximizing player(alpha) is assured of (i.e. beta <= alpha), the maximizing player need not consider the descendants of this node as they will never be reached in actual play. (from wiki)

Heuristic improvements

Using ordering heuristics to search parts of the tree that are likely to force alpha–beta cutoffs early. For example, in chess, moves that capture pieces may be examined before moves that do not, or moves that have scored highly in earlier passes through the game-tree analysis may be evaluated before others. Another common, and very cheap, heuristic is the killer heuristic, where the last move that caused a beta-cutoff at the same level in the tree search is always examined first. This idea can also be generalized into a set of refutation tables. (from wiki)

Goals

Part I: Create conditional statements using the IFTTT app

Create conditional statement so Spotify song will add to playlist if email received with ‘#Spotify’ Create conditional statement so Google Calendar will make event with today’s date when if email received with ‘#Calendar’ Create conditional statement so Github will create issue if email received with ‘#github’ Create conditional statement so Facebook will post if email received with ‘#FB’ Create conditional statement so Domino will create order if email received with ‘#Pizza’

Part II: Decode the remote sensor signal for the button pressed

As in lab 3, create a decoder that takes in frequency signal from the remote sensor and interprets the button pressed. Translate these button presses into characters depending on the length periods of the times between presses.

Part III: Create the AWS shadow, certificates/keys for network connection, Add subscribers

Make a shadow using the AWS IoT module for the CC3200 Launchpad Download the certificates/keys for the CC3200 Launchpad to be able to connect the network Add subscriber email where emails will be sent to when CC3200 sends a JSON to the cloud

Par IV: Write the outer program

Prints main menu to allow user to select which action to do out of the 5 Detects inputs from user and triggers a AWS rule

Part V: Write program for the Connect 4

Write program for player vs player Write program for player vs computer

Methods

Part I: Create conditional statements using the IFTTT app

Spotify:

If email received with ‘#Spotify’, take the content of email and use to query the Spotify search for the song and add that to the Playlist specified in the IFTTT (note: the Spotify account has to enable permissions) Google Calendar: If email received with ‘#Calendar’, take the content of the email and use it as the content of the new event. Creates the event an add to today’s date on Google Calendar (note: the Gmail account has to enable this permission)

Github:

If email received with ‘#github’, take the content of the email and use it as the Github’s issue content. Creates the issue on this project’s repository under the user specified in the IFTTT action (note: the Github user has to enable this action)

Connect 4 Game:

If email received with ‘#FB’, take the content of the email and use it as the content of the Facebook post. Specify the Facebook account that you want to post on (note: the Facebook account has to enable permissions for IFTTT to make posts)

Order Domino’s pizza:

If email received with ‘#Pizza’, trigger Domino’s app on phone connected to IFTTT to order a pizza. (note: the user has to input their credit card information on the Domino’s app)

Part II: Remote Control Sensor Decode Signals

detectSignal()

decodeButton(): additions to this function: when the button detected is 11, send the AWS cloud a JSON with whatever string is in the argument. (this includes ‘#github’, ‘#Pizza’, ‘#FB’, etc…) This JSON with that string will be included in the email along any information in the buffer that the user has inputted. (for example: song name for Spotify stored in ). decodeBinary()

decodeChar()

All these functions and their explanations can be found in Lab 3.

Part III: Create the AWS shadow, certificates/keys for network connection, Add subscribers

Connecting to AWS (securely) and updating your Shadow and download security keys and certificates First, we need to set up an AWS account and then login to the console. Create a Device Thing/Shadow, and then generate the certificate keys. Copy all certificate keys to notepad or download all of the required keys. Activate the security key. The security keys you saved are in .pem extension convert it into .der extension using the command line and OpenSSL by the following command: For the Root Certificate Authority (rootCA) file and the certificate file >openssl x509 -outform der -in ~\rootCA.pem -out ...\rootCA.der >openssl x509 -outform der -in ~\certificate.pem.crt -out ~\client.der For the Private Key File: >openssl rsa -outform der -in ~\private.pem.key -out ~\private.der Use Uniflash to attach the keys to the LaunchPad. Configure the POST function to accept message then use it to post some message onto the AWS. Create an GET function to get information from the AWS. AWS is using SSL/TLS encryption scheme, and we use 8443 as our port to do the connection. We also use IFTTT as a helper to send message and post on different end points. It is also using ssl/tls encryption scheme, and its port number is 443. The way we get certificate is to go to https://certs.godaddy.com/repository/ and download the file with the name Go Daddy Root Certificate Authority – G2 Add subscribers’ email in the AWS using SNS (see lab 5)

Part IV:

Functions:

Spotify(): prompts on OLED to asks for the song name on and stores in buffer

Calendar(): prompts on OLED the event content and stores in buffer

Github(): prompts on OLED the issue content and stores in buffer

Pizza(): prompts on OLED ask for confirmation.

Game(): prompts the OLED to ask for moves until win / draw detected.

All these functions call the decodeButton() to get the buttons pressed and also sends an AWS to trigger a rule to send an email with hashtag string and buffer information.

We use SPI to connect the launchpad with OLED, and get input from IR Receiver. We use http_post to send message to AWS and then AWS will give feedback through SNS to email address by email. We build several small applets on IFTTT that once we receive an email in certain email account with certain hashtag(#), it will perform different actions.

Part V: Connect 4 Game

For two players: Functions: int check_won(): this function iterates through the board’s current state specified in the 2x2 global array moves[ ][ ]and checks if there is a win after every move made, either by either player or the computer for computer vs player. It returns 1 if a win is detected, 0 otherwise. It checks the win by looking for any consecutive 4 horizontal moves belonging to the same player, as well as for vertical moves, left-to-right diagonals and right-to-left diagonals.

void connect42P(): Infinite while loop that detects the next move that the player is chooses using the remote control. Calls decodeButton() for this. When a valid column is pressed, update the state of the board which is stored in the 2x2 array moves[ ][ ] and display the new move on the Connect 4 board on the OLED screen in the appropriate location.

void Game() function is the outer shell of the Connect4 program. It prompts the user and asks whether they would like to play in player vs player mode or computer vs player mode. It displays the Connect4 Board on the OLED screen in the beginning when the game starts. When there is a win, it will send trigger a rule on the AWS that will send an email to the subscribers to the AWS. This email will include a “#FB” hashtag with the winning post string that will trigger the IFTTT app to post the content to the connected Facebook account. For player versus computer:

Functions:

Int evaluateMove(): This function will return a int representing the computer next move. First, this function will find all the possible steps. Then try to drop on those possible steps to see if it will win or lose immediately. That’s how it define the winning and blocking strategies. If it neither will win or lose in next move. Then it will calculate the alpha score on each position by calling alphabeta() and choose the step with the highest alpha score as it best move.

int alphabeta(int alpha, int beta, int depth, int player, int row, int col) // alpha-beta algorithm: This function is used to calculate the alpha score. We define the MAX_DEPTH of this algorithm to 8 so it won’t run out of memory. In this function, we keep iterating all the possible steps. We check the current player, if is human playing, we try minimize its score, and if it is AI playing, we try maximize its score. Calling this function recursively with increment of current depth till it reach the MAX_DEPTH. In the base case, we use the Heuristic to improve the performance without losing accuracy. The function is getHeuristicScore();

int getHeuristicScore(depth, col): Return the heuristic score based on current position. It calculate: comCount and p1Count which indicates how many pieces are already connected for AI and player1, the unit score by calling getUnitScore() for all possible next move on each row, col, and two diagonals.

int getUnitScore(player, p1Count, comCount): This function return the unit score calculate based on raw_score predefined.

Overall, the AI algorithm try to mimic the human players’ move and play against it till reach the MAX_DEPTH and return the best move based on the score which calculate in the process. After the game finished, a message will be post to facebook which using IFTTT.

Discussion

The biggest challenge we faced was the alpha-beta algorithm. None of us have ever had any experience or related knowledge about AI. So we read a lot of articles and documents to implement this algorithm. The next thing is the processor is not strong enough on CC3200 to support the full min-max algorithm and the memory space is not enough to do all the calculation. So we improved the original min-max to alpha-beta and use heuristic to improve the performance. For the memory space, instead of making copy of arrays, variables, we keep using the same variable, and use two variables ‘row’ and ‘col’ to recode the current change to the board. After each ‘trying’, we undo those changes to the board. That’s how to reduce the memory consumption to minimum. It is hard to organize all the codes and design the code flow.

Improvement

We can let user to choose the AI difficulty level. By let user input the MAX_DEPTH. We can reduce the memory consumption by using bitmap to represent the game board array. We can further shrunk down our code size and refine the process. The algorithm can been improved further for better performance and response time.

Conclusion

Lab 6 is a challenge to us since we need to use all of the knowledge we have learned in the previous labs. It is also hard for us to make this decision from that. We also learning new technical and algorithm in this lab. We used the OLED screen as the user interface, the remote sensor for interaction from the user, the AWS to send emails to subscribers and IFTTT to detect and read emails and perform an action based on hashtag content in email.