Websocket demo for app board by C. Styles with updated libraries. DHCP service and a network cable is required.

Dependencies:   C12832 EthernetInterface HTTPServer LM75B MMA7660 WebSocketClient mbed-rtos mbed

Fork of app-board-Ethernet-Websocket by jim hamblen

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "EthernetInterface.h"
00003 #include "C12832.h"
00004 #include "Websocket.h"
00005 #include "MMA7660.h"
00006 #include "LM75B.h"
00007 
00008 // accelerometer
00009 MMA7660 acc(p28, p27);
00010 
00011 // temperature sensor
00012 LM75B tmp(p28,p27);
00013 
00014 // rgb led
00015 PwmOut r (p23);
00016 PwmOut g (p24);
00017 PwmOut b (p25);
00018 
00019 // lcd
00020 C12832 lcd(p5, p7, p6, p8, p11);
00021 
00022 // speaker
00023 DigitalIn fire(p14);
00024 PwmOut spkr(p26);
00025 
00026 // led
00027 DigitalOut l1(LED1);
00028  
00029 int main() {
00030     char json_str[100];
00031     
00032     for(float i = 0.0; i < 1.0 ; i += 0.001) {
00033             float p = 3 * i;
00034             r = 1.0 - ((p < 1.0) ? 1.0 - p : (p > 2.0) ? p - 2.0 : 0.0);
00035             g = 1.0 - ((p < 1.0) ? p : (p > 2.0) ? 0.0 : 2.0 - p);
00036             b = 1.0 - ((p < 1.0) ? 0.0 : (p > 2.0) ? 3.0 - p : p - 1.0);  ;  
00037             wait (0.01);
00038         }
00039     
00040     if (acc.testConnection())
00041         l1 = 1;
00042  
00043     EthernetInterface eth;
00044     eth.init(); //Use DHCP
00045     wait(2);
00046     eth.connect(60000);
00047     printf("IP Address is %s\n\r", eth.getIPAddress());
00048     
00049     // See the output on http://sockets.mbed.org/app-board/viewer
00050     // you need to specify a channel! otherwise this shit won't work!
00051     // "the /ws is important!"
00052     Websocket ws("ws://192.168.0.101:8001/ws");
00053     ws.connect();
00054     char recv[50];
00055     lcd.printf("SF1 Dev Week, here we go!");
00056     ws.send(json_str);
00057     while (1) {
00058         // create json string with acc/tmp data
00059         //sprintf(json_str, "{\"id\":\"geekymartianboard\",\"ax\":%d,\"ay\":%d,\"az\":%d, \"tmp\":%d}", (int)(acc.x()*360), (int)(acc.y()*360), (int)(acc.z()*360), (int)tmp.read());
00060         
00061         // send str
00062         //ws.send(json_str);
00063         
00064         wait(5.0);
00065         
00066         if(ws.read(recv)) {
00067             printf(recv);
00068             lcd.printf("Received a message:\n");
00069             lcd.printf(recv);
00070             
00071             for (float i=2000.0; i<10000.0; i+=100) {
00072                 spkr.period(1.0/i);
00073                 spkr=0.5;
00074                 wait(0.1);
00075             }
00076             spkr=0.0;
00077             while(!fire) {}
00078             
00079         }
00080     }
00081 }