Program to test WirelessInterface library.

Dependencies:   WirelessInterface mbed

Committer:
gboggs3
Date:
Thu Apr 28 22:09:57 2016 +0000
Revision:
1:c7cfaf8c38f2
Parent:
0:6b892e818f10
Updated wifi communication;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gboggs3 0:6b892e818f10 1 #include "mbed.h"
gboggs3 0:6b892e818f10 2 #include "WirelessInterface.h"
gboggs3 0:6b892e818f10 3 //Wireless Interface contains include files for the Huzzah and BT Friend
gboggs3 0:6b892e818f10 4 #include <string>
gboggs3 0:6b892e818f10 5
gboggs3 0:6b892e818f10 6 //Debug serial port
gboggs3 0:6b892e818f10 7 RawSerial pc(USBTX, USBRX);
gboggs3 0:6b892e818f10 8
gboggs3 1:c7cfaf8c38f2 9 WirelessInterface cmd(p28, p27, p26, pc, 9600); //Comment this out to manually configure each device individually
gboggs3 0:6b892e818f10 10 //Huzzah cmd(p28,p27,p26,pc);
gboggs3 0:6b892e818f10 11 //BTFriend cmd(p28,p27,p26,pc);
gboggs3 0:6b892e818f10 12 DigitalOut sel(p22);
gboggs3 0:6b892e818f10 13 InterruptIn pb(p18);
gboggs3 1:c7cfaf8c38f2 14 DigitalOut led1(LED1);
gboggs3 1:c7cfaf8c38f2 15 DigitalOut led2(LED2);
gboggs3 1:c7cfaf8c38f2 16 RawSerial dev = cmd.getLocalSerial();
gboggs3 1:c7cfaf8c38f2 17
gboggs3 1:c7cfaf8c38f2 18
gboggs3 1:c7cfaf8c38f2 19 bool command(const char *cmd)
gboggs3 1:c7cfaf8c38f2 20 {
gboggs3 1:c7cfaf8c38f2 21 char result[240];
gboggs3 1:c7cfaf8c38f2 22 strcpy(result,cmd);
gboggs3 1:c7cfaf8c38f2 23 strcat(result,"\r\n");
gboggs3 1:c7cfaf8c38f2 24 //pc.printf(result);
gboggs3 1:c7cfaf8c38f2 25 led2 = !led2;
gboggs3 1:c7cfaf8c38f2 26 wait(0.250);
gboggs3 1:c7cfaf8c38f2 27 for (int i = 0; i<strlen(result); i++)
gboggs3 1:c7cfaf8c38f2 28 {
gboggs3 1:c7cfaf8c38f2 29
gboggs3 1:c7cfaf8c38f2 30 dev.putc(result[i]);
gboggs3 1:c7cfaf8c38f2 31 //pc.putc(result[i]);
gboggs3 1:c7cfaf8c38f2 32 }
gboggs3 1:c7cfaf8c38f2 33 wait(0.250);
gboggs3 1:c7cfaf8c38f2 34 return true;
gboggs3 1:c7cfaf8c38f2 35 }
gboggs3 0:6b892e818f10 36
gboggs3 0:6b892e818f10 37 //Interrupt function to change the selected device to communicate with
gboggs3 0:6b892e818f10 38 void changeDevice()
gboggs3 0:6b892e818f10 39 {
gboggs3 0:6b892e818f10 40 sel = !sel;
gboggs3 0:6b892e818f10 41
gboggs3 0:6b892e818f10 42 char selDev[256];
gboggs3 0:6b892e818f10 43 if(!sel)
gboggs3 0:6b892e818f10 44 sprintf(selDev, "Huzzah WiFi Module");
gboggs3 0:6b892e818f10 45 else
gboggs3 0:6b892e818f10 46 sprintf(selDev, "Bluetooth Module");
gboggs3 0:6b892e818f10 47
gboggs3 0:6b892e818f10 48 pc.printf("Communicating with: '%s'\r\n", selDev);
gboggs3 0:6b892e818f10 49 wait(1); //Debounce
gboggs3 0:6b892e818f10 50 }
gboggs3 0:6b892e818f10 51
gboggs3 0:6b892e818f10 52 int main()
gboggs3 0:6b892e818f10 53 {
gboggs3 0:6b892e818f10 54 sel = 0; //0 = WiFi, 1 = Bluetooth
gboggs3 0:6b892e818f10 55
gboggs3 0:6b892e818f10 56 /* An external interrupt can be setup to change the sel pin during runtime */
gboggs3 0:6b892e818f10 57 pb.rise(&changeDevice);
gboggs3 1:c7cfaf8c38f2 58
gboggs3 1:c7cfaf8c38f2 59 /*Setting up the server to allow serial passthrough */
gboggs3 1:c7cfaf8c38f2 60 command("sk = net.createConnection(net.TCP, 0)");
gboggs3 1:c7cfaf8c38f2 61 wait(1);
gboggs3 1:c7cfaf8c38f2 62 command("sk:on(\"receive\", function(sck, c) print(\"Received: \" .. c) end)");
gboggs3 1:c7cfaf8c38f2 63 wait(1);
gboggs3 1:c7cfaf8c38f2 64 command("sk:on(\"connection\", function(sck,c)");
gboggs3 1:c7cfaf8c38f2 65 wait(1);
gboggs3 1:c7cfaf8c38f2 66 command("end)");
gboggs3 1:c7cfaf8c38f2 67 wait(1);
gboggs3 1:c7cfaf8c38f2 68 command("sk:connect(5001,\"107.161.22.189\")");
gboggs3 1:c7cfaf8c38f2 69 wait(1);
gboggs3 1:c7cfaf8c38f2 70 //Send sk:send(STRING_HERE)
gboggs3 1:c7cfaf8c38f2 71 //Received messages start with Received:
gboggs3 1:c7cfaf8c38f2 72
gboggs3 1:c7cfaf8c38f2 73 while(1)
gboggs3 1:c7cfaf8c38f2 74 {
gboggs3 0:6b892e818f10 75 sleep();
gboggs3 0:6b892e818f10 76 }
gboggs3 1:c7cfaf8c38f2 77 }