root root / Mbed 2 deprecated espyun

Dependencies:   WIZnetInterface mbed

Fork of espyun1 by jiang hao

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "EthernetInterface.h"
00003 #include "cJSON.h"
00004 
00005 DigitalInOut myIOD10(D10);
00006 DigitalIn myInD11(D11);
00007 DigitalOut myOutD12(D12);
00008 AnalogIn myInA1(A1);
00009 int socketPort = 8000;
00010 int httpPort = 80;
00011 
00012 Serial pc(PA_13,PA_14);
00013 
00014 int MallocSize = 800*sizeof(char);
00015 //bool socket_GET(TCPSocketConnection &conn);
00016 bool http_GET(TCPSocketConnection &conn);
00017 bool http_POST(TCPSocketConnection &conn,int vall);
00018 //bool socket_POST(TCPSocketConnection &conn,int vall);
00019 //bool closeConnect(TCPSocketConnection &conn);
00020 void receive_dht11();
00021 unsigned char receive_byte();
00022 void button_led();
00023 Timeout receive_dht11Timeout;
00024 Timeout button_ledTimeout;
00025 
00026 void delay(unsigned char ms)
00027 { 
00028     unsigned int y;
00029     while(ms--)
00030         for(y=1920; y>0; y--);
00031 }
00032 
00033 void Delay_us(unsigned char us)
00034 {
00035     unsigned char y;
00036     while(us--)
00037         for(y=2; y>0; y--);
00038 }
00039 
00040 float val;
00041 bool out=false;
00042 unsigned char RH,RL,TH,TL;
00043 unsigned char data_byte;
00044 unsigned int  U8FLAG;
00045 static unsigned char num = 4;
00046 const char receive_module[4][5] = {"Led", "Gas", "Hum", "Temp", };
00047 #if 1
00048 int main() { 
00049     int phy_link;
00050     pc.printf("Wait a second...\r\n");
00051     uint8_t mac_addr[6] = {0x78, 0x08, 0xDC, 0x1c, 0xa8, 0x95}; 
00052     receive_dht11Timeout.attach(&receive_dht11,0.2f);
00053     button_ledTimeout.attach(&button_led,0.1f);
00054     
00055     EthernetInterface eth;
00056     eth.init(mac_addr); //Use DHCP
00057     eth.connect();
00058     pc.printf("start IP Address is %s\r\n", eth.getIPAddress());
00059     
00060     do{
00061         phy_link = eth.ethernet_link();
00062         pc.printf("...");
00063         pc.printf("phy_link=%d...", phy_link);
00064         wait(2);
00065     }while(!phy_link);
00066     printf("\r\n");
00067     
00068     pc.printf("IP Address is %s\r\n", eth.getIPAddress());
00069 
00070     TCPSocketConnection conn;
00071     while(true)
00072     {   
00073         //wait(0.5f);
00074         if(num)
00075         {
00076             if (num == 4)
00077                 val = TH;
00078             else if (num == 3)
00079                 val = RH;
00080             else if (num == 2)
00081                 val = myInA1.read_u16();
00082             else if (num == 1)
00083                 val = myOutD12.read();
00084         }
00085         //int mathtest = ceil(val);//math.
00086         pc.printf("start http_post\r\n");
00087         wait(1);
00088         
00089         conn.connect("iot.espressif.cn", httpPort);
00090 
00091         http_POST(conn, val);
00092         
00093         conn.close();
00094         val = 0.0;  
00095         //mathtest = 0; 
00096         
00097         if(out)break;
00098     }
00099     
00100     //conn.close();            
00101     eth.disconnect();
00102     pc.printf("out the program!\r\n");
00103     return 0;                     
00104 }
00105 #else
00106 
00107 int main() { 
00108     pc.printf("Wait a second...\r\n");
00109     uint8_t mac_addr[6] = {0x78, 0x08, 0xDC, 0x1c, 0xa8, 0x95}; 
00110     
00111     EthernetInterface eth;
00112     eth.init(mac_addr); //Use DHCP
00113     eth.connect();
00114     pc.printf("start IP Address is %s\r\n", eth.getIPAddress());
00115     eth.ethernet_link();
00116     pc.printf("IP Address is %s\r\n", eth.getIPAddress());
00117 
00118     TCPSocketConnection conn;
00119     
00120     
00121     while(true)
00122     {   
00123         int ret = 0;
00124         pc.printf("start http_post\r\n");
00125         //ret = conn.connect("192.168.1.41", 10086);
00126         ret = conn.connect("iot.espressif.cn", httpPort);
00127         
00128         pc.printf("connect ret=%d\n",ret);
00129         ret = conn.send_all("asdfghh", 7);//http_cmd for http,pJSON for 
00130         pc.printf("send ret=%d\n",ret); 
00131         wait(1.0f);
00132         ret = conn.close();
00133         pc.printf("close ret=%d\n",ret);  
00134         wait(1.0f);
00135     }
00136     
00137            
00138     eth.disconnect();
00139     pc.printf("out the program!\r\n");
00140     return 0;                     
00141 }
00142 
00143 #endif
00144 /*http POST*/
00145 bool http_POST(TCPSocketConnection &conn, int val)
00146 {
00147     pc.printf("%s():%d\n",__FUNCTION__, __LINE__);
00148     //wait(0.5);
00149     /*if(conn.is_connected()){
00150         pc.printf("connect is already ok!\r\n");    
00151     }else{pc.printf("%s():%d\n",__FUNCTION__, __LINE__);
00152         if(!conn.connect("iot.espressif.cn", httpPort)) //80 for http,8000 for socket
00153             pc.printf("connect OK!\r\n");
00154         else{
00155             pc.printf("connct ERR!\r\n");
00156             out = true;
00157             return false;    
00158         }
00159     }*///conn.connect("iot.espressif.cn", httpPort);
00160     
00161     cJSON* http_post=NULL;  
00162     http_post = cJSON_CreateObject();    
00163     if(NULL == http_post)
00164     {
00165          pc.printf("OUT4\r\n");
00166          out = true;
00167          return false;
00168     }
00169     
00170     cJSON* http_post_json=NULL;  
00171     http_post_json = cJSON_CreateObject();    
00172     if(NULL == http_post_json)
00173     {
00174          cJSON_Delete(http_post);
00175          pc.printf("OUT5\r\n");
00176          out = true;
00177          return false;
00178     }
00179     cJSON_AddNumberToObject(http_post_json, "x", val);
00180     //cJSON_AddNumberToObject(http_post_json, "y", 2);
00181     //cJSON_AddNumberToObject(http_post_json, "z", 2);
00182     cJSON_AddItemToObject(http_post, "datapoint", http_post_json);
00183     char* pJSON_http_post = cJSON_PrintUnformatted(http_post);
00184     
00185     if(NULL == pJSON_http_post)
00186     {
00187         cJSON_Delete(http_post);
00188         pc.printf("OUT6\r\n");
00189         out = true;
00190         return false;
00191     }
00192     
00193     //Pay attention to out of memory
00194     //char http_POST_cmd[500] = "POST /v1/datastreams/jhtest/datapoint/ HTTP/1.1\r\nUser-Agent: curl/7.22.0 (i686-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3\r\n"\
00195                      // "Host:iot.espressif.cn\r\nAccept: */*\r\nAuthorization: token ebe1671e9d9f42a5231d017ec550552b9bae2cdf\r\n"\
00196                      // "Content-Length: 33\r\nContent-Type: application/x-www-form-urlencoded\r\n\r\n";
00197     char http_POST_cmd[500];// = "POST /v1/datastreams/test/datapoint/ HTTP/1.1\r\nUser-Agent: curl/7.22.0 (i686-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3\r\n"\
00198     //                  "Host:iot.espressif.cn\r\nAccept: */*\r\nAuthorization: token b1e92cf17fe4482647924941949ab2b07ffab548\r\n"\
00199     //                  "Content-Length: ";
00200     sprintf(http_POST_cmd,"POST /v1/datastreams/%s/datapoint/ HTTP/1.1\r\nUser-Agent: curl/7.35.0\r\n"\
00201                       "Host:iot.espressif.cn\r\nAccept: */*\r\nAuthorization: token b1e92cf17fe4482647924941949ab2b07ffab548\r\n"\
00202                       "Content-Length: ", receive_module[num-1]);
00203     num--;
00204     if (num == 0)
00205         num = 4;
00206         
00207     char len[2]; 
00208     len[0]=strlen(pJSON_http_post)/10%10+48;
00209     len[1]=strlen(pJSON_http_post)%10+48;
00210     strcat(http_POST_cmd,len);
00211     strcat(http_POST_cmd,"\r\nContent-Type: application/x-www-form-urlencoded\r\n\r\n");
00212      
00213     strcat(http_POST_cmd,pJSON_http_post);
00214     conn.send_all(http_POST_cmd, strlen(http_POST_cmd));//http_cmd for http,pJSON for socket,should be three changes
00215     pc.printf("%s\r\n",http_POST_cmd); 
00216     
00217     char *buffer = NULL;
00218     buffer = (char *)malloc(MallocSize);
00219     if(!buffer)pc.printf("malloc buffer err\r\n");
00220 
00221     /*int ret;
00222     pc.printf("6666\r\n");
00223     while (true) 
00224     {
00225         wait(1);pc.printf("%s():%d\n",__FUNCTION__, __LINE__);
00226         ret = conn.receive_all(buffer, MallocSize-1);pc.printf("%s():%d\n",__FUNCTION__, __LINE__);
00227         pc.printf("ret = %d\r\n",ret);
00228         if (ret <= 0) {
00229             ;//break;
00230         }
00231         buffer[ret] = '\0';
00232         pc.printf("Received %d chars from server: %s\n", ret, buffer);     
00233     }*/
00234     pc.printf("http_POST OUT\r\n");
00235 
00236     free(pJSON_http_post);
00237     free(buffer);
00238     cJSON_Delete(http_post);
00239     
00240     return true;
00241 }
00242 
00243 /*http_GET*/
00244 bool http_GET(TCPSocketConnection &conn)
00245 {
00246     if(conn.is_connected()){
00247         pc.printf("connect is already ok!\r\n");    
00248     }else{
00249         if(!conn.connect("iot.espressif.cn", httpPort)) //80 for http,8000 for socket
00250             pc.printf("connect OK!\r\n");
00251         else{
00252             pc.printf("connct ERR!\r\n");
00253             out = true;
00254             return false;    
00255         }
00256     }
00257     
00258     char http_cmd[] = "GET /v1/datastreams/Led/datapoint/ HTTP/1.1\r\nUser-Agent: curl/7.22.0 (i686-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3\r\n"\
00259                       "Host:iot.espressif.cn\r\nAccept: */*\r\nAuthorization: token b1e92cf17fe4482647924941949ab2b07ffab548\r\n\r\n";
00260     
00261     conn.send_all(http_cmd, strlen(http_cmd));//http_cmd for http,pJSON for socket,should be three changes
00262     pc.printf("%s\r\n",http_cmd);
00263     
00264     //char *buffer = NULL;
00265     //buffer = (char *)malloc(MallocSize);
00266     //if(!buffer)pc.printf("malloc buffer err\r\n");
00267     char buffer[500];
00268     int ret;
00269 
00270     while (true) {
00271         wait(1);
00272         ret = conn.receive(buffer, 500-1);
00273         pc.printf("ret = %d\r\n",ret);
00274         if (ret <= 0) {
00275             break;
00276         }
00277         buffer[ret] = '\0';
00278         pc.printf("Received %d chars from server: %s\n", ret, buffer);     
00279     }
00280     pc.printf("http_GET OUT\r\n");
00281     /* 
00282     if(conn.close()){
00283         pc.printf("conn.close OK\r\n");    
00284     }else{
00285         pc.printf("conn.close ERR\r\n");    
00286     }
00287     */
00288     //free(buffer);
00289     return true;
00290 }
00291 
00292 unsigned char receive_byte()
00293 {
00294     unsigned char i,temp;
00295     for(i=0;i<8;i++)
00296     {
00297         U8FLAG = 2;
00298         while(!myIOD10.read() && U8FLAG++);
00299         Delay_us(30);
00300         temp=0;
00301         if(myIOD10.read()==1)
00302         temp=1;
00303         U8FLAG = 2;
00304         while(myIOD10.read() && U8FLAG++);
00305         
00306         if(U8FLAG==1)break;
00307         data_byte<<=1;//
00308         data_byte|=temp;
00309     }
00310     return data_byte;
00311 }
00312 
00313 void receive_dht11()
00314 {
00315     unsigned char T_H,T_L,R_H,R_L,check,num_check;
00316     
00317    
00318     myIOD10.output();
00319     myIOD10.write(0);
00320     delay(20);
00321     
00322     myIOD10.write(1);
00323     Delay_us(25);
00324     myIOD10.write(1);
00325     
00326     myIOD10.input();
00327     if(!myIOD10.read())
00328     {
00329         while(!myIOD10.read());
00330         while(myIOD10.read());
00331         R_H=receive_byte();
00332         R_L=receive_byte();
00333         T_H=receive_byte();
00334         T_L=receive_byte();
00335         check=receive_byte();
00336         //pc.printf("--R_H--%d---R_L---%d--T_H--%d---T_L---%d\r\n", R_H,R_L,T_H,T_L);
00337         Delay_us(20);
00338         num_check=R_H+R_L+T_H+T_L;//pc.printf("--num_check--%d---check---%d\r\n", num_check,check);
00339         if(num_check==check)
00340         {
00341             RH=R_H;
00342             RL=R_L;
00343             TH=T_H;
00344             TL=T_L;
00345             check=num_check;
00346         }
00347     }
00348     
00349     //pc.printf("humidity [H] -out--%d\r\n", RH);
00350     //pc.printf("humidity [L] -out--%d\r\n", RL);
00351     //pc.printf("temperature [H] -out--%d\r\n", TH);
00352     //pc.printf("temperature [L] -out--%d\r\n", TL);
00353     
00354     receive_dht11Timeout.attach(&receive_dht11,0.2f);
00355 }
00356 
00357 void button_led()
00358 {  
00359     while(myInD11.read())
00360     {
00361         wait(0.01f);
00362         if(!myInD11.read())
00363         {
00364             myOutD12.write(!myOutD12.read());
00365             break;
00366         }
00367     }
00368     
00369     button_ledTimeout.attach(&button_led,0.1f);
00370 }