Advertisement
Guest User

Untitled

a guest
Feb 20th, 2020
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. #include <SoftwareSerial.h>
  2.  
  3. // On LinkIt 7697, the RX pin must be one of the EINT pins, such as P2 and P3.
  4. // There are no limitations on TX pin.
  5. //SoftwareSerial mySerial(3, 11); // RX, TX
  6.  
  7.  
  8.  
  9. #include "DHT.h"
  10. #define DHTPIN 2 // what digital pin we're connected to
  11. #define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
  12. SoftwareSerial mySerial(5, 4);
  13. DHT dht(DHTPIN, DHTTYPE);
  14.  
  15. void setup() {
  16. Serial.begin(115200);
  17. while (!Serial) {
  18. ;
  19. }
  20. Serial.println("DHTxx test!");
  21.  
  22. mySerial.begin(115200);
  23. while (!mySerial) {
  24. ;
  25. }
  26. dht.begin();
  27.  
  28.  
  29. pinMode(12, OUTPUT); //for rest EK-S76SXB
  30. digitalWrite(12, LOW); //for rest EK-S76SXB
  31. delay(100); // wait for 0.1 second
  32. pinMode(12, INPUT); //for rest EK-S76SXB
  33. delay(2000);
  34.  
  35. mySerial.print("mac join abp"); //reset the board
  36. Serial.println("mac join abp");
  37. delay(5000);
  38.  
  39. mySerial.print("mac tx ucnf 2 8888888888"); //reset the board
  40. Serial.println("mac tx ucnf 2 8888888888"); //just for test
  41.  
  42. delay(5000);
  43.  
  44. }
  45.  
  46. int val_h; //humidity int part
  47. int val_hp; //humidity point part
  48. int val_t; //add 溫度
  49. int val_tp; //add 溫度 point part
  50. String lora_string; //string for TX
  51.  
  52.  
  53. void loop() {
  54. float h = dht.readHumidity(); // Read temperature as Celsius (the default)
  55. float t = dht.readTemperature(); // Read temperature as Fahrenheit (isFahrenheit = true)
  56. float f = dht.readTemperature(true);
  57. if (isnan(h) || isnan(t) || isnan(f)) { //如果讀不到值就代表沒讀到sensor
  58. Serial.println("Failed to read from DHT sensor!");
  59. return;
  60. }
  61.  
  62. float hif = dht.computeHeatIndex(f, h);
  63. float hic = dht.computeHeatIndex(t, h, false);
  64.  
  65. Serial.print("Humidity: ");
  66. Serial.print(h);
  67. Serial.print(" %\t");
  68. Serial.print("Temperature: ");
  69. Serial.print(t);
  70. Serial.print(" *C ");
  71. Serial.print(f);
  72. Serial.print(" *F\t");
  73. Serial.print("Heat index: ");
  74. Serial.print(hic);
  75. Serial.print(" *C ");
  76. Serial.print(hif);
  77. Serial.println(" *F");
  78.  
  79.  
  80. ////////////next for LoRa tx ////////////////////////////////////////////////////////////
  81. //hum 以下處理濕度
  82. val_h = (int) h;
  83. val_hp=int ((h-val_h)*100);
  84. lora_string="mac tx ucnf 2 01"; //for tx header
  85. if (val_h<10) lora_string+="0"; //for hex format
  86. lora_string+=val_h;
  87. if (val_hp<10) lora_string+="0";
  88. lora_string+=val_hp;
  89. //temperture 以下處理溫度
  90. val_t = (int) t;
  91. val_tp=int ((t-val_t)*100);
  92. if (val_t<10) lora_string+="0";
  93. lora_string+=val_t;
  94. if (val_tp<10) lora_string+="0";
  95. lora_string+=val_tp;
  96.  
  97. mySerial.print(lora_string);
  98. delay(3000);
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement