2014-11-14 11:39:29 [显示全部楼层]
22982浏览
查看: 22982|回复: 42

通过网页按钮控制台灯,wido做服务器

[复制链接]
本帖最后由 lisper 于 2014-11-26 21:05 编辑

本帖用到之前做的插座:https://mc.dfrobot.com.cn/thread-2631-1-1.html

结合目前很火的WiDo WiFi物联网主控器https://www.dfrobot.com.cn/goods-997.html

和一个普通的台灯,即可实现远程网络控制台灯的开关,不需要APP,不要命令,只需要知道“台灯”的IP地址,用浏览器打开,就可以出现控制页面,目前页面上只有两个按钮,open和close。


  1. /*
  2. * Copyright (C) 2014 DFRobot                                                  
  3. *                                                                             
  4. */
  5. /*
  6. *        Author:                                lisper <lisper.li@dfrobot.com>
  7. *        Date:                                2014-08-21
  8. *        official website:                http://www.dfrobot.com
  9. *        Description:                        control led by web button
  10. */
  11. #include <Adafruit_CC3000.h>
  12. #include <SPI.h>
  13. #include "utility/debug.h"
  14. #include "utility/socket.h"
  15. // These are the interrupt and control pins
  16. #define ADAFRUIT_CC3000_IRQ   7  // MUST be an interrupt pin!
  17. // These can be any two pins
  18. #define ADAFRUIT_CC3000_VBAT  5
  19. #define ADAFRUIT_CC3000_CS    10
  20. // Use hardware SPI for the remaining pins
  21. // On an UNO, SCK = 13, MISO = 12, and MOSI = 11
  22. Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT,
  23. SPI_CLOCK_DIVIDER); // you can change this clock speed
  24. #define WLAN_SSID       "yourssid"           // cannot be longer than 32 characters!
  25. #define WLAN_PASS       "password"
  26. // Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2
  27. #define WLAN_SECURITY   WLAN_SEC_WPA2
  28. #define LISTEN_PORT           80   // What TCP port to listen on for connections.
  29. Adafruit_CC3000_Server webServer(LISTEN_PORT);
  30. boolean led_state;
  31. //
  32. void setup(void) {
  33.         pinMode (11, OUTPUT);
  34.         pinMode (12, OUTPUT);
  35.         pinMode (13, OUTPUT);
  36.         //pin normal state
  37.         digitalWrite (11, LOW);  
  38.         digitalWrite (12, HIGH);
  39.         digitalWrite (13, LOW);
  40.         Serial.begin(115200);
  41.         Serial.println(F("Hello, CC3000!\n"));
  42.         //while (!Serial);
  43.         //Serial.println ("Input any key to start:");
  44.         //while (!Serial.available ());
  45.         Serial.print("Free RAM: ");
  46.         Serial.println(getFreeRam(), DEC);
  47.         /* Initialise the module */
  48.         Serial.println(F("\nInitializing..."));
  49.         if (!cc3000.begin()) {
  50.                 Serial.println(F("Couldn't begin()! Check your wiring?"));
  51.                 while(1);
  52.         }
  53.         Serial.print(F("\nAttempting to connect to "));
  54.         Serial.println(WLAN_SSID);
  55.         if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
  56.                 Serial.println(F("Failed!"));
  57.                 while(1);
  58.         }
  59.         Serial.println(F("Connected!"));
  60.         Serial.println(F("Request DHCP"));
  61.         while (!cc3000.checkDHCP()) {
  62.                 delay(100); // ToDo: Insert a DHCP timeout!
  63.         }  
  64.         /* Display the IP address DNS, Gateway, etc. */
  65.         while (! displayConnectionDetails()) {
  66.                 delay(1000);
  67.         }
  68.         // Start listening for connections
  69.         webServer.begin();
  70.         Serial.println(F("Listening for connections..."));
  71. }
  72. //
  73. void loop(void) {
  74.         // Try to get a client which is connected.
  75.         Adafruit_CC3000_ClientRef client = webServer.available();
  76.         if (client) {
  77.                 processInput (client);
  78.                 sendWebPage (client);
  79.         }
  80.         client.close();
  81. }
  82. //
  83. void processInput (Adafruit_CC3000_ClientRef client) {
  84.         char databuffer[45];
  85.         while (client.available ()) {
  86.                 client.read (databuffer, 40);
  87.                 char* sub = strchr (databuffer, '\r');
  88.                 if (sub > 0)
  89.                         *sub = '\0';
  90.                 Serial.println (databuffer);
  91.                 sub = strstr (databuffer, "control");
  92.                 if (!sub) {
  93.                         //Serial.println ("no control");                        
  94.                         break;
  95.                 }
  96.                 sub = strstr (sub, "led");
  97.                 if (!sub) {
  98.                         //Serial.println ("no led");                        
  99.                         break;
  100.                 }
  101.                 sub += 4;
  102.                 if (strncmp (sub, "open", 4) == 0) {
  103.                         Serial.println ("clicked open");
  104.                         digitalWrite (11, HIGH);  
  105.                         digitalWrite (12, LOW);  
  106.                         digitalWrite (13, HIGH);
  107.                         led_state = true;
  108.                 }
  109.                 else if (strncmp (sub, "close", 5) == 0) {
  110.                         Serial.println ("clicked close");
  111.                         digitalWrite (11, LOW);  
  112.                         digitalWrite (12, HIGH);
  113.                         digitalWrite (13, LOW);
  114.                         led_state = false;
  115.                 }
  116.                 break;
  117.         }
  118. }
  119. void sendWebPage (Adafruit_CC3000_ClientRef client) {
  120.         webServer.write ("<!DOCTYPE html>");
  121.         webServer.write ("<html>");
  122.         webServer.write ("<body  style="font-size:50px">");
  123.         webServer.write ("<form action="control" method="get">");
  124.         webServer.write ("<button name="led" style="height:200px;width:400px;font-size:50px"");
  125.         webServer.write ("type="submit" value="open">Open</button>");
  126.         webServer.write ("<br><br>");
  127.         webServer.write ("<button name="led" style="height:200px;width:400px;font-size:50px"");
  128.         webServer.write ("type="submit" value="close">Close</button>");
  129.         webServer.write ("</form>");
  130.         webServer.write ("<br><br><p>Pin11 status: ");
  131.         webServer.write (led_state ? "HIGH" : "LOW");        
  132.         webServer.write ("<br><p>Pin12 status: ");
  133.         webServer.write (led_state ? "LOW" : "HIGH");               
  134.         webServer.write ("</p></body>");
  135.         webServer.write ("</html>");
  136.         delay (20);
  137.         client.close();
  138. }
  139. //
  140. bool displayConnectionDetails(void) {
  141.         uint32_t ipAddress, netmask, gateway, dhcpserv, dnsserv;
  142.         if(!cc3000.getIPAddress(&ipAddress, &netmask, &gateway, &dhcpserv, &dnsserv)) {
  143.                 Serial.println(F("Unable to retrieve the IP Address!\r\n"));
  144.                 return false;
  145.         }
  146.         else {
  147.                 Serial.print(F("\nIP Addr: "));
  148.                 cc3000.printIPdotsRev(ipAddress);
  149.                 Serial.print(F("\nNetmask: "));
  150.                 cc3000.printIPdotsRev(netmask);
  151.                 Serial.print(F("\nGateway: "));
  152.                 cc3000.printIPdotsRev(gateway);
  153.                 Serial.print(F("\nDHCPsrv: "));
  154.                 cc3000.printIPdotsRev(dhcpserv);
  155.                 Serial.print(F("\nDNSserv: "));
  156.                 cc3000.printIPdotsRev(dnsserv);
  157.                 Serial.println();
  158.                 return true;
  159.         }
  160. }
