Help with my sketch

Hi All I am senor Noob at coding Arduino

Need some help with my sketch on my mega based PLduino. I used this sketch on a mega with ethernet shield and it loaded just fine and ran on the cayenne dashboard very well, better than my Rpi3. Now I have commented out the ethernet library and included the wifi library available in blynk. The mfr states the wifi is an esp8266 but actually just an esp-02 and that’s just one subject to deal with but now I get ‘cayenne’ was not declared in this scope. I did change 2 other things concerning cayenne in and out by adding brackets which left me with this mess.

here’s my sketch,

#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiServer.h>
#include <WiFiUdp.h>



/*
Cayenne Ethernet Example

This sketch connects to the Cayenne server using an Arduino Ethernet Shield W5100
and runs the main communication loop.

The Cayenne 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. Set the token variable to match the Arduino token from the Dashboard.
2. Compile and upload this sketch.

For Cayenne Dashboard widgets using digital or analog pins this sketch will automatically
send data on those pins to the Cayenne server. If the widgets use Virtual Pins, data
should be sent to those pins using virtualWrites. Examples for sending and receiving
Virtual Pin data are under the Basics folder.
*/

//#define CAYENNE_DEBUG         // Uncomment to show debug messages
#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
// #include <CayenneEthernet.h>
#define VIRTUAL_PIN 1
#define RELAY_DIGITAL_PIN 4
#include <CayenneTMP102.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_TSL2561_U.h>
#include <Wire.h>
#define VIRTUAL_PIN V2

// Your network name and password.
char ssid[] = "free";
char password[] = "xxxxxxx";

// Address used to read from the TMP102. This is determined by the ADD0 pin on the TMP102.
// Connecting it to ground means the sensor will use 0x48 for the address.  See the TMP102 datasheet for more info.
const int tmp102Address = 0x48;

// Address used to read from the TSL2561. This is determined by the ADDR pin on the TSL2561.
// If ADDR is unconnected it means the sensor will use TSL2561_ADDR_FLOAT (0x39) for the address.  See the TSL2561 datasheet for more info.
const int address = TSL2561_ADDR_FLOAT;

// TEMPURATURE
TMP102 tmpSensor(tmp102Address);
//LUX
Adafruit_TSL2561_Unified tsl = Adafruit_TSL2561_Unified(address, 12345);

// Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
char token[] = "r2aeszx58k";


void setup()
{
   // RELAYS
   // set digital pin to output
  pinMode(RELAY_DIGITAL_PIN, OUTPUT);
  
  Serial.begin(9600);
  Wire.begin();
{
  Cayenne.begin(token);   //HERE'S THE RUB//
}                                                                                                                                                                    
  // TSL
  if (!tsl.begin())
  {
    CAYENNE_LOG("No TSL2561 detected");
  }

  tsl.enableAutoRange(true);
  /* Changing the integration time gives you better sensor resolution (402ms = 16-bit data) */
  // tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_13MS);      /* fast but low resolution */
  tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_101MS);  /* medium resolution and speed   */
  // tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_402MS);  /* 16-bit data but slowest conversions */

}

{
CAYENNE_IN(VIRTUAL_PIN)
}
// Button Section
{
  // get value sent from dashboard
  int currentValue = getValue.asInt(); // 0 to 1

  // assuming you wire your relay as normally open
  if (currentValue == 0) {
    digitalWrite(RELAY_DIGITAL_PIN, HIGH);
  } else {
    digitalWrite(RELAY_DIGITAL_PIN, LOW);
  }
}









void loop()
{
 Cayenne.run();
 
} 
{
// This function is called when the Cayenne widget requests data for the Virtual Pin.
CAYENNE_OUT(VIRTUAL_PIN)
}
// Tempurature
{
 // This command writes the temperature in Celsius to the Virtual Pin.
  //Cayenne.celsiusWrite(VIRTUAL_PIN, tmpSensor.getCelsius());
  // To send the temperature in Fahrenheit or Kelvin use the corresponding code below.
  Cayenne.fahrenheitWrite(VIRTUAL_PIN, tmpSensor.getFahrenheit());
  //Cayenne.kelvinWrite(VIRTUAL_PIN, tmpSensor.getKelvin());

{
  // Send the command to get luminosity.
  sensors_event_t event;
  tsl.getEvent(&event);

  if (event.light)
  {
    // Send the value to Cayenne in lux.
    Cayenne.luxWrite(VIRTUAL_PIN, event.light);
  }
  else
  {
    /* If event.light = 0 lux the sensor is probably saturated
    and no reliable data could be generated! */
    CAYENNE_LOG("No sensor data");
  }
}
}

