Example sketches for the new Arduino IDE for ESP8266

Moderator: igrr

User avatar
By gakks
#39136 Hi Melvin, interested in your sous vide setup. Do you have a tutorial/script somewhere? I am just starting to get into the ESP8266 and finding it tricky to code for a sous vide and set up the html page with outputs in one go.
User avatar
By krzychb
#40437 Hello all :D

I have prepared a new section to OnlineHumidifier project that is using ESP8266 with openHAB.
This one is on HTTP binding, an easier alternative to previously discussed MQTT binding.
Give it a try If you are not convinced to MQTT or just baffled by MQTT broker :roll:

Krzysztof
User avatar
By karabey
#43786 Hey krzychb,

Thanks for the great Tutorial but Libraries are missing like RemoteTransmitter.h and even compiling is a problem.
The File structure could be better :P

How said RemoteTransmitter.h is in the same folder like OnlineHumidifier-openHAB.ino and if I want to compile it than this is the result:

Code: Select allOnlineHumidifier-openHAB:32: error: redefinition of 'DHT dht'
OnlineHumidifier-MQTT:31: error: 'DHT dht' previously declared here
OnlineHumidifier-openHAB:33: error: redefinition of 'float humidity'
OnlineHumidifier-MQTT:32: error: 'float humidity' previously declared here
OnlineHumidifier-openHAB:34: error: redefinition of 'float humiditySetPoint'
OnlineHumidifier-MQTT:33: error: 'float humiditySetPoint' previously defined here
OnlineHumidifier-openHAB:46: error: redefinition of 'ActionTransmitter actionTransmitter'
OnlineHumidifier-MQTT:45: error: 'ActionTransmitter actionTransmitter' previously declared here
OnlineHumidifier-openHAB:47: error: redefinition of 'bool humidifier'
OnlineHumidifier-MQTT:46: error: 'bool humidifier' previously defined here
OnlineHumidifier-openHAB:48: error: redefinition of 'bool autoMode'
OnlineHumidifier-MQTT:47: error: 'bool autoMode' previously defined here
OnlineHumidifier-openHAB:49: error: redefinition of 'char rcDevice'
OnlineHumidifier-MQTT:48: error: 'char rcDevice' previously defined here
OnlineHumidifier-openHAB:61: error: redefinition of 'const char* ssid'
OnlineHumidifier-MQTT:60: error: 'const char* ssid' previously defined here
OnlineHumidifier-openHAB:62: error: redefinition of 'const char* password'
OnlineHumidifier-MQTT:61: error: 'const char* password' previously defined here
OnlineHumidifier-openHAB:63: error: redefinition of 'ESP8266WebServer server'
OnlineHumidifier-MQTT:62: error: 'ESP8266WebServer server' previously declared here
OnlineHumidifier-openHAB:77: error: redefinition of 'const char* host'
OnlineHumidifier-MQTT:76: error: 'const char* host' previously defined here
OnlineHumidifier-openHAB:87: error: redefinition of 'const char* emoncmsServer'
OnlineHumidifier-MQTT:86: error: 'const char* emoncmsServer' previously defined here
OnlineHumidifier-openHAB:89: error: redefinition of 'String apiKeyEmoncms'
OnlineHumidifier-MQTT:88: error: 'String apiKeyEmoncms' previously declared here
OnlineHumidifier-openHAB:90: error: redefinition of 'WiFiClient client'
OnlineHumidifier-MQTT:89: error: 'WiFiClient client' previously declared here
OnlineHumidifier-openHAB:99: error: redefinition of 'WiFiClient wclient'
OnlineHumidifier-MQTT:98: error: 'WiFiClient wclient' previously declared here
OnlineHumidifier-openHAB:100: error: redefinition of 'PubSubClient MQTTclient'
OnlineHumidifier-MQTT:99: error: 'PubSubClient MQTTclient' previously declared here
OnlineHumidifier-openHAB.ino: In function 'void setup()':
OnlineHumidifier-openHAB:170: error: redefinition of 'void setup()'
OnlineHumidifier-MQTT:102: error: 'void setup()' previously defined here
OnlineHumidifier-openHAB.ino: In function 'void loop()':
OnlineHumidifier-openHAB:183: error: redefinition of 'void loop()'
OnlineHumidifier-MQTT:113: error: 'void loop()' previously defined here
redefinition of 'DHT dht'


Sketch uses 235,369 bytes (54%) of program storage space. Maximum is 434,160 bytes.
Global variables use 31,780 bytes (38%) of dynamic memory, leaving 50,140 bytes for local variables. Maximum is 81,920 bytes.


The Sketch OnlineHumidifier-openHAB.ino

