• <table id="0ws66"></table>
  • <noscript id="0ws66"><source id="0ws66"></source></noscript>
    Arduino室內光照強度檢測

    Arduino室內光照強度檢測的詳細介紹

    創作者:貝殼物聯 | 更新日期:2016-12-14 | 在線時長:7天
    Arduino室內光照強度檢測,來自貝殼物聯

    功能介紹

    實時上報室內光照情況

    硬件組成

    Arduino uno r3

    光敏電阻

    室內光照接線圖

    代碼說明

    #include <SPI.h>
    #include <Ethernet.h>
    #include <aJSON.h>
    //=============  此處必須修該============
    const String  APIKEY="xxxxxxxxx"; // 此處替換為你自己的API KEY
    const String DEVICEID="xx"; // 此處替換為你的設備編號
    const String INPUTID="xx";
    //=======================================
    const int LM35 = 0;//LM35 pin
    byte mac[] = {0x00, 0x1D, 0x72, 0x82, 0x35, 0x9D};
    EthernetClient client ;
    IPAddress ip(192, 168, 0, 177);//local IP
    char server[] = "121.42.180.30";  
    int port= 8181 ;
    aJsonStream serial_stream(&client);
    unsigned long lastCheckInTime = 0; 
    unsigned long lastUpdateTime = 0; 
    const unsigned long postingInterval = 40000; // delay between 2 datapoints, 30s
    const unsigned long updateInterval = 5000; // delay between 2 datapoints, 30s
    boolean isCheckIn = false;
    void setup() {
      Serial.begin(57600);
      if (Ethernet.begin(mac) == 0) {// start the Ethernet connection with DHCP:
        Serial.println("Failed to configure Ethernet using DHCP");
        Ethernet.begin(mac, ip);   
      }
      else {
        Serial.println("Ethernet configuration OK");
      } 
      delay(10000);
      Serial.println("connecting...");
      if(client.connect(server, port)){
        Serial.println("connected");
      }else{
        Serial.println("connection failed");
      }
    }
    void loop() {
      if(millis() - lastCheckInTime > postingInterval) {
        Serial.println("checking in"); 
        checkIn();
      }
      if((millis() - lastUpdateTime > updateInterval) && isCheckIn) {
        int val;//定義變量
        int dat;//定義變量
        dat=analogRead( LM35 );// 讀取傳感器的模擬值并賦值給dat
        //val=(125*dat)>>8;//溫度計算公式
        val = dat * (4.76 / 1023.0*100);  
        update(DEVICEID,INPUTID,val);
      }
     if (serial_stream.available()) {
        /* First, skip any accidental whitespace like newlines. */
        serial_stream.skip();
      }
      if (serial_stream.available()) {
        /* Something real on input, let's take a look. */
        aJsonObject *msg = aJson.parse(&serial_stream);
        processMessage(msg);
        aJson.deleteItem(msg);
      } 
    }
    void checkIn() {
      if (!client.connected()) {
        Serial.println("connection failed"); 
        client.stop();
        isCheckIn=false;
        client.connect(server, port);
        Serial.println("connecting..."); 
        delay(10000);
      }
      else{
        Serial.println("connection success"); 
        client.print("{\"M\":\"checkin\",\"ID\":\"");
        client.print(DEVICEID);
        client.print("\",\"K\":\"");
        client.print(APIKEY);
        client.println("\"}");
        lastCheckInTime = millis();
        Serial.println("checking...");    
      }
    }
    void processMessage(aJsonObject *msg){
      aJsonObject* method = aJson.getObjectItem(msg, "M");
      String M=method->valuestring;
      char* st = aJson.print(msg);        
      if (st != NULL) {
        Serial.println(st); 
        free(st);
        if(M=="checkinok" ){      
          isCheckIn=true;
          Serial.println("check in OK!");   
        }
      } 
    }
    void update(String did, String inputid, int value){
      client.print("{\"M\":\"update\",\"ID\":\"");
      client.print(did);
      client.print("\",\"V\":{\"");
      client.print(inputid);
      client.print("\":\"");
      client.print(value);
      client.println("\"}}");
      lastCheckInTime = millis();
      lastUpdateTime= millis();
      Serial.print("update:");   
      Serial.print(inputid);   
      Serial.print("->");   
      Serial.println(value);   
    }


    国产综合在线