Hello and thank’s for this help. Sorry i had so many changes in this code that it took me some time (and a broken internet access…). Thank’s for your patience.
The aim: each device have 4 sensors, and should send it’s data (int) to the other. As @bko wrote it, I’m not currently using a JSON structure but would be ok to do it, but i’m currently not able to translate the data (one char) from the cloud in multiple integer.
I’m using also particle.publish() to debug, here is what I currently get (after your corrections @ScruffR):
lehio_3 - 0,60,71,94 // as expected
debug1 - 0,60,71,94 // as expected
debug2 - 536871772,536871760,536871748,536871780 // UNEXPECTED
Here is the full & corrected code:
int led = D0;
int boardLed = D7;
int lightsensor = A0;
int soundsensor = A1;
int movesensor = A2;
int pushsensor = A3;
int power = A5;
int sensorValue = 0;
int needUpdate = 1;
int lightValue = 0;
int soundValue = 0;
int moveValue = 0;
int pushValue = 0;
int buddyLightValue = 0;
int buddySoundValue = 0;
int buddyMoveValue = 0;
int buddyPushValue = 0;
char message[40];
char receivedMessage[40];
char myName[12] = "lehio_3";
char buddyName[12] = "lehio_3";
// SETUP
void setup() {
pinMode(led,OUTPUT); // Our LED pin is output (lighting up the LED)
pinMode(boardLed,OUTPUT); // Our on-board LED is output as well
pinMode(power,OUTPUT); // Our on-board LED is output as well
// Register to buddy
Particle.subscribe(buddyName, myHandler);
analogWrite(power,255); // max 4095
}
void loop() {
delay(5000); // 1000 = 1s - So scan every 5 sec
// light data analysis
sensorValue = analogRead(lightsensor)/16; // from 0-255
analogWrite(led,sensorValue);
if (sensorValue != lightValue) {
needUpdate = 1;
lightValue = sensorValue;
}
// sound data analysis
sensorValue = analogRead(soundsensor)/16; // from 0-255
if (sensorValue != soundValue) {
needUpdate = 1;
soundValue = sensorValue;
}
// move data analysis
sensorValue = analogRead(movesensor)/16; // from 0-255
if (sensorValue != moveValue) {
needUpdate = 1;
moveValue = sensorValue;
}
// push data analysis
sensorValue = analogRead(pushsensor)/16; // from 0-255
if (sensorValue != pushValue) {
needUpdate = 1;
pushValue = sensorValue;
}
// send datas only if required
if (needUpdate == 1) {
sprintf(message,"%d,%d,%d,%d",lightValue,soundValue,moveValue,pushValue);
Particle.publish(myName,message); // sendind data to the cloud eg:
needUpdate = 0;
}
}
void myHandler(const char *event, const char *data)
{
Particle.publish("debug1",data);
sscanf(data,"%d,%d,%d,%d",&buddyLightValue,&buddySoundValue,&buddyMoveValue,&buddyPushValue);
sprintf(message,"%d,%d,%d,%d",&buddyLightValue,&buddySoundValue,&buddyMoveValue,&buddyPushValue); // just to verify the data
Particle.publish("debug2",message);
}
Thank’s for this help!