Arduino+w5100+传感器将数据上传贝壳物联遇到问题

作者:camero | 更新时间:2019-05-30

问题就是连不上贝壳物联,请大神们瞧瞧,代码和截图如下

/*
 Arduino IDE 版本1.6.5-1.8.2
 */

#include <SPI.h>
#include <Ethernet.h>
#include <aJSON.h>
#include <dht11.h>

 dht11 DHT11;
//=========================
const String  APIKEY="5d0c4e3cc"; // 
const String DEVICEID="4542"; // 设备编号
const String INPUTID1="4085";
const String INPUTID2="4099";
const String INPUTID3="4104";
const String INPUTID4="4106";
//=======================================
const int dht = 2;//dht11 pin
int flame=A5;//定义火焰接口
int fla=0;//定义数字变量
//int dal=0;
int mqyw = 0;
int MQYW = A3;//烟雾

int mqkr = 0;
int MQKR = A1;//天然气可燃气

byte mac[] = {0x00, 0x1D, 0x72, 0x82, 0x35, 0x9D};
EthernetClient client ;
IPAddress ip(192, 168, 1, 114);//local IP
//IPAddress server(121,42,180,30);
char server[] = "www.baidu.com";  
int port= 80;

aJsonStream serial_stream(&client);
unsigned long lastCheckInTime = 0; 
unsigned long lastUpdateTime = 0; 
const unsigned long postingInterval = 10000; //  40s
const unsigned long updateInterval = 5000; //  5s
boolean isCheckIn = false;

void setup() {
  Serial.begin(9600);
  //dal=analogRead(flame);
  //pinMode(MQYW,INPUT);
  //pinMode(MQ-A.INPUT);
  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");
    if (!client.connected())
{Serial.println("???"); }
  }else{
    Serial.println("connection failed");
  }
  checkIn();
}

void loop() {
  if(millis() - lastCheckInTime > postingInterval)
  {
    Serial.println("checking in"); 
    checkIn();
  }
  if((millis() - lastUpdateTime > updateInterval) && isCheckIn) 
  {
    int val;//定义变量
    int dat;//定义变量
     DHT11.read(dht);
    dat = DHT11.humidity;
    val = DHT11.temperature;
    fla = analogRead(flame);
    mqyw = analogRead(MQYW);
    mqkr = analogRead(MQKR);
    update2(DEVICEID,INPUTID1,val,INPUTID2,fla,INPUTID3,mqyw,INPUTID4,mqkr);
  }
 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 update1(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);   
}
*/
//同时上传四个接口数据函数
void update2(String did, String inputid1, int value1, String inputid2, int value2,String inputid3, int value3,String inputid4, int value4)
{
  client.print("{\"M\":\"update\",\"ID\":\"");
  client.print(did);
  client.print("\",\"V\":{\"");
  client.print(inputid1);
  client.print("\":\"");
  client.print(value1);
  client.print("\",\"");
  client.print(inputid2);
  client.print("\":\"");
  client.print(value2);
  client.print("\",\"");
  client.print(inputid3);
  client.print("\":\"");
  client.print(value3);
  client.print("\",\"");
  client.print(inputid4);
  client.print("\":\"");
  client.print(value4);
  client.println("\"}}");
  lastCheckInTime = millis();
  lastUpdateTime= millis();
  Serial.print("update:");   
  Serial.print(inputid1);   
  Serial.print("->"); 
  Serial.print(value1);  
  Serial.print(":"); 
  Serial.print(inputid2);   
  Serial.print("->"); 
  Serial.println(value2);
  Serial.print(":");
   Serial.print(inputid3);   
  Serial.print("->"); 
  Serial.println(value3);
  Serial.print(":");
  Serial.print(inputid4);   
  Serial.print("->"); 
  Serial.println(value4);
}

 

 


评论:共0条

评论支持使用[code][/code]标签添加代码
返回顶部