CT Coil False Readings With Sinric

Hey,

I am trying to get a CT Sensor to see if there is Current in a light cable then it will update Sinric to On or Off depending on what the Current value is.

I have an issue with the CT output when it is built into the Sinric sketch, the CT value reads 104 whether the light is or or not. I have installed a test sketch

// EmonLibrary examples openenergymonitor.org, Licence GNU GPL V3

#include "EmonLib.h"                   // Include Emon Library
EnergyMonitor emon1;                   // Create an instance

void setup()
{  
  Serial.begin(115200);
  
  emon1.current(A0, 111.1);             // Current: input pin, calibration.
}

void loop()
{
  double Irms = emon1.calcIrms(1480);  // Calculate Irms only
  
  Serial.print(Irms*120.0);         // Apparent power
  Serial.print(" Watts   ");
  Serial.print(Irms);          // Irms
  Serial.println(" Amps_..");
}

Which works as it should, when the light is on it shows current and when it is off it shows no current.

If anyone could have a look at my code below and offer any suggestions it would be amazing as i am now running out of ideas.

/*
 Version 0.3 - March 06 2018
*/ 


#include "EmonLib.h"
EnergyMonitor emon1;
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <WebSocketsClient.h> //  https://github.com/kakopappa/sinric/wiki/How-to-add-dependency-libraries
#include <ArduinoJson.h> // https://github.com/kakopappa/sinric/wiki/How-to-add-dependency-libraries
#include <StreamString.h>

ESP8266WiFiMulti WiFiMulti;
WebSocketsClient webSocket;
WiFiClient client;
const int ledPin = D1;
#define MyApiKey "***" // TODO: Change to your sinric API Key. Your API Key is displayed on sinric.com dashboard
#define MySSID "***" // TODO: Change to your Wifi network SSID
#define MyWifiPassword "***" // TODO: Change to your Wifi network password
int lightstatus;
int ctreading;

//#define HEARTBEAT_INTERVAL 300000 // 5 Minutes 
#define HEARTBEAT_INTERVAL 60000 // 1 Minute

uint64_t heartbeatTimestamp = 0;
bool isConnected = false;

void setPowerStateOnServer(String deviceId, String value);
//void setTargetTemperatureOnServer(String deviceId, String value, String scale);

// deviceId is the ID assgined to your smart-home-device in sinric.com dashboard. Copy it from dashboard and paste it here

void turnOn(String deviceId) {
  
  if (deviceId == "***") 
  {
    if (lightstatus == 0)
    {
    digitalWrite(ledPin, !digitalRead(ledPin));
    Serial.print("Turn on device id: ");
    Serial.println(deviceId);
   // digitalWrite(ledPin, HIGH);
    setPowerStateOnServer("***", "ON");
    lightstatus = 1;
    }
    else
    {
      Serial.println("light Is Already On");
    }
   }
  else 
  {
    Serial.print("Turn on for unknown device id: ");
    Serial.println(deviceId);   
  }     
}

void turnOff(String deviceId) {
   if (deviceId == "***") // Device ID of first device
   {  
    if (lightstatus == 1)
    {
     digitalWrite(ledPin, !digitalRead(ledPin));
     Serial.print("Turn off Device ID: ");
     Serial.println(deviceId);
     //digitalWrite(ledPin, HIGH);
     setPowerStateOnServer("***", "OFF");
     lightstatus = 0;
    }
    else
    {
      Serial.println("light Is Already Off");
    }
   }
   
  else 
  {
     Serial.print("Turn off for unknown device id: ");
     Serial.println(deviceId);    
  }
}

