Hi all,
My arduino keeps randomly disconnecting from the dash board (chrome) and then comes back… Its intermittent.
Its a Wimos D2. Sketch below, but basically it polls from serial and sends the information to a virtual pin.
I put in a delay into the main loop of 5000ms to see if that helped, and it actually made it worse.
Any help would be appreciated.
#include <ModbusMaster.h>
#include <ESP8266WiFi.h>
#include “CayenneDefines.h”
#include “BlynkSimpleEsp8266.h”
#include “CayenneWiFiClient.h”
const int debug = 1; //change to 0 when you are finished debugging
float bvoltage, ctemp, btemp, bremaining, lpower, lcurrent, pvvoltage, pvcurrent, pvpower;
// instantiate ModbusMaster object
ModbusMaster node;
// put something here if you want it to occur before or after transmission to the serial interface…i.e. delay etc. I have these blank and it works fine
void preTransmission()
{
}
void postTransmission()
{
}
// Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
char token = “ubyb8ppfx0”;
char ssid = “Swamp”; // your network SSID (name)
char pass = “martyman123”; // your network password
// Virtual Pin of the widget.
#define VIRTUAL_PIN V1
#define VIRTUAL_PIN2 V2
#define VIRTUAL_PIN3 V3
#define VIRTUAL_PIN4 V4
#define VIRTUAL_PIN5 V5
#define VIRTUAL_PIN6 V6
#define VIRTUAL_PIN7 V7
#define VIRTUAL_PIN8 V8
#define VIRTUAL_PIN9 V9
void setup()
{
// Modbus communication runs at 115200 baud → you can make this slower if you want, but works fine either way. I would suggest unplugging the RS485 from the ESP when you are uploading the code. Plug it back in as soon as the code successfully uploads
Serial.begin(115200);
// Modbus slave ID 1 → this is the default for the 2210, so shouldn’t need to change
node.begin(1, Serial);
// Callbacks allow us to configure the RS485 transceiver correctly
node.preTransmission(preTransmission);
node.postTransmission(postTransmission);
Cayenne.begin(token, ssid, pass);
}
bool rs485DataReceived = true;
void loop()
{
delay(5000);
uint8_t result,time1, time2, time3, date1, date2, date3, dateDay, dateMonth, dateYear, timeHour, timeMinute, timeSecond;
// uint16_t data[6];
char buf[10];
String dtString;
if (debug == 1){
Serial.print("Beginning Loop ");
}
//Get Date and Time, and update Controller Data and Time
delay(500);
// Read 20 registers starting at 0x3100)
result = node.readInputRegisters(0x3100, 20);
if (result == node.ku8MBSuccess)
{
if (debug == 1){
Serial.println(“------------------------------------------------------------------”);
Serial.print("Controller Temperature: ");
}
ctemp = node.getResponseBuffer(0x11)/100.0f;
if (debug == 1){
Serial.println(ctemp);
Serial.print("Battery Voltage: ");
}
bvoltage = node.getResponseBuffer(0x04)/100.0f;
if (debug == 1){
Serial.println(bvoltage);
Serial.print("Load Power: ");
}
lpower = ((long)node.getResponseBuffer(0x0F)<<16|node.getResponseBuffer(0x0E))/100.0f;
if (debug == 1){
Serial.println(lpower);
Serial.print("Load Current: ");
}
lcurrent = (long)node.getResponseBuffer(0x0D)/100.0f;
if (debug == 1){
Serial.println(lcurrent);
Serial.print("PV Voltage: ");
}
pvvoltage = (long)node.getResponseBuffer(0x00)/100.0f;
if (debug == 1){
Serial.println(pvvoltage);
Serial.print("PV Current: ");
}
pvcurrent = (long)node.getResponseBuffer(0x01)/100.0f;
if (debug == 1){
Serial.println(pvcurrent);
Serial.print(“PV Power: “);
}
pvpower = ((long)node.getResponseBuffer(0x03)<<16|node.getResponseBuffer(0x02))/100.0f;
if (debug == 1){
Serial.println(pvpower);
Serial.println(”------------------------------------------------------------------”);
delay(500);
}
}else{
rs485DataReceived = false;
}
delay(500);
result = node.readInputRegisters(0x311A, 2);
if (result == node.ku8MBSuccess)
{
if (debug == 1){
Serial.println(“------------------------------------------------------------------”);
Serial.print("Battery Remaining %: ");
}
bremaining = node.getResponseBuffer(0x00)/1.0f;
if (debug == 1){
Serial.println(bremaining);
Serial.print(“Battery Temperature: “);
}
btemp = node.getResponseBuffer(0x01)/100.0f;
if (debug == 1){
Serial.println(btemp);
Serial.println(”------------------------------------------------------------------”);
delay(500);
}
}else{
rs485DataReceived = false;
}
Cayenne.run();
}
CAYENNE_OUT(VIRTUAL_PIN)
{
// Read data from the sensor and send it to the virtual channel here.
// You can write data using virtualWrite or other Cayenne write functions.
// For example, to send a temperature in Celsius you can use the following:
//unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
//Cayenne.virtualWrite(VIRTUAL_PIN,(uS / US_ROUNDTRIP_CM/2));
/* The following trigPin/echoPin cycle is used to determine the
distance of the nearest object by bouncing soundwaves off of it. */
float result = (100);
Cayenne.virtualWrite(VIRTUAL_PIN,bvoltage);
Cayenne.virtualWrite(VIRTUAL_PIN2,ctemp);
Cayenne.virtualWrite(VIRTUAL_PIN3,btemp);
Cayenne.virtualWrite(VIRTUAL_PIN4,bremaining);
Cayenne.virtualWrite(VIRTUAL_PIN5,lpower);
Cayenne.virtualWrite(VIRTUAL_PIN6,lcurrent);
Cayenne.virtualWrite(VIRTUAL_PIN7,pvvoltage);
Cayenne.virtualWrite(VIRTUAL_PIN8,pvcurrent);
Cayenne.virtualWrite(VIRTUAL_PIN9,pvpower);
}