Esp8266 mcp3008


#1

I can get a reading by serial port on the device but know reading to this web site. The esp8266 is found but no sensor.
Code
int val = adc.readADC(0); // read Chanel 0 from MCP3008 ADC
Serial.println(“Sensor 1”);
Serial.println(val);
Cayenne.virtualWrite(0,adc.readADC(0));
Thanks for the help in advance


#2

Try changing Cayenne.virtualWrite(0,adc.readADC(0)); to just Cayenne.virtualWrite(0,val);

Also, can you post your whole code? Wondering if your using MQTT or not… Thanks!


#3

#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>
#include <MCP3008.h>
#include <SPI.h>
// WiFi network info.
char ssid[] = “xxxxxxxxx”;
char wifiPassword[] = “xxxxxxxxxxx”;

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = “46a91ff0-49fa-11e7-8d6e-f398b869a12a”;
char password[] = “e8bef5e8f00bb607e079a2500e28b183b0b2d774”;
char clientID[] = “34167860-bb38-11e7-b177-579293954599”;
char server[] = “mqtt.mydevices.com”;
char port[] = “1883”;
// define pin connections
#define CS_PIN 15
#define CLOCK_PIN 14
#define MOSI_PIN 13
#define MISO_PIN 12

// put pins inside MCP3008 constructor
MCP3008 adc(CLOCK_PIN, MOSI_PIN, MISO_PIN, CS_PIN);

void setup() {

// open serial port
Serial.begin(115200);
Cayenne.begin(username, password, clientID, ssid, wifiPassword);

}

void loop() {
Cayenne.loop();

  CAYENNE_IN(0)

{

int val = adc.readADC(0); // read Chanel 0 from MCP3008 ADC
Serial.println(“Sensor 1”);
Serial.println(val);
Cayenne.virtualWrite(0,val);
Serial.print("\t");

int val1 = adc.readADC(1); // read Chanel 1 from MCP3008 ADC
Serial.println(“Sensor 2”);
Serial.println(val);
Cayenne.virtualWrite(1,val);
Serial.print("\t");

int val2 = adc.readADC(2); // read Chanel 2 from MCP3008 ADC
Serial.println(“Sensor 3”);
Serial.println(val);
Cayenne.virtualWrite(2,val);
Serial.print("\t");
}


#4

Hi Raymond,

I think You don’t need CAYENNE_IN(0)
Try this
void loop()
{
Cayenne.loop();

int val = adc.readADC(0); // read Chanel 0 from MCP3008 ADC
Serial.println(“Sensor 1”);
Serial.println(val);
Cayenne.virtualWrite(0,val);
Serial.print("\t");

int val1 = adc.readADC(1); // read Chanel 1 from MCP3008 ADC
Serial.println(“Sensor 2”);
Serial.println(val1);
Cayenne.virtualWrite(1,val1);
Serial.print("\t");

int val2 = adc.readADC(2); // read Chanel 2 from MCP3008 ADC
Serial.println(“Sensor 3”);
Serial.println(val2);
Cayenne.virtualWrite(2,val2);
Serial.print("\t");
}
Salutations


#5

@self.raymond the code @Bernard gave is absolutely correct and should get you going. only a small change in code.

unsigned long lastMillis = 0;
void loop()
{
Cayenne.loop();
 if (millis() - lastMillis > 10000) {
    lastMillis = millis();
Cayenne.virtualWrite(0,val);
Cayenne.virtualWrite(1,val1);
Cayenne.virtualWrite(2,val2);
}
int val = adc.readADC(0); // read Chanel 0 from MCP3008 ADC
Serial.println(“Sensor 1”);
Serial.println(val);

int val1 = adc.readADC(1); // read Chanel 1 from MCP3008 ADC
Serial.println(“Sensor 2”);
Serial.println(val1);

int val2 = adc.readADC(2); // read Chanel 2 from MCP3008 ADC
Serial.println(“Sensor 3”);
Serial.println(val2);
}

#6

This work and thanks so much for the help


#7

How long of a delay needs to be put in the code for reading.


#8

You can use any delay you choose but if you send faster than every 2-5 seconds you will get rate limited by the Cayenne servers anyway.