复制代码

该例由于使用的是动态IP,第一次运行要先通过串口查看wido的IP,然后在浏览器地址输入框中输入其IP,就可显示控制页面
IMG_20141114_111714.jpg
Screenshot_2014-11-14-11-38-35.png

ejiyuan1  初级技师

发表于 2014-11-14 14:30:04

lisper 发表于 2014-11-14 13:49
wido自己做服务器,它还要连什么服务器?!

前辈问你一下我看到威龙驱动器有一个设置驱动器地址的命令,是不是意味着我用一个串口可以并行控制好几个威龙驱动器,只要将他们的ID地址设置为不同的值就可以

还有就是之前技术给了我一个威龙的库,我想问的是我用串口闭环模式读取转速时,是UNO需要在RX端并上一个电阻,当使用2560和wido时不用并电阻就可以读取转速吗?
回复

使用道具 举报

lisper  中级技匠
 楼主|

发表于 2014-11-18 17:38:44

Jane 发表于 2014-11-18 17:17
好吧 怎么连接的呢?貌似光有个代码也不够啊。此帖太深奥,我们普通人看不懂啊。劳驾楼主能否写再详细点 ...

不知道怎么接继电器的没必要看这个帖子,不知道怎么接VCC,GND,数字口的没必要看这个帖子
回复

