Last active
May 18, 2018 21:39
-
-
Save teos0009/844dd1da7e457004a32e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//ESP8266 (ESP01) with ESP01 BoB | |
//MQTT stream temperature data DS18B20 with 1wire on ESP8266 ESP01 and then turn load on/off. perfect for sous vide over the internet. | |
//shin-ajaran.blogspot.com | |
#include <ESP8266WiFi.h> | |
#include <PubSubClient.h> | |
#include <OneWire.h> | |
#include <DallasTemperature.h> | |
const char *ssid = "SSID_here"; // cannot be longer than 32 characters! | |
const char *pass = "password_here"; // | |
const char* serverTS = "api.thingspeak.com"; | |
String apiKey ="API_key_here"; | |
// Update these with values suitable for your network. | |
IPAddress server(192, 168, 1, 104); //node red hosted on ubuntu vm bridged to host. | |
#define BUFFER_SIZE 100 | |
#define ONE_WIRE_BUS 2 // DS18B20 on GPIO2 | |
#define mySSR 0 // Solid State Relay on GPIO0 | |
#define myPeriodic 5 //in sec | Thingspeak pub is 15sec | |
void callback(const MQTT::Publish& pub) { | |
Serial.print(pub.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); | |
} | |
pub.payload_stream()->stop(); | |
Serial.println(""); | |
} else | |
Serial.println(pub.payload_string()); | |
Serial.println(pub.payload_string()); | |
}//end | |
WiFiClient wclient; | |
PubSubClient client(wclient, server); | |
OneWire oneWire(ONE_WIRE_BUS); | |
DallasTemperature DS18B20(&oneWire); | |
float oPID = 0;//subs to PID out node red | |
float setPoint = 30;//temperature setpoint used in PID | |
void setup() { | |
// Setup console | |
pinMode(mySSR, OUTPUT); | |
Serial.begin(115200); | |
delay(10); | |
Serial.println(); | |
Serial.println(); | |
client.set_callback(callback); | |
} | |
void loop() { | |
float temp; | |
//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); | |
//dummy code to turn load | |
if (temp < setPoint ){ | |
digitalWrite(mySSR, HIGH); // turn on the load | |
} | |
else{ | |
digitalWrite(mySSR, LOW); // turn on the load | |
} | |
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) { | |
if (!client.connected()) { | |
if (client.connect("arduinoClient")) { | |
client.subscribe("utopic"); //rx cmd via mqtt | |
//client.publish("mytopic",String(temp));//tx data via mqtt | |
} | |
} | |
if (client.connected()) | |
client.loop(); | |
client.publish("mytopic",String(temp));//tx data via mqtt | |
}//end if | |
int count = myPeriodic; | |
while(count--){ | |
delay(1000); | |
} | |
//send TS | |
sendTemperatureTS(temp); | |
}//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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment