A lampost demo program - it takes in SMS commands to control a light using the shiftbrite LED.
Dependencies: VodafoneUSBModem_bleedingedge mbed-rtos mbed
Fork of 3GShiftBrite by
main.cpp@1:662b59c2ce3a, 2012-11-19 (annotated)
- Committer:
- nherriot
- Date:
- Mon Nov 19 17:34:14 2012 +0000
- Revision:
- 1:662b59c2ce3a
- Parent:
- 0:9a2bd692bc95
shiftbrite-lamp-post
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ashleymills | 0:9a2bd692bc95 | 1 | #define __DEBUG__ 4 //Maximum verbosity |
ashleymills | 0:9a2bd692bc95 | 2 | #ifndef __MODULE__ |
ashleymills | 0:9a2bd692bc95 | 3 | #define __MODULE__ "net_3g_basic_http_test.cpp" |
ashleymills | 0:9a2bd692bc95 | 4 | #endif |
ashleymills | 0:9a2bd692bc95 | 5 | |
ashleymills | 0:9a2bd692bc95 | 6 | #include "mbed.h" |
ashleymills | 0:9a2bd692bc95 | 7 | #include "rtos.h" |
ashleymills | 0:9a2bd692bc95 | 8 | #include "VodafoneUSBModem.h" |
ashleymills | 0:9a2bd692bc95 | 9 | #include "socket/bsd_socket.h" |
ashleymills | 0:9a2bd692bc95 | 10 | |
ashleymills | 0:9a2bd692bc95 | 11 | DigitalOut led1(LED1); |
ashleymills | 0:9a2bd692bc95 | 12 | DigitalOut led2(LED2); |
ashleymills | 0:9a2bd692bc95 | 13 | DigitalOut led3(LED3); |
ashleymills | 0:9a2bd692bc95 | 14 | DigitalOut led4(LED4); |
ashleymills | 0:9a2bd692bc95 | 15 | |
ashleymills | 0:9a2bd692bc95 | 16 | DigitalOut latch(p15); |
ashleymills | 0:9a2bd692bc95 | 17 | DigitalOut enable(p16); |
ashleymills | 0:9a2bd692bc95 | 18 | SPI spi(p11, p12, p13); |
ashleymills | 0:9a2bd692bc95 | 19 | |
ashleymills | 0:9a2bd692bc95 | 20 | void setLEDColor(int red, int green, int blue) { |
ashleymills | 0:9a2bd692bc95 | 21 | unsigned int low_color=0; |
ashleymills | 0:9a2bd692bc95 | 22 | unsigned int high_color=0; |
ashleymills | 0:9a2bd692bc95 | 23 | high_color=(blue<<4)|((red&0x3C0)>>6); |
ashleymills | 0:9a2bd692bc95 | 24 | low_color=(((red&0x3F)<<10)|(green)); |
ashleymills | 0:9a2bd692bc95 | 25 | spi.write(high_color); |
ashleymills | 0:9a2bd692bc95 | 26 | spi.write(low_color); |
ashleymills | 0:9a2bd692bc95 | 27 | latch=1; |
ashleymills | 0:9a2bd692bc95 | 28 | latch=0; |
ashleymills | 0:9a2bd692bc95 | 29 | } |
ashleymills | 0:9a2bd692bc95 | 30 | |
ashleymills | 0:9a2bd692bc95 | 31 | void setupShifty() { |
ashleymills | 0:9a2bd692bc95 | 32 | spi.format(16,0); |
ashleymills | 0:9a2bd692bc95 | 33 | spi.frequency(500000); |
ashleymills | 0:9a2bd692bc95 | 34 | enable=0; |
ashleymills | 0:9a2bd692bc95 | 35 | latch=0; |
ashleymills | 0:9a2bd692bc95 | 36 | } |
ashleymills | 0:9a2bd692bc95 | 37 | |
nherriot | 1:662b59c2ce3a | 38 | void doRainbow() { |
nherriot | 1:662b59c2ce3a | 39 | for(int r=0; r<500; r++) { |
nherriot | 1:662b59c2ce3a | 40 | for(int g=0; g<500; g++) { |
nherriot | 1:662b59c2ce3a | 41 | for(int b=0; b<500; b++) { |
nherriot | 1:662b59c2ce3a | 42 | setLEDColor(r,g,b); |
nherriot | 1:662b59c2ce3a | 43 | } |
nherriot | 1:662b59c2ce3a | 44 | } |
nherriot | 1:662b59c2ce3a | 45 | } |
nherriot | 1:662b59c2ce3a | 46 | setLEDColor(500,500,500); |
nherriot | 1:662b59c2ce3a | 47 | Thread::wait(1000); |
nherriot | 1:662b59c2ce3a | 48 | setLEDColor(0,0,0); |
nherriot | 1:662b59c2ce3a | 49 | } |
nherriot | 1:662b59c2ce3a | 50 | |
nherriot | 1:662b59c2ce3a | 51 | void doFlash() { |
nherriot | 1:662b59c2ce3a | 52 | int interval = 100; |
nherriot | 1:662b59c2ce3a | 53 | for(int i=0; i<5; i++) { |
nherriot | 1:662b59c2ce3a | 54 | setLEDColor(500,0,0); |
nherriot | 1:662b59c2ce3a | 55 | Thread::wait(interval); |
nherriot | 1:662b59c2ce3a | 56 | setLEDColor(0,0,0); |
nherriot | 1:662b59c2ce3a | 57 | Thread::wait(interval); |
nherriot | 1:662b59c2ce3a | 58 | setLEDColor(500,500,500); |
nherriot | 1:662b59c2ce3a | 59 | Thread::wait(interval); |
nherriot | 1:662b59c2ce3a | 60 | setLEDColor(0,0,0); |
nherriot | 1:662b59c2ce3a | 61 | Thread::wait(interval); |
nherriot | 1:662b59c2ce3a | 62 | setLEDColor(0,0,500); |
nherriot | 1:662b59c2ce3a | 63 | Thread::wait(interval); |
nherriot | 1:662b59c2ce3a | 64 | setLEDColor(0,0,0); |
nherriot | 1:662b59c2ce3a | 65 | Thread::wait(interval); |
nherriot | 1:662b59c2ce3a | 66 | } |
nherriot | 1:662b59c2ce3a | 67 | setLEDColor(500,0,0); |
nherriot | 1:662b59c2ce3a | 68 | } |
nherriot | 1:662b59c2ce3a | 69 | |
ashleymills | 0:9a2bd692bc95 | 70 | |
ashleymills | 0:9a2bd692bc95 | 71 | void lightListener(void const*) { |
ashleymills | 0:9a2bd692bc95 | 72 | VodafoneUSBModem modem; |
ashleymills | 0:9a2bd692bc95 | 73 | // socket stuff |
ashleymills | 0:9a2bd692bc95 | 74 | int sockfd,ret = OK; |
ashleymills | 0:9a2bd692bc95 | 75 | uint16_t port = 3232; |
ashleymills | 0:9a2bd692bc95 | 76 | struct sockaddr_in serverAddress; |
ashleymills | 0:9a2bd692bc95 | 77 | struct hostent *server; |
ashleymills | 0:9a2bd692bc95 | 78 | char urlBuffer[128]; |
ashleymills | 0:9a2bd692bc95 | 79 | int urlBufferLength = 128; |
ashleymills | 0:9a2bd692bc95 | 80 | |
ashleymills | 0:9a2bd692bc95 | 81 | // declare space for phone number and message storage |
nherriot | 1:662b59c2ce3a | 82 | char numBuffer[20], msgBuffer[256]; |
ashleymills | 0:9a2bd692bc95 | 83 | size_t numSMS; // variable to track number of received messages |
ashleymills | 0:9a2bd692bc95 | 84 | |
ashleymills | 0:9a2bd692bc95 | 85 | //int ret = modem.connect("internet","web","web"); |
ashleymills | 0:9a2bd692bc95 | 86 | |
ashleymills | 0:9a2bd692bc95 | 87 | if(ret == OK) { |
ashleymills | 0:9a2bd692bc95 | 88 | int count = 30; |
ashleymills | 0:9a2bd692bc95 | 89 | int rssi; |
ashleymills | 0:9a2bd692bc95 | 90 | int r=0,g=0,b=0; |
ashleymills | 0:9a2bd692bc95 | 91 | LinkMonitor::REGISTRATION_STATE regState; |
ashleymills | 0:9a2bd692bc95 | 92 | LinkMonitor::BEARER bearer; |
ashleymills | 0:9a2bd692bc95 | 93 | while(1) { |
ashleymills | 0:9a2bd692bc95 | 94 | |
ashleymills | 0:9a2bd692bc95 | 95 | //modem.getLinkState(&rssi, ®State, &bearer); |
ashleymills | 0:9a2bd692bc95 | 96 | //DBG("RSSI: %d dBm; Reg state: %d; Bearer: %d", rssi, regState, bearer); |
ashleymills | 0:9a2bd692bc95 | 97 | |
ashleymills | 0:9a2bd692bc95 | 98 | // retrieve the short message count into numSMS |
ashleymills | 0:9a2bd692bc95 | 99 | if(modem.getSMCount(&numSMS)==OK) { |
ashleymills | 0:9a2bd692bc95 | 100 | // check if any short messages have been received |
ashleymills | 0:9a2bd692bc95 | 101 | if(numSMS>0) { |
ashleymills | 0:9a2bd692bc95 | 102 | DBG("SM count > 0"); |
ashleymills | 0:9a2bd692bc95 | 103 | // get the oldest short message in the queue |
ashleymills | 0:9a2bd692bc95 | 104 | if(modem.getSM(numBuffer,msgBuffer,256)==OK) { |
nherriot | 1:662b59c2ce3a | 105 | if(strcmp(msgBuffer,"flash")==0) { |
nherriot | 1:662b59c2ce3a | 106 | doFlash(); |
nherriot | 1:662b59c2ce3a | 107 | continue; |
nherriot | 1:662b59c2ce3a | 108 | } else if(strcmp(msgBuffer,"rainbow")==0) { |
nherriot | 1:662b59c2ce3a | 109 | doRainbow(); |
nherriot | 1:662b59c2ce3a | 110 | continue; |
nherriot | 1:662b59c2ce3a | 111 | } else if(strcmp(msgBuffer,"red")==0) { |
nherriot | 1:662b59c2ce3a | 112 | setLEDColor(1000,0,0); |
nherriot | 1:662b59c2ce3a | 113 | continue; |
nherriot | 1:662b59c2ce3a | 114 | } else if(strcmp(msgBuffer,"green")==0) { |
nherriot | 1:662b59c2ce3a | 115 | setLEDColor(0,1000,0); |
nherriot | 1:662b59c2ce3a | 116 | continue; |
nherriot | 1:662b59c2ce3a | 117 | } else if(strcmp(msgBuffer,"blue")==0) { |
nherriot | 1:662b59c2ce3a | 118 | setLEDColor(0,0,1000); |
nherriot | 1:662b59c2ce3a | 119 | continue; |
nherriot | 1:662b59c2ce3a | 120 | } else if(strcmp(msgBuffer,"purple")==0) { |
nherriot | 1:662b59c2ce3a | 121 | setLEDColor(1000,0,1000); |
nherriot | 1:662b59c2ce3a | 122 | continue; |
nherriot | 1:662b59c2ce3a | 123 | } else if(strcmp(msgBuffer,"off")==0) { |
nherriot | 1:662b59c2ce3a | 124 | setLEDColor(0,0,0); |
nherriot | 1:662b59c2ce3a | 125 | continue; |
nherriot | 1:662b59c2ce3a | 126 | } |
nherriot | 1:662b59c2ce3a | 127 | |
nherriot | 1:662b59c2ce3a | 128 | if(sscanf(msgBuffer,"%d,%d,%d",&r,&g,&b)!=3) { |
nherriot | 1:662b59c2ce3a | 129 | continue; |
nherriot | 1:662b59c2ce3a | 130 | } |
ashleymills | 0:9a2bd692bc95 | 131 | DBG("Setting light to: %d,%d,%d",r,g,b); |
nherriot | 1:662b59c2ce3a | 132 | |
nherriot | 1:662b59c2ce3a | 133 | if(r<256) { |
nherriot | 1:662b59c2ce3a | 134 | r *= 4; |
nherriot | 1:662b59c2ce3a | 135 | } |
nherriot | 1:662b59c2ce3a | 136 | |
nherriot | 1:662b59c2ce3a | 137 | if(g<256) { |
nherriot | 1:662b59c2ce3a | 138 | g *= 4; |
nherriot | 1:662b59c2ce3a | 139 | } |
nherriot | 1:662b59c2ce3a | 140 | |
nherriot | 1:662b59c2ce3a | 141 | if(b<256) { |
nherriot | 1:662b59c2ce3a | 142 | b *= 4; |
nherriot | 1:662b59c2ce3a | 143 | } |
nherriot | 1:662b59c2ce3a | 144 | |
nherriot | 1:662b59c2ce3a | 145 | if(r>1023) { |
nherriot | 1:662b59c2ce3a | 146 | r = 1023; |
nherriot | 1:662b59c2ce3a | 147 | } |
nherriot | 1:662b59c2ce3a | 148 | |
nherriot | 1:662b59c2ce3a | 149 | if(g>1023) { |
nherriot | 1:662b59c2ce3a | 150 | g = 1023; |
nherriot | 1:662b59c2ce3a | 151 | } |
nherriot | 1:662b59c2ce3a | 152 | |
nherriot | 1:662b59c2ce3a | 153 | if(b>1023) { |
nherriot | 1:662b59c2ce3a | 154 | b = 1023; |
nherriot | 1:662b59c2ce3a | 155 | } |
nherriot | 1:662b59c2ce3a | 156 | |
nherriot | 1:662b59c2ce3a | 157 | setLEDColor(r,g,b); |
nherriot | 1:662b59c2ce3a | 158 | |
nherriot | 1:662b59c2ce3a | 159 | |
ashleymills | 0:9a2bd692bc95 | 160 | // connect to socket and push message |
ashleymills | 0:9a2bd692bc95 | 161 | /* |
ashleymills | 0:9a2bd692bc95 | 162 | // create the socket |
ashleymills | 0:9a2bd692bc95 | 163 | if((sockfd=::socket(AF_INET,SOCK_STREAM,0))<0) { DBG("Error opening socket\r\n"); } else { DBG("Socket open\r\n"); } |
ashleymills | 0:9a2bd692bc95 | 164 | |
ashleymills | 0:9a2bd692bc95 | 165 | // create the socket address |
ashleymills | 0:9a2bd692bc95 | 166 | std::memset(&serverAddress, 0, sizeof(struct sockaddr_in)); |
ashleymills | 0:9a2bd692bc95 | 167 | if((server=::gethostbyname("m2mthings.com"))==NULL) { |
ashleymills | 0:9a2bd692bc95 | 168 | DBG("Error resolving address, setting manually."); |
ashleymills | 0:9a2bd692bc95 | 169 | serverAddress.sin_addr.s_addr = inet_addr("109.74.199.96"); |
ashleymills | 0:9a2bd692bc95 | 170 | } else { |
ashleymills | 0:9a2bd692bc95 | 171 | DBG("Address resolved OK"); |
ashleymills | 0:9a2bd692bc95 | 172 | memcpy((char*)&serverAddress.sin_addr.s_addr, (char*)server->h_addr_list[0], server->h_length); |
ashleymills | 0:9a2bd692bc95 | 173 | } |
ashleymills | 0:9a2bd692bc95 | 174 | // set server address family |
ashleymills | 0:9a2bd692bc95 | 175 | serverAddress.sin_family = AF_INET; |
ashleymills | 0:9a2bd692bc95 | 176 | // set server port |
ashleymills | 0:9a2bd692bc95 | 177 | serverAddress.sin_port = htons(port); |
ashleymills | 0:9a2bd692bc95 | 178 | |
ashleymills | 0:9a2bd692bc95 | 179 | // do socket connect |
ashleymills | 0:9a2bd692bc95 | 180 | DBG("Connecting socket to %s:%d", inet_ntoa(serverAddress.sin_addr), ntohs(serverAddress.sin_port)); |
ashleymills | 0:9a2bd692bc95 | 181 | if((ret=::connect(sockfd, (const struct sockaddr *)&serverAddress, sizeof(serverAddress)))<0) { |
ashleymills | 0:9a2bd692bc95 | 182 | ::close(sockfd); |
ashleymills | 0:9a2bd692bc95 | 183 | DBG("Could not connect"); |
ashleymills | 0:9a2bd692bc95 | 184 | } else { |
ashleymills | 0:9a2bd692bc95 | 185 | DBG("Connection OK"); |
ashleymills | 0:9a2bd692bc95 | 186 | } |
ashleymills | 0:9a2bd692bc95 | 187 | |
ashleymills | 0:9a2bd692bc95 | 188 | DBG("Sending password"); |
ashleymills | 0:9a2bd692bc95 | 189 | ::write(sockfd,"hi3h892!",8); |
ashleymills | 0:9a2bd692bc95 | 190 | |
ashleymills | 0:9a2bd692bc95 | 191 | ret = ::recv(sockfd,urlBuffer,urlBufferLength,0); |
ashleymills | 0:9a2bd692bc95 | 192 | if (ret<0) { |
ashleymills | 0:9a2bd692bc95 | 193 | DBG("Error receiving ACK."); |
ashleymills | 0:9a2bd692bc95 | 194 | } else { |
ashleymills | 0:9a2bd692bc95 | 195 | DBG("Received(%d): %s",urlBufferLength,urlBuffer); |
ashleymills | 0:9a2bd692bc95 | 196 | modem.sendSM(numBuffer,urlBuffer); |
ashleymills | 0:9a2bd692bc95 | 197 | } |
ashleymills | 0:9a2bd692bc95 | 198 | |
ashleymills | 0:9a2bd692bc95 | 199 | ::close(sockfd); |
ashleymills | 0:9a2bd692bc95 | 200 | */ |
ashleymills | 0:9a2bd692bc95 | 201 | |
ashleymills | 0:9a2bd692bc95 | 202 | } |
ashleymills | 0:9a2bd692bc95 | 203 | } |
ashleymills | 0:9a2bd692bc95 | 204 | } |
ashleymills | 0:9a2bd692bc95 | 205 | // wait 500ms |
ashleymills | 0:9a2bd692bc95 | 206 | Thread::wait(500); |
ashleymills | 0:9a2bd692bc95 | 207 | } |
ashleymills | 0:9a2bd692bc95 | 208 | |
ashleymills | 0:9a2bd692bc95 | 209 | } |
ashleymills | 0:9a2bd692bc95 | 210 | |
nherriot | 1:662b59c2ce3a | 211 | //modem.disconnect(); |
nherriot | 1:662b59c2ce3a | 212 | //DBG("Disconnected"); |
ashleymills | 0:9a2bd692bc95 | 213 | |
ashleymills | 0:9a2bd692bc95 | 214 | while (1) { |
ashleymills | 0:9a2bd692bc95 | 215 | Thread::wait(100); |
ashleymills | 0:9a2bd692bc95 | 216 | } |
ashleymills | 0:9a2bd692bc95 | 217 | } |
ashleymills | 0:9a2bd692bc95 | 218 | |
ashleymills | 0:9a2bd692bc95 | 219 | void keepAlive(void const*) { |
ashleymills | 0:9a2bd692bc95 | 220 | while (1) { |
ashleymills | 0:9a2bd692bc95 | 221 | led1 = !led1; |
ashleymills | 0:9a2bd692bc95 | 222 | Thread::wait(250); |
ashleymills | 0:9a2bd692bc95 | 223 | led1 = !led1; |
nherriot | 1:662b59c2ce3a | 224 | Thread::wait(750); |
ashleymills | 0:9a2bd692bc95 | 225 | } |
ashleymills | 0:9a2bd692bc95 | 226 | } |
ashleymills | 0:9a2bd692bc95 | 227 | |
ashleymills | 0:9a2bd692bc95 | 228 | int main() { |
ashleymills | 0:9a2bd692bc95 | 229 | DBG_INIT(); |
ashleymills | 0:9a2bd692bc95 | 230 | DBG_SET_SPEED(115200); |
ashleymills | 0:9a2bd692bc95 | 231 | DBG_SET_NEWLINE("\r\n"); |
ashleymills | 0:9a2bd692bc95 | 232 | int red=0; |
ashleymills | 0:9a2bd692bc95 | 233 | int green=0; |
ashleymills | 0:9a2bd692bc95 | 234 | int blue=0; |
ashleymills | 0:9a2bd692bc95 | 235 | setupShifty(); |
ashleymills | 0:9a2bd692bc95 | 236 | setLEDColor(500,200,200); |
ashleymills | 0:9a2bd692bc95 | 237 | |
ashleymills | 0:9a2bd692bc95 | 238 | Thread lightTask(lightListener, NULL, osPriorityNormal, 1024 * 6); |
ashleymills | 0:9a2bd692bc95 | 239 | keepAlive(NULL); |
ashleymills | 0:9a2bd692bc95 | 240 | } |