for znrobotics workshop
Dependencies: DHT22 HTTPClient SDFileSystem WIZnet_Library mbed
Fork of archlink_Temperture_dweetio by
main.cpp
00001 // provided by znrobotics. 00002 #include "mbed.h" 00003 #include "SDFileSystem.h" 00004 #include "WIZnetInterface.h" 00005 #include "HTTPClient.h" 00006 #include "DHT22.h" 00007 00008 #define USE_DHCP 1 00009 #define LOOPBACKPORT 5000 00010 00011 const int USE_HTTP_PROTOCAL = 1; 00012 const int USE_COAP_PROTOCAL = 2; 00013 const int USE_MQTT_PROTOCAL = 3; 00014 00015 int W5500_Test(void); 00016 00017 float* temperature_get(); /* Grove - Temperature Sensor V1.2 */ 00018 float th[2]; 00019 00020 SPI spi(SPI_PSELMOSI0, SPI_PSELMISO0, SPI_PSELSCK0); 00021 WIZnetInterface ethernet(&spi, p24, p17); // Spi ,cs, reset 00022 int ret, dummy, lv = 1; 00023 00024 const char* http_port = "80"; 00025 const char* coap_port = "5683"; 00026 const char* mqtt_port = "1883"; 00027 00028 // todo:: your settings here. 00029 //const char * SERVER_REMOTE = "58.214.20.195"; 00030 const char * SERVER_REMOTE = "52.76.14.222"; 00031 const char * IP_Addr = "192.168.1.223"; 00032 const char * IP_Subnet = "255.255.255.0"; 00033 const char * IP_Gateway = "192.168.1.1"; 00034 const char * DEVICE_ID = "1570f165-89ca-4fdf-a625-86736c8afd23"; 00035 unsigned char MAC_Addr[6] = {0x00,0x08,0xDC,0x1C,0xAA,0xCA}; 00036 // which protocal to use 00037 // 1 => HTTP; 00038 // 2 => CoAP; 00039 // 3 => MQTT 00040 const int PROTOCAL_INUSE = 1; 00041 // unit: seconds; 00042 const int time_interval = 60; //seconds 00043 00044 // timer event; 00045 Ticker flipper; 00046 void timer_ticked(); 00047 00048 DigitalOut myled(LED1); 00049 //Arch Link 00050 Serial pc(USBTX, USBRX); /* uart */ 00051 DHT22 dht22(p6); 00052 00053 AnalogIn ain(A0); 00054 00055 int main() { 00056 wait(1); 00057 pc.baud(9600); 00058 wait(1); 00059 00060 if(W5500_Test() == 0) { // Internet is ok 00061 printf("W5500 tested OK \r\n"); 00062 00063 // start timer tick event. 00064 flipper.attach(&timer_ticked, time_interval); 00065 } 00066 00067 while(1) { 00068 myled = !myled; 00069 wait(10); 00070 } 00071 } 00072 00073 void timer_ticked() 00074 { 00075 char str[512]; 00076 char get_msg[512]= ""; 00077 00078 float* th = temperature_get(); 00079 printf("temperature: %.2f -- Humidity: %.2f.\r\n",th[0], th[1]); 00080 00081 pc.printf("posting message to znrobotics server.\r\n"); 00082 00083 switch (PROTOCAL_INUSE) 00084 { 00085 case USE_HTTP_PROTOCAL: 00086 { 00087 HTTPClient http; 00088 00089 sprintf(get_msg, "http://%s/sensors/things?deviceid=%s&temperature=%.2f", SERVER_REMOTE, DEVICE_ID, th[0]); 00090 pc.printf("msg : %s\r\n",get_msg); 00091 ret = http.get(get_msg, str, sizeof(str)); 00092 if(!ret) 00093 { 00094 pc.printf("Response fetched successfully - read %d characters\r\n", strlen(str)); 00095 pc.printf("Result: %s\r\n", str); 00096 } 00097 else 00098 { 00099 pc.printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); 00100 } 00101 00102 sprintf(get_msg, "http://%s/sensors/things?deviceid=%s&humidity=%.2f", SERVER_REMOTE, DEVICE_ID, th[1]); 00103 pc.printf("msg : %s\r\n",get_msg); 00104 ret = http.get(get_msg, str, sizeof(str)); 00105 if(!ret) 00106 { 00107 pc.printf("Response fetched successfully - read %d characters\r\n", strlen(str)); 00108 pc.printf("Result: %s\r\n", str); 00109 } 00110 else 00111 { 00112 pc.printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); 00113 } 00114 } 00115 break; 00116 case USE_COAP_PROTOCAL: 00117 { 00118 00119 } 00120 break; 00121 case USE_MQTT_PROTOCAL: 00122 { 00123 00124 } 00125 break; 00126 } 00127 00128 pc.printf("\n"); 00129 } 00130 00131 int W5500_Test(void) 00132 { 00133 mbed_mac_address((char *)MAC_Addr); //Use mbed mac addres 00134 wait(1); 00135 printf("Start to test ethernet!\r\n"); 00136 00137 #if USE_DHCP 00138 printf("use DHCP\r\n"); 00139 ret = ethernet.init(MAC_Addr); 00140 #else 00141 printf("do NOT use DHCP\r\n"); 00142 int ret = ethernet.init(MAC_Addr,IP_Addr,IP_Subnet,IP_Gateway); 00143 #endif 00144 00145 if (!ret) { 00146 pc.printf("Initialized, MAC: %s\r\n", ethernet.getMACAddress()); 00147 ret = ethernet.connect(); 00148 if (!ret) { 00149 pc.printf("IP: %s, MASK: %s, GW: %s\r\n", ethernet.getIPAddress(), ethernet.getNetworkMask(), ethernet.getGateway()); 00150 return 0; 00151 } else { 00152 pc.printf("Error ethernet.connect() - ret = %d\r\n", ret); 00153 //exit(0); 00154 return -1; 00155 } 00156 } else { 00157 pc.printf("Error ethernet.init() - ret = %d\r\n", ret); 00158 //exit(0); 00159 return -1; 00160 } 00161 } 00162 00163 float* temperature_get() 00164 { 00165 int error = 0; 00166 00167 float temp, hum; 00168 00169 error = dht22.sample(); 00170 00171 // read successfully 00172 if (1 == error) { 00173 // YOUR CODE GOES HERE, read temperature and humidity 00174 temp = dht22.getTemperature()/10.0f; //TODO; 00175 hum = dht22.getHumidity()/10.0f; //TODO; 00176 th[0] = temp; 00177 th[1] = hum; 00178 // printf("temp: %2.2f , hum:%2.2f \r\n",temp,hum); 00179 } else { 00180 printf("Error: %d\n", error); 00181 } 00182 00183 return th; 00184 }
Generated on Wed Jul 13 2022 23:40:02 by 1.7.2