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;
}
}