|
// libraries
#include <MKRGSM.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
// Define Sensors Pins
#define DHTPIN 1
#define LDR A1
#define MQ2 A2
#define MQ135 A3
//Thing Speak Write Key
String ThingSpeakWriteKey="-----ThingSpeakWriteKey----------";
#define DHTTYPE DHT11
DHT_Unified dht(DHTPIN, DHTTYPE);
uint32_t delayMS;
String Values;
String MQ2_Val,MQ135_Val,LDR_Val;
const char PINNUMBER[] = " ";
// APN data
const char GPRS_APN[] = "hologram";
const char GPRS_LOGIN[] = " ";
const char GPRS_PASSWORD[] = " ";
//Hologram Config
String HOLOGRAM_DEVICE_KEY = "----Hologram Deveice Key--------";
String HOLOGRAM_TOPIC = "MKR1400";
// initialize the library instance
GSMClient client;
GPRS gprs;
GSM gsmAccess;
// Hologram's Embedded API (hologram點io/docs/reference/cloud/embedded/) URL and port
char server[] = "cloudsocket.hologram.io";
int port = 9999;
void setup() {
// initialize serial communications and wait for port to open:
Serial.begin(9600);
dht.begin();
Serial.println("Starting Arduino web client.");
// connection state
boolean connected = false;
// After starting the modem with GSM.begin()
// attach to the GPRS network with the APN, login and password
while (!connected) {
Serial.println("Begin GSM Access");
if ((gsmAccess.begin() == GSM_READY) &&
(gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD) == GPRS_READY)) {
connected = true;
Serial.println("GSM Access Success");
}
else {
Serial.println("Not connected");
delay(1000);
}
}
delay(5000);
}
void loop() {
Values="api_key="+ThingSpeakWriteKey+"&";
//Values="";
delay(delayMS);
// Get temperature event and print its value.
sensors_event_t event;
dht.temperature().getEvent(&event);
if (isnan(event.temperature)) {
Serial.println("Error reading temperature!");
Values+="field2=0&";
}
else {
Serial.print("Temperature: ");
Serial.print(event.temperature);
Serial.println(" *C");
Values+="field2="+String(event.temperature)+"&";
}
// Get humidity event and print its value.
dht.humidity().getEvent(&event);
if (isnan(event.relative_humidity)) {
Serial.println("Error reading humidity!");
Values+="field3=0&";
}
else {
Serial.print("Humidity: ");
Serial.print(event.relative_humidity);
Serial.println("%");
Values+="field3="+String(event.relative_humidity)+"&";
}
//Get Light Level
Serial.print("Light Level: ");
LDR_Val=String(analogRead(LDR));
Serial.println(LDR_Val);
Values+="field1="+LDR_Val+"&";
//Get Smoke Level
MQ2_Val=String(analogRead(MQ2));
Serial.print("MQ2-Gas Level: ");
Serial.println(MQ2_Val);
Values+="field4="+MQ2_Val+"&";
//Get Environment Level
MQ135_Val=String(analogRead(MQ135));
Serial.print("MQ135-Env Level: ");
Serial.println(MQ135_Val);
Values+="field5="+MQ135_Val;
//Send the Data to Hologram Network
if (client.connect(server, port)) {
Serial.println("connected");
// Send a Message request:
client.println("{\"k\":\"" + HOLOGRAM_DEVICE_KEY +"\",\"d\":\""+ Values+ "\",\"t\":\""+HOLOGRAM_TOPIC+"\"}");
} else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
client.stop();
delay(10000);
} |
|