Driving the ESP8266 GPIO (Web Server) from Jeedom to TCP / IP Wireless – Part 2

Here is the second part of the article to communicate an ESP8266 in TCP/IP with Jeedom. In this tutorial we will see how to drive the GPIO of an ESP8266 from Jeedom using HTTP requests (TCP / IP). The principle is the same as for Domoticz (article), we start a small Web server on the ESP8266 which will launch a function every time a request is received on an entry point (here /gpio). Implementation rated Jeedom is a little more complicated than for Domoticz. It will be necessary to create a script capable of executing HTTP requests and then call this script in a virtual instrument (what we are going to do) or a scenario.

Previous article: ESP8266 (Web Client): sending data to Jeedom in TCP / IP wireless (JSON API) – Part 1

Equipment used

The material and editing remains identical to the previous tutorials.

esp8266 Wemos D1 mini Any ESP8266 ESP-12 module, for example Wemos D1 Mini
 BMP180 Atmospheric pressure

BMP180

Broches DHT22 Temperature and humidity sensor

DHT11 or DHT22

wemos d1 mini dht22 shield Shield DHT22 or DHT11 for Wemos D1 Mini.

The sensor is connected to pin D4 (GPIO2)

wemos d1 mini relai shield Relay shield  (250VAC/10A or 30VDC/10A) for Wemos D1 Mini.

The relay is connected to pin D1 (GPIO5)

jumper dupont Jumper Dupont
breadboard Breadboard

Circuit

Composant Pin ESP8266 Pin (Wemos D1 mini)
DHT22 (shield Wemos D1 Mini) VCC 3V3
GND G
Data D4
BMP180 VCC 3V3
GND G
SDA D2
SCK D1
Relay Shield D1

ESP8266 DHT22 BMP180 wemos Shield Relay jeedom ESP8266HTTPCLIENT_

Changing the I2C bus pins on the ESP8266

If you are using a sensor running on the I2C bus, you will need to change one or two pins of the I2C bus. To do this, we use the Wire.h library

Declare the library at the beginning of the program

#include <Wire.h>

In the setup(), the I2C bus is initialized by specifying the new pins. Here, I moved by pin D1 (SCK) to pin D3, which gives

Wire.begin (D2, D3);

You can now move the I2C bus over the pins you want.

Driving the ESP8266 GPIO from Jeedom

For this tutorial, I used the shield relay for Wemos D1 mini that comes stacked above the ESP8266 to form a compact (and economical, less than 8 €).

Create a script to drive the GPIO by HTTP request

Go to plugins -> Programming -> Script (install the script plugin if the menu does not exist) then add a new script:

  • Give it a name beginning with script_xxxx. This will allow you to distinguish the scripts from other devices and find them more easily in the list of choices.
  • Assign read a parent object (optional),
  • Activate the
  • Do not tick visible

Go to the Commands tab and click Add a script command:

  • Give it the name ON
  • Script type: HTTP
  • Type: action
  • Query: Replace the IP address, GPIO id, and token
    • http://#IP_ESP8266#/gpio?id=#ID#&etat=1&token=#token#
  • The request must contain:
    • Enter success:1 (or another return that will have to be modified in the Arduino code)
  • A maximum response time of the ESP8266 (timeout). For example 2 seconds
  • Maximum number of attempts: 5 (optional)
  • Untick View

Duplicate the command by pressing the icon (superimposed documents) next to the test button. Change state=1 by state=0. Save it.

jeedom script commande http pilotage gpio esp8266

Create a virtual button (equipment)

Go to plugins -> Programming -> Virtual (install the virtual plugin if the menu does not exist) then add a new device. Give luis a name, a parent object and check enable and visible. Then go to the Commands tab and add 2 virtual commands

For the first order:

  • Give it the name On
  • Click Find Equipment. Find the script and then the ON command

jeedom selection script on

Do the same for the Off command. Save it.

jeedom equipement virtuel bouton commande gpio esp8266

The configuration of Jeedom is complete.

Arduino Code / ESP8266

The Arduino code is exactly the same as the Domoticz version. To learn more about how it works, I invite you to read the previous tutorial. The only difference is the return of the updateGpio() function. While Domoticz does not expect anything from the ESP8266, it is necessary to respond to Jeedom. This does not interfere with the operation of the home automation server, but Jeedom will display a notification and add an error to the event log. So much to do things. We will just return a string (success:1) that will be tested by the script. That’s all.

server.send(200, "application/json","success:1");

Create a new skit from the Arduino IDE. Edit the variables before uploading the program.

