本帖最后由 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。
-
- /*
- * Copyright (C) 2014 DFRobot
- *
- */
-
- /*
- * Author: lisper <lisper.li@dfrobot.com>
- * Date: 2014-08-21
- * official website: http://www.dfrobot.com
- * Description: control led by web button
- */
-
- #include <Adafruit_CC3000.h>
- #include <SPI.h>
- #include "utility/debug.h"
- #include "utility/socket.h"
-
- // These are the interrupt and control pins
- #define ADAFRUIT_CC3000_IRQ 7 // MUST be an interrupt pin!
- // These can be any two pins
- #define ADAFRUIT_CC3000_VBAT 5
- #define ADAFRUIT_CC3000_CS 10
- // Use hardware SPI for the remaining pins
- // On an UNO, SCK = 13, MISO = 12, and MOSI = 11
- Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT,
- SPI_CLOCK_DIVIDER); // you can change this clock speed
-
-
- #define WLAN_SSID "yourssid" // cannot be longer than 32 characters!
- #define WLAN_PASS "password"
- // Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2
- #define WLAN_SECURITY WLAN_SEC_WPA2
-
- #define LISTEN_PORT 80 // What TCP port to listen on for connections.
-
- Adafruit_CC3000_Server webServer(LISTEN_PORT);
- boolean led_state;
- //
- void setup(void) {
- pinMode (11, OUTPUT);
- pinMode (12, OUTPUT);
- pinMode (13, OUTPUT);
-
- //pin normal state
- digitalWrite (11, LOW);
- digitalWrite (12, HIGH);
- digitalWrite (13, LOW);
-
- Serial.begin(115200);
- Serial.println(F("Hello, CC3000!\n"));
- //while (!Serial);
- //Serial.println ("Input any key to start:");
- //while (!Serial.available ());
- Serial.print("Free RAM: ");
- Serial.println(getFreeRam(), DEC);
-
- /* Initialise the module */
- Serial.println(F("\nInitializing..."));
- if (!cc3000.begin()) {
- Serial.println(F("Couldn't begin()! Check your wiring?"));
- while(1);
- }
-
- Serial.print(F("\nAttempting to connect to "));
- Serial.println(WLAN_SSID);
- if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
- Serial.println(F("Failed!"));
- while(1);
- }
-
- Serial.println(F("Connected!"));
-
- Serial.println(F("Request DHCP"));
- while (!cc3000.checkDHCP()) {
- delay(100); // ToDo: Insert a DHCP timeout!
- }
-
- /* Display the IP address DNS, Gateway, etc. */
- while (! displayConnectionDetails()) {
- delay(1000);
- }
-
- // Start listening for connections
- webServer.begin();
- Serial.println(F("Listening for connections..."));
- }
-
- //
- void loop(void) {
- // Try to get a client which is connected.
- Adafruit_CC3000_ClientRef client = webServer.available();
- if (client) {
- processInput (client);
- sendWebPage (client);
- }
- client.close();
- }
-
- //
- void processInput (Adafruit_CC3000_ClientRef client) {
- char databuffer[45];
- while (client.available ()) {
- client.read (databuffer, 40);
-
- char* sub = strchr (databuffer, '\r');
- if (sub > 0)
- *sub = '\0';
- Serial.println (databuffer);
- sub = strstr (databuffer, "control");
- if (!sub) {
- //Serial.println ("no control");
- break;
- }
- sub = strstr (sub, "led");
- if (!sub) {
- //Serial.println ("no led");
- break;
- }
- sub += 4;
- if (strncmp (sub, "open", 4) == 0) {
- Serial.println ("clicked open");
- digitalWrite (11, HIGH);
- digitalWrite (12, LOW);
- digitalWrite (13, HIGH);
- led_state = true;
- }
- else if (strncmp (sub, "close", 5) == 0) {
- Serial.println ("clicked close");
- digitalWrite (11, LOW);
- digitalWrite (12, HIGH);
- digitalWrite (13, LOW);
- led_state = false;
- }
- break;
- }
- }
-
- void sendWebPage (Adafruit_CC3000_ClientRef client) {
- webServer.write ("<!DOCTYPE html>");
- webServer.write ("<html>");
- webServer.write ("<body style="font-size:50px">");
- webServer.write ("<form action="control" method="get">");
- webServer.write ("<button name="led" style="height:200px;width:400px;font-size:50px"");
- webServer.write ("type="submit" value="open">Open</button>");
- webServer.write ("<br><br>");
- webServer.write ("<button name="led" style="height:200px;width:400px;font-size:50px"");
- webServer.write ("type="submit" value="close">Close</button>");
- webServer.write ("</form>");
- webServer.write ("<br><br><p>Pin11 status: ");
- webServer.write (led_state ? "HIGH" : "LOW");
- webServer.write ("<br><p>Pin12 status: ");
- webServer.write (led_state ? "LOW" : "HIGH");
- webServer.write ("</p></body>");
- webServer.write ("</html>");
- delay (20);
- client.close();
- }
-
- //
- bool displayConnectionDetails(void) {
- uint32_t ipAddress, netmask, gateway, dhcpserv, dnsserv;
-
- if(!cc3000.getIPAddress(&ipAddress, &netmask, &gateway, &dhcpserv, &dnsserv)) {
- Serial.println(F("Unable to retrieve the IP Address!\r\n"));
- return false;
- }
- else {
- Serial.print(F("\nIP Addr: "));
- cc3000.printIPdotsRev(ipAddress);
- Serial.print(F("\nNetmask: "));
- cc3000.printIPdotsRev(netmask);
- Serial.print(F("\nGateway: "));
- cc3000.printIPdotsRev(gateway);
- Serial.print(F("\nDHCPsrv: "));
- cc3000.printIPdotsRev(dhcpserv);
- Serial.print(F("\nDNSserv: "));
- cc3000.printIPdotsRev(dnsserv);
- Serial.println();
- return true;
- }
- }
-
-
-
复制代码
该例由于使用的是动态IP,第一次运行要先通过串口查看wido的IP,然后在浏览器地址输入框中输入其IP,就可显示控制页面 |