#include <SPI.h>
#include <Wire.h>
#include <Ethernet.h>
#include <PubSubClient.h>
#include <MQ135.h>
#include <Adafruit_SSD1306.h>
float conPM25;
unsigned long durationPM25;
unsigned long starttime;
unsigned long sampletime_ms = 15000;
unsigned long lowpulseoccupancyPM25 = 0;
uint8_t co2Value = 0;
uint8_t CO2Pin = A0;
uint8_t coValue = 0;
uint8_t analogPinC0 = A1;
uint8_t PM25PIN = 7;
MQ135 co2Sensor = MQ135(CO2Pin);
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for SSD1306 display connected using I2C
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
EthernetClient ethClient;
PubSubClient mqttClient(ethClient);
const char broker[] = "test.mosquitto.org";
void reconnect() {
// Loop until we're reconnected
while (!mqttClient.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (mqttClient.connect("arduinoClient")) {
Serial.println("connected");
// Once connected, publish an announcement...
mqttClient.publish("outTopic","hello world");
// ... and resubscribe
// mqttClient.subscribe("inTopic");
} else {
Serial.print("failed, rc=");
Serial.print(mqttClient.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
pinMode(analogPinC0, INPUT);
pinMode(PM25PIN,INPUT);
// initialize the OLED object
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
delay(2000);
display.clearDisplay();
Serial.println("MQTT Arduino: ");
// start the Ethernet connection:
Serial.println("Initialize Ethernet with DHCP:");
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
} else if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
}
// no point in carrying on, so do nothing forevermore:
while (true) {
delay(1);
}
}
// print your local IP address:
Serial.print("My IP address: ");
Serial.println(Ethernet.localIP());
delay(1500);
mqttClient.setServer( broker, 1883);
}
void loop() {
// put your main code here, to run repeatedly:
if (!mqttClient.connected()) {
reconnect();
}
// call regularly to allow the library to send MQTT keep alive which
// avoids being disconnected by the broker
mqttClient.loop();
}
This Sketch uses 28670 bytes (88%) of program storage space. Maximum is 32256 bytes.
Global variables use 1274 bytes (62%) of dynamic memory, leaving 774 bytes for local variables. Maximum is 2048 bytes.
When I upload it with the crux of the program (the code in the loop function) it excceds to 113% program storage space. But the maximum space being used is here . Need help to optimize this.