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;
}
}