// This example shows how to connect to Cayenne using an ESP8266 and send/receive sample data.
// Make sure you install the ESP8266 Board Package via the Arduino IDE Board Manager and select the correct ESP8266 board before compiling.
#define CAYENNE_PRINT Serial #define CAYENNE_DEBUG
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username = “–”;
char password = “verystrong”;
char clientID = “hackersnotwelcome”;
#define SENSOR_PIN 16 // Do not use digital pins 0 or 1 since those conflict with the use of Serial. #define VIRTUAL_CHANNEL 9
int previousState = -1;
int currentState = -1;
unsigned long previousMillis = 0;
void checkSensor()
{
unsigned long currentMillis = millis();
// Check sensor data every 250 milliseconds
if (currentMillis - previousMillis >= 1000) {
// Check the sensor state and send data when it changes.
currentState = digitalRead(SENSOR_PIN);
if (currentState != previousState) {
Cayenne.virtualWrite(VIRTUAL_CHANNEL, currentState, “digital_sensor”, “d”);
previousState = currentState;
}
previousMillis = currentMillis;
}
}
Looks fine to me. That garbage is from the ESP booting. If you set the baud to 115200 you should see some actual info. No clue why it’s not printing debug messages though. Does it show up as online on the dashboard?
The temporary widget appears in the cayenne dashboard, and it gives out 0/1 values, but not correct ones. When I put the widget with a icon, it literally “blinks”, but it should be 1 when the motion sensor detects movement and 0 when it does not detect movement.
The motion sensor is working, because when I connect it directly to raspberry GPIOs instead of the ESP8266, it works perfectly in the dashboard.
/*
This example shows how to connect to Cayenne using an ESP8266 and send/receive sample data.
The CayenneMQTT Library is required to run this sketch. If you have not already done so you can install it from the Arduino IDE Library Manager.
Steps:
1. If you have not already installed the ESP8266 Board Package install it using the instructions here: https://github.com/esp8266/Arduino#installing-with-boards-manager.
2. Select your ESP8266 board from the Tools menu.
3. Set the Cayenne authentication info to match the authentication info from the Dashboard.
4. Set the network name and password.
5. Compile and upload the sketch.
6. A temporary widget will be automatically generated in the Cayenne Dashboard. To make the widget permanent click the plus sign on the widget.
*/
#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>
// WiFi network info.
char ssid[] = "ssid";
char wifiPassword[] = "wifiPassword";
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "MQTT_USERNAME";
char password[] = "MQTT_PASSWORD";
char clientID[] = "CLIENT_ID";
void setup() {
Serial.begin(9600);
Cayenne.begin(username, password, clientID, ssid, wifiPassword);
}
void loop() {
Cayenne.loop();
}
// Default function for sending sensor data at intervals to Cayenne.
// You can also use functions for specific channels, e.g CAYENNE_OUT(1) for sending channel 1 data.
CAYENNE_OUT_DEFAULT()
{
// Write data to Cayenne here. This example just sends the current uptime in milliseconds on virtual channel 0.
Cayenne.virtualWrite(0, millis());
// Some examples of other functions you can use to send data.
//Cayenne.celsiusWrite(1, 22.0);
//Cayenne.luxWrite(2, 700);
//Cayenne.virtualWrite(3, 50, TYPE_PROXIMITY, UNIT_CENTIMETER);
}
// Default function for processing actuator commands from the Cayenne Dashboard.
// You can also use functions for specific channels, e.g CAYENNE_IN(1) for channel 1 commands.
CAYENNE_IN_DEFAULT()
{
CAYENNE_LOG("Channel %u, value %s", request.channel, getValue.asString());
//Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError("Error message");
}
The problem was that I did not connect the ground pin of my ESP8266 to the ground pin of the raspberry pi (that was feeding/powering the motion sensor module).
I had to shunt the ground pin of the raspberry pi with the ground pin of the ESP8266, now I’m receiving correct data on the cayenne dashboard and correspondent widget.