Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h> //Required Library
- #include <PubSubClient.h> //Required Library
- const char* ssid = "Chirag";
- const char* password = "welcome123";
- const char* mqttServer = "ec2-18-217-227-236.us-east-2.compute.amazonaws.com"; //I3 broker
- const int mqttPort = 1883; //Your port number
- const char* mqttUser = "chirag";
- const char* mqttPassword = "garethbale7";
- WiFiClient ESPClient;
- PubSubClient client(ESPClient);
- void setup() {
- Serial.begin(115200);
- delay(500);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.println("USC AIR QUALITY MONITOR");
- Serial.println("Initializing......................");
- Serial.println("Please wait while connecting to WiFi");
- }
- Serial.println("Connected to WiFi");
- client.setServer(mqttServer, mqttPort);
- while (!client.connected()) {
- Serial.println("Connecting to MQTT...");
- if (client.connect("USC", mqttUser, mqttPassword )) {
- Serial.println("connected to USC I3 BROKER");
- } else {
- Serial.print("ERROR > failed with state ");
- Serial.print(client.state());
- delay(2000);
- }
- }
- client.publish("USC-TEST", "Message from ESP8266/ESP32");
- }
- void loop() {
- client.loop();
- int h = analogRead(A0);
- delay(1000);
- String msg = String(h);
- Serial.println("Air Quality = ");
- Serial.println(msg);
- int numh = h;
- char cshr[16];
- itoa(numh, cshr, 10);
- delay(1500);
- client.publish("Air Quality(ppm) = ", cshr);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement