/* Basic test of the ESP8266 to send UDP packet - ping and/or dht11 temp details Reference http://www.electrodragon.com/w/Wi07c Be sure to bring CH_PD and RST high via 10k resistor. USB power alone is not enough, module needs 200ma. So external power is a must. If you get garbled text at 9600 then the module is running in update mode (due to a hault condition - not enough power) and thus is running at 115200. Provide a better power supply. The ESP8266 needs to be connected to the TX/RX lines of the Arduino (hardware uart). Use a level shifter to convert from the 5v of the arduino to 3.3v of the module. The PC can be connected to 10/11 to see debug output. DHT11 data line connects to pin 9 */ #define SSID "my_ssid" #define PASS "aabbccddeeff" #define DEST_IP "192.168.0.10" #define TIMEOUT 5000 // mS #define CONTINUE false #define HALT true #define ENDWITHCLRF true #define ENDWITHNOTHING false // #define ECHO_COMMANDS // Un-comment to echo AT+ commands to serial monitor #include SoftwareSerial Serial1(10, 11); // RX, TX #include "DHT.h" DHT dht; // Print error message and loop stop. void errorHalt(String msg) { Serial1.println(msg); Serial1.println("HALT"); while(true){}; } // Read characters from WiFi module and echo to serial until keyword occurs or timeout. boolean echoFind(String keyword, long timeout = TIMEOUT) { byte current_char = 0; byte keyword_length = keyword.length(); // Fail if the target string has not been sent by deadline. long deadline = millis() + TIMEOUT; while(millis() < deadline) { if (Serial.available()) { char ch = Serial.read(); Serial1.write(ch); if (ch == keyword[current_char]) if (++current_char == keyword_length) { Serial1.println(); return true; } } } return false; // Timed out } // Read and echo all available module output. // (Used when we're indifferent to "OK" vs. "no change" responses or to get around firmware bugs.) void echoFlush() {while(Serial.available()) Serial1.write(Serial.read());} // Echo module output until 3 newlines encountered. // (Used when we're indifferent to "OK" vs. "no change" responses.) void echoSkip() { echoFind("\n"); // Search for nl at end of command echo echoFind("\n"); // Search for 2nd nl at end of response. echoFind("\n"); // Search for 3rd nl at end of blank line. } // Send a command to the module and wait for acknowledgement string // (or flush module output if no ack specified). // Echoes all data received to the serial monitor. boolean echoCommand(String cmd, String ack, boolean halt_on_fail, boolean doTerminateCLRF = true, long timeout = TIMEOUT) { if(doTerminateCLRF){ Serial.println(cmd); }else{ Serial.print(cmd); } #ifdef ECHO_COMMANDS Serial1.print("--"); Serial1.println(cmd); #endif // If no ack response specified, skip all available module output. if (ack == "") echoSkip(); else // Otherwise wait for ack. if (!echoFind(ack)) // timed out waiting for ack string if (halt_on_fail) errorHalt(cmd+" failed");// Critical failure halt. else return false; // Let the caller handle it. return true; // ack blank or ack found } // Connect to the specified wireless network. boolean connectWiFi() { String cmd = "AT+CWJAP=\""; cmd += SSID; cmd += "\",\""; cmd += PASS; cmd += "\""; if (echoCommand(cmd, "OK", CONTINUE, ENDWITHCLRF, TIMEOUT*2)) // Join Access Point, takes longer than the other commands so lets give it al onger timeout { Serial1.println("Connected to WiFi."); return true; } else { Serial1.println("Connection to WiFi failed."); return false; } } int i=0; void sendPing(){ // Build submission. String cmd = "PING " + String(i); i++; if(i>254) i=0; // Ready the module to receive raw data if (!echoCommand("AT+CIPSEND=0,"+String(cmd.length()), ">", CONTINUE)) { //echoCommand("AT+CIPCLOSE", "", CONTINUE); Serial1.println("Connection timeout."); return; } // Send the raw request echoCommand(cmd, "SEND OK", CONTINUE, ENDWITHNOTHING); // GET } void sendTemp(){ char buffer[5]; char buffer2[5]; float humidity = dht.getHumidity(); float temperature = dht.getTemperature(); String hString = dtostrf(humidity, 1, 1, buffer); String tString = dtostrf(temperature, 1, 1, buffer2); String cmd = "TEMP " + (String)dht.getStatusString() + " " + hString + " " + tString; // Ready the module to receive raw data if (!echoCommand("AT+CIPSEND=0,"+String(cmd.length()), ">", CONTINUE)) { //echoCommand("AT+CIPCLOSE", "", CONTINUE); Serial1.println("Connection timeout."); return; } // Send the raw request echoCommand(cmd, "SEND OK", CONTINUE, false); // GET } // ******** SETUP ******** void setup() { Serial1.begin(9600); // Communication with PC monitor via USB Serial.begin(9600); // Communication with ESP8266 via 5V/3.3V level shifter Serial.setTimeout(TIMEOUT); Serial1.println("ESP8266 Demo"); dht.setup(9); // data pin 9 delay(2000); echoCommand("AT+RST", "ready", HALT); // Reset & test if the module is ready Serial1.println("Module is ready."); echoCommand("AT+GMR", "OK", CONTINUE); // Retrieves the firmware ID (version number) of the module. echoCommand("AT+CWMODE?","OK", CONTINUE);// Get module access mode. // echoCommand("AT+CWLAP", "OK", CONTINUE); // List available access points - DOESN't WORK FOR ME echoCommand("AT+CWMODE=1", "", HALT); // client mode echoCommand("AT+CIPMUX=1", "", HALT); // Allow multiple connections (we'll only use the first). //connect to the wifi boolean connection_established = false; for(int i=0;i<5;i++) { if(connectWiFi()) { connection_established = true; break; } } if (!connection_established) errorHalt("Connection failed"); delay(5000); //echoCommand("AT+CWSAP=?", "OK", CONTINUE); // Test connection echoCommand("AT+CIFSR", "", HALT); // Echo IP address. (Firmware bug - should return "OK".) //echoCommand("AT+CIPMUX=0", "", HALT); // Set single connection mode // Establish UDP connection String cmd = "AT+CIPSTART=0,\"UDP\",\""; cmd += DEST_IP; cmd += "\",3333"; if (!echoCommand(cmd, "OK", CONTINUE)) return; //delay(2000); // Get connection status if (!echoCommand("AT+CIPSTATUS", "OK", CONTINUE)) return; } // ******** LOOP ******** void loop() { sendPing(); sendTemp(); // Loop forever echoing data received from destination server. while (Serial.available()){ Serial1.write(Serial.read()); } delay(2000); }