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