CC3000 Digital In Sketch Help - Adding Delay to DS18S20 Temp

Moderators: adafruit_support_bill, adafruit

Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.
Locked
User avatar
moyerek
 
Posts: 4
Joined: Tue Jun 02, 2015 10:59 pm

CC3000 Digital In Sketch Help - Adding Delay to DS18S20 Temp

Post by moyerek »

Apologize for such a simply question but haven't been able to figure out where I should place delay so I am only reading my sensor less often. I really only need to record the temperature every 10 minutes. I am using the "Adafruit CC3000 Digital In" example sketch that I modified to work with a DS18S20 temp sensor. Everything is running fine except every time I insert a delay somewhere it never connects to Wifi.

I didn't include the top of the sketch defining all the set-ups, etc. The below sketch works fine but again, no delay.

Any help would be most appreciated.

Code: Select all

/*************************** Sketch Code ************************************/

//Temperature chip i/o
OneWire ds(DS18S20_Pin);  // on digital pin 2 

void setup() {

  Serial.begin(115200);

  Serial.println(F("Adafruit IO Example:"));
  Serial.print(F("Free RAM: ")); Serial.println(getFreeRam(), DEC);

  // Initialise the CC3000 module
  Serial.print(F("\nInit the CC3000..."));
  if (! cc3000.begin())
    halt("Failed");

  // attempt wifi connection
  while (! CC3000connect(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
    Serial.println(F("Retrying WiFi"));
    while(1);
  }
  Serial.println(F("Connected to WiFi!"));

  // connect to adafruit io
  connect();

}

void loop() {

  // Make sure to reset watchdog every loop iteration!
  Watchdog.reset();

  float tempC = getTemp();
  float tempF = (tempC * 9.0)/ 5.0 + 32.0;
if(tempF < 60)
return;

  // ping adafruit io a few times to make sure we remain connected
  if(! mqtt.ping(3)) {
    // reconnect to adafruit io
    if(! mqtt.connected())
      connect();
  }


  // Now we can publish stuff!
  Serial.print(F("\nSending temp value: "));
  Serial.print(tempF);
  Serial.print("... ");

  if (! temp.publish(tempF))
    Serial.println(F("Failed."));
  else
    Serial.println(F("Success!"));

}

// connect to adafruit io via MQTT
void connect() {

  Serial.print(F("Connecting to Adafruit IO... "));

  int8_t ret;

  while ((ret = mqtt.connect()) != 0) {

    switch (ret) {
      case 1: Serial.println(F("Wrong protocol")); break;
      case 2: Serial.println(F("ID rejected")); break;
      case 3: Serial.println(F("Server unavail")); break;
      case 4: Serial.println(F("Bad user/pass")); break;
      case 5: Serial.println(F("Not authed")); break;
      case 6: Serial.println(F("Failed to subscribe")); break;
      default:
        Serial.println(F("Connection failed"));
        CC3000connect(WLAN_SSID, WLAN_PASS, WLAN_SECURITY);
        break;
    }

    if(ret >= 0)
      mqtt.disconnect();

    Serial.println(F("Retrying connection..."));
    delay(5000);

  }

  Serial.println(F("Adafruit IO Connected!"));

}

float getTemp(){
  //returns the temperature from one DS18S20 in DEG Celsius

  byte data[12];
  byte addr[8];

  if ( !ds.search(addr)) {
      //no more sensors on chain, reset search
      ds.reset_search();
      return -1000;
  }

  if ( OneWire::crc8( addr, 7) != addr[7]) {
      Serial.println("CRC is not valid!");
      return -1000;
  }

  if ( addr[0] != 0x10 && addr[0] != 0x28) {
      Serial.print("Device is not recognized");
      return -1000;
  }

  ds.reset();
  ds.select(addr);
  ds.write(0x44,1); // start conversion, with parasite power on at the end

  byte present = ds.reset();
  ds.select(addr);    
  ds.write(0xBE); // Read Scratchpad

  
  for (int i = 0; i < 9; i++) { // we need 9 bytes
    data[i] = ds.read();
  }
  
  ds.reset_search();
  
  byte MSB = data[1];
  byte LSB = data[0];

  float tempRead = ((MSB << 8) | LSB); //using two's compliment
  float TemperatureSum = tempRead / 16;
  
  return TemperatureSum;
  
}

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: CC3000 Digital In Sketch Help - Adding Delay to DS18S20

Post by adafruit_support_rick »

Just did the very same thing last night (except with an ESP8266). I put the delay here, and it worked just fine:

Code: Select all

  if (! temp.publish(tempF))
    Serial.println(F("Failed."));
  else
    Serial.println(F("Success!"));

  delay(60000);
That's not working for you?

User avatar
moyerek
 
Posts: 4
Joined: Tue Jun 02, 2015 10:59 pm

Re: CC3000 Digital In Sketch Help - Adding Delay to DS18S20

Post by moyerek »

Thanks for the help.

Locked
Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.

Return to “Internet of Things: Adafruit IO and Wippersnapper”