my goodness…
what was the question?
An Esp-2 is the same family as Esp8266. If you peel the can off the Esp-2 chip, you’ll see a teeny Esp8266 inside. All the Esp family (Esp1, Esp2, Esp7, Esp12, Esp32 are all based on the Esp8266 chip. :wink:
As far as I know.

The different Esp chips are just different GPIO configurations available for the Esp8266, and perhaps integrated bluetooth radios.

I get my kicks with the Esp12-e because it has 4 usable GPIO pins, an analog-in pin, and an SPI bus.

Plus- if the chips gets any smaller (Esp32!), I wouldn’t be able to see and solder it.

The last I heard, the ESP family was not yet supported, so…
I’ll help where I can.
I’m still trying to make my MCP3208 (A/D converter) work with my Esp12-e and Cayenne.

1 Like

So I slapped your code into my IDE to see if I could make it compile. I think I have it there. There is just a warning about re-defining VIRTUAL_PIN that you may want to resolve, since when it’s defined a 2nd time its going to overwrite whatever you where up to the 1st time.

You can compare this to above to see everything that I changed, I’m struggling with the whole list but off the top of my head:

  • cleaned up some brackets around your CAYENNE_IN statements that were confusing the compiler
  • changed some of the #include statements at the top of the file
  • Cayenne.begin() was missing ssid and password parameters, so I added those

Not 100% if this is going to do exactly what you’re looking for without further tweaking, but it at least compiles now!

#include "CayenneWiFi.h"
#include "CayenneWiFiClient.h"
#include "WiFiServer.h"
#include "WiFiUdp.h"



/*
Cayenne Ethernet Example

This sketch connects to the Cayenne server using an Arduino Ethernet Shield W5100
and runs the main communication loop.

The Cayenne 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. Set the token variable to match the Arduino token from the Dashboard.
2. Compile and upload this sketch.

For Cayenne Dashboard widgets using digital or analog pins this sketch will automatically
send data on those pins to the Cayenne server. If the widgets use Virtual Pins, data
should be sent to those pins using virtualWrites. Examples for sending and receiving
Virtual Pin data are under the Basics folder.
*/

//#define CAYENNE_DEBUG         // Uncomment to show debug messages
#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
// #include <CayenneEthernet.h>
#define VIRTUAL_PIN 1
#define RELAY_DIGITAL_PIN 4
#include <CayenneTMP102.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_TSL2561_U.h>
#include <Wire.h>
#define VIRTUAL_PIN V2

// Your network name and password.
char ssid[] = "free";
char password[] = "xxxxxxx";

// Address used to read from the TMP102. This is determined by the ADD0 pin on the TMP102.
// Connecting it to ground means the sensor will use 0x48 for the address.  See the TMP102 datasheet for more info.
const int tmp102Address = 0x48;

// Address used to read from the TSL2561. This is determined by the ADDR pin on the TSL2561.
// If ADDR is unconnected it means the sensor will use TSL2561_ADDR_FLOAT (0x39) for the address.  See the TSL2561 datasheet for more info.
const int address = TSL2561_ADDR_FLOAT;

// TEMPURATURE
TMP102 tmpSensor(tmp102Address);
//LUX
Adafruit_TSL2561_Unified tsl = Adafruit_TSL2561_Unified(address, 12345);

// Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
char token[] = "r2aeszx58k";


void setup()
{
   // RELAYS
   // set digital pin to output
  pinMode(RELAY_DIGITAL_PIN, OUTPUT);
  
  Serial.begin(9600);
  Wire.begin();
  
{

  Cayenne.begin(token, ssid, password);   //HERE'S THE RUB//

}                                                                                                                                                                    
  // TSL
  if (!tsl.begin())
  {
    CAYENNE_LOG("No TSL2561 detected");
  }

  tsl.enableAutoRange(true);
  /* Changing the integration time gives you better sensor resolution (402ms = 16-bit data) */
  // tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_13MS);      /* fast but low resolution */
  tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_101MS);  /* medium resolution and speed   */
  // tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_402MS);  /* 16-bit data but slowest conversions */

}


CAYENNE_IN(VIRTUAL_PIN)


// Button Section
{
  // get value sent from dashboard
  int currentValue = getValue.asInt(); // 0 to 1

  // assuming you wire your relay as normally open
  if (currentValue == 0) {
    digitalWrite(RELAY_DIGITAL_PIN, HIGH);
  } else {
    digitalWrite(RELAY_DIGITAL_PIN, LOW);
  }
}









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

// This function is called when the Cayenne widget requests data for the Virtual Pin.
CAYENNE_OUT(VIRTUAL_PIN)

// Tempurature
{
 // This command writes the temperature in Celsius to the Virtual Pin.
  //Cayenne.celsiusWrite(VIRTUAL_PIN, tmpSensor.getCelsius());
  // To send the temperature in Fahrenheit or Kelvin use the corresponding code below.
  Cayenne.fahrenheitWrite(VIRTUAL_PIN, tmpSensor.getFahrenheit());
  //Cayenne.kelvinWrite(VIRTUAL_PIN, tmpSensor.getKelvin());

{
  // Send the command to get luminosity.
  sensors_event_t event;
  tsl.getEvent(&event);

  if (event.light)
  {
    // Send the value to Cayenne in lux.
    Cayenne.luxWrite(VIRTUAL_PIN, event.light);
  }
  else
  {
    /* If event.light = 0 lux the sensor is probably saturated
    and no reliable data could be generated! */
    CAYENNE_LOG("No sensor data");
  }
}
}
1 Like

