Last active
July 10, 2024 15:32
-
-
Save eadmaster/80f9fa8df312c3d6b6f3976b3fc19dac to your computer and use it in GitHub Desktop.
derived from http://thierrydphotographe.free.fr/index.php/2023/03/13/thermal-camera-for-android-diy-do-it-yourself/
This file contains hidden or 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
//Version 1.02 | |
#include <WiFi.h> | |
#include <Adafruit_MLX90640.h> | |
Adafruit_MLX90640 mlx; | |
//#include <U8g2lib.h> | |
/* | |
#define SDA_PIN 41 | |
#define SCL_PIN 40 | |
*/ | |
//U8G2_SSD1306_72X40_ER_F_SW_I2C u8g2(U8G2_R0, /* SCL=*/ SCL_PIN, /* SDA=*/ SDA_PIN, /* reset=*/ U8X8_PIN_NONE); | |
float frame[769]; | |
float frameForSerial[769]; | |
float frameTemp [768]; | |
float Emissivity = 0.95F; | |
TaskHandle_t xHandleUpdateFrame ; | |
bool SerialInProgress = false; | |
bool CaptureFrameInProgress = false; | |
void WriteDisplay(String Str1, String Str2) { | |
/* | |
char buf1[30]; | |
char buf2[30]; | |
Str1.toCharArray(buf1,Str1.length() + 1); | |
Str2.toCharArray(buf2,Str2.length() + 1); | |
u8g2.clearBuffer(); | |
u8g2.drawStr( 0, 0, buf1); | |
u8g2.drawStr( 0, 22, buf2); | |
u8g2.sendBuffer(); | |
*/ | |
Serial.print(Str1); | |
Serial.print(" "); | |
Serial.println(Str2); | |
} | |
void setup() { | |
/* | |
u8g2.begin(); | |
u8g2.setFont(u8g2_font_6x10_tf); | |
u8g2.setFontRefHeightExtendedText(); | |
u8g2.setFontDirection(0); | |
u8g2.setDrawColor(1); | |
u8g2.setFontPosTop(); | |
*/ | |
//Serial.setTxBufferSize (3076); // removed? | |
Serial.setRxBufferSize(20); | |
Serial.begin(115200); | |
WriteDisplay("Create a", "first array"); | |
float Temp = 20.18; | |
for (uint8_t h=0; h<24; h++) { | |
Temp++; | |
for (uint8_t w=0; w<32; w++) { frame[h*32 + w] = Temp; } | |
} | |
copyArray(frame, frameForSerial,769); | |
WriteDisplay("Disable", "WIFI"); | |
WiFi.mode(WIFI_OFF); | |
WriteDisplay("INIT", "Serial"); | |
WriteDisplay("INIT", "MLX90640"); | |
xTaskCreatePinnedToCore( TaskUpdateFrame, "TaskUpdateFrame", 10000, NULL, 1, &xHandleUpdateFrame, 0); /* pin task to core 0 */ | |
WriteDisplay("INIT", "finish"); | |
} | |
void loop() { | |
if (Serial.available()) { | |
String Data = ""; | |
while (Serial.available()) { | |
char Temp = Serial.read(); | |
Data = Data + Temp; | |
} | |
Serial.flush(); | |
if (Data.startsWith("SENDDATA")) { | |
while (CaptureFrameInProgress == true) {delay(1);} | |
SerialInProgress = true; | |
Serial.write((byte *) &frameForSerial, 3076); | |
SerialInProgress = false; | |
} | |
else if (Data.startsWith("EMISSIVITY")) { | |
Emissivity = Data.substring(Data.indexOf("|") + 1, Data.indexOf("²") - Data.indexOf("|") - 1).toFloat(); | |
mlx.setEmissivity(Emissivity); | |
} | |
else if (Data.startsWith("GETEMISSIVITY")) { Serial.print("EMISSIVITY|" + FloatToString(Emissivity, 2) + "²"); } | |
else if (Data.startsWith("CHANGE4")) { mlx.setRefreshRate(MLX90640_4_HZ); } | |
else if (Data.startsWith("CHANGE8")) { mlx.setRefreshRate(MLX90640_8_HZ); } | |
else if (Data.startsWith("CHANGE16")) { mlx.setRefreshRate(MLX90640_16_HZ); } | |
else if (Data.startsWith("HELLO4")) { mlx.setRefreshRate(MLX90640_4_HZ); Serial.print(Data); } | |
else if (Data.startsWith("HELLO8")) { mlx.setRefreshRate(MLX90640_8_HZ); Serial.print(Data); } | |
else if (Data.startsWith("HELLO16")) { mlx.setRefreshRate(MLX90640_16_HZ); Serial.print(Data); } | |
else if (Data.startsWith("HELLO")) { mlx.setRefreshRate(MLX90640_8_HZ); Serial.print(Data); } | |
} | |
delay(5); | |
} | |
void TaskUpdateFrame( void *pvParameters ) { | |
Wire.begin(6,7); // init i2C using SDA=pin6, SCL=pin7 | |
Wire.setClock(800000L); | |
if (! mlx.begin(MLX90640_I2CADDR_DEFAULT, &Wire)) { | |
WriteDisplay("MLX90640", "not found!"); | |
while (1) {delay(1000); WriteDisplay("MLX90640", "not found!");} | |
} else { | |
WriteDisplay("MLX90640", "init correct"); | |
} | |
mlx.setMode(MLX90640_INTERLEAVED); | |
mlx.setResolution(MLX90640_ADC_18BIT); | |
mlx.setRefreshRate(MLX90640_8_HZ); | |
for( ;; ) | |
{ | |
mlx.getFrame(frameTemp); | |
copyArray(frameTemp, frame,768); | |
float TA = mlx.getTA(); | |
Emissivity = mlx.getEmissivity(); | |
frame[768] = TA; | |
while (SerialInProgress == true) {delay(1);} | |
CaptureFrameInProgress = true; | |
copyArray(frame, frameForSerial,769); | |
CaptureFrameInProgress = false; | |
vTaskDelay( pdMS_TO_TICKS( 13 ) ); | |
} | |
} | |
void copyArray(float* src, float* dst, int len) { | |
for (int i = 0; i < len; i++) { *dst++ = *src++; } | |
} | |
String FloatToString(float Val, byte decimal) { | |
String Str = ""; | |
long Tmp = 0; | |
if (Val < 0){Str = "-"; Val = -Val;} | |
Val = (float)Val + (float)(0.5 / pow(10, decimal)); | |
Tmp = (long)Val; | |
Str = Str + (String)Tmp; | |
Val = (float)Val - (float)Tmp; | |
if (decimal > 0){Str = Str + ".";} | |
for (int i=0; i<decimal; i++) { | |
Val = (float)Val * (float)10; | |
Tmp = (long)Val; | |
Str = Str + (String)Tmp; | |
Val = (float)Val - (float)Tmp; | |
} | |
return Str; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment