Esp8266 and luminocity sensor

#1

Hello there!I m trying to make a project with the use of cayenne,ifttt,esp and some other tools so when after 8 o clock in the noon i ll be at home and the home is dark,my lights will be turned on.
Everything is working properly EXCEPT (yes,you are guessing it correctly) ESP and cayenne luminosity connection.
I m using the following code and it makes no impact.maybe I m doing something wrong?

    #define CAYENNE_PRINT Serial   // Comment this out to disable prints and save space
#include <CayenneMQTTESP8266.h>
// WiFi network info.
char ssid[] = "";
char wifiPassword[] = "";

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = ";
char password[] = "";
char clientID[] = "";

#define SENSOR_PIN 2
#define VIRTUAL_CHANNEL 4

void setup()
{
  Serial.begin(9600);
  Cayenne.begin(username, password, clientID);
}

void loop()
{
  Cayenne.loop();
}

// This function is called at intervals to send sensor data to Cayenne.
CAYENNE_OUT(VIRTUAL_CHANNEL)
{
  Cayenne.virtualWrite(VIRTUAL_CHANNEL, analogRead(SENSOR_PIN), "analog_sensor", "null");
}

I just want to use ESP because if I use arduino I have to put it near my modem and it will also read my screens light so I ll have wrong data.Any help would be much appreciated.

0 Likes

#2

need more info on which esp8266 device and the luminosity sensor you are using. mistake which i see in your code is you are reading analog value from a digital pin 2.

0 Likes

#3

hello again!I m using esp8266 201.Also I m using this photoresistor.I may have other mistakes too,but I m not too familiar with ESP or Cayenne.and the fact is that it only lets me choose virtual pin (which I dont understand)

0 Likes

#4

if you are using esp8266 ESP_201, the ADC pin is 17. so change #define SENSOR_PIN 2 to #define SENSOR_PIN 17 .
For the photoresistor, a voltage divider is compulsory.

So virtual pin in cayenne is the MQTT channel number. it is used for transfer of data between cayenne and the device.

0 Likes

#5

for testing purposes:this code (an example from cayenne)
should work with arduino uno,right?
it doesn’t though.

#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
#include <CayenneMQTTEthernet.h>

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "";
char password[] = "";
char clientID[] = "";

#define SENSOR_PIN 0
#define VIRTUAL_CHANNEL 0

void setup()
{
  Serial.begin(9600);
  Cayenne.begin(username, password, clientID);
}

void loop()
{
  Cayenne.loop();
}

// This function is called at intervals to send sensor data to Cayenne.
CAYENNE_OUT(VIRTUAL_CHANNEL)
{
  Cayenne.virtualWrite(VIRTUAL_CHANNEL, analogRead(SENSOR_PIN), "analog_sensor", "null");
}

probably I m making another mistake there.
after I get back home I ll test the previous example with the esp8266 201 so if it will work everything will be ok!

0 Likes

#6

this code looks correct. can you add #define CAYENNE_DEBUG in your code and check the issue in the serial monitor.

0 Likes

#7

ok,i’ve found a mistake although I can’t find how to correct it.The value is read correctly.BUT in order to display it I have to reload the caynenne page each time.Could I somehow just auto renew the value?

0 Likes

#8

because I ve spend all my evening and I m surely doing something wrong.Maybe there are some complete instructions on how to install this sensor?it works now.and it renews.BUT I cannt make any webhook triggers because somehow it doesn’t recognize that it is an analog luminosity sensor.I can provide you any info you want (code I m running is the one I already told you before).Maybe we could figure something out?Really thanks in advance!!

0 Likes

#9

first check if your trigger counter is incrementing or not.

Also if you are using cayenne for trigger notification you need to use the code from here Sending MQTT messages within notification limit

0 Likes

#10

ok.and thanks for your help!the problem with triggers is solved!!
To give you an idea I ll tell you the full idea and the schematics so you may have a bright idea about my last consern.
so,I have arduino or ESP to measure light.It sends a trigger to Apilio service (a boolean true or false in case it’s dark or not).
Another boolean on Apilio gets trigered by time (time is after 4.00 and before 10.00) from ifttt.
last boolean is triggered again by Apilio and ifttt.If my mobile device is connected to my wifi network (so I m home).A simple AND between those three sends a command to ifttt to turn on my lights.the thing is that even i turn them off,if it gets dark and the time is within the time given they get back on after 30 seconds.I am wondering if there is a smarter way to do that.let’s say if I am at the living room (another app needed probably) or any,ANY idea you may have.smarter time app suppossed to understand sleeping time or room etc…but i have no idea…

0 Likes

#11

you need to figure out which is the trigger in the AND statement that is making the light turn ON. is it cayenne or IFTTT. well from my guess it must be your cayenne code which makes the statement true again and again which sends a webhook trigger every time.
First of all, you need to use the code I shared above to avoid trigger flood.
now, the next step is to make the code to not send data once it has triggered. One thing to do is manually turn the function ON/OFF using a button on the cayenne APP, while then it won’t be much of a smart thing.
another option use the cayenne scheduler to control the data send between 4 to 10.

0 Likes