arduino + w5100实现控制 (by leweiclient)-同一网段

2013-02-19 16:58

 

 arduino 代码由@grissiom 提供

 

推荐阅读:http://www.lewei50.com/home/news/92 代替本文档

  

 

相关下载:

 1 乐联网app 下载 :open.lewei50.com/home/sitecontent/ydkhd

 2 leweiclient 下载:https://github.com/lewei50/leweiclient

设置网关可控,ip地址为arduino 程序 中的ip地址(红色标出)。

C++代码
  1. #include <LeweiClient.h>  
  2. #include <SPI.h>  
  3. #include <Ethernet.h>  
  4. #include <Wire.h> //BH1750 IIC Mode  
  5.   
  6. #define LW_USERKEY "8xxxx05"  
  7. #define LW_GATEWAY "01"  
  8.   
  9. #define MY_NAME    "UNO1"  
  10. #define MY_DESC    "UNO test case"  
  11. #define MY_ADDR    "http://192.168.1.233/api"  
  12.   
  13. //delay between updates  
  14. #define POST_INTERVAL (10*1000)  
  15.   
  16. LeWeiClient *lwc;  
  17.   
  18. const unsigned long postingInterval = 10*1000;  
  19.   
  20.   
  21.   
  22. class dummy_act: public LeWeiActuator    
  23.     
  24. {    
  25.     
  26.    private:char led;     
  27.     
  28.    public:    
  29.     
  30.         dummy_act(    
  31.     
  32.                 const char *id,    
  33.     
  34.                 const char *type,     
  35.     
  36.                 const char *name) : LeWeiActuator(id, type, name)    
  37.     
  38.     {};    
  39.     
  40.        /* 用户代码1 :根绝dev id 做相关控制操作 */
  41.         virtual bool updateValue(char* id,int val)    
  42.         
  43.         {    
  44.     
  45.             Serial.print("dev id = ");     
  46.             Serial.println(id);    
  47.             if(!strcmp(id,"DA"))  
  48.             {  
  49.               Serial.println("handle da routin");    
  50.             }  
  51.             if(!strcmp(id,"DA2"))  
  52.             {  
  53.               Serial.println("handle da2 routin");    
  54.             }  
  55.             Serial.print("update actuator ");    
  56.     
  57.             Serial.println(val);    
  58.     
  59.             led=val;    
  60.     
  61.             return val;    
  62.     
  63.         }    
  64.           /* 用户代码1结束 */
  65.  
  66.         virtual bool getValue(int *val)    
  67.     
  68.         {    
  69.     
  70.             static int i = 0;    
  71.     
  72.             *val = led;    
  73.     
  74.             return true;    
  75.     
  76.         }    
  77.     
  78. };    
  79.   
  80.       /* 用户代码2 :注册 DA DA2 两个执行设备 */ 
  81. dummy_act the_act("DA""dummy actuator""dummy-da");  
  82. dummy_act the_act2("DA2""dummy actuator""dummy-da2");  
  83.        /* 用户代码2 结束 */  
  84. void setup() {  
  85.     Serial.begin(9600);  
  86.   
  87.     uint8_t mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};  
  88. #if 1  
  89.     IPAddress  myip(192, 168, 1, 233);  
  90.     IPAddress  dnsip(8, 8, 8, 8);  
  91.     IPAddress  gateway(192, 168, 1, 253);  
  92.     Ethernet.begin(mac, myip, dnsip, gateway);  
  93. #else  
  94.     if (Ethernet.begin(mac) == 0)  
  95.     {  
  96.         Serial.print(F("Failed to configure Ethernet using DHCP "));  
  97.     }  
  98.     else  
  99.     {  
  100.         Serial.print(F("Ethernet configuration OK "));  
  101.     }  
  102. #endif  
  103.   
  104.     // hope no exception here  
  105.     lwc = new LeWeiClient(LW_USERKEY, LW_GATEWAY,  
  106.             MY_NAME, MY_DESC, MY_ADDR, LeWeiClient::none);  
  107.   
  108.   
  109.   
  110.     lwc->registerActuator(the_act);  
  111.     lwc->registerActuator(the_act2);  
  112.   
  113.     Serial.print(lwc->nrSensors());  
  114.     Serial.println(F(" sensors registered."));  
  115.     Serial.print(lwc->nrActuators());  
  116.     Serial.println(F(" actuators registered."));  
  117.   
  118.     lwc->initDevices();  
  119.   
  120.     lwc->beginServe(80);  
  121.   
  122.     Serial.println(F("upload gateway info to server"));  
  123.     int retry = 10;  
  124.     while (lwc->uploadInfo() < 0 && --retry)  
  125.     {  
  126.         delay(1000);  
  127.     }  
  128.     if (!retry)  
  129.         Serial.println(F("uploadInfo failed in 10 times"));  
  130.     else  
  131.         Serial.println(F("uploadInfo done"));  
  132. }  
  133.   
  134. void loop() {  
  135.     static unsigned int loop_count;  
  136.     if (lwc) {  
  137.         loop_count++;  
  138.   
  139.         Serial.print(F("*** loop nr: "));  
  140.         Serial.println(loop_count);  
  141.         /* 
  142.          *Serial.print(F("*** start log send ")); 
  143.          *lwc->sendLog("I'm alive."); 
  144.          */  
  145.         //delay(POST_INTERVAL);  
  146.         Serial.println(F("*** serve"));  
  147.         for (int i = 0; i < 100; i++) {  
  148.             lwc->serve();  
  149.             delay(100);  
  150.         }  
  151.     }  
  152.   
  153. }  

 

 

下载乐联网app :open.lewei50.com/home/sitecontent/ydkhd

注意:手机需要wifi模式,而且需要与arduino 处在同一网段。

打开手机,选择 反向控制,api地址应该自动填写好http://192.168.1.233/api 请求类型get

点击”列出可控设备“

点击dummy1 和dummy 2 就可以执行相应的操作了。