Last active
May 12, 2018 05:20
-
-
Save IOT-123/ba9ae39fbb7dd71c637d4e677b2f8301 to your computer and use it in GitHub Desktop.
I2C Master logging from I2C slave with plotter support.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Wire.h> | |
#define ADDRESS_SLAVE 10 | |
bool _outputPlotterOnly = false; | |
bool _confirmedMetadata = false; | |
int _packetSegment = 0; | |
bool _i2cNodeProcessed = false; | |
char _property[2][24] = {"name", "value"}; | |
void setup() { | |
Wire.begin(); // join i2c bus (address optional for master) | |
Serial.begin(9600); // start serial for output | |
delay(1000); | |
if (!_outputPlotterOnly){ | |
Serial.println("setup"); | |
Serial.println(); | |
} | |
} | |
void loop() { | |
if (_i2cNodeProcessed){ | |
if (!_confirmedMetadata){// let the slave know to start sending sensor data | |
delay(1); | |
Wire.beginTransmission(ADDRESS_SLAVE); | |
Wire.write(1); | |
Wire.endTransmission(); | |
delay(100); | |
_confirmedMetadata = true; | |
} | |
_i2cNodeProcessed = false; | |
if (!_outputPlotterOnly){ | |
Serial.println(); | |
} | |
return; | |
} | |
Wire.requestFrom(ADDRESS_SLAVE, 16); | |
_packetSegment++; | |
char packet[16]; | |
int index = 0; | |
bool isContinueSegment = false;// continueSegment (the 3rd) 1=more, 0=last | |
while (Wire.available()) { // slave may send less than requested | |
char c = Wire.read(); | |
packet[index] = int(c) > -1 ? c : ' ';// replace invalid chars with spaces | |
if (_packetSegment == 3){ | |
_packetSegment = 0; | |
isContinueSegment = true; | |
//Serial.println("-------------"); | |
//Serial.println(int(c)); | |
//Serial.println("-------------"); | |
if (int(c) == 48 || int(c) == 86){// 0 on last property | |
_i2cNodeProcessed = true; | |
// send values to MQTT | |
break; | |
} | |
} | |
index++; | |
} | |
if (!isContinueSegment){ | |
if (!_outputPlotterOnly){ | |
Serial.println(packet); | |
} | |
strcpy(_property[_packetSegment - 1], packet);// set local var with name/value | |
}else{ | |
if (_outputPlotterOnly && _confirmedMetadata){ | |
if (_i2cNodeProcessed){ | |
Serial.println(_property[1]); | |
}else{ | |
Serial.print(_property[1]); | |
Serial.print(" "); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment