//ESP8266 (ESP12E) with SMD BoB //MQTT stream temperature data DS18B20 with 1wire on ESP8266 ESP12E and then turn load on/off. sous vide over the internet. #include #include #include #include const char *ssid = "SSID here"; const char *pass = "password here"; const char* serverTS = "api.thingspeak.com"; String apiKey = "API key here"; //server IP IPAddress server(192, 168, 1, 104); #define BUFFER_SIZE 100 #define ONE_WIRE_BUS 4 // DS18B20 on GPIO4 #define mySSR 5 // Solid State Relay on GPIO5 #define myPeriodic 5 //in sec | Thingspeak pub is 15sec float setPoint = 65;//temperature setpoint used in PID float c1 = 0;//contains PID out from callback int count = 0; float temp=0; void callback(const MQTT::Publish& pub) {//param is for publish, but using it to extract subscribe data Serial.print(pub.topic());//publish to a topic Serial.print(" ==> "); if (pub.has_stream()) { uint8_t buf[BUFFER_SIZE]; int read; while (read = pub.payload_stream()->read(buf, BUFFER_SIZE)) { Serial.write(buf, read); }//end while pub.payload_stream()->stop(); Serial.println("**"); }//end if else { Serial.println("##");//commented off then show on serial comm Serial.println(pub.payload_string());//deadbeef //---rx mqtt ctrl-------- //nodered PID returns y.xxxx before setpoint or -x.xxxx after setpoint float e1 = atoi(pub.payload_string().c_str());//e1 is node red pid output if (e1 > 0) { digitalWrite(5, HIGH); Serial.print("e1 high "); Serial.println(e1); } else{ digitalWrite(5, LOW); c1 = e1;//copy out var Serial.print("e1 low "); Serial.print(e1); } //---end rx mqtt----------- } }//end /* void callback(const MQTT::Subscribe& sub) { float e1 = 0; if (sub.has_stream()) { } //---rx mqtt ctrl-------- //nodered PID returns 0.0xxxx before setpoint or -0.00xxxx after setpoint //float e1 = atoi(sub.payload_string().c_str());//e1 is pid output if (e1 > 0) { //Serial.println("high " + sub.payload_string()); digitalWrite(mySSR, HIGH); // turn on the load } else if (e1 < 0) { //Serial.println("low " + sub.payload_string()); digitalWrite(mySSR, LOW); // turn on the load } //---end rx mqtt----------- }//end */ WiFiClient wclient; PubSubClient client(wclient, server);//worked OneWire oneWire(ONE_WIRE_BUS); DallasTemperature DS18B20(&oneWire); void reconnect() { while (!client.connected()) { if (client.connect("arduinoClient")) { Serial.println("REconnected"); client.publish("myTopic", "0"); client.subscribe("uTopic"); } else { Serial.print("failed, rc="); Serial.println(" retry in 5 seconds"); delay(5000); } } } void setup() { // Setup console pinMode(mySSR, OUTPUT); Serial.begin(115200); delay(10); Serial.println(); Serial.println(); client.set_callback(callback); } void loop() { //char buffer[10]; DS18B20.requestTemperatures(); temp = DS18B20.getTempCByIndex(0); //String tempC = dtostrf(temp, 4, 1, buffer);//handled in sendTemp() //Serial.print(" Temperature: "); Serial.println(temp); //---hard off ensure below setpoint---- if (temp > setPoint ) { digitalWrite(mySSR, LOW); // turn off the load } if (c1 > 0) { Serial.println("set SSR high "); } else{ Serial.println("set SSR low "); } //----------- if (WiFi.status() != WL_CONNECTED) { Serial.print("Connecting to "); Serial.print(ssid); Serial.println("..."); WiFi.begin(ssid, pass); if (WiFi.waitForConnectResult() != WL_CONNECTED) return; Serial.println("WiFi connected"); }//end if if (WiFi.status() == WL_CONNECTED) { Serial.println("IP address: "); Serial.println(WiFi.localIP()); if (!client.connected()) { if (client.connect("arduinoClient")) { Serial.println("connected Client"); client.subscribe("utopic"); //rx cmd via mqtt Serial.println("sub utopic"); //client.publish("mytopic",String(temp));//tx data via mqtt }//if can connect else { reconnect();//spam till connected client.subscribe("utopic"); //rx cmd via mqtt Serial.println("reconnect sub utopic"); //client.set_callback(callback);//done at setup }//if cant connect }//not connected } if (client.connected()) client.loop(); client.publish("mytopic", String(temp)); //tx data via mqtt //Serial.println("pub mytopic"); delay(1000); count++; if (count == myPeriodic) { sendTemperatureTS(temp); }//send TS }//end loop void sendTemperatureTS(float temp) { WiFiClient tclient;//not to be confused with "client" in PubSub{}, and wclient for mqtt if (tclient.connect(serverTS, 80)) { // use ip 184.106.153.149 or api.thingspeak.com //Serial.println("WiFi Client connected "); String postStr = apiKey; postStr += "&field1="; postStr += String(temp); postStr += "\r\n\r\n"; tclient.print("POST /update HTTP/1.1\n"); tclient.print("Host: api.thingspeak.com\n"); tclient.print("Connection: close\n"); tclient.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n"); tclient.print("Content-Type: application/x-www-form-urlencoded\n"); tclient.print("Content-Length: "); tclient.print(postStr.length()); tclient.print("\n\n"); tclient.print(postStr); delay(1000); }//end if tclient.stop(); }//end send to ts