使用道具 举报

lisper  中级技匠
 楼主|

发表于 2014-11-14 17:19:56

串电阻还是并电阻?“当使用2560和wido时不用并电阻就可以读取转速吗?”为什么这么问?串电阻是因为Uno的io电压是5V,驱动器是3.3V,因为串联分压,所以Uno需要串电阻给io口降压,uno,mega2560,wido的io口都是5V,所以。。。
回复

使用道具 举报

Rockets  NPC

发表于 2014-11-14 12:14:04

敢不敢在geeker一些
太直接了吧
有这样就直接进入主体的吗?
回复

使用道具 举报

lisper  中级技匠
 楼主|

发表于 2014-11-14 13:00:06

Rockets 发表于 2014-11-14 12:14
敢不敢在geeker一些
太直接了吧
有这样就直接进入主体的吗?

你不懂。。。
回复

使用道具 举报

ejiyuan1  初级技师

发表于 2014-11-14 13:14:26

把连接服务器的程序放在loop里是不是更好些,要不然断线的怎么办
回复

使用道具 举报

lisper  中级技匠
 楼主|

发表于 2014-11-14 13:49:59

ejiyuan1 发表于 2014-11-14 13:14
把连接服务器的程序放在loop里是不是更好些,要不然断线的怎么办

:owido自己做服务器,它还要连什么服务器?!
回复

使用道具 举报

hdc  中级技师

发表于 2014-11-15 23:01:56

lisper 发表于 2014-11-14 13:49
wido自己做服务器,它还要连什么服务器?!

能不能固定IP的? 然后还可以加上定时功能?
回复

使用道具 举报

lisper  中级技匠
 楼主|

发表于 2014-11-17 09:39:06

hdc 发表于 2014-11-15 23:01
能不能固定IP的? 然后还可以加上定时功能?

可以的~
回复

使用道具 举报

ejiyuan1  初级技师

发表于 2014-11-17 13:55:23

使用wido写了不到200行的代码,内存空间就快用完了,有没有解决的办法
回复

使用道具 举报

lisper  中级技匠
 楼主|

发表于 2014-11-17 16:11:23

ejiyuan1 发表于 2014-11-17 13:55
使用wido写了不到200行的代码,内存空间就快用完了,有没有解决的办法

用MK2烧写器,把bootloader占用的空间腾出来用
回复

使用道具 举报

冰渕  中级技师

发表于 2014-11-17 17:55:20

貌似wido最近很火嘛~~
回复

使用道具 举报

Ricky  NPC

发表于 2014-11-17 19:54:24

图啊,连线图呢?  
回复

使用道具 举报

Jane  高级技匠

发表于 2014-11-18 17:01:25

楼主这是来炫代码的吗~除了代码还是代码
回复

使用道具 举报

lisper  中级技匠
 楼主|

发表于 2014-11-18 17:05:16

冰渕 发表于 2014-11-17 17:55
貌似wido最近很火嘛~~

已经火了有一阵子了
回复

使用道具 举报

Grey  中级技匠

发表于 2014-11-18 17:10:50

大神好厉害!
回复

使用道具 举报

lisper  中级技匠
 楼主|

发表于 2014-11-18 17:11:01

Jane 发表于 2014-11-18 17:01
楼主这是来炫代码的吗~除了代码还是代码

这是应大量客户的要求发的帖子
回复

使用道具 举报

Jane  高级技匠

发表于 2014-11-18 17:17:43

lisper 发表于 2014-11-18 17:11
这是应大量客户的要求发的帖子

好吧 怎么连接的呢?貌似光有个代码也不够啊。此帖太深奥,我们普通人看不懂啊。劳驾楼主能否写再详细点啊~中间省略10000字的习惯不好啊~
回复

使用道具 举报

lisper  中级技匠
 楼主|

发表于 2014-11-18 17:39:36


为什么?
回复

使用道具 举报

Grey  中级技匠

发表于 2014-11-18 18:13:06

有掉线的现象吗?
回复

使用道具 举报

123下一页
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail