1. 主页
  2. 文档
  3. 小钉锤物联网云平台文档
  4. 应用案例
  5. i4 液体物联网温度计

i4 液体物联网温度计

案例详情: https://xdc.link/portfolio-items/i4-%e5%b0%8f%e9%92%89%e9%94%a4%e7%89%a9%e8%81%94%e7%bd%91%e6%b6%b2%e4%bd%93%e6%b8%a9%e5%ba%a6%e8%ae%a1%e5%b8%a6%e6%98%be%e7%a4%ba%e5%b1%8f-%e5%b8%a6%e6%8a%a5%e8%ad%a6

源代码:


#include <PubSubClient.h>
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>    
#include <ArduinoJson.h>
#include <DS18B20.h>

//设置显示屏
#include <U8g2lib.h>
#include <Wire.h>
U8G2_SSD1306_128X64_NONAME_1_SW_I2C u8g2(U8G2_R0,  D3, D4, U8X8_PIN_NONE);

//设置显示图案
static const unsigned char PROGMEM bitmap[400] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x83,0x01,0x00,0x30,0x00,0xC0,0x00,0x00,0xC0,0x01,0x70,0x00,0x06,0x07,0x00,0xC0,0x86,0x03,0x00,0x70,0x00,0xE0,0x01,0xC0,0xC0,0x01,0xFF,0x00,0x0E,0x07,0xE0,0xFF,0xCF,0x01,0x00,0x30,0x00,0xE0,0xF0,0xFF,0xC1,0xF1,0x3F,0xC0,0x86,0x03,0xE0,0x31,0xCE,0x00,0x00,0x30,0x00,0x60,0x2C,0x0C,0xC0,0x16,0x06,0xC0,0x87,0x01,0x83,0x31,0xCC,0x00,0x00,0x30,0x00,0xE0,0x1F,0x0C,0xE0,0x0F,0x06,0xC0,0x86,0xFF,0x87,0x31,0x64,0x03,0x00,0x30,0x00,0x70,0x00,0x0C,0x60,0x00,0x86,0xE1,0xDE,0x6E,0x87,0xF1,0xFF,0x07,0x80,0x31,0x01,0x30,0x00,0x0C,0x60,0xFC,0xFF,0xE3,0xFF,0x66,0x83,0xBF,0x39,0x00,0xC0,0x33,0x03,0x38,0x06,0x0C,0x30,0x7E,0x66,0x60,0x66,0x76,0x83,0x31,0x38,0x00,0xC0,0x31,0x06,0xF8,0x0F,0x0C,0xF0,0x6F,0x66,0x60,0x36,0x77,0x83,0x31,0x38,0x00,0xC0,0x31,0x1E,0xDC,0x00,0x0C,0x98,0x61,0x66,0x30,0x36,0x33,0x83,0x31,0x38,0x00,0xE0,0x30,0x3C,0xCC,0x00,0x0C,0x88,0x61,0xE6,0x11,0x86,0x33,0x83,0x31,0x38,0x06,0x70,0x30,0x78,0xC4,0x0C,0x0C,0x80,0xFD,0xFF,0x13,0xBE,0x39,0x83,0xFF,0xFF,0x0F,0x30,0x30,0x70,0xFC,0x1F,0x0C,0xF8,0x7F,0x66,0x00,0xCF,0x18,0x83,0xF1,0x38,0x00,0x38,0x30,0x70,0xC8,0x00,0x0C,0x90,0x61,0x66,0xE0,0xE7,0x1C,0x83,0x31,0x78,0x00,0x1C,0x30,0x60,0xC0,0x00,0x0C,0x80,0x61,0x66,0xF0,0x66,0x0C,0x83,0xF1,0x78,0x00,0x0E,0x30,0x60,0xC0,0x18,0x0C,0x80,0x61,0xE6,0x61,0x36,0x0E,0x83,0x7F,0xF8,0x00,0x02,0x30,0x00,0xC0,0x1C,0x0C,0x80,0xF9,0xFF,0x03,0x06,0x87,0xE3,0x3F,0xDC,0x00,0x00,0x30,0x00,0xC0,0x07,0x0C,0x80,0x1F,0x06,0x00,0x06,0x83,0xE3,0x33,0xCE,0x01,0x00,0x30,0x00,0xC0,0x03,0x0C,0xC0,0x03,0x06,0x00,0x86,0x81,0xC1,0x30,0x86,0x03,0x00,0x3F,0x00,0xC0,0xC1,0x0F,0xC0,0x01,0x86,0x00,0xE6,0xFC,0x01,0x30,0x87,0x0F,0x00,0x3E,0x00,0xC0,0x80,0x0F,0xC0,0xF8,0xFF,0x01,0x76,0xF0,0x01,0xB0,0x03,0x0F,0x00,0x1C,0x00,0x00,0x00,0x07,0x00,0x10,0x00,0x00,0x16,0x60,0x00,0xF0,0x00,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

