Difference between revisions of "PulseCounter"

From Let's Control It
Jump to navigation Jump to search
(Blanked the page)
 
(12 intermediate revisions by 2 users not shown)
Line 1: Line 1:
[[File:TCR5000.jpg]]
 
  
= Introductie =
 
Een ESP8266 module kan worden gebruikt als een digitale pulsenteller om pulsen te verwerken van digitale sensoren. Denk b.v aan optische sensoren met een digitale uitgang. Te gebruiken voor o.a. gasmeter, watermeter, deur open/dicht tellers etc.
 
 
In dit voorbeeld sluiten we een optische sensor aan. Sluit de digitale uitgang aan op pin GPIO 2 van de ESP modules. Laad de juiste software en volg de specifieke instructies.
 
 
= Hardware =
 
Je hebt voldoende aan een ESP8266 module als de ESP-01. We gebruiker een TCR5000 optische sensor die goed verkrijgbaar is. Sluit pin 3 (DO) aan op pin GPIO 2van de ESP en voorzie zowel de ESP als de sensor van voedingsspanning.
 
 
= Software =
 
== NodeMCU script ==
 
todo
 
 
== Arduino Custom Sketch ==
 
In Domoticz maken we een speciaal type hardware aan, n.l. een "dummy". Vervolgens maken we hiermee een virtueel device. Noteer het IDX van het nieuwe device, dit moeten we invoeren in de config van de sketch.
 
 
Voor meer informatie over Domoticz virtuele hardware en devices, raadpleeg de 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 Connexio ==
 
Hier gebruiken we het pulse commando om de pulscount op te slaan in een variabele.
 
 
== Arduino ESP Easy ==
 
We stellen via de webinterface van de ESP, het IDX in van het counter device van Domoticz. Dat is alles.
 

Latest revision as of 22:41, 12 May 2017