-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAirQualityArduino.ino
78 lines (65 loc) · 1.99 KB
/
AirQualityArduino.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
const int sensor=A0; // Assigning analog pin A0 to variable 'sensor'
float tempc; //variable to store temperature in degree Celsius
float tempf; //variable to store temperature in Fahreinheit
float vout; //temporary variable to hold sensor reading
//-------- Customise these values ----------
const char* ssid = "PC-Faster-2049";
const char* password = "pcf26143";
#define ORG "jwczs2"
#define DEVICE_TYPE "mydevice"
#define DEVICE_ID "nodemcu1"
#define TOKEN "0kjkChN)+lw!j*MbNc"
//-------- Customise the above values --------
char server[] = ORG ".messaging.internetofthings.ibmcloud.com";
char topic[] = "iot-2/evt/Data/fmt/json";
char authMethod[] = "use-token-auth";
char token[] = TOKEN;
char clientId[] = "d:" ORG ":" DEVICE_TYPE ":" DEVICE_ID;
WiFiClient wifiClient;
PubSubClient client(server, 1883,wifiClient);
void setup() {
Serial.begin(115200);
Serial.println();
pinMode(sensor,INPUT);
Serial.print("Connecting to ");
Serial.print(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("WiFi connected, IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
vout=analogRead(sensor);
vout=(vout*500)/1024;
tempc=vout; // Storing value in Degree Celsius
tempf=(vout*1.8)+32; // Converting to Fahrenheit
PublishData(tempf);
delay(10000);
}
void PublishData(float temp){
if (!!!client.connected()) {
Serial.print("Reconnecting client to ");
Serial.println(server);
while (!!!client.connect(clientId, authMethod, token)) {
Serial.print(".");
delay(500);
}
Serial.println();
}
String payload = "{\"d\":{\"temperature\":";
payload += temp;
payload += "}}";
Serial.print("Sending payload: ");
Serial.println(payload);
if (client.publish(topic, (char*) payload.c_str())) {
Serial.println("Publish ok");
} else {
Serial.println("Publish failed");
}
}