OneWire oneWire_0(0);

//设置温度计引脚
DS18B20 ds(D2);





#define TOKEN "505548111"

char thingsboardServer[] = "cn.xdc.link";



WiFiClient wifiClient;

PubSubClient client(wifiClient);

int status = WL_IDLE_STATUS;
unsigned long lastSend;

//显示开始
void page1() {
  u8g2.drawXBMP(0, 0, 125, 25, bitmap);
  
  u8g2.setFont(u8g2_font_timR18_tf);
  u8g2.setCursor(60,27);
  u8g2.print("°C");
  u8g2.setFont(u8g2_font_timR14_tf);
  u8g2.setCursor(0,49);
  u8g2.print("xdc.link");
  
  u8g2.setFont(u8g2_font_timR18_tf);
  u8g2.setFontPosTop();
  u8g2.setCursor(0,27);
  u8g2.print(ds.getTempC());
delay(10);
}
//显示结束




void setup()
{
  Serial.begin(9600);        
      WiFiManager wifiManager;
      wifiManager.autoConnect("xdc.link");
      Serial.println("connected...yeey :)");

  delay(10);

  InitWiFi();
  client.setServer( thingsboardServer, 1883 );
  lastSend = 0;

//显示开始
  u8g2.begin();
  u8g2.enableUTF8Print();

  
}





void loop()                                                  //主程序  
{


 //物联网
  
  if ( !client.connected() ) {   //客户端没有连接                       //原程序
    reconnect();
  }

  if ( millis() - lastSend > 1000 ) { // Update and send only after 1 seconds
    getAndSendTemperatureAndHumidityData();                                       //发送数据到物联网
    lastSend = millis();
  }

  client.loop();


  //显示开始

u8g2.firstPage();
  do
  {
    page1();
  }while(u8g2.nextPage());
  //显示结束
 
}

 


 

                              void getAndSendTemperatureAndHumidityData()                       //定义获取并发送的温湿度数据函数
                              {
                                Serial.println("Collecting temperature data.");
                              
                                float h = ds.getTempC();                                   //读取湿度
                          
                                float t = ds.getTempC();
                        
                              
                              
                              // Just debug messages
                               Serial.print( "Sending temperature and humidity : [" );
                                Serial.print("Humidity: ");
                                Serial.print(h);                // 串口打印湿度数据
                                Serial.print(" %\t");
                                Serial.print("Temperature: ");
                                Serial.print(t);                // 串口打印温度数据
                                Serial.print(" *C ");

                                  
                             
   

                              
                                String temperature = String(t);
                                String humidity = String(h);
                              
                              
                              
                               
                              
                              
                                  
                                // Prepare a JSON payload string
                                String payload = "{";
                                payload += "\"temperature\":"; payload += temperature; payload += ",";
                                payload += "\"humidity\":"; payload += humidity;
                                payload += "}";
                              
                                // Send payload                         paylode,对于接受者有用的数据
                                char attributes[100];
                                payload.toCharArray( attributes, 100 );
                                client.publish( "v1/devices/me/telemetry", attributes );
                                Serial.println( attributes );
                              
                              }
//连服务器
void InitWiFi()
{
  Serial.println("Connecting to AP ...");
  // attempt to connect to WiFi network

 // WiFi.begin(WIFI_AP, WIFI_PASSWORD);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("Connected to AP");
}


void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    status = WiFi.status();
    if ( status != WL_CONNECTED) {
     // WiFi.begin(WIFI_AP, WIFI_PASSWORD);
      while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
      }
      Serial.println("Connected to AP");
    }
    Serial.print("Connecting to ThingsBoard node ...");
    // Attempt to connect (clientId, username, password)
    if ( client.connect("ESP8266 Device", TOKEN, NULL) ) {
      Serial.println( "[DONE]" );
    } else {
      Serial.print( "[FAILED] [ rc = " );
      Serial.print( client.state() );
      Serial.println( " : retrying in 5 seconds]" );
      // Wait 5 seconds before retrying
      delay( 5000 );
    }
  }
}
这篇文章对您有用吗?

我们要如何帮助您?