THAT was useful! :slight_smile:

1 Like

@rsiegel gotta sketch here that I have implemented the built in lcd to give an error if I assume it cannot connect. I unplugged my other mega from the internet and used the same token but no luck. Did a search for a new board adding the new token to the sketch and uploaded but it’s not finding the board. The lcd shows esp02 error 3 times then goes to a white screen.

Is there a way to get the ip address from this board so I can check the dhcp client list to see if it’s connected?

for whatever reason I cannot copy paste my sketch here, some items are missing. I’ll try github repo and give you the link

I would try building the connectivity part of your sketch around the ESP8266 Cayenne connection example posted in this thread

As far as checking the IP on this board, I’m not sure. @kreggly do you think this example sketch from the Arduino site will behave on ESP? I’m not sure what its doing when it looks for the presence of a shield.

Gentlemen,

The PLDuino uses a Mega with an ESP attached to the second hardware serial port.

We can’t use the Wifi library as it is not compatible, you need to use the ESP Shield sketch.

The above sketch is already set to use the second serial port, so assuming the ESP already has the AT command set programmed, I think it may work.

Give it a shot, and let us know.

Cheers,

Craig

2 Likes

For interracting with the ESP, you can send AT commands to Serial1, and read the response.

Cheers,

Craig

sorry to pester so much but that sketch, second one, gives me an error.

Arduino: 1.8.1 (Windows 10), Board: “Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)”

In file included from C:\Users\Pancho\AppData\Local\Temp\arduino_modified_sketch_323758\esp8266.ino:5:0:

C:\Users\Pancho\Documents\Arduino\libraries\Cayenne/BlynkSimpleEsp8266.h:15:2: error: #error This code is intended to run on the ESP8266 platform! Please check your Tools->Board setting.

#error This code is intended to run on the ESP8266 platform! Please check your Tools->Board setting.

^

In file included from C:\Users\Pancho\AppData\Local\Temp\arduino_modified_sketch_323758\esp8266.ino:5:0:

C:\Users\Pancho\Documents\Arduino\libraries\Cayenne/BlynkSimpleEsp8266.h:25:25: fatal error: ESP8266WiFi.h: No such file or directory

#include <ESP8266WiFi.h>

                     ^

compilation terminated.

exit status 1
Error compiling for board Arduino/Genuino Mega or Mega 2560.

This report would have more information with
“Show verbose output during compilation”
option enabled in File → Preferences.

Got this sketch to connect to router finally, at least it shows up in the DHCP client list. But won’t connect to mydevices:(

William

What do you see in the serial monitor when attempting to connect to Cayenne?

It shouldn’t work at all. Follow the instructions in the post I linked above. I’m hoping that will get you connected to Cayenne, then you can add your bits.

Cheers,

Craig

Using the above sketch and following an older post I get the gist better.

So I moved some library files around because the sketch compiler couldn’t find (another lesson learned) The serial monitor shows (519) connecting to free (1530) failed to disable echo (5001( state 0, tconn 0 (20021) failure to connect.

I don’t think I have a good connection to the esp because AT commands don’t receive a response.

william

All I can think of is that the ESP does not have a firmware compatible with the ESP library and will need to be reprogrammed.

I’m reading up on that now-

-why did you choose that firmware?

It comes with that one I suppose from the factory.

This is the PLC “PLduino” I’m using: If I get connected to Cayenne then the rest should fall into place with a little help from the community.

Open Source PLC! - 15 Day Free Trial! - Ships Overnight Hardware desription

http://www.digital-loggers.com/plcschematics.pdf Schematics

GitHub - digitalloggers/PLDuino: PLDuino controller's repository for code, docs and other related stuff. This one I can’t make heads or tails of just yet but I am pushing forward.

William

I don’t have one, so I’m only guessing.

Maybe try different baud rates and see if it responds. I am assuming it has NodeMCU in it.

Craig

It does use NodeMCU per their github repo. I’m going to have to study more intensely :slight_smile:

I’m pretty sure that they are using the ESP only as a WiFi-to-serial device (like a hat) to establish network communication with a WiFi network. If this is true, then you should be able to talk to the ESP via it’s own serial com Tx and Rx. You wouldn’t be able to use the PLduino RS232 via WiFi until the ESP makes the handshake.

@wmontg5988,

See what happens when you try issuing AT commands with this sketch:

char serialBuf[128];

void setup()
{
  Serial.begin(9600);
  Serial1.begin(115200);
  

  Serial.println("Ready for commands:");      
   }

void loop()
{
  Serial.readBytesUntil('\r',serialBuf,128); //gets command from console
  Serial.println(serialBuf); //echo back to console (may not be needed)
  Serial1.println(serialBuf); //send command to ESP
  
  delay(500); //let it process
  
  Serial1.readBytesUntil('\r',serialBuf,128); //gets readback from ESP
  Serial.println(serialBuf); //dump readback to console
  

}

Cheers,

Craig