Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Sketch which publishes temperature data from a DS1820 sensor to a MQTT topic.
- This sketch goes in deep sleep mode once the temperature has been sent to the MQTT
- topic and wakes up periodically (configure SLEEP_DELAY_IN_SECONDS accordingly).
- Hookup guide:
- - connect D0 pin to RST pin in order to enable the ESP8266 to wake up periodically
- - DS18B20:
- + connect VCC (3.3V) to the appropriate DS18B20 pin (VDD)
- + connect GND to the appopriate DS18B20 pin (GND)
- + connect D4 to the DS18B20 data pin (DQ)
- + connect a 4.7K resistor between DQ and VCC.
- */
- #include <ESP8266WiFi.h>
- #include <PubSubClient.h>
- #include <OneWire.h>
- #include <DallasTemperature.h>
- #include <Streaming.h>
- #include <Adafruit_NeoPixel.h>
- #ifdef __AVR__
- #include <avr/power.h>
- #endif
- #define SLEEP_DELAY_IN_SECONDS 120
- #define ONE_WIRE_BUS D4 // DS18B20 pin
- #define PIN 2
- const char* ssid = "Time";
- const char* password = "motocarro";
- //const char* ssid = "sfy";
- //const char* password = "WifiSoftForYou17";
- const char* mqtt_server = "mqtt.thingspeak.com";
- const char* mqtt_username = "joaquinsfy@gmail.com";
- const char* mqtt_password = "Motocarro13";
- const char* mqtt_topic = "sensors/test/temperature";
- WiFiClient espClient;
- PubSubClient client(espClient);
- OneWire oneWire(ONE_WIRE_BUS);
- DallasTemperature DS18B20(&oneWire);
- char temperatureString[6];
- void setup() {
- // setup serial port
- Serial.begin(115200);
- // setup WiFi
- setup_wifi();
- client.setServer(mqtt_server, 1883);
- client.setCallback(callback);
- // setup OneWire bus
- DS18B20.begin();
- }
- void setup_wifi() {
- Serial.println();
- Serial.print("Connecting to ");
- Serial.println(ssid);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED ) {
- Serial.println(".");
- delay(500);
- }
- Serial.println("");
- Serial.println("WiFi connected");
- Serial.println("IP address: ");
- Serial.println(WiFi.localIP());
- }
- void callback(char* topic, byte* payload, unsigned int length) {
- Serial.print("Message arrived [");
- Serial.print(topic);
- Serial.print("] ");
- for (int i = 0; i < length; i++) {
- Serial.print((char)payload[i]);
- }
- Serial.println();
- }
- void reconnect() {
- // Loop until we're reconnected
- while (!client.connected()) {
- Serial.print("Attempting MQTT connection...");
- // Attempt to connect
- if (client.connect("ESP8266Client", mqtt_username, mqtt_password)) {
- Serial.println("connected");
- } else {
- Serial.print("failed, rc=");
- Serial.print(client.state());
- Serial.println(" try again in 5 seconds");
- // Wait 5 seconds before retrying
- delay(5000);
- }
- }
- }
- float getTemperature(unsigned int sensor) {
- Serial << "Requesting DS18B20 temperature..." << endl;
- float temp;
- do {
- DS18B20.requestTemperatures();
- temp = DS18B20.getTempCByIndex(sensor);
- delay(100);
- } while (temp == 85.0 || temp == (-127.0));
- return temp;
- }
- void loop()
- {
- enviatemperatura1();
- Serial << "Closing WiFi connection..." << endl;
- WiFi.disconnect();
- Serial << "Entering deep sleep mode for " << SLEEP_DELAY_IN_SECONDS << " seconds..." << endl;
- ESP.deepSleep(SLEEP_DELAY_IN_SECONDS * 1000000, WAKE_RF_DEFAULT);
- //ESP.deepSleep(10 * 1000, WAKE_NO_RFCAL);
- delay(500); // wait for deep sleep to happen
- }
- void enviatemperatura1()
- { if (!client.connected()) {
- reconnect();
- }
- client.loop();
- // temperatura 1
- float temperature = getTemperature(0);
- // convert temperature to a string with two digits before the comma and 2 digits for precision
- dtostrf(temperature, 2, 2, temperatureString);
- // send temperature to the serial console
- Serial << "Sending temperature: " << temperatureString << endl;
- // send temperature to the MQTT topic
- String data = String(temperature, DEC);
- // Get the data string length
- int length = data.length();
- char msgBuffer[length];
- data.toCharArray(msgBuffer, length + 1);
- Serial.println(msgBuffer);
- // Publish data to ThingSpeak. Replace <YOUR-CHANNEL-ID> with your channel ID and <YOUR-CHANNEL-WRITEAPIKEY> with your write API key
- client.publish("channels/218099/publish/fields/field3/P0FMMIR5ADIW03HO", msgBuffer);
- Serial << "Closing MQTT connection..." << endl;
- client.disconnect();
- }
Add Comment
Please, Sign In to add comment