ESP8266通过AT模式把Arduino上其他SofterwareSerial数据发到ThingSpeak上
/* * ================= Connection ======================= * [ESP8266-01] | [Arduino] [ESP8266-01] * UTXD GND == Antenna| 3.3V ----> VCC + -> CH_PD * CH_PD GPIO2 | | GND ----> GND * RST GPIO0 | | RX ----> UTXD * VCC URXD | | TX ----> URXD * ====================================================== * Arduino side RX and TX are not 0,1, the SofterwareSerial u difine at below * Put your own SSID Password and APIKey */ #define ssid "SSID"//type your own SSID name #define password "WIFIpassword"//type your own WIFI password #define APIKey "APIkey"//type your own ThingSpeak write APIkey #define ESP_RX 2 //to ESP8266-01 TXD any pin u wanna use on arduino PWM #define ESP_TX 3 //to ESP8266-01 RXD any pin u wanna use on arduino PWM #define timeout 500 //Set the timeout of TCP connect steps #define Interval 60000 //Set the time interval of data sending to Thingspeak unsigned long t1 = 0, t2; short s = 0; String post;//post1; //post1 for Post way only #include <SoftwareSerial.h> SoftwareSerial WFSerial(ESP_RX, ESP_TX); void setup() { Serial.begin(9600); while (!Serial) { ; } Serial.println("welcome"); /* //Set ESP8266-01 working in 9600, u also can use 115200 but the arduino might cannot find the correct bytes feedback from ESP8266-01 WFSerial.begin(115200); while (!WFSerial) { ; } WFSerial.println("AT+UART=9600,8,1,0,0\r\n"); */ WFSerial.begin(9600); while (!WFSerial) { ; } /*//Set ESP8266-01 working in station mode, 1st time running u need to change the running mode to station WFSerial.println("AT+CWMODE=1\r\n"); while (!WFSerial.find("OK")) { Serial.write('-');; } */ //rest ESP8266-01 WFSerial.println("AT+RST\r\n"); while (!WFSerial.find("OK")) { Serial.write('*');; } //connect WIFI Serial.write("connecting"); WFSerial.println("AT+CWJAP=\"" ssid "\",\"" password "\"\r\n"); while (!WFSerial.find("WIFI GOT IP")) { Serial.write('.'); } Serial.println("OK"); } void loop() { //Show ESP8266-01 feedback via arduino HardwareSerial,when you put it as practical can remove this part if (WFSerial.available()) { Serial.write(WFSerial.read()); } int Var = 500; // the variable transmit if (t1 == 0 || (millis() - t1) > Interval) { switch (s) { case 0: { WFSerial.println("AT+CIPSTART=\"TCP\",\"184.106.153.149\",80\r\n"); t2 = millis(); s++; } break; case 1: { if (millis() - t2 > timeout) { /* * To upload data to ThingSpeak must choose one POST or GET * 2 functions are same. POST take more memory,if u wanna use need add post1 declare * Since Arduino memory is 2KB only the "User-Agent: tslib-arduino/1.3 (ESP8266)" removed * and let arduino wait overtime better than check the feed back from ESP8266 * lots of time AT Command cannot full showed * other things all same as offical library doing */ //POST Way /* post = "POST /update HTTP/1.1\nHost: api.thingspeak.com\nConnection: close\nX-THINGSPEAKAPIKEY: "APIKey "\nContent-Type: application/x-www-form-urlencoded\nContent-Length: "; String post1 = "api_key=" APIKey "&field1=" + String(Var); post += String(postStr1.length()) + "\n\n" + post1; */ //GET Way //post = "GET /update?key="APIKey"&field1=" + String(Var) + " HTTP/1.1\nHost: api.thingspeak.com\nAccept: */*\n\n"; WFSerial.println("AT+CIPSEND=" + String(post.length()) + "\r\n"); t2 = millis(); s++; } } break; case 2: { if (millis() - t2 > timeout) { WFSerial.println(post); t1 = millis(); s = 0; } break; } } } }
扫描二维码,在手机上阅读