![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
This is a demo to show how to use Arch Link and dweet.io
Dependencies: BLE_API DHT HTTPClient SDFileSystem WIZnet_Library mbed nRF51822
Fork of archlink_Temperture_ethernet_http by
main.cpp@3:cfc6ff08a668, 2015-07-02 (annotated)
- Committer:
- Kevin_Lee
- Date:
- Thu Jul 02 10:05:43 2015 +0000
- Revision:
- 3:cfc6ff08a668
- Parent:
- 2:8b304d2b4ca5
- Child:
- 4:81af9a9f7844
This is a demo to upload temperature info. to dweet.io
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Kevin_Lee | 0:29bbc7eec5e1 | 1 | #include "mbed.h" |
Kevin_Lee | 0:29bbc7eec5e1 | 2 | #include "SDFileSystem.h" |
Kevin_Lee | 0:29bbc7eec5e1 | 3 | #include "BLE.h" |
Kevin_Lee | 0:29bbc7eec5e1 | 4 | #include "WIZnetInterface.h" |
Kevin_Lee | 2:8b304d2b4ca5 | 5 | #include "HTTPClient.h" |
Kevin_Lee | 3:cfc6ff08a668 | 6 | #include "DHT.h" |
Kevin_Lee | 3:cfc6ff08a668 | 7 | |
Kevin_Lee | 3:cfc6ff08a668 | 8 | #define DHT22_Test 0 |
Kevin_Lee | 0:29bbc7eec5e1 | 9 | |
Kevin_Lee | 2:8b304d2b4ca5 | 10 | #define USE_DHCP 0 |
Kevin_Lee | 0:29bbc7eec5e1 | 11 | #define LOOPBACKPORT 5000 |
Kevin_Lee | 0:29bbc7eec5e1 | 12 | int W5500_Test(void); |
Kevin_Lee | 1:0f6a216263aa | 13 | |
Kevin_Lee | 3:cfc6ff08a668 | 14 | float temperature_get(); /* Grove - Temperature Sensor V1.2 */ |
Kevin_Lee | 3:cfc6ff08a668 | 15 | |
Kevin_Lee | 0:29bbc7eec5e1 | 16 | SPI spi(SPI_PSELMOSI0, SPI_PSELMISO0, SPI_PSELSCK0); |
Kevin_Lee | 0:29bbc7eec5e1 | 17 | WIZnetInterface ethernet(&spi, p24, p17); // Spi ,cs, reset |
Kevin_Lee | 0:29bbc7eec5e1 | 18 | int ret, dummy, lv = 1; |
Kevin_Lee | 0:29bbc7eec5e1 | 19 | const char * IP_Addr = "192.168.21.247"; |
Kevin_Lee | 0:29bbc7eec5e1 | 20 | const char * IP_Subnet = "255.255.255.0"; |
Kevin_Lee | 0:29bbc7eec5e1 | 21 | const char * IP_Gateway = "192.168.21.2"; |
Kevin_Lee | 0:29bbc7eec5e1 | 22 | unsigned char MAC_Addr[6] = {0x00,0x08,0xDC,0x1C,0xAA,0xCA}; |
Kevin_Lee | 0:29bbc7eec5e1 | 23 | |
Kevin_Lee | 0:29bbc7eec5e1 | 24 | |
Kevin_Lee | 0:29bbc7eec5e1 | 25 | DigitalOut myled(LED1); |
Kevin_Lee | 0:29bbc7eec5e1 | 26 | //Arch Link |
Kevin_Lee | 0:29bbc7eec5e1 | 27 | Serial pc(USBTX, USBRX); /* uart */ |
Kevin_Lee | 0:29bbc7eec5e1 | 28 | |
Kevin_Lee | 0:29bbc7eec5e1 | 29 | |
Kevin_Lee | 3:cfc6ff08a668 | 30 | DHT dht22(A1,DHT22); |
Kevin_Lee | 3:cfc6ff08a668 | 31 | |
Kevin_Lee | 3:cfc6ff08a668 | 32 | AnalogIn ain(A0); |
Kevin_Lee | 3:cfc6ff08a668 | 33 | |
Kevin_Lee | 0:29bbc7eec5e1 | 34 | int main() { |
Kevin_Lee | 1:0f6a216263aa | 35 | char buffer[256]; |
Kevin_Lee | 0:29bbc7eec5e1 | 36 | wait(1); |
Kevin_Lee | 0:29bbc7eec5e1 | 37 | pc.baud(115200); |
Kevin_Lee | 0:29bbc7eec5e1 | 38 | wait(1); |
Kevin_Lee | 0:29bbc7eec5e1 | 39 | printf("helloworld\r\n"); |
Kevin_Lee | 0:29bbc7eec5e1 | 40 | |
Kevin_Lee | 3:cfc6ff08a668 | 41 | /* |
Kevin_Lee | 3:cfc6ff08a668 | 42 | float resistance = (float)(1023.0-val)*10000.0/(float)val; |
Kevin_Lee | 3:cfc6ff08a668 | 43 | float temp = 1/(log(resistance/10000)/B+1/298.15)-273.15; |
Kevin_Lee | 3:cfc6ff08a668 | 44 | */ |
Kevin_Lee | 3:cfc6ff08a668 | 45 | |
Kevin_Lee | 3:cfc6ff08a668 | 46 | #if DHT22_Test |
Kevin_Lee | 3:cfc6ff08a668 | 47 | int result; |
Kevin_Lee | 3:cfc6ff08a668 | 48 | while(1) { |
Kevin_Lee | 3:cfc6ff08a668 | 49 | printf("dht22.readData()\r\n"); |
Kevin_Lee | 3:cfc6ff08a668 | 50 | printf("ain A0: %.2f\r\n",ain.read()); |
Kevin_Lee | 3:cfc6ff08a668 | 51 | printf("temperature_get %.2f\r\n",temperature_get()); |
Kevin_Lee | 3:cfc6ff08a668 | 52 | result = dht22.readData(); |
Kevin_Lee | 3:cfc6ff08a668 | 53 | printf("the result is %d.\r\n",result); |
Kevin_Lee | 3:cfc6ff08a668 | 54 | if(result == 0) { |
Kevin_Lee | 3:cfc6ff08a668 | 55 | printf("Temperature is %f.2 C\r\n",dht22.ReadTemperature(CELCIUS)); |
Kevin_Lee | 3:cfc6ff08a668 | 56 | printf("Humidity is %f.2 \%\r\n",dht22.ReadHumidity()); |
Kevin_Lee | 3:cfc6ff08a668 | 57 | } |
Kevin_Lee | 3:cfc6ff08a668 | 58 | wait(4); |
Kevin_Lee | 3:cfc6ff08a668 | 59 | } |
Kevin_Lee | 3:cfc6ff08a668 | 60 | #endif |
Kevin_Lee | 3:cfc6ff08a668 | 61 | |
Kevin_Lee | 3:cfc6ff08a668 | 62 | |
Kevin_Lee | 2:8b304d2b4ca5 | 63 | if(W5500_Test() == 0) { // Internet is ok |
Kevin_Lee | 0:29bbc7eec5e1 | 64 | printf("W5500 tested OK \r\n"); |
Kevin_Lee | 1:0f6a216263aa | 65 | |
Kevin_Lee | 3:cfc6ff08a668 | 66 | char str[512]; |
Kevin_Lee | 3:cfc6ff08a668 | 67 | char get_msg[128]= ""; |
Kevin_Lee | 2:8b304d2b4ca5 | 68 | |
Kevin_Lee | 2:8b304d2b4ca5 | 69 | /* |
Kevin_Lee | 3:cfc6ff08a668 | 70 | http://dweet.io/follow/ArchLink |
Kevin_Lee | 2:8b304d2b4ca5 | 71 | */ |
Kevin_Lee | 3:cfc6ff08a668 | 72 | char nameYouWant[] = "ArchLink"; |
Kevin_Lee | 3:cfc6ff08a668 | 73 | while(1) |
Kevin_Lee | 2:8b304d2b4ca5 | 74 | { |
Kevin_Lee | 3:cfc6ff08a668 | 75 | sprintf(get_msg,"http://dweet.io/dweet/for/%s?Temperature=%.2f",nameYouWant,temperature_get()); |
Kevin_Lee | 3:cfc6ff08a668 | 76 | printf("temperature_get: %.2f",temperature_get()); |
Kevin_Lee | 3:cfc6ff08a668 | 77 | HTTPClient http; |
Kevin_Lee | 2:8b304d2b4ca5 | 78 | |
Kevin_Lee | 3:cfc6ff08a668 | 79 | pc.printf("Send post message to dweet.io\r\n"); |
Kevin_Lee | 3:cfc6ff08a668 | 80 | pc.printf("msg : %s\r\n",get_msg); |
Kevin_Lee | 3:cfc6ff08a668 | 81 | ret = http.get(get_msg, str, sizeof(str)); |
Kevin_Lee | 3:cfc6ff08a668 | 82 | if(!ret) |
Kevin_Lee | 3:cfc6ff08a668 | 83 | { |
Kevin_Lee | 3:cfc6ff08a668 | 84 | pc.printf("\r\nPage fetched successfully - read %d characters\r\n", strlen(str)); |
Kevin_Lee | 3:cfc6ff08a668 | 85 | pc.printf("Result: %s\r\n", str); |
Kevin_Lee | 3:cfc6ff08a668 | 86 | } |
Kevin_Lee | 3:cfc6ff08a668 | 87 | else |
Kevin_Lee | 3:cfc6ff08a668 | 88 | { |
Kevin_Lee | 3:cfc6ff08a668 | 89 | pc.printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); |
Kevin_Lee | 3:cfc6ff08a668 | 90 | } |
Kevin_Lee | 3:cfc6ff08a668 | 91 | wait(10); |
Kevin_Lee | 3:cfc6ff08a668 | 92 | } |
Kevin_Lee | 0:29bbc7eec5e1 | 93 | } |
Kevin_Lee | 0:29bbc7eec5e1 | 94 | |
Kevin_Lee | 0:29bbc7eec5e1 | 95 | while(1) { |
Kevin_Lee | 0:29bbc7eec5e1 | 96 | myled = 1; |
Kevin_Lee | 0:29bbc7eec5e1 | 97 | printf("light1\r\n"); |
Kevin_Lee | 0:29bbc7eec5e1 | 98 | wait(3); |
Kevin_Lee | 0:29bbc7eec5e1 | 99 | myled = 0; |
Kevin_Lee | 0:29bbc7eec5e1 | 100 | printf("light0\r\n"); |
Kevin_Lee | 0:29bbc7eec5e1 | 101 | wait(3); |
Kevin_Lee | 0:29bbc7eec5e1 | 102 | } |
Kevin_Lee | 0:29bbc7eec5e1 | 103 | } |
Kevin_Lee | 0:29bbc7eec5e1 | 104 | |
Kevin_Lee | 0:29bbc7eec5e1 | 105 | int W5500_Test(void) |
Kevin_Lee | 0:29bbc7eec5e1 | 106 | { |
Kevin_Lee | 0:29bbc7eec5e1 | 107 | mbed_mac_address((char *)MAC_Addr); //Use mbed mac addres |
Kevin_Lee | 0:29bbc7eec5e1 | 108 | wait(1); |
Kevin_Lee | 0:29bbc7eec5e1 | 109 | printf("Start to test ethernet!\r\n"); |
Kevin_Lee | 0:29bbc7eec5e1 | 110 | |
Kevin_Lee | 0:29bbc7eec5e1 | 111 | #if USE_DHCP |
Kevin_Lee | 0:29bbc7eec5e1 | 112 | printf("use DHCP\r\n"); |
Kevin_Lee | 0:29bbc7eec5e1 | 113 | ret = ethernet.init(MAC_Addr); |
Kevin_Lee | 0:29bbc7eec5e1 | 114 | #else |
Kevin_Lee | 0:29bbc7eec5e1 | 115 | printf("do NOT use DHCP\r\n"); |
Kevin_Lee | 0:29bbc7eec5e1 | 116 | int ret = ethernet.init(MAC_Addr,IP_Addr,IP_Subnet,IP_Gateway); |
Kevin_Lee | 0:29bbc7eec5e1 | 117 | #endif |
Kevin_Lee | 0:29bbc7eec5e1 | 118 | |
Kevin_Lee | 0:29bbc7eec5e1 | 119 | if (!ret) { |
Kevin_Lee | 0:29bbc7eec5e1 | 120 | pc.printf("Initialized, MAC: %s\r\n", ethernet.getMACAddress()); |
Kevin_Lee | 0:29bbc7eec5e1 | 121 | ret = ethernet.connect(); |
Kevin_Lee | 0:29bbc7eec5e1 | 122 | if (!ret) { |
Kevin_Lee | 0:29bbc7eec5e1 | 123 | pc.printf("IP: %s, MASK: %s, GW: %s\r\n", |
Kevin_Lee | 0:29bbc7eec5e1 | 124 | ethernet.getIPAddress(), ethernet.getNetworkMask(), ethernet.getGateway()); |
Kevin_Lee | 0:29bbc7eec5e1 | 125 | return 0; |
Kevin_Lee | 0:29bbc7eec5e1 | 126 | } else { |
Kevin_Lee | 0:29bbc7eec5e1 | 127 | pc.printf("Error ethernet.connect() - ret = %d\r\n", ret); |
Kevin_Lee | 0:29bbc7eec5e1 | 128 | //exit(0); |
Kevin_Lee | 0:29bbc7eec5e1 | 129 | return -1; |
Kevin_Lee | 0:29bbc7eec5e1 | 130 | } |
Kevin_Lee | 0:29bbc7eec5e1 | 131 | } else { |
Kevin_Lee | 0:29bbc7eec5e1 | 132 | pc.printf("Error ethernet.init() - ret = %d\r\n", ret); |
Kevin_Lee | 0:29bbc7eec5e1 | 133 | //exit(0); |
Kevin_Lee | 0:29bbc7eec5e1 | 134 | return -1; |
Kevin_Lee | 0:29bbc7eec5e1 | 135 | } |
Kevin_Lee | 0:29bbc7eec5e1 | 136 | /* |
Kevin_Lee | 0:29bbc7eec5e1 | 137 | TCPSocketServer server; |
Kevin_Lee | 0:29bbc7eec5e1 | 138 | server.bind(LOOPBACKPORT); |
Kevin_Lee | 0:29bbc7eec5e1 | 139 | server.listen(); |
Kevin_Lee | 0:29bbc7eec5e1 | 140 | |
Kevin_Lee | 0:29bbc7eec5e1 | 141 | while (1) { |
Kevin_Lee | 0:29bbc7eec5e1 | 142 | pc.printf("\nWait for new connection...\r\n"); |
Kevin_Lee | 0:29bbc7eec5e1 | 143 | TCPSocketConnection client; |
Kevin_Lee | 0:29bbc7eec5e1 | 144 | server.accept(client); |
Kevin_Lee | 0:29bbc7eec5e1 | 145 | client.set_blocking(false, 0); // Timeout=0. |
Kevin_Lee | 0:29bbc7eec5e1 | 146 | pc.printf("Connection from: %s\r\n", client.get_address()); |
Kevin_Lee | 0:29bbc7eec5e1 | 147 | while (client.is_connected() == true) { |
Kevin_Lee | 0:29bbc7eec5e1 | 148 | int n = client.receive(buffer, sizeof(buffer)); |
Kevin_Lee | 0:29bbc7eec5e1 | 149 | if(n > 0) |
Kevin_Lee | 0:29bbc7eec5e1 | 150 | client.send_all(buffer, n); |
Kevin_Lee | 0:29bbc7eec5e1 | 151 | if(client.is_fin_received()) |
Kevin_Lee | 0:29bbc7eec5e1 | 152 | client.close(); |
Kevin_Lee | 0:29bbc7eec5e1 | 153 | } |
Kevin_Lee | 0:29bbc7eec5e1 | 154 | pc.printf("Disconnected.\r\n"); |
Kevin_Lee | 0:29bbc7eec5e1 | 155 | }*/ |
Kevin_Lee | 3:cfc6ff08a668 | 156 | } |
Kevin_Lee | 3:cfc6ff08a668 | 157 | |
Kevin_Lee | 3:cfc6ff08a668 | 158 | |
Kevin_Lee | 3:cfc6ff08a668 | 159 | float temperature_get() |
Kevin_Lee | 3:cfc6ff08a668 | 160 | { |
Kevin_Lee | 3:cfc6ff08a668 | 161 | int B = 3975; |
Kevin_Lee | 3:cfc6ff08a668 | 162 | float val = (ain.read()*1023); |
Kevin_Lee | 3:cfc6ff08a668 | 163 | float resistance = (float)(1023.0-val)*10000.0/(float)val; |
Kevin_Lee | 3:cfc6ff08a668 | 164 | float temp = 1/(log(resistance/10000)/B+1/298.15)-273.15; |
Kevin_Lee | 3:cfc6ff08a668 | 165 | return temp; |
Kevin_Lee | 3:cfc6ff08a668 | 166 | } |
Kevin_Lee | 3:cfc6ff08a668 | 167 |