Hi Makers,
I hope you are doing well in your projects,
I think i’m mistaking something here with my code when i try to send some variables to the cloud
Would please help me about that,it is neither displaying the address in the console or publishing it as a variable.
Here’s my entire code :
// This #include statement was automatically added by the Particle IDE.
#include <OneWire.h>
#include <Particle.h>
void updateHexData(byte *data, size_t dataLength);
String sensorData;
String sensorAdd1;
String sensorAdd2;
String sensorAdd3;
String sensorAdd4;
OneWire wire = OneWire(D1); // 1-wire signal on pin D4
unsigned long lastUpdate = 0;
void setup() {
Serial.begin(9600);
}
// Every 3 seconwire check for the next address on the bus
// The scan resets when no more addresses are available
void loop() {
int counter=0;
unsigned long now = millis();
// change the 3000(ms) to change the operation frequency
// better yet, make it a variable!
if ((now - lastUpdate) < 3000) {
return;
}
lastUpdate = now;
byte addr[8];
if (!wire.search(addr)) {
Serial.println("No more addresses.");
Serial.println();
wire.reset_search();
//delay(250);
return;
}
counter++;
// Show the CRC status
// you should always do this on scanned addresses
if (OneWire::crc8(addr, 7) != addr[7]) {
Serial.println("CRC is not valid!");
return;
}
if(counter==1)
{
updateHexData(addr, sizeof(addr));
sensorAdd1=sensorData;
Particle.variable("FirstDevAdd", &sensorAdd1, STRING);
Particle.publish("AddressesDS", "NewAddrDetected", 60, PUBLIC);
}
else if(counter==2)
{
updateHexData(addr, sizeof(addr));
sensorAdd2=sensorData;
Particle.variable("SecondDevAdd", &sensorAdd2, STRING);
Particle.publish("AddressesDS", "NewAddrDetected", 60, PUBLIC);
}
else if(counter==3)
{
updateHexData(addr, sizeof(addr));
sensorAdd3=sensorData;
Particle.variable("ThirdDevAdd", &sensorAdd3, STRING);
Particle.publish("AddressesDS", "NewAddrDetected", 60, PUBLIC);
}
else if(counter==4)
{
updateHexData(addr, sizeof(addr));
sensorAdd4=sensorData;
Particle.variable("ForthDevAdd", &sensorAdd4, STRING);
Particle.publish("AddressesDS", "NewAddrDetected", 60, PUBLIC);
}
else
{Serial.println("There's another addresses.");}// You can add them though if statements
wire.reset(); // clear bus for next use
}
void updateHexData(byte *data, size_t dataLength) {
// Reserve buffer space so we only need to do one memory allocation when appending to sensorData
// Note that Particle.variable has a limit of 622 bytes, so the maximum dataLength is 310 or so bytes.
sensorData.reserve(dataLength * 2 + 1);
// This is where we store the one byte of hex data (two bytes of ASCII 0-9 and a-f).
char hexBuf[4];
// Convert the input data to hex
for(size_t ii = 0; ii < dataLength; ii++) {
snprintf(hexBuf, sizeof(hexBuf), "%02x", data[ii]);
if (ii == 0) {
sensorData = hexBuf;
}
else {
sensorData += hexBuf;
}
}
}