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++代码
- #include <LeweiClient.h>
- #include <SPI.h>
- #include <Ethernet.h>
- #include <Wire.h> //BH1750 IIC Mode
- #define LW_USERKEY "8xxxx05"
- #define LW_GATEWAY "01"
- #define MY_NAME "UNO1"
- #define MY_DESC "UNO test case"
- #define MY_ADDR "http://192.168.1.233/api"
- //delay between updates
- #define POST_INTERVAL (10*1000)
- LeWeiClient *lwc;
- const unsigned long postingInterval = 10*1000;
- class dummy_act: public LeWeiActuator
- {
- private:char led;
- public:
- dummy_act(
- const char *id,
- const char *type,
- const char *name) : LeWeiActuator(id, type, name)
- {};
- /* 用户代码1 :根绝dev id 做相关控制操作 */
- virtual bool updateValue(char* id,int val)
- {
- Serial.print("dev id = ");
- Serial.println(id);
- if(!strcmp(id,"DA"))
- {
- Serial.println("handle da routin");
- }
- if(!strcmp(id,"DA2"))
- {
- Serial.println("handle da2 routin");
- }
- Serial.print("update actuator ");
- Serial.println(val);
- led=val;
- return val;
- }
- /* 用户代码1结束 */
- virtual bool getValue(int *val)
- {
- static int i = 0;
- *val = led;
- return true;
- }
- };
- /* 用户代码2 :注册 DA DA2 两个执行设备 */
- dummy_act the_act("DA", "dummy actuator", "dummy-da");
- dummy_act the_act2("DA2", "dummy actuator", "dummy-da2");
- /* 用户代码2 结束 */
- void setup() {
- Serial.begin(9600);
- uint8_t mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
- #if 1
- IPAddress myip(192, 168, 1, 233);
- IPAddress dnsip(8, 8, 8, 8);
- IPAddress gateway(192, 168, 1, 253);
- Ethernet.begin(mac, myip, dnsip, gateway);
- #else
- if (Ethernet.begin(mac) == 0)
- {
- Serial.print(F("Failed to configure Ethernet using DHCP "));
- }
- else
- {
- Serial.print(F("Ethernet configuration OK "));
- }
- #endif
- // hope no exception here
- lwc = new LeWeiClient(LW_USERKEY, LW_GATEWAY,
- MY_NAME, MY_DESC, MY_ADDR, LeWeiClient::none);
- lwc->registerActuator(the_act);
- lwc->registerActuator(the_act2);
- Serial.print(lwc->nrSensors());
- Serial.println(F(" sensors registered."));
- Serial.print(lwc->nrActuators());
- Serial.println(F(" actuators registered."));
- lwc->initDevices();
- lwc->beginServe(80);
- Serial.println(F("upload gateway info to server"));
- int retry = 10;
- while (lwc->uploadInfo() < 0 && --retry)
- {
- delay(1000);
- }
- if (!retry)
- Serial.println(F("uploadInfo failed in 10 times"));
- else
- Serial.println(F("uploadInfo done"));
- }
- void loop() {
- static unsigned int loop_count;
- if (lwc) {
- loop_count++;
- Serial.print(F("*** loop nr: "));
- Serial.println(loop_count);
- /*
- *Serial.print(F("*** start log send "));
- *lwc->sendLog("I'm alive.");
- */
- //delay(POST_INTERVAL);
- Serial.println(F("*** serve"));
- for (int i = 0; i < 100; i++) {
- lwc->serve();
- delay(100);
- }
- }
- }
下载乐联网app :open.lewei50.com/home/sitecontent/ydkhd
注意:手机需要wifi模式,而且需要与arduino 处在同一网段。
打开手机,选择 反向控制,api地址应该自动填写好http://192.168.1.233/api 请求类型get
点击”列出可控设备“
点击dummy1 和dummy 2 就可以执行相应的操作了。