http://www.geek-workshop.com/thread-11266-1-1.html
ITEAD首款智能wifi通用改装件成功上市:http://www.geek-workshop.com/thread-17944-1-1.html
----------------------------------------------------2015年6月10日更新-------------------------------------------------------
最新wifi库下载地址:https://github.com/itead/ITEADLIB_Arduino_WeeESP8266/archive/master.zip
库默认是使用硬件串口与ESP8266通信,需要软串口通信只需取消注释Esp8266.h文件里的一行代码
#define ESP8266_USE_SOFTWARE_SERIAL
--------------------------------------------------2015年2月5日发布---------------------------------------------------------
#define SSID "ITEAD_2" //type your own SSID name #define PASSWORD "27955416" //type your own WIFI password #include "uartWIFI.h" #includeh> WIFI wifi; extern int chlID; //client id(0-4) void setup ( ) { pinMode ( 6, OUTPUT ); //使用mega的时候在不改变接线的情况下,此处改为24脚 wifi. begin ( ); bool b = wifi. Initialize (STA, SSID, PASSWORD ); if (!b ) { DebugSerial. println ( "Init error" ); } delay ( 8000 ); //make sure the module can have enough time to get an IP address String ipstring = wifi. showIP ( ); DebugSerial. println (ipstring ); //show the ip address of module delay ( 2000 ); wifi. confMux ( 1 ); delay ( 100 ); if (wifi. confServer ( 1, 8080 ) ) DebugSerial. println ( "Server is set up" ); } void loop ( ) { char buf [ 100 ]; int iLen = wifi. ReceiveMessage (buf ); if (iLen > 0 ) { if (strcmp (buf, "CH0ON" ) == 0 ) { digitalWrite ( 6, HIGH ); //使用mega的时候在不改变接线的情况下,此处改为24脚 DebugSerial. println ( "CH0ON" ); } if (strcmp (buf, "CH0OFF" ) == 0 ) { digitalWrite ( 6, LOW ); //使用mega的时候在不改变接线的情况下,此处改为24脚 DebugSerial. println ( "CH0OFF" ); } } }
/* ESP8266 library When you use with UNO board, uncomment the follow line in uartWIFI.h. #define UNO When you use with MEGA board, uncomment the follow line in uartWIFI.h. #define MEGA Connection: When you use it with UNO board, the connection should be like these: ESP8266_TX->D0 ESP8266_RX->D1 ESP8266_CH_PD->3.3V ESP8266_VCC->3.3V ESP8266_GND->GND FTDI_RX->D3 //The baud rate of software serial can't be higher that 19200, so we use software serial as a debug port FTDI_TX->D2 When you use it with MEGA board, the connection should be like these: ESP8266_TX->RX1(D19) ESP8266_RX->TX1(D18) ESP8266_CH_PD->3.3V ESP8266_VCC->3.3V ESP8266_GND->GND When you want to output the debug information, please use DebugSerial. For example, DebugSerial.println("hello"); Note: The size of message from ESP8266 is too big for arduino sometimes, so the library can't receive the whole buffer because the size of the hardware serial buffer which is defined in HardwareSerial.h is too small. Open the file from \arduino\hardware\arduino\avr\cores\arduino\HardwareSerial.h. See the follow line in the HardwareSerial.h file. #define SERIAL_BUFFER_SIZE 64 The default size of the buffer is 64. Change it into a bigger number, like 256 or more. */ #define SSID "Itead_1(Public)" //type your own SSID name #define PASSWORD "27955416" //type your own WIFI password #include "uartWIFI.h" #includeh> WIFI wifi; extern int chlID; //client id(0-4) void setup ( ) { pinMode ( 4, OUTPUT ); pinMode ( 5, OUTPUT ); pinMode ( 6, OUTPUT ); pinMode ( 7, OUTPUT ); wifi. begin ( ); bool b = wifi. Initialize (STA, SSID, PASSWORD ); if (!b ) { DebugSerial. println ( "Init error" ); } delay ( 8000 ); //make sure the module can have enough time to get an IP address String ipstring = wifi. showIP ( ); DebugSerial. println (ipstring ); //show the ip address of module delay ( 2000 ); wifi. confMux ( 1 ); delay ( 100 ); if (wifi. confServer ( 1, 8080 ) ) DebugSerial. println ( "Server is set up" ); } void loop ( ) { char buf [ 100 ]; int iLen = wifi. ReceiveMessage (buf ); if (iLen > 0 ) { //mini Rboard D4 pin is relay,mini Rboard URL:[url=http://item.taobao.com/item.htm?spm=a1z10.3.w4002-2605688463.12.EMkeTJ&id=39334222369]http://item.taobao.com/item.htm? ... keTJ&id=39334222369[/url] if (strcmp (buf, "CH0ON" ) == 0 ) { digitalWrite ( 4, HIGH ); DebugSerial. println ( "CH0ON" ); } if (strcmp (buf, "CH0OFF" ) == 0 ) { digitalWrite ( 4, LOW ); DebugSerial. println ( "CH0OFF" ); } /* the other 3 chanels below are for Rboard D5 D6 D7 , Rboard URL:[url=http://item.taobao.com/item.htm?spm=a1z10.3.w4002-2605688463.12.EMkeTJ&id=39334222369]http://item.taobao.com/item.htm? ... keTJ&id=39334222369[/url] if (strcmp(buf, "CH1ON") == 0) { digitalWrite(5,HIGH); DebugSerial.println("CH1ON"); } if (strcmp(buf, "CH1OFF") == 0) { digitalWrite(5,LOW); DebugSerial.println("CH1OFF"); } if (strcmp(buf, "CH2ON") == 0) { digitalWrite(6,HIGH); DebugSerial.println("CH2ON"); } if (strcmp(buf, "CH2OFF") == 0) { digitalWrite(6,LOW); DebugSerial.println("CH2OFF"); } if (strcmp(buf, "CH3ON") == 0) { digitalWrite(7,HIGH); DebugSerial.println("CH3ON"); } if (strcmp(buf, "CH3OFF") == 0) { digitalWrite(7,LOW); DebugSerial.println("CH3OFF"); } */ } }
#define SSID "Itead_1(Public)" //type your own SSID name #define PASSWORD "27955416" //type your own WIFI password #include "uartWIFI.h" #includeh> WIFI wifi; extern int chlID; //client id(0-4) // for yeelink api #define APIKEY "3a362e99d6f1daf974561163a8c99a85" // replace your yeelink api key here //replace the device ID and sensor ID for temperature sensor. #define DEVICEID0 15483 // replace your device ID #define SENSORID0 26660 // replace your sensor ID //replace the device ID and sensor ID for humidity sensor. #define DEVICEID1 15483 // replace your device ID #define SENSORID1 26661 // replace your sensor ID char server [ ] = "api.yeelink.net"; // name address for yeelink API unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds boolean lastConnected = false; // state of the connection last time through the main loop const unsigned long postingInterval = 5* 1000; // delay between 2 datapoints, 5s String returnValue = ""; boolean ResponseBegin = false; int DHT11PIN= 25; //Connect D25 to data pin of DHT11 int humidity; int temperature; int post_number; void setup ( ) { wifi. begin ( ); bool b = wifi. Initialize (STA, SSID, PASSWORD ); if (!b ) { DebugSerial. println ( "Init error" ); } delay ( 8000 ); //make sure the module can have enough time to get an IP address String ipstring = wifi. showIP ( ); DebugSerial. println (ipstring ); //show the ip address of module } void loop ( ) { char message [ 400 ]; // if you're not connected, and ten seconds have passed since // your last connection, then connect again and send data: if ( ( millis ( ) - lastConnectionTime > postingInterval ) ) { //read dht11 int chk = dht11_read (DHT11PIN ); if (chk== 0 ) { if (post_number== 0 ) { sendData (DEVICEID0,SENSORID0,temperature ); post_number++; } else { post_number = 0; sendData (DEVICEID1,SENSORID1,humidity ); } } } // if there's incoming data from the net connection. // send it out the serial port. This is for debugging // purposes only: if (wifi. ReceiveMessage (message ) ) { DebugSerial. println (message ); } delay ( 10 ); } // this method makes a HTTP connection to the server: void sendData ( int device_id, int sensor_id, int thisData ) { // if there's a successful connection: if (wifi. ipConfig (TCP,server, 80 ) ) { DebugSerial. println ( "connecting..." ); // send the HTTP PUT request: String cmd; cmd = "POST /v1.0/device/"; cmd += String (device_id ); cmd += "/sensor/"; cmd += String (sensor_id ); cmd += "/datapoints"; cmd += " HTTP/1.1\r\n"; cmd += "Host: api.yeelink.net\r\n"; cmd += "Accept: *"; cmd += "/"; cmd += "*\r\n"; cmd += "U-ApiKey: "; cmd += APIKEY; cmd += "\r\n"; cmd += "Content-Length: "; int thisLength = 10 + getLength (thisData ); cmd += String (thisLength ); cmd += "\r\n"; cmd += "Content-Type: application/x-www-form-urlencoded\r\n"; cmd += "Connection: close\r\n"; cmd += "\r\n"; cmd += "{\"value\ ":"; cmd += String (thisData ); cmd += "}\r\n"; DebugSerial. println (cmd ); wifi. Send (cmd ); // note the time that the connection was made: lastConnectionTime = millis ( ); } else { // if you couldn't make a connection: DebugSerial. println ( "connection failed" ); DebugSerial. println ( "disconnecting." ); wifi. closeMux ( ); } } int getLength ( int someValue ) { // there's at least one byte: int digits = 1; // continually divide the value by ten, // adding one to the digit count for each // time you divide, until you're at 0: int dividend = someValue / 10; while (dividend > 0 ) { dividend = dividend / 10; digits++; } // return the number of digits: return digits; } int dht11_read ( int pin ) { // BUFFER TO RECEIVE int bits [ 5 ]; int cnt = 7; int idx = 0; // EMPTY BUFFER for ( int i= 0; i< 5; i++ ) {bits [i ]= 0; } [/i ] [i ] // REQUEST SAMPLE[/i] [i ] pinMode (pin, OUTPUT ); [/i ] [i ] digitalWrite (pin, LOW ); [/i ] [i ] delay ( 18 ); [/i ] [i ] digitalWrite (pin, HIGH ); [/i ] [i ] delayMicroseconds ( 40 ); [/i ] [i ] pinMode (pin, INPUT ); [/i ] [i ] // ACKNOWLEDGE or TIMEOUT[/i] [i ] unsigned int loopCnt = 10000; [/i ] [i ] while ( digitalRead (pin ) == LOW ) [/i ] [i ] if (loopCnt-- == 0 ) return -2; [/i ] [i ] loopCnt = 10000; [/i ] [i ] while ( digitalRead (pin ) == HIGH ) [/i ] [i ] if (loopCnt-- == 0 ) return -2; [/i ] [i ] // READ OUTPUT - 40 BITS => 5 BYTES or TIMEOUT[/i] [i ] for ( int i= 0; i< 40; i++ ) [/i ] [i ] { [/i ] [i ] loopCnt = 10000; [/i ] [i ] while ( digitalRead (pin ) == LOW ) [/i ] [i ] if (loopCnt-- == 0 ) return -2; [/i ] [i ] unsigned long t = micros ( ); [/i ] [i ] loopCnt = 10000; [/i ] [i ] while ( digitalRead (pin ) == HIGH ) [/i ] [i ] if (loopCnt-- == 0 ) return -2; [/i ] [i ] if ( ( micros ( ) - t ) > 40 ) bits [idx ] |= ( 1 << cnt ); [/i ] [i ] if (cnt == 0 ) // next byte?[/i] [i ] { [/i ] [i ] cnt = 7; // restart at MSB[/i] [i ] idx++; // next byte![/i] [i ] } [/i ] [i ] else cnt--; [/i ] [i ] } [/i ] [i ] // WRITE TO RIGHT VARS[/i] [i ] // as bits[1] and bits[3] are allways zero they are omitted in formulas.[/i] [i ] humidity = bits [ 0 ]; [/i ] [i ] temperature = bits [ 2 ]; [/i ] [i ] int sum = bits [ 0 ] + bits [ 2 ]; [/i ] [i ] if (bits [ 4 ] != sum ) return -1; [/i ] [i ] return 0; [/i ] [i ] }