Advice on wireless thermostat project with Arduino MKR GSM 1400

I may have slightly bitten off more than I can chew for my first project, but I have tried to build a wireless thermometer, the long term use will be to monitor the temperature of my fridge. Using this project

and replacing the client code with the code from the example below

I have managed to get temperature data to be published to my Adafruit feed, however, I cannot figure out how to get this to work when the Arduino is plugged into an external power supply rather than my computer. From what I understand the last sketch loaded onto an Arduino will start automatically when it is plugged into power.

Also, do I need to stop the script at any point, or is unplugging the Arduino enough to do this? If I unplug and then plug in the Arduino, I usually have to double click RST before it is recognised by my laptop again.

//---------------Libraries---------------
// DHT Library
#include <DHT.h>;

// Adafruit MQTT Library
#include <Adafruit_MQTT.h>
#include <Adafruit_MQTT_Client.h>
#include <Adafruit_MQTT_FONA.h>

// MKR GSM Library
#include <MKRGSM.h>

//---------------Constants---------------
#define DHTPIN 7     // what pin we're connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302)

// Adafruit IO 
#define AIO_SERVER      "io.adafruit.com" 
#define AIO_SERVERPORT   
#define AIO_USERNAME    "" 
#define AIO_KEY         "" 

// MKR GSM
#define PINNUMBER ""
#define GPRS_APN       "" 
#define GPRS_LOGIN     ""
#define GPRS_PASSWORD  ""

//---------------Variables---------------
int b;
float hum;  //Stores humidity value
float temp; //Stores temperature value

//---------------Initialise---------------
// DHT 
DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino

// MKR GSM
GSMClient client;
GPRS gprs;
GSM gsmAccess;

// Adafruit MQTT Library
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY); // initialize the library instance
Adafruit_MQTT_Publish temperature = Adafruit_MQTT_Publish(&mqtt,  AIO_USERNAME "/feeds/arduinoTemp"); // Setup feed for temperature 

//========================================

// URL, path & port (for example: arduino.cc)
char server[] = "arduino.cc";
char path[] = "/asciilogo.txt";
int port = 80; // port 80 is the default for HTTP

void setup() {
  dht.begin();
  // initialize serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  Serial.println("Starting Arduino web client.");
  // connection state
  bool notConnected = true;

  // After starting the modem with GSM.begin()
  // attach the shield to the GPRS network with the APN, login and password
  while (notConnected) {
    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("connecting...");

  // if you get a connection, report back via serial:
  if (client.connect(server, port)) {
    Serial.println("connected");
    // Make a HTTP request:
    client.print("GET ");
    client.print(path);
    client.println(" HTTP/1.1");
    client.print("Host: ");
    client.println(server);
    client.println("Connection: close");
    client.println();
  } else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}

void loop() 
{ 
 ping_Adafruit(); 
 read_Temperature(); 
 publish_Temperature();
 check_Failure();
 //repeat every 1min 
 delay(60000); 
} 

//---------------Functions---------------

void connect() {
  // connect to adafruit io via MQTT
  Serial.print(F("Connecting to Adafruit IO... "));

  int8_t ret;

  while ((ret = mqtt.connect()) != 0) {

    switch (ret) {
      case 1: Serial.println(F("Wrong protocol")); break;
      case 2: Serial.println(F("ID rejected")); break;
      case 3: Serial.println(F("Server unavail")); break;
      case 4: Serial.println(F("Bad user/pass")); break;
      case 5: Serial.println(F("Not authed")); break;
      case 6: Serial.println(F("Failed to subscribe")); break;
      default: Serial.println(F("Connection failed")); break;
    }

    if(ret >= 0)
      mqtt.disconnect();

    Serial.println(F("Retrying connection..."));
    delay(5000);

  }

  Serial.println(F("Adafruit IO Connected!"));

}

void ping_Adafruit()
{
  // ping adafruit io a few times to make sure we remain connected 
 if(! mqtt.ping(3)) { 
   // reconnect to adafruit io 
   if(! mqtt.connected()) 
     connect(); 
 } 
}

 void read_Temperature() 
{
    // Grab the current state of the sensor 
   temp = dht.readTemperature(); 
   //convert int temp to char array 
   char b[4]; 
   String str; 
   str=String(temp); 
   for(int i=0; i<str.length(); i++) 
   { 
     b[i]=str.charAt(i); 
   } 
   b[(str.length())+1]=0; 
 }

void publish_Temperature()
{
  // Publish data 
 if (!temperature.publish((char*)b)) { 
   Serial.println(F("Failed to publish temp")); 
 } else { 
   Serial.print(F("Temp published: ")); 
   Serial.println(temp); 
 } 
}

void check_Failure()
{
  // Check if any reads failed and exit early (to try again). 
 if (isnan(temp)) { 
   Serial.println("Failed to read from DHT sensor!"); 
   return; 
 } 
}

From what I understand the last sketch loaded onto an Arduino will start automatically when it is plugged into power.

Correct.

Also, do I need to stop the script at any point, or is unplugging the Arduino enough to do this?

You cannot stop it other than removing power.

What exactly is your problem?

pylon:
What exactly is your problem?

I can successfully get the Arduino to send temperature data to Adafruit when it is plugged into my computer, and I upload the sketch directly. But I cannot figure out how to get it to work when it is plugged into an external power source.

I am not sure if I am searching for the words keywords, but have not found any troubleshooting tips on this. I have found that after removing the Arduino from computer power, I have to reset it a few times before I can get it connected again to upload another sketch.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html .
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

How do you power the Arduino when USB is disconnected?

Thanks.. Tom.. :slight_smile:

Post a wiring diagram of your setup that shows the external powering!