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 Kevin Lee

Committer:
Kevin_Lee
Date:
Mon Jul 06 01:55:07 2015 +0000
Revision:
4:81af9a9f7844
Parent:
3:cfc6ff08a668
upload the data to dweet.io

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Kevin_Lee 4:81af9a9f7844 1 /* This is just a demo by Kevin Lee */
Kevin_Lee 0:29bbc7eec5e1 2 #include "mbed.h"
Kevin_Lee 0:29bbc7eec5e1 3 #include "SDFileSystem.h"
Kevin_Lee 0:29bbc7eec5e1 4 #include "BLE.h"
Kevin_Lee 0:29bbc7eec5e1 5 #include "WIZnetInterface.h"
Kevin_Lee 2:8b304d2b4ca5 6 #include "HTTPClient.h"
Kevin_Lee 3:cfc6ff08a668 7 #include "DHT.h"
Kevin_Lee 3:cfc6ff08a668 8
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 4:81af9a9f7844 39 printf("helloworld\r\n");
Kevin_Lee 3:cfc6ff08a668 40
Kevin_Lee 2:8b304d2b4ca5 41 if(W5500_Test() == 0) { // Internet is ok
Kevin_Lee 0:29bbc7eec5e1 42 printf("W5500 tested OK \r\n");
Kevin_Lee 1:0f6a216263aa 43
Kevin_Lee 3:cfc6ff08a668 44 char str[512];
Kevin_Lee 3:cfc6ff08a668 45 char get_msg[128]= "";
Kevin_Lee 2:8b304d2b4ca5 46
Kevin_Lee 2:8b304d2b4ca5 47 /*
Kevin_Lee 3:cfc6ff08a668 48 http://dweet.io/follow/ArchLink
Kevin_Lee 2:8b304d2b4ca5 49 */
Kevin_Lee 3:cfc6ff08a668 50 char nameYouWant[] = "ArchLink";
Kevin_Lee 3:cfc6ff08a668 51 while(1)
Kevin_Lee 2:8b304d2b4ca5 52 {
Kevin_Lee 3:cfc6ff08a668 53 sprintf(get_msg,"http://dweet.io/dweet/for/%s?Temperature=%.2f",nameYouWant,temperature_get());
Kevin_Lee 3:cfc6ff08a668 54 printf("temperature_get: %.2f",temperature_get());
Kevin_Lee 3:cfc6ff08a668 55 HTTPClient http;
Kevin_Lee 2:8b304d2b4ca5 56
Kevin_Lee 3:cfc6ff08a668 57 pc.printf("Send post message to dweet.io\r\n");
Kevin_Lee 3:cfc6ff08a668 58 pc.printf("msg : %s\r\n",get_msg);
Kevin_Lee 3:cfc6ff08a668 59 ret = http.get(get_msg, str, sizeof(str));
Kevin_Lee 3:cfc6ff08a668 60 if(!ret)
Kevin_Lee 3:cfc6ff08a668 61 {
Kevin_Lee 3:cfc6ff08a668 62 pc.printf("\r\nPage fetched successfully - read %d characters\r\n", strlen(str));
Kevin_Lee 3:cfc6ff08a668 63 pc.printf("Result: %s\r\n", str);
Kevin_Lee 3:cfc6ff08a668 64 }
Kevin_Lee 3:cfc6ff08a668 65 else
Kevin_Lee 3:cfc6ff08a668 66 {
Kevin_Lee 3:cfc6ff08a668 67 pc.printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
Kevin_Lee 3:cfc6ff08a668 68 }
Kevin_Lee 3:cfc6ff08a668 69 wait(10);
Kevin_Lee 3:cfc6ff08a668 70 }
Kevin_Lee 0:29bbc7eec5e1 71 }
Kevin_Lee 0:29bbc7eec5e1 72
Kevin_Lee 0:29bbc7eec5e1 73 while(1) {
Kevin_Lee 0:29bbc7eec5e1 74 myled = 1;
Kevin_Lee 0:29bbc7eec5e1 75 printf("light1\r\n");
Kevin_Lee 0:29bbc7eec5e1 76 wait(3);
Kevin_Lee 0:29bbc7eec5e1 77 myled = 0;
Kevin_Lee 0:29bbc7eec5e1 78 printf("light0\r\n");
Kevin_Lee 0:29bbc7eec5e1 79 wait(3);
Kevin_Lee 0:29bbc7eec5e1 80 }
Kevin_Lee 0:29bbc7eec5e1 81 }
Kevin_Lee 0:29bbc7eec5e1 82
Kevin_Lee 0:29bbc7eec5e1 83 int W5500_Test(void)
Kevin_Lee 0:29bbc7eec5e1 84 {
Kevin_Lee 0:29bbc7eec5e1 85 mbed_mac_address((char *)MAC_Addr); //Use mbed mac addres
Kevin_Lee 0:29bbc7eec5e1 86 wait(1);
Kevin_Lee 0:29bbc7eec5e1 87 printf("Start to test ethernet!\r\n");
Kevin_Lee 0:29bbc7eec5e1 88
Kevin_Lee 0:29bbc7eec5e1 89 #if USE_DHCP
Kevin_Lee 0:29bbc7eec5e1 90 printf("use DHCP\r\n");
Kevin_Lee 0:29bbc7eec5e1 91 ret = ethernet.init(MAC_Addr);
Kevin_Lee 0:29bbc7eec5e1 92 #else
Kevin_Lee 0:29bbc7eec5e1 93 printf("do NOT use DHCP\r\n");
Kevin_Lee 0:29bbc7eec5e1 94 int ret = ethernet.init(MAC_Addr,IP_Addr,IP_Subnet,IP_Gateway);
Kevin_Lee 0:29bbc7eec5e1 95 #endif
Kevin_Lee 0:29bbc7eec5e1 96
Kevin_Lee 0:29bbc7eec5e1 97 if (!ret) {
Kevin_Lee 0:29bbc7eec5e1 98 pc.printf("Initialized, MAC: %s\r\n", ethernet.getMACAddress());
Kevin_Lee 0:29bbc7eec5e1 99 ret = ethernet.connect();
Kevin_Lee 0:29bbc7eec5e1 100 if (!ret) {
Kevin_Lee 0:29bbc7eec5e1 101 pc.printf("IP: %s, MASK: %s, GW: %s\r\n",
Kevin_Lee 0:29bbc7eec5e1 102 ethernet.getIPAddress(), ethernet.getNetworkMask(), ethernet.getGateway());
Kevin_Lee 0:29bbc7eec5e1 103 return 0;
Kevin_Lee 0:29bbc7eec5e1 104 } else {
Kevin_Lee 0:29bbc7eec5e1 105 pc.printf("Error ethernet.connect() - ret = %d\r\n", ret);
Kevin_Lee 0:29bbc7eec5e1 106 //exit(0);
Kevin_Lee 0:29bbc7eec5e1 107 return -1;
Kevin_Lee 0:29bbc7eec5e1 108 }
Kevin_Lee 0:29bbc7eec5e1 109 } else {
Kevin_Lee 0:29bbc7eec5e1 110 pc.printf("Error ethernet.init() - ret = %d\r\n", ret);
Kevin_Lee 0:29bbc7eec5e1 111 //exit(0);
Kevin_Lee 0:29bbc7eec5e1 112 return -1;
Kevin_Lee 0:29bbc7eec5e1 113 }
Kevin_Lee 3:cfc6ff08a668 114 }
Kevin_Lee 3:cfc6ff08a668 115
Kevin_Lee 3:cfc6ff08a668 116
Kevin_Lee 3:cfc6ff08a668 117 float temperature_get()
Kevin_Lee 3:cfc6ff08a668 118 {
Kevin_Lee 4:81af9a9f7844 119 /*
Kevin_Lee 4:81af9a9f7844 120 float resistance = (float)(1023.0-val)*10000.0/(float)val;
Kevin_Lee 4:81af9a9f7844 121 float temp = 1/(log(resistance/10000)/B+1/298.15)-273.15;
Kevin_Lee 4:81af9a9f7844 122 */
Kevin_Lee 3:cfc6ff08a668 123 int B = 3975;
Kevin_Lee 3:cfc6ff08a668 124 float val = (ain.read()*1023);
Kevin_Lee 3:cfc6ff08a668 125 float resistance = (float)(1023.0-val)*10000.0/(float)val;
Kevin_Lee 3:cfc6ff08a668 126 float temp = 1/(log(resistance/10000)/B+1/298.15)-273.15;
Kevin_Lee 3:cfc6ff08a668 127 return temp;
Kevin_Lee 3:cfc6ff08a668 128 }
Kevin_Lee 3:cfc6ff08a668 129