/*
 * HTTP communication between ESP8266 and Jeedom Smart Home Server
 * Communication HTTP (TCP/IP) entre ESP8266 et le serveur domotique Jeedom
 * Copyright (C) 2017 http://www.projetsdiy.fr - https://www.diyprojects.io
 *   
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include <ESP8266WebServer.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClientSecure.h>
#include <DHT.h>
#include <Adafruit_BMP085.h>

#define DHTTYPE   DHT22       // DHT type (DHT11, DHT22)
#define DHTPIN    D4          // Broche du DHT / DHT Pin

const char* ssid     = "xxxxxxxx";
const char* password = "xxxxxxxx";
const char* host     = "xxx.xxx.xxx.xxx";
const int   port     = 80;
const char* apiKey   = "mVuQikcXm69l2mDMYZiCsLj0wamT0HsCcrBCuXRxntJDO1kn";
const char* idTemp   = "78";
const char* idHumi   = "79";
const char* idPA     = "80";
const int   watchdog = 60000;
unsigned long previousMillis = millis(); 
const uint8_t GPIOPIN[5] = {D1,D5,D6,D7,D8};  // Led

DHT dht(DHTPIN, DHTTYPE);
Adafruit_BMP085 bmp;
ESP8266WebServer server ( 80 );
HTTPClient http;

void updateGpio(){
  Serial.println("Update GPIO command from Jeedom : ");
  for ( int i = 0 ; i < server.args(); i++ ) {
    Serial.print(server.argName(i)); Serial.println(server.arg(i));
  }
  String gpio = server.arg("id"); 
  String token = server.arg("token");
  // Vérifie si le serveur est autorisé - Check if server is authorized
  if ( token != "123abCde" ) {
    Serial.println("Not authentified ");
    return;
  }
  int etat = server.arg("etat").toInt();
  int pin = D1;
  if ( gpio == "D1" ) {
    pin = D1;
  } else if ( gpio == "D7" ) {
    pin = D7;
  } else if ( gpio == "D8" ) {
    pin = D8;  
  } else {   
    pin = D1;
  }
  Serial.print("Update GPIO "); Serial.println(pin);
  if ( etat == 1 ) {
    digitalWrite(pin, HIGH);
    Serial.println("GPIO updated : On");
    server.send(200, "application/json","success:1");
  } else if ( etat == 0 ) {
    digitalWrite(pin, LOW);
    Serial.println("GPIO updated : Off");
    server.send(200, "application/json","success:1");
  } else {
    Serial.println("Bad state Value !");
    server.send(200, "application/json","success:0");
  }
}

void setup() {
  Serial.begin(115200);
  delay(10);
  
  Wire.begin(D2,D3);            // Modifie les broches du bus I2C - Change I2C Pins
  if ( !bmp.begin() ) {
    Serial.println("BMP180 KO!");
    while (1);
  } else {
    Serial.println("BMP180 OK");
  }
  
  Serial.setDebugOutput(true);  
  Serial.println("Connecting Wifi...");

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  
  for ( int x = 0 ; x < 5 ; x++ ) {
    pinMode(GPIOPIN[x], OUTPUT);
  }
  
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.print(WiFi.localIP()); 
  
  server.on("/gpio", updateGpio);
  server.begin();
}

int value = 0;

void loop() {
  server.handleClient();
  unsigned long currentMillis = millis();

  if ( currentMillis - previousMillis > watchdog ) {
    previousMillis = currentMillis;

    if(WiFi.status() != WL_CONNECTED) {
      Serial.println("WiFi not connected !");
    } else {  
      Serial.println("Send data to Jeedom");
      
      float t = dht.readTemperature();
      float h = dht.readHumidity();
      float pa = bmp.readPressure() / 100.0F;
      
      if ( isnan(t) || isnan(h) ) {
        Serial.println("DHT KO");
      } else {
        String baseurl = "/core/api/jeeApi.php?apikey="; 
        baseurl += apiKey;
        baseurl += "&type=virtual&id="; 
        String url = baseurl + idTemp;
        url += url + "&value="; url += String(t); 
        sendToJeedom(url);
        delay(1000);
        
        url = baseurl + idHumi;
        url += url + "&value="; url += String(h);
        sendToJeedom(url);
        delay(1000);

        url = baseurl + idPA;
        url += url + "&value="; url += String(pa);
        sendToJeedom(url);
        delay(1000);
      }
    }
  }
}

boolean sendToJeedom(String url){
  Serial.print("connecting to ");
  Serial.println(host);
  Serial.print("Requesting URL: ");
  Serial.println(url);
  http.begin(host,port,url);
  int httpCode = http.GET();
  http.end();
}

You can now control the GPIO of any ESP8266 from Jeedom.

https://www.projetsdiy.fr/wp-content/uploads/2017/04/jeedom-wemos-d1-mini-shiedl-relay-http-request.png

Subscribe to the weekly newsletter

No spam and no other use will be made of your email. You can unsubscribe anytime.

Tags:

We will be happy to hear your thoughts

Leave a Reply

DIY Projects
%d bloggers like this: