<
From version < 80.1 >
edited by Kilight Cao
on 2022/11/08 17:21
To version < 82.1 >
edited by Kilight Cao
on 2022/11/08 17:24
>
Change comment: Uploaded new attachment "image-20221108172432-7.png", version {1}

Summary

Details

Log-Temperature-Sensor-and-send-data-to-Node-red.ino
Author
... ... @@ -1,0 +1,1 @@
1 +XWiki.Kilight
Size
... ... @@ -1,0 +1,1 @@
1 +3.6 KB
Content
... ... @@ -1,0 +1,150 @@
1 +#include <SoftwareSerial.h>
2 +#include <Adafruit_Sensor.h>
3 +#include <DHT.h>
4 +#include <DHT_U.h>
5 +/*
6 +
7 +*/
8 +#define DHTPIN 8 // Digital pin connected to the DHT sensor
9 +#define DHTTYPE DHT11 // DHT 11
10 +DHT_Unified dht(DHTPIN, DHTTYPE);
11 +
12 +String inputString = ""; // a String to hold incoming data
13 +bool stringComplete = false; // whether the string is complete
14 +
15 +long old_time=millis();
16 +long new_time;
17 +
18 +long uplink_interval=30000; //ms
19 +
20 +float DHT11_temp;
21 +float DHT11_hum;
22 +
23 +SoftwareSerial ss(10, 11); // Arduino RX, TX ,
24 +
25 +char rxbuff[128];
26 +uint8_t rxbuff_index=0;
27 +
28 +void setup() {
29 + // initialize serial
30 + Serial.begin(9600);
31 +
32 + ss.begin(9600);
33 + ss.listen();
34 +
35 + // reserve 200 bytes for the inputString:
36 + inputString.reserve(200);
37 +
38 + dht.begin();
39 + sensor_t sensor;
40 + dht.temperature().getSensor(&sensor);
41 + dht.humidity().getSensor(&sensor);
42 +
43 + ss.println("ATZ");//reset LA66
44 +}
45 +
46 +void loop() {
47 +
48 + while ( ss.available()) {
49 + // get the new byte:
50 + char inChar = (char) ss.read();
51 + // add it to the inputString:
52 + inputString += inChar;
53 +
54 + rxbuff[rxbuff_index++]=inChar;
55 +
56 + if(rxbuff_index>128)
57 + {
58 + rxbuff[rxbuff_index]='\0';
59 + rxbuff_index=0;
60 + break;
61 + }
62 +
63 + // if the incoming character is a newline, set a flag so the main loop can
64 + // do something about it:
65 + if (inChar == '\n' || inChar == '\r') {
66 + stringComplete = true;
67 + rxbuff[rxbuff_index]='\0';
68 + rxbuff_index=0;
69 + }
70 + }
71 +
72 + while ( Serial.available()) {
73 + // get the new byte:
74 + char inChar = (char) Serial.read();
75 + // add it to the inputString:
76 + inputString += inChar;
77 + // if the incoming character is a newline, set a flag so the main loop can
78 + // do something about it:
79 + if (inChar == '\n' || inChar == '\r') {
80 + ss.print(inputString);
81 + inputString = "\0";
82 + }
83 + }
84 +
85 + // print the string when a newline arrives:
86 + if (stringComplete) {
87 + Serial.print(inputString);
88 +
89 + // clear the string:
90 + inputString = "\0";
91 + stringComplete = false;
92 + }
93 +
94 + new_time = millis();
95 +
96 + if(new_time-old_time>=uplink_interval){
97 + old_time = new_time;
98 +
99 + Serial.print(F("\r\n"));
100 + // Get temperature event and print its value.
101 + sensors_event_t event;
102 + dht.temperature().getEvent(&event);
103 + if (isnan(event.temperature)) {
104 + Serial.println(F("Error reading temperature!"));
105 + DHT11_temp=327.67;
106 + }
107 + else {
108 + DHT11_temp=event.temperature;
109 +
110 + if(DHT11_temp>60){
111 + DHT11_temp=60;
112 + }
113 + else if(DHT11_temp<-20){
114 + DHT11_temp=-20;
115 + }
116 + }
117 + // Get humidity event and print its value.
118 + dht.humidity().getEvent(&event);
119 + if (isnan(event.relative_humidity)) {
120 + DHT11_hum=327.67;
121 + Serial.println(F("Error reading humidity!"));
122 + }
123 + else {
124 + DHT11_hum=event.relative_humidity;
125 +
126 + if(DHT11_hum>100){
127 + DHT11_hum=100;
128 + }
129 + else if(DHT11_hum<0){
130 + DHT11_hum=0;
131 + }
132 + }
133 +
134 + Serial.print(F("Temperature: "));
135 + Serial.print(DHT11_temp);
136 + Serial.println(F("°C"));
137 + Serial.print(F("Humidity: "));
138 + Serial.print(DHT11_hum);
139 + Serial.println(F("%"));
140 + Serial.print(F("\r\n"));
141 +
142 + char sensor_data_buff[128]="\0";
143 +
144 + //confirm status,Fport,payload length,payload(HEX)
145 + snprintf(sensor_data_buff,128,"AT+SEND=0,%02X%02X%02X%02X,0,0",(short)(DHT11_temp*100)>>8 & 0xFF,(short)(DHT11_temp*100) & 0xFF,(short)(DHT11_hum*10)>>8 & 0xFF,(short)(DHT11_hum*10) & 0xFF);
146 + ss.print(sensor_data_buff);
147 + ss.print('\r');
148 + }
149 +}
150 +
image-20221108172432-7.png
Author
... ... @@ -1,0 +1,1 @@
1 +XWiki.Kilight
Size
... ... @@ -1,0 +1,1 @@
1 +45.9 KB
Content
Copyright ©2010-2022 Dragino Technology Co., LTD. All rights reserved
Dragino Wiki v2.0