title: ESP8266 NodeMCU can't send TCP commands - same code works on Arduino Uno?
body: <p>I'm a noob at this, so hopefully a stupidly obvious one.</p>
  
  <p>I'm trying to create a simple temperature/humidity sensor that takes readings from a DHT22 and uses an ESP8266 to ping them to Thingspeak's API to then graph out/store etc.</p>
  
  <p>I've pasted code below - it worked on Arduino Uno and I'm trying to shrinkify it onto a ESP8266 so I can produce lots of little temperature sensors for around the house.</p>
  
  <p><strong>Symptoms</strong></p>
  
  <ul>
  <li>It's connecting fine to wifi</li>
  <li>It's generating a correct API string (I've tested by manually cutting and pasting it into browser)</li>
  <li>Temp sensors are generating correct readings also</li>
  <li>It's returning 'Data Fail!' in the serial monitor, suggesting it's this point in the code where the error is occurring</li>
  </ul>
  
  <p>I can't tell if it's something odd about moving from Arduino Uno to ESP8266 that's causing the problem (ie different libraries required, TCP commands being different etc)</p>
  
  <p>Any help from a more seasoned veteran would be much appreciated!</p>
  
  <p>Here's a snippet of the serial monitor output and the code (just  passwords/apis etc)</p>
  
  <pre><code>22:16:50.266 -&gt; **************
  22:16:57.579 -&gt; Wifi Connection Successful
  22:16:57.579 -&gt; The IP Address of the Sensor is:192.168.1.211
  22:16:57.579 -&gt; Humidity: 41.50
  22:16:57.579 -&gt; Temperature: 21.70
  22:16:57.579 -&gt; AT+CIPSTART="TCP","api.thingspeak.com",80
  22:17:00.574 -&gt; AT+CIPSEND=63
  22:17:01.561 -&gt; AT+CIPCLOSE
  22:17:02.577 -&gt; Data Fail!
  22:17:02.577 -&gt; GET /update?apikey=&lt;REMOVED&gt;&amp;field1=21.70&amp;field2=41.50
  
  </code></pre>
  
  <pre><code>
  #include&lt;stdlib.h&gt;
  #include "DHT.h"
  #include &lt;ESP8266WiFi.h&gt;
  
  #define SSID "&lt;REMOVED&gt;" //your network name
  #define PASS "&lt;REMOVED&gt;" //your network password
  #define API "&lt;REMOVED&gt;" //api string
  #define IP "api.thingspeak.com" // thingspeak.com
  #define DHTPIN 4     // what pin the DHT sensor is connected to
  #define DHTTYPE DHT22   // Change to DHT22 if that's what you have
  #define Baud_Rate 115200 //Another common value is 9600
  #define DELAY_TIME 300000 //time in ms between posting data to ThingSpeak
  
  //Can use a post also
  String GET = String("GET /update?apikey=") + API + "&amp;field1=";
  String FIELD2 = "&amp;field2=";
  
  //if you want to add more fields this is how
  //String FIELD3 = "&amp;field3=";
  
  bool updated;
  
  DHT dht(DHTPIN, DHTTYPE);
  
  //this runs once
  void setup()
  {
    delay(5000);
    Serial.begin(Baud_Rate);
    // Connect to WIFI
    WiFi.begin(SSID, PASS);
    while (WiFi.status() != WL_CONNECTED)
    {
      delay(500);
      Serial.print("*");
    }
  
    Serial.println("");
    Serial.println("Wifi Connection Successful");
    Serial.print("The IP Address of the Sensor is:");
    Serial.println(WiFi.localIP()); //Print the IP Address
  
  
    //initalize DHT sensor
    dht.begin();
  }
  
  //this runs over and over
  void loop() {
    float h = dht.readHumidity();
    Serial.print("Humidity: ");
    Serial.println(h);
    // Read temperature as Fahrenheit (isFahrenheit = true)
    float c = dht.readTemperature();
    Serial.print("Temperature: ");
    Serial.println(c);
  
    // Check if any reads failed and exit early (to try again).
    if (isnan(h) || isnan(c)) {
      Serial.println("Reading DHT22 Failed, exiting");
      return;
    }
  
    //update ThingSpeak channel with new values
    updated = updateTemp(String(c), String(h));
  
     //wait for delay time before attempting to post again
    delay(DELAY_TIME);
  }
  
  bool updateTemp(String tempC, String humid) {
    //initialize your AT command string
    String cmd = "AT+CIPSTART=\"TCP\",\"";
  
    //add IP address and port
    cmd += IP;
    cmd += "\",80";
  
    //connect
    Serial.println(cmd);
    delay(2000);
    if (Serial.find("Error")) {
      return false;
    }
  
    //build GET command, ThingSpeak takes Post or Get commands for updates, I use a Get
    cmd = GET;
    cmd += tempC;
    cmd += FIELD2;
    cmd += humid;
    cmd += "\r\n";  
  
    //continue to add data here if you have more fields such as a light sensor
    //cmd += FIELD3;
    //cmd += &lt;field 3 value&gt;
  
    //Serial.println(cmd);
    //Use AT commands to send data
    Serial.print("AT+CIPSEND=");
    Serial.println(cmd.length());
    if (Serial.find("&gt;")) {
      //send through command to update values
      Serial.print(cmd);
    } else {
      Serial.println("AT+CIPCLOSE");
    }
  
    if (Serial.find("OK")) {
      //success! Your most recent values should be online.
      Serial.println("Data Sent!");
      return true;
    } else {
      Serial.println("Data Fail!");
      Serial.println(cmd);
      return false;
    }
  }
  
  boolean connectWiFi() {
    //set ESP8266 mode with AT commands
    Serial.println("AT+CWMODE=1");
    delay(2000);
  
    //build connection command
    String cmd = "AT+CWJAP=\"";
    cmd += SSID;
    cmd += "\",\"";
    cmd += PASS;
    cmd += "\"";
  
    //connect to WiFi network and wait 5 seconds
    Serial.println(cmd);
    delay(5000);
  
    //if connected return true, else false
    if (Serial.find("OK")) {
      Serial.println("WIFI connected");
      return true;
    } else {
      Serial.println("WIFI not connected");
      return false;
    }
  }
  
  
  </code></pre>