无法在Arduino UNO上使用带有GSM屏蔽的DS18B20温度传感器

时间:2017-03-08 14:55:34

标签: arduino sensor gsm temperature

我尝试了所有解决方案,但没办法......

我有官方的Antenova GSM屏蔽和传感器DS18B20。

如果我只连接没有传感器的GSM屏蔽,我从传感器获得-127并且屏蔽能够成功地将HTTP发送到我的服务器。如果传感器已连接,则返回正确的温度,但client.connect(server, port)永不返回。我将传感器引脚设置为12而不是2以避免出现问题,但看起来它们存在冲突。我已经在使用外部电源了。

// libraries
#include <GSM.h>
#include <OneWire.h>
#include <DallasTemperature.h>
// PIN Number
#define PINNUMBER "1218"
// APN data
#define GPRS_APN       "web.omnitel.it" // replace your GPRS APN
#define GPRS_LOGIN     ""    // replace with your GPRS login
#define GPRS_PASSWORD  "" // replace with your GPRS password
// Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 12

// Setup a oneWire instance to communicate with any OneWire devices 
// (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
// initialize the library instance
GSMClient client;
GPRS gprs;
GSM gsmAccess;
// URL, path & port (for example: arduino.cc)
char server[] = "mancioboxblog.altervista.org";
char path[] = "/add.php";
int port = 80; // port 80 is the default for HTTP
// check the connection status
int con = 0;
// string to save the temp
String data = "";
// temp random 
long temp;
void setup() {
  //file version
  Serial.println("Version: 1.6");

  // initialize serial communications and wait for port to open:
  Serial.begin(9600);

  /* only for old USB usually not required 
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }*/
  Serial.println("Starting Arduino web client.");
  // connection state
  boolean notConnected = true;
  // After starting the modem with GSM.begin()
  // attach the shield to the GPRS network with the APN, login and password
  while (notConnected) {
    Serial.println("I'm trying to connect");

    if ((gsmAccess.begin(PINNUMBER) == GSM_READY) &
        (gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD) == GPRS_READY)) {
      notConnected = false;
    } else {
      Serial.println("Not connected");
      delay(1000);
    }
  }
  Serial.println("I'm connected");

  // this operation is faster than connect to sim card
  // Start up the temperature sensor library
  Serial.println("initialize sensors");
  sensors.begin();
  // wait the the gsm shield is initialized
  delay(1000); 

}  


void loop() {
  /* GET THE TEMPERATURE */
  // call sensors.requestTemperatures() to issue a global temperature
  // request to all devices on the bus
  Serial.print(" Requesting temperatures...");
  sensors.requestTemperatures(); // Send the command to get temperatures
  Serial.println("DONE");
  Serial.print("Temperature for Device 1 is: ");
  temp = sensors.getTempCByIndex(0);

  Serial.print(temp); // Why "byIndex"? 
  // You can have more than one IC on the same bus. 
  // 0 refers to the first IC on the wire

  //only for test
  //temp = random(33);

  data = "temp=" + (String)temp;
  Serial.println("temp stored = " + data);
  delay(2000);

  con = client.connect(server, port);

  // wait connection engaged
  delay(2000);

  // if you get a connection, report back via serial:
  if (con == 1) {

    Serial.println("connected");
    // Make a HTTP request:
    client.print("POST ");
    client.print(path);
    client.println(" HTTP/1.1");
    client.print("Host: ");
    client.println(server);
    client.println("Content-Type: application/x-www-form-urlencoded; charset=UTF-8");
    client.println("Connection: close");
    client.print("Content-Length: ");
    client.println(data.length());
    client.println("");
    client.println(data);
    client.println("");
    //what I'm sending
    Serial.print("POST ");
    Serial.print(path);
    Serial.println(" HTTP/1.1");
    Serial.print("Host: ");
    Serial.println(server);
    Serial.println("Content-Type: application/x-www-form-urlencoded; charset=UTF-8");
    Serial.println("Connection: close");
    Serial.print("Content-Length: ");
    Serial.println(data.length());
    Serial.println("");
    Serial.println(data);
    Serial.println("");

  // if you didn't get a connection to the server:
  } else if(con == -1){
    Serial.println("connection failed: TIMED_OUT -1");
  } else if(con == -2){
    Serial.println("connection failed: INVALID_SERVER -2");
  } else if(con == -3){
    Serial.println("connection failed: TRUNCATED -3");
  } else if(con == -4){
    Serial.println("connection failed: INVALID_RESPONSE -4");
  }
  if(client.connected()){
    client.stop();  // DISCONNECT FROM THE SERVER
    Serial.println("enter in the if and stop the client");
  }else{
    Serial.println("the client is not connected");
  }

  //keep over 2 second otherwise is not able to close and reopen connection
  delay(2000); // WAIT MINUTES BEFORE SENDING AGAIN
  Serial.println("code repeat");

}

我的代码有问题吗?也许在客户端连接之前我不应该读取传感器?

0 个答案:

没有答案