当前位置:网站首页>Single bus temperature sensor 18B20 data on cloud (Alibaba cloud)

Single bus temperature sensor 18B20 data on cloud (Alibaba cloud)

2022-06-12 13:53:00 TMS320VC5257H

Abstract : adopt d1 Module collects single bus temperature sensor 18B20 data , interval 1.5 Send the latest temperature data to the Alibaba cloud Internet of things platform every second .

Ying netizen “ A drop in the bucket ” Please , Help achieve 18B20 Sensor data on the cloud . The source code has been “ A drop in the bucket ” Agree to publish in this article .

1. Hardware and software environment

Software :win7x64,arduino ide 1.8.13

Hardware :d1,18b20

2. Hardware wiring

As shown in the figure below , Single bus power supply is used 5V,GND And D1 Of GND Connected to a .

The signal line is connected to D1 The pin of D4.

 3. Source code

If you don't understand the basic knowledge of data cloud , Please refer to the article series

If your ide If the board and library have not been configured , Please also refer to the series of articles , take ide The required board support package and library file are loaded , Otherwise, various missing components will be prompted .

from arduino The board begins to achieve Alibaba cloud app control --WeMos D1 or ESP8266 Connect the Alibaba cloud article collection and source code

/*
 *  Program name :aliyun002
 *  author : A drop in the bucket 
 *  Last revision : peanut ,2021 year 8 month 22 Japan 09:58:39
 *  Program function : Read 18B20 Single bus temperature sensor data , Send it to Alibaba cloud Internet of things platform 
 *  Hardware connection :VCC---5V,GND---GND,DQ---D4. On the left is the sensor pin , The right side is D1 Module pin 
 *  Cloud configuration : The product name “ Blowers ”, Equipment name arduino18b20001, There are two properties , They are the second temperature temperature002, Floating point type ; Blower temperature gufengjiwendu,int32 type 
 *  Running effect : Burn this procedure to D1 modular , Relevant information can be observed through the serial port . First, it will connect to WIFI, Then connect to alicloud , At this time, you can log in to the Alibaba cloud Internet of things platform through the browser , Observe that the device is online .
 *  Then use online debugging tools , towards gufengjiwendu Set the number 1, Will be displayed on the serial port “ The temperature is set to 1”, This data flows from the cloud to the device .
 *  Equipment per 1.5 Submit to the cloud once every second 18B20 Temperature of , The data is in the form of XX.YY, Retain 2 Is a decimal . If you hold the sensor with your fingers , You will find that the temperature will change constantly .
 * 
 *  Use this program , You can master a basic skill of data collection on the cloud .
*/

//  introduce  wifi  modular , And instantiate , Different chips may have different dependencies here 
#include <ESP8266WiFi.h>
static WiFiClient espClient;

//  Introducing Alibaba cloud  IoT SDK
#include <AliyunIoTSDK.h>

#include <OneWire.h>
#include <DallasTemperature.h>
 
#define ONE_WIRE_BUS D4

OneWire oneWire(ONE_WIRE_BUS);

DallasTemperature sensors(&oneWire);

//  Set product and device information , Check from alicloud device information 
#define PRODUCT_KEY "a1JXXXXae7Sv"
#define DEVICE_NAME "arduino18b20001"
#define DEVICE_SECRET "f06c15XXXXXXc65b8721af4d"
#define REGION_ID "cn-shanghai"

//  Set up  wifi  Information 
//#define WIFI_SSID "XXXXXXi"
//#define WIFI_PASSWD "1XXXXXX678"


void setup()
{
    Serial.begin(115200);

    sensors.begin();// initialization 18b20
    
    //  initialization  wifi
    wifiInit(WIFI_SSID, WIFI_PASSWD);
    
    //  initialization  iot, Need to transfer in  wifi  Of  client, And device product information 
    AliyunIoTSDK::begin(espClient, PRODUCT_KEY, DEVICE_NAME, DEVICE_SECRET, REGION_ID);
    
    //  Bind a device property callback , When this property is modified remotely , Will trigger  powerCallback
    // PowerSwitch  It refers to the Internet of things model defined in the device product  id
    AliyunIoTSDK::bindData("gufengjiwendu", powerCallback);// This binding callback , Cannot bind Chinese characters , What should be bound is the function identifier , This identifier cannot use Chinese characters    
}

void loop()
{
    AliyunIoTSDK::loop();// This is alicloud SDK The main loop inside , In this cycle, the device and cloud will be waiting all the time MQTT news , Whether it's up or down , It is it that responds .
                         //
//-------------------------------------------------------------------//
// The following code reproduces a drop in the ocean to read the temperature program . The purpose is to observe the temperature through the serial port .
  Serial.print(" Requesting temperatures...");
  sensors.requestTemperatures(); 
  Serial.println("DONE"); 
  Serial.print("Temperature for Device 1 is: ");
  Serial.print(sensors.getTempCByIndex(0));  
  AliyunIoTSDK::send("temperature002",sensors.getTempCByIndex(0));  
  delay(1500);
//------------------------------------------------------------------//
}  

//  initialization  wifi  Connect 
void wifiInit(const char *ssid, const char *passphrase)
{
    WiFi.mode(WIFI_STA);
    WiFi.begin(ssid, passphrase);
    while (WiFi.status() != WL_CONNECTED)
    {
        delay(1000);
        Serial.println("WiFi not Connect");
    }
    Serial.println("Connected to AP");// What can be printed by serial port is : state is connected  ah ? Why print state is connected Well ? Because the program is running , Alibaba cloud has been running SDK, This SDK Inside , Will constantly shake hands with Alibaba cloud , If you maintain a connection with Alibaba cloud , So the hint is stateis connected.
}

// Callback function for power attribute modification , analysis json The identifier of the data cannot be Chinese characters . What is the data distributed in the cloud , This will respond to something . For example, the cloud sends numbers 1, Then the string will be sent through the serial port .
// If it is other data , Because there is no response code in the callback function , There will be no reaction .
void powerCallback(JsonVariant p)
{
    int PowerSwitch = p["gufengjiwendu"];
    if (PowerSwitch == 1)
    {
        //  Boot device 
         Serial.println(" The temperature is set to 1");
    } 
}

4. Realization effect

 * Running effect : Burn this procedure to D1 modular , Relevant information can be observed through the serial port . First, it will connect to WIFI, Then connect to alicloud , At this time, you can log in to the Alibaba cloud Internet of things platform through the browser , Observe that the device is online .


 * Then use online debugging tools , towards gufengjiwendu Set the number 1, Will be displayed on the serial port “ The temperature is set to 1”, This data flows from the cloud to the device .


 * Equipment per 1.5 Submit to the cloud once every second 18B20 Temperature of , The data is in the form of XX.YY, Retain 2 Is a decimal . If you hold the sensor with your fingers , You will find that the temperature will change constantly .

 

 

 

The above content requires a certain knowledge of Alibaba cloud Internet of things platform , Interested students can praise and pay attention to .

For a very detailed introduction, please refer to the collection of articles

from arduino The board begins to achieve Alibaba cloud app control --WeMos D1 or ESP8266 Connect the Alibaba cloud article collection and source code

原网站

版权声明
本文为[TMS320VC5257H]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/163/202206121345059539.html