Code: Select all/*
  Control of Humidifier Connected to a RC Socket
  https://github.com/krzychb/OnlineHumidifier
  Part A2 - openHAB

  Copyright (c) 2015 Krzysztof Budzynski. All rights reserved.
  This file is part of OnlineHumidifier project - https://github.com/krzychb/OnlineHumidifier

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/



//
// DHT Sensor Setup
//
#include "DHT.h"
//  dht(DHTPIN, DHTTYPE);
DHT dht(D2, DHT21);
float humidity;
float humiditySetPoint = 43;
// function prototype required by Arduino IDE 1.6.7
void measureHumidity(void);


//
// RF 433 transmitter configuration
//
// download - https://bitbucket.org/fuzzillogic/433mhzforarduino/wiki/Home/
//
#include <RemoteTransmitter.h>
#define RF433_TR_PIN 14   // pin where RF433 Transmitter is connected to
ActionTransmitter actionTransmitter(RF433_TR_PIN);
bool humidifier = LOW;
bool autoMode = true;
char rcDevice = 'A';
// function prototype required by Arduino IDE 1.6.7
void executeHumidityControl(void);


//
// Wi-Fi Setup
//
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
const char* ssid = "********";     // your network SSID (name)
const char* password = "********";  // your network password
ESP8266WebServer server(80);
// function prototypes required by Arduino IDE 1.6.7
void setupWiFi(void);
void handleRoot(void);
void handleNotFound(void);
void showControlScreen(void);
void setupWebserver(void);


//
// OTA configuration
//
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
const char* host = "OTA-hygrostat";
// function prototypes required by Arduino IDE 1.6.7
void setupOTA(void);


//
// Emoncms configuration
//
// Domain name of emoncms server - "emoncms.org"
// If unable to connect with domant name, use IP adress instead - "80.243.190.58"
const char* emoncmsServer = "emoncms.org";
//                       enter your Read & Write API Key below
String apiKeyEmoncms = "be71f01adf17bfa1a85118923c0140b4";
WiFiClient client;
// function prototypes required by Arduino IDE 1.6.7
void sendDataToEmoncms(void);


//
// MQTT configuration
//
#include <PubSubClient.h>
WiFiClient wclient;
PubSubClient MQTTclient(wclient, "test.mosquitto.org");
const char* MQTTclientName = "hygrostat";

void MQTTcallback(const MQTT::Publish& pub)
{
  Serial.print("MQTT CALLBACK > ");
  Serial.print(pub.topic());
  Serial.print(" ");
  Serial.println(pub.payload_string());

  if (pub.topic() == "krzychb/openhab/hygrostat/HumiditySetPoint")
  {
    // adjust the stepoint
    humiditySetPoint = pub.payload_string().toFloat();
  }

  if (pub.topic() == "krzychb/openhab/hygrostat/Humidifier")
  {
    // turn humidifier on or off from openHAB
    humidifier = (pub.payload_string() == "ON") ? HIGH : LOW;
    actionTransmitter.sendSignal(1, rcDevice, humidifier);
  }

  // turn the control mode on or off from openHAB
  if (pub.topic() == "krzychb/openhab/hygrostat/AutoMode")
  {
    autoMode = (pub.payload_string() == "ON") ? true : false;
  }
}


void MQTTpublishData(void)
{
  Serial.print("MQTT PUBLISH / SUBSCRIBE > ");
  if (!MQTTclient.connected())
  {
    Serial.print("connecting > ");
    MQTTclient.connect(MQTTclientName);
  }
  if (MQTTclient.connected())
  {
    MQTTclient.publish("krzychb/home/sensor/Humidity", (String) humidity);
    MQTTclient.publish("krzychb/home/hygrostat/HumiditySetPoint", (String) humiditySetPoint);
    MQTTclient.publish("krzychb/home/hygrostat/Humidifier", (humidifier == HIGH) ? "ON" : "OFF");
    MQTTclient.publish("krzychb/home/hygrostat/AutoMode", (autoMode == true) ? "ON" : "OFF");
    MQTTclient.subscribe("krzychb/openhab/hygrostat/#");
    Serial.println("done");
  }
  else
  {
    Serial.println("connection failed!");
  }
}


void MQTTkeepAlive(void)
{
  // keep MQTT connected
  if (!MQTTclient.connected())
  {
    MQTTclient.connect(MQTTclientName);
  }
  else
  {
    // handle incoming MQTT messages
    MQTTclient.loop();
  }
}


void setup(void)
{
  Serial.begin(115200);

  dht.begin();
  setupWiFi();
  setupWebserver();
  setupOTA();

  MQTTclient.set_callback(MQTTcallback);
}


void loop(void)
{

  // monitor / keep alive connections
  ArduinoOTA.handle();
  server.handleClient();
  MQTTkeepAlive();

  // execute actions below evey 45 seconds
  if (millis() % 45000 == 0)
  {
    measureHumidity();
    if (autoMode == true)
    {
      executeHumidityControl();
    }
    sendDataToEmoncms();
    MQTTpublishData();
  }
}
User avatar
By krzychb
#43814 Hi karabey,

Installation of RemoteTransmitter library is described in section 7-Control.
Have you check it?
If not, before trying OnlineHumidifier-openHAB.ino sketch, I recommend that you check this section and as a minimum compile example sketch RF433-SocketTester.ino.

Krzysztof