You can see my code here: BBQThermostat/BBQ_Thermostat.ino at master · djotaku/BBQThermostat · GitHub as of commit Added an mqtt topic for the smoker temp and one for the SSID · djotaku/BBQThermostat@da23769 · GitHub if it helps later if I update the file. I think the relevant code is:
void setup() {
// Serial init code. This is for debugging and should be commented out when it's running at the smoker if debugging is not taking place
Serial.begin(9600);
while (!Serial);
// make sure THERM shield will initialize
if (!THERM.begin()) {
Serial.println("Failed to initialize MKR THERM shield!");
while (1);
}
//attempt to connect to Wifi Network
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
// you're connected now, so print out the data: - this is also for debugging and should be commented out when running at the smoker if not debugging
Serial.print("You're connected to the network");
printCurrentNet();
printWifiData();
//mqtt
client.setServer(server, 1883);
client.setCallback(callback);
}
void loop() {
// debugging - wifi check
printCurrentNet();
// debugging - therm check
Serial.print("Reference temperature ");
Serial.print(THERM.readReferenceTemperature());
Serial.println(" °C");
Serial.print("Temperature ");
Serial.print(THERM.readTemperature());
Serial.println(" °C");
Serial.println();
//mqtt
int smokertempnum = THERM.readTemperature();
char cstr[100];
itoa(smokertempnum,cstr,10);
if (!client.connected()) {
reconnect();
}
client.publish("smoker/temp",cstr);
client.publish("smoker/WiFi/SSID",WiFi.SSID());
delay(10000);
}
Right now if I connect the board to power, I don't get anything published to MQTT. As soon as I connect a serial monitor (from the Arduino IDE), it APPEARS to me to print code as if it were at the beginning of the program. In other words, it prints that it's connected to the Wifi, the Wifi data, and then that it's connecting to the MQTT. Then I start getting stuff published to MQTT.
So that leads me to believe that if I'm printing to the serial and in my code, but I'm not connected to the serial console it will not go through the code. Is there a way to easily change this so that when I'm debugging and connected to the console I can see the data, but when I'm not connected to the console it works? I mean, it's not the end of the world if I have to comment out the serial lines, but I was wondering if there were a more elegant solution.
Thanks!