TMP36 not working with arduino uno? I have an Arduino Uno hooked up with a TMP36 and an ESP8266 to send the temperature reading to a PHP file which receives the post request and stores it in a text file.

The ESP8266 and PHP works fine and I'm positive of that. But the temperature my code gets are ridiculous and change a lot. I'll include my code and a Fritzing sketch and the URL to the data.txt (you can see the results real time). It might be a problem with my code or something else I'm not sure I've fiddled around to no success and hopefully some fresh eyes will help.



Most of the code is copypasta as I've never used ESP8266 before.

#include "SoftwareSerial.h"

String ssid ="correctssid";
String password="correctpwd";

float val=0;
int temppin = 2;
float volts=0.0;      
float tempC=0.0;

SoftwareSerial esp(6, 7);

String data;
String server = "www.elijahdur.co.uk";
String url = "/arduino/esp8266/file.php"; //I've changed this just well because

void setup() {
  esp.begin(9600);
  Serial.begin(9600);
  reset();
  connectWifi();
}

void reset() {
  esp.println("AT+RST");
  delay(1000);
  if(esp.find("OK") ){
    Serial.println("Module Reset");
  }
}

void connectWifi() {
  String cmd = "AT+CWJAP=\"" +ssid+"\",\"" + password + "\"";
  esp.println(cmd);
  delay(4000);

  if(esp.find("OK")) {
    Serial.println("Connected!");
  }

  else {
    connectWifi();
    Serial.println("Cannot connect to wifi"); 
  }
}

void loop () {
  val = analogRead(temppin);
  volts = val * 5.0;
  volts /= 1024.0; 
  tempC = (volts - 0.5) * 100;

  String string = (String) tempC;
  data = "data=" + string;
  httppost();

  delay(30000);
}

void httppost () {
  esp.println("AT+CIPSTART=\"TCP\",\"" + server + "\",80");//start a TCP connection.

  if( esp.find("OK")) {
    Serial.println("TCP connection ready");
  } 

  delay(1000);

  String postRequest =
  "POST " + url + " HTTP/1.0\r\n" +
  "Host: " + server + "\r\n" +
  "Accept: *" + "/" + "*\r\n" +
  "Content-Length: " + data.length() + "\r\n" +
  "Content-Type: application/x-www-form-urlencoded\r\n" +
  "\r\n" + data;

  String sendCmd = "AT+CIPSEND=";//determine the number of caracters to be sent.

  esp.print(sendCmd);
  esp.println(postRequest.length());

  delay(500);

  if(esp.find("&gt;")) { 
    Serial.println("Sending.."); 
    esp.print(postRequest);
  }

  if( esp.find("SEND OK")) { 
    Serial.println("Packet sent");
  }

  while (esp.available()) {
    String tmpResp = esp.readString();
    Serial.println(tmpResp);
  }

  esp.println("AT+CIPCLOSE");

}


The txt file is located at http://elijahdur.co.uk/arduino/esp8266/data.txt

Sometimes is will straight up give -50 and it's because it reads 0 and takes away the 5V from the analog.

Any help is greatly appreciated thank you!

EDIT: Apologies, but I had mixed up my sensors and was using an LM35, it still does not work however so I'm still stuck :/
First off, as a general debugging tip: Always use a minimal example to only test one component to make sure it works. You should have tested the temperature sensor on its own, then the ESP8266 WiFi connection.

Second, regarding this code:

  /* as global variable */  
  float val=0;

void loop () {
  val = analogRead(temppin);
  volts = val * 5.0;
  volts /= 1024.0; 
  tempC = (volts - 0.5) * 100;

  String string = (String) tempC;
  data = "data=" + string;
  httppost();

  delay(30000);
}


The line String string = (String) tempC; looks very suspicious to me. You are casting a float to a String object, i.e., you re-interpret it with a cast operator. That should not work because a float is not a String object. 

What should work is creating a new String object using its constructor, i.e., write

 String string(tempC);


If this is does not work, use the sketch at https://learn.adafruit.com/tmp36-temperature-sensor/using-a-temp-sensor, change sensorPin to 2, double check your wiring and remove the ESP8266.

And as 3rd note: You are hooking up your Arduino's TX directly to the ESP8266's RX pin, according to your schematic. This will damage the ESP8266 module, because it is a 3.3V device and should only receive 3.3V signals -- your Arduino Uno outputs 5V as a logic 'high'. You need a voltage divider or a logic level shifter between them.