Difference between revisions of "PulseCounter"

From Let's Control It
Jump to navigation Jump to search
(Blanked the page)
 
(10 intermediate revisions by 2 users not shown)
Line 1: Line 1:
[[File:TCR5000.jpg]]
 
  
= Introductie =
 
The ESP8266 module can be used als a general purpose digital pulsecounter device for various digitale sensors. Think about optical sensors with a digital output. To be used for things like gas, water, electricity, door open/dicht counters, etc.
 
 
In this example we will connect an optical sensor. Connect the digital output to some chosen GPIO pin on the ESP module. Load the firmware and follow the firmware specific instructions.
 
 
= Hardware =
 
A simple ESP8266 module like the ESP-01 is sufficient. We will use a TCR5000 optical sensor that is widely available. Connect pin 3 (DO) to some GPIO on the ESP and power up both the ESP and the sensor.
 
 
= Software =
 
== NodeMCU script ==
 
todo
 
 
== Arduino Custom Sketch ==
 
In Domoticz we will first create a virtual hardware device. The we will create a virtual device. Note the IDX from this newly created device since we need to enter this into the sketch.
 
 
For more information about Domoticz virtual hardware and devices, please consult the Domoticz website.
 
 
<code>
 
/*  Pulscounter demo sketch for Domoticz
 
*  Gets data from Domotics using json api, increment the value and update Domoticz counter
 
*  Connect your device to GPIO2 pin.
 
*  Change the Wifi config and the Domoticz virtual devide IDX value (check Domoticz/Setup/Devices)
 
*  And set the IP and port for your Domoticz server
 
*/
 
 
// ******************** User defined settings ****************************************
 
#define WIFI_SSID          "........"        // Your local Wifi SSID
 
#define WIFI_KEY            "........"          // Your local Wifi WPA key
 
#define DOMOTICZ_IP        "192.168.0.8"        // Domoticz server address
 
#define DOMOTICZ_PORT      8080                // Domoticz server portnumber
 
#define DOMOTICZ_IDX        72                  // IDX of Domoticz virtual device
 
#define DOMOTICZ_SENDDELAY  5                    // seconds between updates
 
#define ESP_COUNTER_IO_PIN  2                    // IO Pin used, default is GPIO2
 
// ********************* End User defined settings ************************************
 
 
// Do not change anything below...
 
#include <ESP8266WiFi.h>
 
 
// Pulse counter stuff
 
unsigned long pulseCounter=0;
 
 
const char* ssid    = WIFI_SSID;
 
const char* password = WIFI_KEY;
 
const char* host    = DOMOTICZ_IP;
 
const int httpPort  = DOMOTICZ_PORT;
 
 
float value=0;
 
 
void setup() {
 
  Serial.begin(115200);
 
  delay(10);
 
  Serial.print("Connecting to ");
 
  Serial.println(ssid);
 
 
 
  WiFi.begin(ssid, password);
 
 
 
  while (WiFi.status() != WL_CONNECTED) {
 
    delay(500);
 
    Serial.print(".");
 
  }
 
 
  Serial.println("");
 
  Serial.println("WiFi connected"); 
 
  Serial.println("IP address: ");
 
  Serial.println(WiFi.localIP());
 
 
  attachInterrupt(ESP_COUNTER_IO_PIN,ISR,FALLING);
 
 
}
 
 
void loop() {
 
  if (getdata(&value))
 
    {
 
      Serial.print("Current Value:");
 
      Serial.println(value);
 
      Serial.print("Delta Value:");
 
      Serial.println(pulseCounter);
 
      value=(value + pulseCounter)*100;
 
      pulseCounter=0;
 
      Serial.print("New Value:");
 
      Serial.println(value);
 
      senddata(value);
 
    }
 
  delay(DOMOTICZ_SENDDELAY * 1000);
 
}
 
 
boolean getdata(float *data)
 
{
 
  boolean success=false;
 
  Serial.print("connecting to ");
 
  Serial.println(host);
 
 
 
  // Use WiFiClient class to create TCP connections
 
  WiFiClient client;
 
  if (!client.connect(host, httpPort)) {
 
    Serial.println("connection failed");
 
    return false;
 
  }
 
 
 
  // We now create a URI for the request
 
  String url = "/json.htm?type=devices&rid=";
 
  url += DOMOTICZ_IDX;
 
 
 
  Serial.print("Requesting URL: ");
 
  Serial.println(url);
 
 
 
  // This will send the request to the server
 
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
 
              "Host: " + host + "\r\n" +
 
              "Connection: close\r\n\r\n");
 
 
  unsigned long timer=millis()+200;
 
  while(!client.available() && millis()<timer) {}
 
 
 
  // Read all the lines of the reply from server and print them to Serial
 
 
  while(client.available()){
 
    String line = client.readStringUntil('\n');
 
    if (line.substring(10,14) == "Data")
 
      {
 
        String strValue=line.substring(19);
 
        byte pos=strValue.indexOf(' ');
 
        strValue=strValue.substring(0,pos);
 
        strValue.trim();
 
        value = strValue.toFloat();
 
        *data=value;
 
        Serial.println("Succes!");
 
        success=true;
 
      }
 
  }
 
  Serial.println("closing connection");
 
  return success;
 
}
 
 
boolean senddata(float value)
 
{
 
  boolean success=false;
 
 
 
  Serial.print("connecting to ");
 
  Serial.println(host);
 
 
 
  // Use WiFiClient class to create TCP connections
 
  WiFiClient client;
 
  if (!client.connect(host, httpPort)) {
 
    Serial.println("connection failed");
 
    return false;
 
  }
 
 
 
  // We now create a URI for the request
 
  String url = "/json.htm?type=command&param=udevice&idx=";
 
  url += DOMOTICZ_IDX;
 
  url += "&svalue=";
 
  url += value;
 
 
  Serial.print("Requesting URL: ");
 
  Serial.println(url);
 
 
 
  // This will send the request to the server
 
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
 
              "Host: " + host + "\r\n" +
 
              "Connection: close\r\n\r\n");
 
 
  unsigned long timer=millis()+200;
 
  while(!client.available() && millis()<timer) {}
 
 
 
  // Read all the lines of the reply from server and print them to Serial
 
  while(client.available()){
 
    String line = client.readStringUntil('\n');
 
    if (line.substring(0,15) == "HTTP/1.0 200 OK")
 
      {
 
        Serial.println("Succes!");
 
        success=true;
 
      }
 
  }
 
  Serial.println("closing connection");
 
  return success;
 
}
 
 
void ISR()
 
{
 
  pulseCounter++;
 
}
 
</code>
 
 
== Arduino ESP Easy ==
 
Use the device tab on the ESP Easy webinterface and create a new task by editing one of the available tasks. Select "Pulse Counter" from the dropdown box and enter the IDX found in the Domoticz device page. Also select the GPIO pin that you have used to connect the pulse counting device. That should be all.
 
 
 
== Arduino ESP Connexio ==
 
todo
 

Latest revision as of 22:41, 12 May 2017