Sensor tmp36

Hi, I am following the next tutorial. https://www.openhomeautomation.net/tiny-wifi-temperature-arduino/ Everything works fine!!! but i have a little problem , when I close the monitor serial windows i got wrong values from sensor. example: 290.22, 289.23 etc... please give me a hand. thanks in advance.

How do we know what you are doing without a program.

Please put your code in its own window as seen in other posts. This can be done by placing     [code]  and [/code]  around the code or use the </> icon. This makes it easier for others to read.

How to use this forum

#include <Adafruit_CC3000.h>
#include <Adafruit_CC3000_Server.h>
#include <ccspi.h>

/*
   Tiny WiFi temperature sensor with Arduino, the TMP36 sensor & the CC3000 chip
   Writtent by Marco Schwartz for Open Home Automation
*/

// Include required libraries

#include <SPI.h>
#include <string.h>
#include "utility/debug.h"
#include<stdlib.h>

// Define CC3000 chip pins
#define ADAFRUIT_CC3000_IRQ   2
#define ADAFRUIT_CC3000_VBAT  A3
#define ADAFRUIT_CC3000_CS    8

// WiFi network (change with your settings !)
#define WLAN_SSID       "SSID"        // cannot be longer than 32 characters!
#define WLAN_PASS       "Password"
#define WLAN_SECURITY   WLAN_SEC_WPA2 // This can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2

// Create CC3000 instance
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT,
                         SPI_CLOCK_DIV2);

// Local server IP, port, and repository (change with your settings !)
uint32_t ip = cc3000.IP2U32(10,0,129,108);
int port = 8080;
String repository = "/arduino/";


void setup(void)
{

  Serial.begin(9600);
  Serial.println("Start setup");



  // Initialise the CC3000 module
  if (!cc3000.begin())
  {
    Serial.println("Impossible to start");
    while (1);
  }

  /*
  Serial.println(F("\nDeleting old connection profiles"));
  if (!cc3000.deleteProfiles()) {
    Serial.println(F("Failed!"));
    while(1);
  }*/

  listSSIDResults();
  connectNetwork();
  displayConnectionDetails();
  /*
  while(!displayConnectionDetails())
  {
    connectNetwork(); 
  }*/


}


void connectNetwork(void)
{
  Serial.println(F("Trying to connect wifi ")); Serial.println(WLAN_SSID) ; 

  
  
  // Connect to  WiFi network
  if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
    Serial.println(F("Failed!"));
    while(1);
  }
  Serial.println("Connected to WiFi network!");


  

  // Check DHCP
  Serial.println(F("Request DHCP"));
  while (!cc3000.checkDHCP())
  {
    delay(1000);
  } 

}


void loop(void)
{

  // Measure the temperature
  int sensorValue = analogRead(A4);
  float milliVoltsValue = sensorValue * 5000. / 1024.;
  float temperature = (milliVoltsValue - 500.) / 10.;

  // Print the result
  Serial.println(temperature);

  // Transform to string
  char temp[5];
  dtostrf(temperature, 5, 2, temp);

  // Send request
  String request = "GET " + repository + "sensor.php?temp=" + String(temp) + " HTTP/1.0";
  send_request(request);

  // Update every second
  delay(4000);
}

// Function to send a TCP request and get the result as a string
void send_request (String request) {

  // Connect
  Serial.println("Starting connection to server...");
  Adafruit_CC3000_Client client = cc3000.connectTCP(ip, port);

  // Send request
  if (client.connected()) {
    client.println(request);
    client.println(F(""));
    Serial.println("Connected & Data sent");
  }
  else {
    Serial.println(F("Connection failed"));
  }

  while (client.connected()) {
    while (client.available()) {
      // Read answer
      char c = client.read();
    }
  }
  Serial.println("Closing connection");
  Serial.println("");
  client.flush();
  client.close();
}



void listSSIDResults(void)
{
  uint32_t index ;
  uint8_t valid , rssi, sec ;
  char ssidname[33];



  if (!cc3000.startSSIDscan(&index)) {
    Serial.println(F("SSID scan failed!"));
    return;
  }


  Serial.print(F("Networks found: ")); Serial.println(index);
  Serial.println(F("================================================"));

  while (index) {
    index--;

    valid = cc3000.getNextSSID(&rssi, &sec, ssidname);

    Serial.print(F("SSID Name    : ")); Serial.print(ssidname);
    Serial.println();
    Serial.print(F("RSSI         : "));
    Serial.println(rssi);
    Serial.print(F("Security Mode: "));
    Serial.println(sec);
    Serial.println();
  }
  Serial.println(F("================================================"));

  cc3000.stopSSIDscan();
}


bool displayConnectionDetails(void)
{
  uint32_t ipAddress, netmask, gateway, dhcpserv, dnsserv;

  if (!cc3000.getIPAddress(&ipAddress, &netmask, &gateway, &dhcpserv, &dnsserv))
  {
    Serial.println(F("Unable to retrieve the IP Address!\r\n"));
    return false;
  }
  else
  {
    Serial.print(F("\nIP Addr: ")); cc3000.printIPdotsRev(ipAddress);
    Serial.print(F("\nNetmask: ")); cc3000.printIPdotsRev(netmask);
    Serial.print(F("\nGateway: ")); cc3000.printIPdotsRev(gateway);
    Serial.print(F("\nDHCPsrv: ")); cc3000.printIPdotsRev(dhcpserv);
    Serial.print(F("\nDNSserv: ")); cc3000.printIPdotsRev(dnsserv);
    Serial.println();
    return true;
  }
}

meanwhile serial monitor is on the result is:
25.68
25.20
25.20
24.71
25.20
25.20
25.20

when I close the serial monitor windows the output is:
289.84
289.84
289.84
289.84

How do you know you are getting wrong values when you close the serial monitor? How are you reading the values??

Weedpharma

weedpharma: How do you know you are getting wrong values when you close the serial monitor? How are you reading the values??

Weedpharma

The temperature is sent as part of the HTTP request.

When you say "close the serial window" could this be powering down the USB? How are things powered and connected - it seems like the analogRead is giving different values which could be due to a different supply voltage?

// Measure the temperature
int sensorValue = analogRead(A4);
float milliVoltsValue = sensorValue * 5000. / 1024.;
float temperature = (milliVoltsValue - 500.) / 10.;

// Print the result
Serial.println(temperature);

Poor programming. Low resolution. 0.5C steps. Practically worse. Weird and course steps, like 24.20C, 24.71C, 25.20C. Supply dependent... Use better code once you have fixed the other problem. Leo..