void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) {
  switch(type) {
    case WStype_DISCONNECTED:
      isConnected = false;    
      Serial.printf("[WSc] Webservice disconnected from sinric.com!\n");
      break;
    case WStype_CONNECTED: {
      isConnected = true;
      Serial.printf("[WSc] Service connected to sinric.com at url: %s\n", payload);
      Serial.printf("Waiting for commands from sinric.com ...\n");        
 }
      break;
    case WStype_TEXT: {
            
        Serial.printf("[WSc] get text: %s\n", payload);
        // Example payloads

        // For Switch or Light device types
        // {"deviceId": xxxx, "action": "setPowerState", value: "ON"} // https://developer.amazon.com/docs/device-apis/alexa-powercontroller.html

        // For Light device type
        // Look at the light example in github
          
        DynamicJsonBuffer jsonBuffer;
        JsonObject& json = jsonBuffer.parseObject((char*)payload); 
        String deviceId = json ["deviceId"];     
        String action = json ["action"];
        
        if(action == "setPowerState" || action == "SetBrightness") { // Switch or Light
            String value = json ["value"];
            
            if ((value == "ON") && (value >= "50")) {
                turnOn(deviceId);
            } else {
                turnOff(deviceId);
            }
        }
        else if (action == "test") {
            Serial.println("[WSc] received test command from sinric.com");
        }

           //setPowerStateOnServer("***", "ON");
//////


 ///////
      }
      break;
    case WStype_BIN:
      Serial.printf("[WSc] get binary length: %u\n", length);
      break;
  }
}

void setup() {
  Serial.begin(115200);
  
  WiFiMulti.addAP(MySSID, MyWifiPassword);
  Serial.println();
  Serial.print("Connecting to Wifi: ");
  Serial.println(MySSID);  

  // Waiting for Wifi connect
  while(WiFiMulti.run() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  if(WiFiMulti.run() == WL_CONNECTED) {
    Serial.println("");
    Serial.print("WiFi connected. ");
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());
  }

  pinMode(ledPin, OUTPUT); // initialize digital ledPin as an output.
  delay(10);
  digitalWrite(ledPin, HIGH); // Wemos BUILTIN_LED is active Low, so high is off

  // server address, port and URL
  webSocket.begin("iot.sinric.com", 80, "/");

  // event handler
  webSocket.onEvent(webSocketEvent);
  webSocket.setAuthorization("apikey", MyApiKey);
  
  // try again every 5000ms if connection has failed
  webSocket.setReconnectInterval(5000);   // If you see 'class WebSocketsClient' has no member named 'setReconnectInterval' error update arduinoWebSockets
  
  
}
void loop() {
  webSocket.loop();
  
  if(isConnected) {
      uint64_t now = millis();
      
      // Send heartbeat in order to avoid disconnections during ISP resetting IPs over night. Thanks @MacSass
      if((now - heartbeatTimestamp) > HEARTBEAT_INTERVAL) {
          heartbeatTimestamp = now;
          webSocket.sendTXT("H"); 
            ////
            
  int volts = 1.0;
  double Irms = emon1.calcIrms(1480);  // Calculate Irms only
  
  if (Irms >= volts)
  {
    setPowerStateOnServer("***", "ON");
    Serial.print("Light Is On ");
    Serial.println(Irms);
    ctreading = (Irms);
    lightstatus = 1;
    Serial.print("CT Reading Is ");
    Serial.println(ctreading);
    Serial.print("Light Status Is ");
    Serial.println(lightstatus);
  }
  else
  {
    setPowerStateOnServer("***", "OFF");
    Serial.print("Light Is Off ");
    Serial.println(Irms);
    ctreading = (Irms);
    lightstatus = 0;
    Serial.print("CT Reading Is ");
    Serial.println(ctreading);
    Serial.print("Light Status Is ");
    Serial.println(lightstatus);
  }  
                 
      }
      
  }       
  
}

// If you are going to use a push button to on/off the switch manually, use this function to update the status on the server
// so it will reflect on Alexa app.

void setPowerStateOnServer(String deviceId, String value) {
  DynamicJsonBuffer jsonBuffer;
  JsonObject& root = jsonBuffer.createObject();
  root["deviceId"] = deviceId;
  root["action"] = "setPowerState";
  root["value"] = value;
  StreamString databuf;
  root.printTo(databuf);
  
  webSocket.sendTXT(databuf);
}

Suggestion! Show a link to the sensor data sheet or technical info.

Paul

You've not initialized emon1. You need to call emon1.current() - take the current only example and start there.