Advertisement
Guest User

Untitled

a guest
May 9th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. #include <ESP8266WiFi.h> //Required Library
  2. #include <PubSubClient.h> //Required Library
  3.  
  4. const char* ssid = "Chirag";
  5. const char* password = "welcome123";
  6. const char* mqttServer = "ec2-18-217-227-236.us-east-2.compute.amazonaws.com"; //I3 broker
  7. const int mqttPort = 1883; //Your port number
  8. const char* mqttUser = "chirag";
  9. const char* mqttPassword = "garethbale7";
  10.  
  11.  
  12. WiFiClient ESPClient;
  13. PubSubClient client(ESPClient);
  14.  
  15. void setup() {
  16.  
  17. Serial.begin(115200);
  18.  
  19.  
  20. delay(500);
  21. WiFi.begin(ssid, password);
  22.  
  23. while (WiFi.status() != WL_CONNECTED) {
  24. delay(500);
  25. Serial.println("USC AIR QUALITY MONITOR");
  26. Serial.println("Initializing......................");
  27. Serial.println("Please wait while connecting to WiFi");
  28. }
  29.  
  30. Serial.println("Connected to WiFi");
  31. client.setServer(mqttServer, mqttPort);
  32. while (!client.connected()) {
  33. Serial.println("Connecting to MQTT...");
  34.  
  35. if (client.connect("USC", mqttUser, mqttPassword )) {
  36.  
  37. Serial.println("connected to USC I3 BROKER");
  38.  
  39. } else {
  40.  
  41. Serial.print("ERROR > failed with state ");
  42. Serial.print(client.state());
  43. delay(2000);
  44.  
  45. }
  46. }
  47.  
  48. client.publish("USC-TEST", "Message from ESP8266/ESP32");
  49.  
  50. }
  51.  
  52. void loop() {
  53. client.loop();
  54.  
  55. int h = analogRead(A0);
  56. delay(1000);
  57. String msg = String(h);
  58. Serial.println("Air Quality = ");
  59. Serial.println(msg);
  60. int numh = h;
  61. char cshr[16];
  62. itoa(numh, cshr, 10);
  63.  
  64.  
  65.  
  66. delay(1500);
  67. client.publish("Air Quality(ppm) = ", cshr);
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement