Skip to content

Instantly share code, notes, and snippets.

@teos0009
Last active October 15, 2016 03:20
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save teos0009/cfc0bdc97873776040e6 to your computer and use it in GitHub Desktop.
//ESP8266 (ESP01) with ESP01 BoB
//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 <OneWire.h>
#include <DallasTemperature.h>
//Def
#define myPeriodic 15 //in sec | Thingspeak pub is 15sec
#define ONE_WIRE_BUS 2 // DS18B20 on GPIO2
#define mySSR 0 // Solid State Relay on GPIO0
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);
float prevTemp = 0;
float setPoint = 30;//temperature setpoint used in PID
const char* server = "api.thingspeak.com";
String apiKey ="XXXXXXXXX";
const char* MY_SSID = "XXXXX";
const char* MY_PWD = "XXXXXXX";
int sent = 0;
void setup() {
pinMode(mySSR, OUTPUT);
Serial.begin(115200);
connectWifi();
}
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(String(sent)+" Temperature: ");
Serial.println(temp);
//dummy code to test output device
if (temp < setPoint )
{
digitalWrite(mySSR, HIGH); // turn on the load
}
else
{
digitalWrite(mySSR, LOW); // turn on the load
}
sendTeperatureTS(temp);
int count = myPeriodic;
while(count--)
delay(1000);
}
void connectWifi()
{
Serial.print("Connecting to "+*MY_SSID);
WiFi.begin(MY_SSID, MY_PWD);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("");
Serial.println("Connected");
Serial.println("");
}//end connect
void sendTeperatureTS(float temp)
{
WiFiClient client;
if (client.connect(server, 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";
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("\n\n");
client.print(postStr);
delay(1000);
}//end if
sent++;
client.stop();
}//end send
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment