player 1

Dependencies:   4DGL-uLCD-SE PinDetect SparkfunAnalogJoystick mbed-rtos mbed SDFileSystem

Fork of 4180FinalLab by Rishi Bhargava

Wireless 2 Player Pong game

Committer:
Mpmart08
Date:
Fri Apr 29 00:51:56 2016 +0000
Revision:
10:b57b3fbf8266
Parent:
9:aa967c554d10
wifi stuff

Who changed what in which revision?

UserRevisionLine numberNew contents of line
fepie3 9:aa967c554d10 1 #include "huzzah.h"
fepie3 9:aa967c554d10 2 #include "mbed.h"
fepie3 9:aa967c554d10 3 #include "rtos.h"
fepie3 9:aa967c554d10 4
Mpmart08 10:b57b3fbf8266 5 Huzzah::Huzzah(char *enterssid, char *enterpassword, Serial *esp) {
fepie3 9:aa967c554d10 6 ssid = enterssid; // enter WiFi router ssid inside the quotes
fepie3 9:aa967c554d10 7 pwd = enterpassword;
fepie3 9:aa967c554d10 8 this->esp = esp;
fepie3 9:aa967c554d10 9 //ESP=esp; // enter WiFi router password inside the quotes
fepie3 9:aa967c554d10 10 }
fepie3 9:aa967c554d10 11 //set ESP baudrate if necessary
fepie3 9:aa967c554d10 12 void Huzzah::setbaudrate()
fepie3 9:aa967c554d10 13 {
fepie3 9:aa967c554d10 14 strcpy(snd, "AT+CIOBAUD=115200\r\n"); // change the numeric value to the required baudrate
fepie3 9:aa967c554d10 15 SendCMD();
fepie3 9:aa967c554d10 16 }
fepie3 9:aa967c554d10 17 //Configurate ESP to Station mode and set username and password
fepie3 9:aa967c554d10 18 void Huzzah::config()
fepie3 9:aa967c554d10 19 {
fepie3 9:aa967c554d10 20 strcpy(snd,".\r\n.\r\n");
fepie3 9:aa967c554d10 21 SendCMD();
fepie3 9:aa967c554d10 22 Thread::wait(1000);
fepie3 9:aa967c554d10 23
fepie3 9:aa967c554d10 24 strcpy(snd,"node.restart()\r\n");
fepie3 9:aa967c554d10 25 SendCMD();
fepie3 9:aa967c554d10 26 Thread::wait(3000);
fepie3 9:aa967c554d10 27
fepie3 9:aa967c554d10 28 strcpy(snd, "wifi.setmode(wifi.STATION)\r\n");
fepie3 9:aa967c554d10 29 SendCMD();
fepie3 9:aa967c554d10 30 Thread::wait(2000);
fepie3 9:aa967c554d10 31
fepie3 9:aa967c554d10 32 strcpy(snd, "wifi.sta.config(\"");
fepie3 9:aa967c554d10 33 strcat(snd, ssid);
fepie3 9:aa967c554d10 34 strcat(snd, "\",\"");
fepie3 9:aa967c554d10 35 strcat(snd, pwd);
fepie3 9:aa967c554d10 36 strcat(snd, "\")\r\n");
fepie3 9:aa967c554d10 37 SendCMD();
fepie3 9:aa967c554d10 38 Thread::wait(2000);
fepie3 9:aa967c554d10 39 }
fepie3 9:aa967c554d10 40
fepie3 9:aa967c554d10 41 void Huzzah::SendCMD()
fepie3 9:aa967c554d10 42 {
fepie3 9:aa967c554d10 43 esp->printf("%s", snd);
fepie3 9:aa967c554d10 44 }
fepie3 9:aa967c554d10 45
fepie3 9:aa967c554d10 46 void Huzzah::sendwebpage(int _highscore1, int _highscore2)
fepie3 9:aa967c554d10 47 {
fepie3 9:aa967c554d10 48 this->_highscore1=_highscore1;
fepie3 9:aa967c554d10 49 this->_highscore2=_highscore2;
fepie3 9:aa967c554d10 50
fepie3 9:aa967c554d10 51 strcpy(snd, "srv=net.createServer(net.TCP)\r\n");
fepie3 9:aa967c554d10 52 SendCMD();
Mpmart08 10:b57b3fbf8266 53 Thread::wait(500);
fepie3 9:aa967c554d10 54 strcpy(snd, "srv:listen(80,function(conn)\r\n");
fepie3 9:aa967c554d10 55 SendCMD();
Mpmart08 10:b57b3fbf8266 56 Thread::wait(500);
fepie3 9:aa967c554d10 57 strcpy(snd, "conn:on(\"receive\",function(conn,payload)\r\n");
fepie3 9:aa967c554d10 58 SendCMD();
Mpmart08 10:b57b3fbf8266 59 Thread::wait(500);
fepie3 9:aa967c554d10 60 strcpy(snd, "print(payload)\r\n");
fepie3 9:aa967c554d10 61 SendCMD();
Mpmart08 10:b57b3fbf8266 62 Thread::wait(500);
fepie3 9:aa967c554d10 63
fepie3 9:aa967c554d10 64 strcpy(snd, "conn:send(\"<!DOCTYPE html>\")\r\n");
fepie3 9:aa967c554d10 65 SendCMD();
Mpmart08 10:b57b3fbf8266 66 Thread::wait(500);
fepie3 9:aa967c554d10 67
fepie3 9:aa967c554d10 68 strcpy(snd, "conn:send(\"<html>\")\r\n");
fepie3 9:aa967c554d10 69 SendCMD();
Mpmart08 10:b57b3fbf8266 70 Thread::wait(500);
fepie3 9:aa967c554d10 71
fepie3 9:aa967c554d10 72 strcpy(snd, "conn:send(\"<h1> PVP Wireless Pong High Scores</h1>\")\r\n");
fepie3 9:aa967c554d10 73 SendCMD();
Mpmart08 10:b57b3fbf8266 74 Thread::wait(500);
fepie3 9:aa967c554d10 75
fepie3 9:aa967c554d10 76 sprintf(snd, "conn:send(\"<h2> Player 1 high score: %i, Player 2 High Score: %i </h2>\")\r\n",_highscore1, _highscore2);
fepie3 9:aa967c554d10 77 SendCMD();
Mpmart08 10:b57b3fbf8266 78 Thread::wait(500);
fepie3 9:aa967c554d10 79
fepie3 9:aa967c554d10 80 strcpy(snd, "conn:send(\"<h2> Great Job!</h2>\")\r\n");
fepie3 9:aa967c554d10 81 SendCMD();
Mpmart08 10:b57b3fbf8266 82 Thread::wait(500);
fepie3 9:aa967c554d10 83
fepie3 9:aa967c554d10 84 strcpy(snd, "conn:send(\"</html>\")\r\n");
fepie3 9:aa967c554d10 85 SendCMD();
Mpmart08 10:b57b3fbf8266 86 Thread::wait(500);
fepie3 9:aa967c554d10 87
fepie3 9:aa967c554d10 88 strcpy(snd, "end)\r\n");
fepie3 9:aa967c554d10 89 SendCMD();
Mpmart08 10:b57b3fbf8266 90 Thread::wait(500);
fepie3 9:aa967c554d10 91
fepie3 9:aa967c554d10 92 strcpy(snd, "conn:on(\"sent\",function(conn) conn:close() end)\r\n");
fepie3 9:aa967c554d10 93 SendCMD();
Mpmart08 10:b57b3fbf8266 94 Thread::wait(500);
fepie3 9:aa967c554d10 95
fepie3 9:aa967c554d10 96 strcpy(snd, "end)\r\n");
fepie3 9:aa967c554d10 97 SendCMD();
Mpmart08 10:b57b3fbf8266 98 Thread::wait(500);
fepie3 9:aa967c554d10 99
fepie3 9:aa967c554d10 100 }