# 即時溫度檢測(使用Blynk) ###### tags: `iot project` `ESP32` `lm35` ## 目的﹕用Blynk app 製作圖表。 ![](https://i.imgur.com/969UsGK.jpg) ## 流程﹕ ![](https://i.imgur.com/7WOMp3H.png) A. Blynk App 設定每2秒鐘執行的Timer B. Blynk App執行呼叫ESP32去取lm35當下的溫度 C. 將讀到的數值傳回Blynk App D. 讀到的溫度為圖表的數據來源,圖表會按得到數據的瞬間而更新顯示內容 ## 電路 ![](https://i.imgur.com/BLeGuMp.jpg) LM35的電路可參考 https://create.arduino.cc/projecthub/infoelectorials/project-003-arduino-lm35-temperature-sensor-project-0a43ba **要留意的是要用ADC(Analog to Digital Converter)的腳為取ml35的類比數據(會轉為數碼數據)。 參考以下圖得知ESP32有兩組ADC(ADC1及ADC2)。 實驗所得EPS32在wifi網絡下,ADC2腳回傳的數值是0,用ADC1才能回傳正確數值。所以此實驗會用ADC1的GPIO36。** ![](https://i.imgur.com/1Iylfcm.png) ## Blynk 新增SuperChart ![](https://i.imgur.com/lTcNkpe.jpg) 新增Datastream ![](https://i.imgur.com/gLIuJNU.jpg) 設為V3-V3就是更新數據的目的地。SuperChart收到資料後會自行處理顯示圖像的更新事宜。 ![](https://i.imgur.com/ub3qPOV.jpg) ## Arduino *這次會包括一個設置的檔案(config.h),在這裡設定Blynk的token及wifi等* superChartEvent會每2秒執行一次-將溫度寫入V3 ```c Blynk.virtualWrite(V3, cel) ``` ```c= /************************************************************* Download latest Blynk library here: https://github.com/blynkkk/blynk-library/releases/latest Blynk is a platform with iOS and Android apps to control Arduino, Raspberry Pi and the likes over the Internet. You can easily build graphic interfaces for all your projects by simply dragging and dropping widgets. Downloads, docs, tutorials: http://www.blynk.cc Sketch generator: http://examples.blynk.cc Blynk community: http://community.blynk.cc Follow us: http://www.fb.com/blynkapp http://twitter.com/blynk_app Blynk library is licensed under MIT license This example code is in public domain. ************************************************************* This example shows how to send values to the Blynk App, when there is a widget, attached to the Virtual Pin and it is set to some frequency Project setup in the app: Value Display widget attached to V5. Set any reading frequency (i.e. 1 second) *************************************************************/ /* Comment this out to disable prints and save space */ #define BLYNK_PRINT Serial #include <WiFi.h> #include <WiFiClient.h> #include <BlynkSimpleEsp32.h> #include "config.h" int val; int ADC_36 = 36; //When using wifi, only ADC1 pins work, ADC2 pins won't work BlynkTimer timer; // This function tells Arduino what to do if there is a Widget // which is requesting data for Virtual Pin (5) void labelEvent(){ val = analogRead(ADC_36);//Read from "INPUT" pin 36 float mv = val / 9.31; float cel = mv; char charVal[4]; //assuming 4 char will be shown String stringVal = ""; //data on buff is copied to this string dtostrf(cel, 2, 2, charVal); //cel, This is the length of the string that will be created, The number of digits after the deimal point to printl, the array to store the results for(int i=0;i<sizeof(charVal);i++) { stringVal+=charVal[i]; //each char became part of string } Serial.println(stringVal); String showwithUnit = stringVal+" °C"; Serial.println(showwithUnit); //V5 is the label in Blynk App Blynk.virtualWrite(V5, showwithUnit); //sending to Blynk } void superChartEvent(){ val = analogRead(ADC_36);//Read from "INPUT" pin 36 float mv = val / 9.31; float cel = mv; Serial.println(cel); Blynk.virtualWrite(V3, cel); //V3 is the source for the chart } void setup() { // Debug console Serial.begin(9600); pinMode(ADC_36, INPUT);//input for the temperature sensor Blynk.begin(auth, ssid, pass, server, 8080); timer.setInterval(2000L, superChartEvent);//2seconds timer.setInterval(900000L, labelEvent);//15mins } void loop() { Blynk.run(); timer.run(); } ``` GitHub: https://github.com/snakeleader/blynk_LM35