Last active
February 27, 2018 18:00
-
-
Save zhiyao/a861c92462b6fe9fc7d15db203bb710a to your computer and use it in GitHub Desktop.
CJMCU-MLX90614 using Arduino
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
/* | |
Software serial to gather information from CJMCU-MLX90614 using an Arduino | |
Receives from the hardware serial, sends to software serial. | |
Receives from software serial, sends to hardware serial. | |
The circuit: | |
* RX is digital pin 10 (connect to TX of other device) | |
* TX is digital pin 11 (connect to RX of other device) | |
*/ | |
#include <SoftwareSerial.h> | |
SoftwareSerial mySerial(10, 11); // RX, TX | |
void setup() { | |
// Open serial communications and wait for port to open: | |
Serial.begin(9600); | |
while (!Serial) { | |
; // wait for serial port to connect. Needed for native USB port only | |
} | |
Serial.println("Start!"); | |
// set the data rate for the SoftwareSerial port | |
mySerial.begin(9600); | |
} | |
int count = 0; | |
char inBytes[9] = {0}; | |
bool isHeader1 = false; | |
void loop() { | |
if (mySerial.available()) { | |
char inByte = mySerial.read(); | |
if (isHeader1 == false && inByte == 0x66) { | |
isHeader1 = true; | |
count = 0; | |
Serial.println("*"); | |
} else if (isHeader1 == true && inByte == 0x66) { | |
isHeader1 = false; | |
count = 1; | |
Serial.println("**"); | |
} | |
inBytes[count % 9] = inByte; | |
count++; | |
// For debugging purpose | |
// Serial.print("0: "); | |
// Serial.println(inBytes[0], HEX); | |
// Serial.print("1: "); | |
// Serial.println(inBytes[1], HEX); | |
// Serial.print("2: "); | |
// Serial.println(inBytes[2], HEX); | |
// Serial.print("3: "); | |
// Serial.println(inBytes[3], HEX); | |
// Serial.print("4: "); | |
// Serial.println(inBytes[4], HEX); | |
// Serial.print("5: "); | |
// Serial.println(inBytes[5], HEX); | |
// Serial.print("6: "); | |
// Serial.println(inBytes[6], HEX); | |
// Serial.print("7: "); | |
// Serial.println(inBytes[7], HEX); | |
// Serial.print("8: "); | |
// Serial.println(inBytes[8], HEX); | |
if (count == 8) { | |
// Gather temperature | |
float temp = ((float)(inBytes[4] << 8 | inBytes[5]) / 100); | |
float ambientTemp = ((float)(inBytes[6] << 8 | inBytes[7]) / 100); | |
Serial.print("Temp: "); | |
Serial.println(temp); | |
Serial.print("Ambient: "); | |
Serial.println(ambientTemp); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment