NodeMCU刷Arduino固件读取红外测温数据并上传到OneNet

#include 
#include 
#include 
#include 

#define WIFINAME "F5dAST"
#define WIFIPW   "2015652066"
const char* host = "183.230.40.33";
const char* devidGet = "23785199";
const char* apiKey = "Cwpofvw5UWw=Y2j=H656576uunXsRCI= ";


void setup() {

  pinMode(BUILTIN_LED, OUTPUT);
  Serial.begin(115200);
  Serial.println("");
  WiFi.begin(WIFINAME, WIFIPW);//link to router
  Serial.print("Connecting..");
  while (WiFi.status() != WL_CONNECTED)//try link to router
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println();
  Serial.print("Connected,IP Address:");//connecting succeed
  Serial.println(WiFi.localIP());

}

void loop() {
// RequestData();
 SendData(11);
}
void SendData(float a)
{
  int lengthofPost=62;
   WiFiClient client;
  Serial.printf("\r\nConnectint to %s ..", host);
  Serial.println("");
  if (client.connect(host, 80))
  {
    //length of the post content remain to be post
    Serial.println("Connected!");
    Serial.println("Send a request");
   client.print(String("POST /devices/") + devidGet +"/datapoints HTTP/1.1\r\n" + 
   "api-key: "+apiKey+  "\r\n" +
    "Host: api.heclouds.com"+  "\r\n"+
   "Content-Length: "+lengthofPost+"\r\n\r\n"
   +"{\"datastreams\":[{\"id\":\"18dB20\",\"datapoints\":[{\"value\":\"11\"}]}]}"+"\r\n"
                );//devidGet the device id 

                //============
                Serial.print(String("POST /devices/") + devidGet +"/datapoints HTTP/1.1\r\n" + 
   "api-key: "+apiKey+  "\r\n" +
    "Host: api.heclouds.com"+  "\r\n"+
   "Content-Length: "+lengthofPost+"\r\n\r\n"
   +"{\"datastreams\":[{\"id\":\"18B20\",\"datapoints\":[{\"value\":\"11\"}]}]}"+"\r\n"
                );//devidGet the device id 
//apiKey: the apiKey
    Serial.println("\r\n[Response:]");
    while (client.connected())
    {
      while (client.connected())
      {
        if (client.available())
        {
          String line = client.readStringUntil('\n');
          Serial.println(line);
        }
      }
      client.stop();
      Serial.println("\nClient stop, disconnected");
    }
  }
  else
  {
    Serial.println("connecting failed!");
    client.stop();
  }
  delay(3000);
  }
//POST /devices/20123901/datapoints HTTP/1.1
//api-key:RH5KM1PmIskXgWV556BtXpqsZ4s=
//Host:api.heclouds.com
//Content-Length: 61
//
//{"datastreams":[{"id":"Temp","datapoints":[{"value":"33"}]}]}



  //==================
  void RequestData()
{
   WiFiClient client;
  Serial.printf("\r\nConnectint to %s ..", host);
  Serial.println("");
  if (client.connect(host, 80))
  {
    Serial.println("Connected!");
    Serial.println("Send a request");
    client.print(String("GET /devices/") + devidGet +"/datastreams/Switch HTTP/1.1\r\n" + "Host:" + host + "\r\n" +
    "api-key: "+apiKey+  "\r\n" +
    "Host: api.heclouds.com\r\n" + "\r\n"
                );//devidGet the device id 
//apiKey: the apiKey
    Serial.println("\r\n[Response:]");
    while (client.connected())
    {
      while (client.connected())
      {
        if (client.available())
        {
          String line = client.readStringUntil('\n');
          Serial.println(line);
        }
      }
      client.stop();
      Serial.println("\nClient stop, disconnected");
    }
  }
  else
  {
    Serial.println("connecting failed!");
    client.stop();
  }
  delay(3000);
  }
//  GET /devices/23785199/datastreams/Switch HTTP/1.1
//api-key: ZyGPZiTMBMvamibfxN3AEgqlhPY=
//Host: api.heclouds.com


// POST /devices/23785199/datapoints HTTP/1.1
// api-key: Cwpofvw5UWw=Y2j=H76uunXsRCI= 
// Host: api.heclouds.com
// Content-Length: 62

// {"datastreams":[{"id":"18B20","datapoints":[{"value":"11"}]}]}


温度计测试



/////////////////////
/*
GY-MCU90615----MINI
VCC----VCC
GND----GND
1:RX---TX,send A5 51 F6 to GY263
2:TX---RX
3:MINI_TX---FT232_RX
*/
//////////////////
unsigned char Re_buf[11],counter=0;
unsigned char sign=0;
float TO=0,TA=0;
void setup() {
   Serial.begin(9600);  
  delay(1);    
  Serial.write(0XA5); 
  Serial.write(0X45);    //初始化,连续输出模式
  Serial.write(0XEA);    //初始化,连续输出模式
}

void loop() {
  unsigned char i=0,sum=0;
  
  if(sign)
  {   
     sign=0;
     for(i=0;i<8;i++)
      sum+=Re_buf[i]; 
     if(sum==Re_buf[i] )        //检查帧头,帧尾
     {  	       
           TO=(float)(Re_buf[4]<<8|Re_buf[5])/100;
           Serial.print("TO:");
           Serial.println(TO);  
           TA=(float)(Re_buf[6]<<8|Re_buf[7])/100;
           Serial.print("TA:");
           Serial.println(TA);             
   }
  } 

}
void serialEvent() {
  while (Serial.available()) {   
    Re_buf[counter]=(unsigned char)Serial.read();
    if(counter==0&&Re_buf[0]!=0x5A) return;      // 检查帧头         
    counter++;       
    if(counter==9)                //接收到数据
    {    
       counter=0;                 //重新赋值,准备下一帧数据的接收 
       sign=1;
    }      
  }
}

你可能感兴趣的:(NodeMCU刷Arduino固件读取红外测温数据并上传到OneNet)