0

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(">")) { 
    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 :/

  • Have you tested the temperature sensor by itself, without the ESP8266? – Craig May 10 '18 at 15:51
2

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.

  • Thanks, as i edited i was actually using an LM35 (apologies) but it still doesnt work and i get results like these hastebin.com/isifakiwef.ini. That is with 5 second intervals and they seem just to bounce between 0 and 40? – Elijah D-R May 10 '18 at 16:58
  • I changed around some cables and now i consistently get 120*C, what on earth is happening? I thought this could be fahrenheit but thats still 46*C? – Elijah D-R May 10 '18 at 17:02
  • @ElijahD-R Did change GND and VCC? Reverse polarity kills your sensor. You seem to be confused about your sensor. Are you absolutely sure it's an LM35 and no TMP36? Can you edit the question to show a picture and your current code? Does this example sketch work for you? – Maximilian Gerhardt May 10 '18 at 17:11
  • I'm going to say that I probably at one point reversed the polarity... Time to find a new temperature sensor, got any recommendations? – Elijah D-R May 10 '18 at 17:16
  • Pretty much any 'Arduino' labeled temperature sensor works, if you don't special requirements on temperature range or accuracy. Maybe something like a DHT11, DHT22 will work for you too? Your old sensor type would also be okay, just make sure you've got the wiring correct and run a minimal test sketch as first thing. – Maximilian Gerhardt May 10 '18 at 17:24
1

This instructables does the TMP36 part very similar using tempPin=0 instead 2 ,
I'd try that code because there shouldn't be a negative value as long you are measuring room temperate and the TMP36 is not defective.

According to these curves

TMP35/36/37 diagram

and table from the TMP35/36/37 data sheet TMP35/36/37 data sheet

you should measure 700-750mV (with VS 3V)

  • Your coment suddenly vanished partly. If the returned voltage is lower than expected, could it possibly be a TMP35/37 sensor and not a TMP36? – LotPings May 10 '18 at 16:55
  • Apologies, i just noticed its a LM35 and the sheet i was given with my package was incorrect (it was a bundle and in my box it said TMP36 and online it says LM35) – Elijah D-R May 10 '18 at 17:04
  • I'm now using some LM35 code but i still get fluctations? hastebin.com/okakudifel.makefile The first number is the number recorded by analogRead and the second one is when that is multiplied by 0.48828125. – Elijah D-R May 10 '18 at 17:06
  • The analogread should return an integer between 0 and 1023 for voltages between 0V and 5 V the diagram is for a Vs of 3V. What do the readings say if output to serial? – LotPings May 10 '18 at 17:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.