Created
November 6, 2015 08:53
-
-
Save teos0009/b5b616d914e513144a4e 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 <OneWire.h> | |
#include <DallasTemperature.h> | |
#include <MQTT.h> //// https://github.com/Imroy/pubsubclient | |
#include <PubSubClient.h> //requires a broker that supports version 3.1.1 of the MQTT standard | |
//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 ="ZS2IA826R67RJQ77"; | |
const char* MY_SSID = "Edge"; | |
const char* MY_PWD = "!1234abcd"; | |
int sent = 0; | |
//mqtt server IP via node-red | |
IPAddress mqttServer(192, 168, 13, 129);//my ubuntu that host mqtt & nodered | |
void callback(const MQTT::Publish& pub) { | |
// handle msg from topic subscribed to aka on/off the SSR | |
} | |
WiFiClient wclient; | |
PubSubClient edgeSensor(wclient, mqttServer); | |
void setup() { | |
pinMode(mySSR, OUTPUT); | |
Serial.begin(115200); | |
connectWifi(); | |
edgeSensor.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(String(sent)+" Temperature: "); | |
Serial.println(temp); | |
if (temp < setPoint ){ | |
digitalWrite(mySSR, HIGH); // turn on the load | |
} | |
else{ | |
digitalWrite(mySSR, LOW); // turn on the load | |
} | |
sendTeperatureTS(temp); | |
if (!edgeSensor.connected()){ | |
if (edgeSensor.connect("arduinoClient")){ | |
edgeSensor.publish("outTopic","hello world"); | |
edgeSensor.subscribe("inTopic"); | |
} | |
} | |
int count = myPeriodic; | |
while(count--) | |
delay(1000); | |
if (edgeSensor.connected()) | |
edgeSensor.loop(); | |
} | |
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