w
Dependencies: MMA8451Q WIZnet_Library mbed
main.cpp@0:6e2f05c8183f, 2015-07-08 (annotated)
- Committer:
- virag
- Date:
- Wed Jul 08 03:55:38 2015 +0000
- Revision:
- 0:6e2f05c8183f
d
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
virag | 0:6e2f05c8183f | 1 | #include "mbed.h" |
virag | 0:6e2f05c8183f | 2 | #include "MMA8451Q.h" |
virag | 0:6e2f05c8183f | 3 | #define MMA8451_I2C_ADDRESS (0x1d<<1) |
virag | 0:6e2f05c8183f | 4 | #include "WIZnetInterface.h" |
virag | 0:6e2f05c8183f | 5 | |
virag | 0:6e2f05c8183f | 6 | unsigned char MAC_Addr[6] = {0x00,0x08,0xDC,0x12,0x07,0x07}; |
virag | 0:6e2f05c8183f | 7 | |
virag | 0:6e2f05c8183f | 8 | char* Public_Key = "g1WVqxqKqNtr2AVG6424TVvoyopy"; |
virag | 0:6e2f05c8183f | 9 | char* Private_Key = "dRbwA0AeAWf7DqyMOdDdHzlA9Av9"; |
virag | 0:6e2f05c8183f | 10 | char* ServerIP = "192.168.44.70"; |
virag | 0:6e2f05c8183f | 11 | |
virag | 0:6e2f05c8183f | 12 | |
virag | 0:6e2f05c8183f | 13 | MMA8451Q acc1(PTE25, PTE24, MMA8451_I2C_ADDRESS); |
virag | 0:6e2f05c8183f | 14 | Serial pc(USBTX,USBRX);//pc is object of class serial hence anything used a pc.function then that function is a part of the class serial |
virag | 0:6e2f05c8183f | 15 | Serial psoc(PTE22,PTE23); |
virag | 0:6e2f05c8183f | 16 | BusOut myled(LED1,LED2,LED3); |
virag | 0:6e2f05c8183f | 17 | SPI spi(PTD2,PTD3,PTD1); |
virag | 0:6e2f05c8183f | 18 | WIZnetInterface ethernet(&spi,PTD0,PTA20); |
virag | 0:6e2f05c8183f | 19 | AnalogIn temp(PTC1); |
virag | 0:6e2f05c8183f | 20 | |
virag | 0:6e2f05c8183f | 21 | char buffer1[20]; |
virag | 0:6e2f05c8183f | 22 | |
virag | 0:6e2f05c8183f | 23 | char buffer2[20]; |
virag | 0:6e2f05c8183f | 24 | int16_t z=0; |
virag | 0:6e2f05c8183f | 25 | int16_t x=0; |
virag | 0:6e2f05c8183f | 26 | int16_t y=0; |
virag | 0:6e2f05c8183f | 27 | |
virag | 0:6e2f05c8183f | 28 | int main() |
virag | 0:6e2f05c8183f | 29 | { |
virag | 0:6e2f05c8183f | 30 | |
virag | 0:6e2f05c8183f | 31 | myled=0xff;; |
virag | 0:6e2f05c8183f | 32 | char choice; |
virag | 0:6e2f05c8183f | 33 | pc.printf("Hello Forum\r\n"); |
virag | 0:6e2f05c8183f | 34 | while(true) |
virag | 0:6e2f05c8183f | 35 | { |
virag | 0:6e2f05c8183f | 36 | if(psoc.readable()) |
virag | 0:6e2f05c8183f | 37 | |
virag | 0:6e2f05c8183f | 38 | { myled=0x02; |
virag | 0:6e2f05c8183f | 39 | choice =psoc.getc(); |
virag | 0:6e2f05c8183f | 40 | |
virag | 0:6e2f05c8183f | 41 | switch(choice) |
virag | 0:6e2f05c8183f | 42 | {case 'D': {myled=0x06; |
virag | 0:6e2f05c8183f | 43 | if(psoc.readable()); |
virag | 0:6e2f05c8183f | 44 | {psoc.gets(buffer1,20); |
virag | 0:6e2f05c8183f | 45 | pc.printf("LDR= %s\r\n",buffer1); |
virag | 0:6e2f05c8183f | 46 | myled=0x06;} |
virag | 0:6e2f05c8183f | 47 | } |
virag | 0:6e2f05c8183f | 48 | break; |
virag | 0:6e2f05c8183f | 49 | |
virag | 0:6e2f05c8183f | 50 | case 'M': {myled=0x05; |
virag | 0:6e2f05c8183f | 51 | if(psoc.readable()); |
virag | 0:6e2f05c8183f | 52 | {psoc.gets(buffer2,20); |
virag | 0:6e2f05c8183f | 53 | buffer2=buffer2/10 |
virag | 0:6e2f05c8183f | 54 | pc.printf("LM35= %s\r\n",buffer2); |
virag | 0:6e2f05c8183f | 55 | myled=0x05;} |
virag | 0:6e2f05c8183f | 56 | } |
virag | 0:6e2f05c8183f | 57 | break; |
virag | 0:6e2f05c8183f | 58 | |
virag | 0:6e2f05c8183f | 59 | case 'X': { |
virag | 0:6e2f05c8183f | 60 | int s=5; |
virag | 0:6e2f05c8183f | 61 | x= -s*(acc1.getAccY()); |
virag | 0:6e2f05c8183f | 62 | pc.printf("x=%d \r\n",x); |
virag | 0:6e2f05c8183f | 63 | myled=0x03;} |
virag | 0:6e2f05c8183f | 64 | break; |
virag | 0:6e2f05c8183f | 65 | |
virag | 0:6e2f05c8183f | 66 | case 'Y': { |
virag | 0:6e2f05c8183f | 67 | int s=5; |
virag | 0:6e2f05c8183f | 68 | y= s*acc1.getAccX(); |
virag | 0:6e2f05c8183f | 69 | pc.printf(" y=%d \r\n",y); |
virag | 0:6e2f05c8183f | 70 | myled=0x03;} |
virag | 0:6e2f05c8183f | 71 | break; |
virag | 0:6e2f05c8183f | 72 | |
virag | 0:6e2f05c8183f | 73 | case 'Z': { |
virag | 0:6e2f05c8183f | 74 | int s=5; |
virag | 0:6e2f05c8183f | 75 | z= s*acc1.getAccZ(); |
virag | 0:6e2f05c8183f | 76 | pc.printf(" z=%d\r\n",z); |
virag | 0:6e2f05c8183f | 77 | myled=0x03;} |
virag | 0:6e2f05c8183f | 78 | break; |
virag | 0:6e2f05c8183f | 79 | |
virag | 0:6e2f05c8183f | 80 | default: myled=0x01; |
virag | 0:6e2f05c8183f | 81 | break; |
virag | 0:6e2f05c8183f | 82 | } |
virag | 0:6e2f05c8183f | 83 | //Set serial port baudrate speed: 115200 |
virag | 0:6e2f05c8183f | 84 | pc.baud(115200); |
virag | 0:6e2f05c8183f | 85 | pc.printf("Start\r\n"); |
virag | 0:6e2f05c8183f | 86 | |
virag | 0:6e2f05c8183f | 87 | while(1) { |
virag | 0:6e2f05c8183f | 88 | int ret = ethernet.init(MAC_Addr); |
virag | 0:6e2f05c8183f | 89 | |
virag | 0:6e2f05c8183f | 90 | if (!ret) { |
virag | 0:6e2f05c8183f | 91 | pc.printf("Initialized, MAC: %s\r\n", ethernet.getMACAddress()); |
virag | 0:6e2f05c8183f | 92 | ret = ethernet.connect(); |
virag | 0:6e2f05c8183f | 93 | if (!ret) { |
virag | 0:6e2f05c8183f | 94 | pc.printf("IP: %s, MASK: %s, GW: %s\r\n", |
virag | 0:6e2f05c8183f | 95 | ethernet.getIPAddress(), ethernet.getNetworkMask(), ethernet.getGateway()); |
virag | 0:6e2f05c8183f | 96 | } else { |
virag | 0:6e2f05c8183f | 97 | pc.printf("Error ethernet.connect() - ret = %d\r\n", ret); |
virag | 0:6e2f05c8183f | 98 | exit(0); |
virag | 0:6e2f05c8183f | 99 | } |
virag | 0:6e2f05c8183f | 100 | } else { |
virag | 0:6e2f05c8183f | 101 | pc.printf("Error ethernet.init() - ret = %d\r\n", ret); |
virag | 0:6e2f05c8183f | 102 | exit(0); |
virag | 0:6e2f05c8183f | 103 | } |
virag | 0:6e2f05c8183f | 104 | |
virag | 0:6e2f05c8183f | 105 | TCPSocketConnection sock; |
virag | 0:6e2f05c8183f | 106 | sock.connect(ServerIP, 8080); |
virag | 0:6e2f05c8183f | 107 | if(sock.is_connected()) |
virag | 0:6e2f05c8183f | 108 | printf("Socket Connected\n\r"); |
virag | 0:6e2f05c8183f | 109 | else |
virag | 0:6e2f05c8183f | 110 | printf("Socket NoT Connected\n\r"); |
virag | 0:6e2f05c8183f | 111 | |
virag | 0:6e2f05c8183f | 112 | char buffer[300]; |
virag | 0:6e2f05c8183f | 113 | int ret_t; |
virag | 0:6e2f05c8183f | 114 | |
virag | 0:6e2f05c8183f | 115 | char http_cmd[256]; |
virag | 0:6e2f05c8183f | 116 | |
virag | 0:6e2f05c8183f | 117 | sprintf(http_cmd,"GET /input/%s?private_key=%s&LDR=%s HTTP/1.0\n\n",Public_Key,Private_Key,counter); |
virag | 0:6e2f05c8183f | 118 | printf("Running - %s\r\n",http_cmd); |
virag | 0:6e2f05c8183f | 119 | sock.send_all(http_cmd, sizeof(http_cmd)-1); |
virag | 0:6e2f05c8183f | 120 | |
virag | 0:6e2f05c8183f | 121 | ret_t = sock.receive(buffer, sizeof(buffer)-1); |
virag | 0:6e2f05c8183f | 122 | buffer[ret_t] = '\0'; |
virag | 0:6e2f05c8183f | 123 | printf("Received %d chars from server:\n%s\r\n", ret_t, buffer); |
virag | 0:6e2f05c8183f | 124 | |
virag | 0:6e2f05c8183f | 125 | sock.close(); |
virag | 0:6e2f05c8183f | 126 | |
virag | 0:6e2f05c8183f | 127 | ethernet.disconnect(); |
virag | 0:6e2f05c8183f | 128 | printf("Socket Closed"); |
virag | 0:6e2f05c8183f | 129 | |
virag | 0:6e2f05c8183f | 130 | while(1) {} |
virag | 0:6e2f05c8183f | 131 | } |
virag | 0:6e2f05c8183f | 132 | } |
virag | 0:6e2f05c8183f | 133 | } |