C/C++

How to deliver data of sensor to the cloud platform

1 410
PreviousNext
How to deliver data of sensor to the cloud platform(LinkNode D1)Summary
DHT22 module applies specific digital blocks collection technology and temperature and humidity sensing technology to ensure that products with high reliability and excellent long-term stability. Sensor comprises a capacitive humidity sensing element and a high-precision temperature component, and with a high-performance 8-bit microcontroller connected. Therefore, the product has excellent quality, fast response, anti-interference ability, high cost and so on.  Measurable temperature range of the module: -40-80 ℃ ± 0.5 ℃, humidity range: 20-90% RH ± 2% RH. The module is widely used in temperature and humidity regulator, weather station, and other relevant temperature and humidity detection control.
Material preparation
  • LinkNode D1 x 1
  • DHT22 x 1
  • DuPont line
  • Arduino IDE
Steps1. Login linksprite.io ,record the “Device ID” and “API key”( If there is no account, please sign up. )
  • 1. My Profile -> API key
  • 2. My device->Create DIY Device
Enter Device Name,Device Type , Group Name , the device number is 03, device name and device grouping can be any.
Note: Device Type must be selected 03(Custom device type) .
Click the device you created and record the “Device ID”.
2. Edit and run the code
Modify the “apikey” and “deviceID”  with content acquired before
Compile the code and upload.
3.  Results
  • Serial Monitor

     2. LinkSprite IO

Code[color=white !important]
[color=white !important]?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

#include <ESP8266WiFi.h>
#include <WString.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>      
  
#include "DHT.h"
  
#define DHTPIN D0     // what pin we're connected to
  
#define DHTTYPE DHT22   // DHT 22  (AM2302)
  
char *tem="";
String apikey = "162d15b0-2c6d-4d44-b288-10f28a527f96";
const char* deviceID="030000000f";
const char* server = "www.linksprite.io";
WiFiClient client;
  
  
DHT dht(DHTPIN, DHTTYPE);
  
void setup() {
  Serial.begin(9600);
  WiFiManager wifiManager;
  wifiManager.setAPStaticIPConfig(IPAddress(10,0,1,1), IPAddress(10,0,1,1), IPAddress(255,255,255,0));
  wifiManager.autoConnect("LinkNodeAP");
  Serial.print("WiFi Connected ...\n");
  Serial.println("WiFi connected");
  Serial.println("DHTxx test!");
   
  dht.begin();
}
  
void loop() {
  
  delay(2000);
  
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // Read temperature as Celsius (the default)
  float f = dht.readTemperature(true); // Read temperature as Fahrenheit (isFahrenheit = true)
  
   // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  
  float hif = dht.computeHeatIndex(f, h); // Compute heat index in Fahrenheit (the default)
  float hic = dht.computeHeatIndex(t, h, false);// Compute heat index in Celsius (isFahreheit = false)
  
  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.print(" *C ");
  Serial.print(f);
  Serial.print(" *F\t");
  Serial.print("Heat index: ");
  Serial.print(hic);
  Serial.print(" *C ");
  Serial.print(hif);
  Serial.println(" *F");
   
  if (client.connect(server,80))
  {  
     String  postStr ="{";
           postStr +="\"action\":\"update\",";
           postStr +="\"apikey\":\"";
           postStr += apikey;
           postStr +="\",";
           postStr +="\"deviceid\":\"";
           postStr += deviceID;
           postStr +="\",";
           postStr +="\"params\":";
           postStr +="{";
            
           postStr +="\"temperature\":\"";
           itoa(t,tem,10);
           postStr +=tem;
           postStr +="\",";
         
           postStr +="\"humidity\":\"";
           itoa(h,tem,10);
           postStr +=tem;
           postStr +="\"\r\n";
           postStr +="}";
           postStr +="}";
        
    client.print("POST /api/http HTTP/1.1\n");
    client.print("Host: ");
    client.print(server);
    client.print("\nContent-Type: application/json\n");
    client.print("Content-Length: ");
    client.print(postStr.length());
    client.print("\n\n");
    client.print(postStr);
      
    String request = "";
    delay(1000);  
    while (client.available())
   {
     char c = client.read();
     request +=c;
   }
  
  if (request!= NULL)
  {
   int index1 = request.indexOf(":{");
   int index2 = request.indexOf("},");
   String param = request.substring(index1, index2 + 1);
   String data="\0" ;
   Serial.print("The param is ");
   Serial.println(param);
  
   
   client.stop();
   Serial.println("waiting ...");   
   delay(1000);  
  }
}
  
}








Thanks for sharing.
I guess this can be also applied on lemaker products...

You have to log in before you can reply Login | Sign Up

Points Rules