0

I am following the tutorial Tiny WiFi Temperature Sensor with Arduino. everthing works fine, but I have a little problem.

When the serial monitor is opened the sensor is reading fine like 23.20, 20.25 etc., but when I closed the serial monitor window I got wrong values, for example, 289.34, 289.90.

What does the serial monitor do?

I am stuck with this problem for last two days. Please give me a hand.


  • The baud rate is as you can see 115200 and i have changed it various time but it doesn't do anything.

  • I haven't test commenting out the Serial.println() lines

  • The values i can see at my server (see the function send_request).

  • The problem is when i am testing the program opening the serial monitor windows everything works fine for example: 29.20, 30.12, 28.12 but when i close the windows i start getting wrong values for exmaple: 289.45, 289.89 , etc... .

Here is my code.

#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 = "/tiny-wifi-temperature/";

void setup(void)
{

  Serial.begin(115200);
  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) ; 

  /*
  uint32_t ipAddress = cc3000.IP2U32(10, 0, 130, 200);
  uint32_t netMask = cc3000.IP2U32(255, 255, 0, 0);
  uint32_t defaultGateway = cc3000.IP2U32(10, 0, 1, 1);
  uint32_t dns = cc3000.IP2U32(10, 0, 1, 1);
  if (!cc3000.setStaticIPAddress(ipAddress, netMask, defaultGateway, dns)) {
    Serial.println(F("Failed to set static IP!"));
    while(1);
  }
  */

  /*
  if (!cc3000.setDHCP()) {
    Serial.println(F("Failed to set DHCP!"));
    while(1);
  }*/

  // 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(100);
  } 

}


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(1000);
}

// 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.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;
  }
}
  • 2
    Even though you've provided a link to the circuit (sort of) and the code that you think you're using, it would be good to include the actual code and some kind of documentation of the circuit – maybe a photo of what you've built. Also tell us what you've done so far towards debugging the problem. That will help us to focus and also to avoid telling you what you already know… – dlu Jan 5 '16 at 17:54
  • The incorrect values, I presume after reading the article, are seen on the browser at localhost? – Greenonline Jan 6 '16 at 7:51
2

The serial monitor gives you a way to "talk" relatively directly with the Arduino – in this case it give you a way to easily confirm that the basic circuit is working and that you are getting sensible temperatures. That's a foundation on which you can build.

It is unlikely – I think – that the serial monitor is causing the problem. But before you believe me, there is one thing you should consider. The serial monitor runs relatively slowly, so outputting the temperature via the serial monitor introduces a delay in your code. That could explain the difference.

So your next step is to trace your bad value. So, first more questions:

  • What baud rate are you running the serial monitor at? Does changing the baud rate affect the result? That would point to a timing related issue.
  • Is there any code that writes to the serial monitor (e.g., a call to Serial.println())? If there is what happens when you comment it out?
  • Where are you seeing the two values (the correct and incorrect values)? I assume that both are at the web server, but I want to confirm this.
  • Can you explain more clearly the process to demonstrate the problem? In the tutorial that you link to I don't think that the code pathway is the same to see the value on the serial monitor as it is on the server, so I'm wondering if an error was made as you changed the code.

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.