5377| 3
|
[讨论交流] wido使用GET方法查询Onenet网站数据流 |
#include <Adafruit_CC3000.h> #include <ccspi.h> #include <SPI.h> #include <HttpPacket.h> #define Wido_IRQ 7 #define Wido_VBAT 5 #define Wido_CS 10 Adafruit_CC3000 Wido = Adafruit_CC3000(Wido_CS, Wido_IRQ, Wido_VBAT, SPI_CLOCK_DIVIDER); // you can change this clock speed // Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2 #define WLAN_SECURITY WLAN_SEC_WPA2 #define WLAN_SSID "MOTO" // cannot be longer than 32 characters! #define WLAN_PASS "12345678" // For connecting router or AP, don't forget to set the SSID and password here!! #define TCP_TIMEOUT 3000 //#define CC3000_TINY_DRIVER #define WEBSITE "api.heclouds.com" #define API_key "xxx" // Update Your API Key. To get your API Key, please check the link below void setup(){ Serial.begin(115200); Serial.println(F("Hello, CC3000!\n")); /* Initialise the module */ Serial.println(F("\nInitialising the CC3000 ...")); if (!Wido.begin()) { Serial.println(F("Unable to initialise the CC3000! Check your wiring?")); while(1); } /* Attempt to connect to an access point */ char *ssid = WLAN_SSID; /* Max 32 chars */ Serial.print(F("\nAttempting to connect to ")); Serial.println(ssid); /* NOTE: Secure connections are not available in 'Tiny' mode! By default connectToAP will retry indefinitely, however you can pass an optional maximum number of retries (greater than zero) as the fourth parameter. */ if (!Wido.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) { Serial.println(F("Failed!")); while(1); } Serial.println(F("Connected!")); /* Wait for DHCP to complete */ Serial.println(F("Request DHCP")); while (!Wido.checkDHCP()) { delay(100); // ToDo: Insert a DHCP timeout! } } float temp = 0; void loop(){ static Adafruit_CC3000_Client WidoClient; static unsigned long RetryMillis = 0; static unsigned long uploadtStamp = 0; static unsigned long sensortStamp = 0; if(!WidoClient.connected() && millis() - RetryMillis > TCP_TIMEOUT){ // 如果连接中断且时间超过TCP_TIMEOUT,则重新连接 RetryMillis = millis(); Serial.println(F("Try to connect the cloud server")); WidoClient.close(); // Connect to the Yeelink Server uint32_t ip = Wido.IP2U32(183,230,40,33); WidoClient = Wido.connectTCP(ip, 80); // Try to connect cloud server } if(WidoClient.connected() && millis() - uploadtStamp > 2000){ uploadtStamp = millis(); // 如果连接正常且上传数据时间超过2000ms,则更新 Serial.println(F("Connected to OneNet server.")); Serial.println(F("Sending headers")); //发送查询数据包 WidoClient.fastrprint(F("GET /devices/")); WidoClient.fastrprint(F(DIV_ID/datastreams")); WidoClient.fastrprint(F("/LED")); WidoClient.fastrprintln(F(" HTTP/1.1")); WidoClient.fastrprint(F("api-key:")); WidoClient.fastrprintln(API_key); WidoClient.fastrprintln(F("Host: api.heclouds.com")); WidoClient.fastrprintln(""); /********** Get the http page feedback ***********/ unsigned long rTimer = millis(); Serial.println(F("Reading Cloud Response!!!\r\n")); while (millis() - rTimer < 2000) { while (WidoClient.connected() && WidoClient.available()) { char c = WidoClient.read(); Serial.print(c); } } /* String comdata = ""; while (Serial.available() > 0) { comdata += char(Serial.read()); delay(2); } if (comdata.length() > 0) { Serial.println(comdata); comdata = ""; } */ delay(100); // Wait for 1s to finish posting the data stream WidoClient.close(); // Close the service connection RetryMillis = millis(); // Reset the timer stamp for applying the connection with the service } } |
© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed