Pages: [1]   Go Down
Author Topic: Wishield + sensors.requestTemperatures() with ds18b20: 23 sec. to get a webpage  (Read 880 times)
0 Members and 1 Guest are viewing this topic.
Monza
Offline Offline
Jr. Member
**
Karma: 0
Posts: 71
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi guys,

i have a little problem i'm not even able to understand...

I wrote this simple sketch to make my Arduino write on a web page a temperature from a ds18b20; it works fine but it takes about 23 seconds to open the page.... too long...

I've also tried to modify some part of the code and i found that is the "sensors.requestTemperatures();" in void loop that makes the web rendering so slow. Infact, if i remove it and assign a static value to the temperature the web page appears instantly.

Can somebody help me?

this is the code:
Code:

#include <WiServer.h>

#define WIRELESS_MODE_INFRA    1
#define WIRELESS_MODE_ADHOC    2
#include <OneWire.h>
#include <DallasTemperature.h>

#define ONE_WIRE_BUS 8
#define TEMPERATURE_PRECISION 12

DeviceAddress sonda1={0X28, 0XAC, 0XF6, 0XA7, 0X03, 0X00, 0X00, 0XCE};



// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

//float tempC = sensors.getTempC(sonda1);

// Wireless configuration parameters —————————————-
unsigned char local_ip[] = {192,168,1,120};    // IP address of WiShield
unsigned char gateway_ip[] = {192,168,1,1};    // router or gateway IP address
unsigned char subnet_mask[] = {255,255,255,0};    // subnet mask for the local network
const prog_char ssid[] PROGMEM = {"Cisco Home 2,4 gHz"};        // max 32 bytes

unsigned char security_type = 3;    // 0 – open; 1 – WEP; 2 – WPA; 3 – WPA2

// WPA/WPA2 passphrase
const prog_char security_passphrase[] PROGMEM = {"xxx"};    // max 64 characters

// WEP 128-bit keys
// sample HEX keys
prog_uchar wep_keys[] PROGMEM = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, // Key 0
 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 1
 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 2
 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // Key 3
};

// setup the wireless mode
// infrastructure – connect to AP
// adhoc – connect to another WiFi device
unsigned char wireless_mode = WIRELESS_MODE_INFRA;

unsigned char ssid_len;
unsigned char security_passphrase_len;
// End of wireless configuration parameters —————————————-


// This is our page serving function that generates web pages
boolean sendMyPage(char* URL) {
  
   // if (strcmp(URL, "/") == 0) {
        // Use WiServer’s print and println functions to write out the page content
        WiServer.print("<html>");
        WiServer.print("Temperatura: ");
        
        //WiServer.print(tempC);
        
        WiServer.print(sensors.getTempC(sonda1));
        
        WiServer.print("</html>");
        // URL was recognized
        return true;
    //}
    // URL not found
    return false;
}



void setup() {
  // Initialize WiServer and have it use the sendMyPage function to serve pages
  WiServer.init(sendMyPage);
  // Enable Serial output and ask WiServer to generate log messages (optional)
  //Serial.begin(57600);
  //WiServer.enableVerboseMode(true);
 
  //Serial.begin(9600);
  sensors.begin();
  sensors.setResolution(sonda1, TEMPERATURE_PRECISION);
}

void loop(){
 
  sensors.requestTemperatures();
  WiServer.server_task();
 // delay(10);
 
}


Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 203
Posts: 8720
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It takes 0.75 seconds for a DS18B20 to measure a temperature.  It is not a good idea to gather the temperature every time through loop().
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Monza
Offline Offline
Jr. Member
**
Karma: 0
Posts: 71
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i supposed so... what can i do? adding a delay() in the loop will slow down much more the entire loop.... if i was able, i would make the arduino read the temp. every 10 seconds and store the value... but i don't know how to do it...
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 203
Posts: 8720
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If the library you use allows it you can tell the DS18B20 to start a conversion and send a separate request for the result at least 750 milliseconds later.

To check every 10 seconds, without putting in a delay(), you can use the "BlinkWithoutDelay" example as a starting point.

Code:
void loop()
    {
    static unsigned long lastSampleTime = 0;
    if (millis() - lastSampleTime > 10000)  // Only after 10 seconds
         {
         lastSampleTime = millis();
         //get temperature again
         }
    }
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Pages: [1]   Go Up
Jump to: