Last active
February 7, 2018 11:43
-
-
Save coka-stefan/423b48f51f9d2e6b89952167199690e5 to your computer and use it in GitHub Desktop.
Arduino air station
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <PubSubClient.h> | |
#include <ArduinoJson.h> | |
#include <dht.h> | |
#include <Arduino.h> | |
#include <SPI.h> | |
#include <SoftwareSerial.h> | |
#include <LiquidCrystal.h> | |
#include <Ethernet.h> | |
#define DHT11_PIN 7 | |
#define RX_PIN 2 | |
#define TX_PIN 3 | |
IPAddress ip(169, 169, 100, 98); | |
byte mac[] = { | |
0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 | |
}; | |
const char *mqtt_server = ""; // Add broker address | |
const int mqtt_port = 0; // Set the port | |
const char *mqtt_user = ""; // Add username | |
const char *mqtt_pass = ""; // Add password | |
const char *mqtt_client_name = "arduinoClient1"; // Client connections cant have the same connection name | |
EthernetClient ethClient; | |
PubSubClient client(ethClient); | |
SoftwareSerial pmSerial(RX_PIN, TX_PIN); | |
dht DHT; | |
int pm1; | |
int pm2_5; | |
int pm10; | |
unsigned long id; | |
//File myFile; | |
String s; | |
StaticJsonBuffer<200> jsonBuffer; | |
JsonObject& root = jsonBuffer.createObject(); | |
void setup() { | |
Serial.begin(57600); | |
pmSerial.begin(9600); | |
id = 0; | |
pm1 = 0; | |
pm2_5 = 0; | |
pm10 = 0; | |
if (Ethernet.begin(mac) == 0) | |
{ | |
Serial.println("Failed to configure Ethernet using DHCP"); | |
// attempt with fixed ip addr | |
Ethernet.begin(mac, ip); | |
} | |
client.setServer(mqtt_server, mqtt_port); | |
client.setCallback(callback); | |
delay(2000); | |
Serial.println(Ethernet.localIP()); | |
client.connect("arduinoClient", mqtt_user, mqtt_pass); | |
Serial.print("rc="); | |
Serial.print(client.state()); | |
Serial.print("\n"); | |
} | |
void loop() { | |
int index = 0; | |
char value; | |
char previousValue; | |
if (!client.connected()) | |
{ | |
if (client.connect("arduinoClient", mqtt_user, mqtt_pass)) { | |
Serial.println("connected"); | |
} | |
} | |
while (pmSerial.available()) { | |
value = pmSerial.read(); | |
if ((index == 0 && value != 0x42) || (index == 1 && value != 0x4d)) { | |
Serial.println("Cannot find the data header."); | |
return; | |
} | |
if (index == 4 || index == 6 || index == 8 || index == 10 || index == 12 || index == 14) { | |
previousValue = value; | |
} | |
else if (index == 5) { | |
pm1 = 256 * previousValue + value; | |
root["pm1"] = abs(pm1); | |
} | |
else if (index == 7) { | |
pm2_5 = 256 * previousValue + value; | |
root["pm2_5"] = abs(pm2_5); | |
} | |
else if (index == 9) { | |
pm10 = 256 * previousValue + value; | |
root["pm10"] = abs(pm10); | |
} | |
else if (index > 15) { | |
break; | |
} | |
index++; | |
} | |
while (pmSerial.available()) pmSerial.read(); | |
int chk = DHT.read11(DHT11_PIN); | |
if (DHT.temperature == -999 || DHT.humidity == -999) { | |
root["temperature"] = "N/A"; | |
root["humidity"] = "N/A"; | |
} else { | |
root["temperature"] = DHT.temperature; | |
root["humidity"] = DHT.humidity; | |
} | |
sendResults(); | |
id++; | |
delay(5000); | |
} | |
void sendResults() { | |
// publish to MQTT | |
char jsonChar[100]; | |
root.printTo(jsonChar); | |
Serial.println(client.publish("arduino", jsonChar)); | |
// debug to serial | |
root.printTo(Serial); | |
Serial.print('\n'); | |
} | |
// Handles messages arrived on subscribed topic(s) | |
void callback(char* topic, byte* payload, unsigned int length) { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment