Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <PubSubClient.h>
  3. #include <RH_ASK.h>
  4. #include <SPI.h>
  5. #include <Arduino.h>
  6. #include <U8x8lib.h>
  7.  
  8. #ifdef U8X8_HAVE_HW_SPI
  9. #endif
  10.  
  11. U8X8_SH1106_128X64_NONAME_4W_HW_SPI u8x8(/* cs=*/ 10, /* dc=*/ 9, /* reset=*/ 8);
  12.  
  13.  
  14. long lastMsg;
  15. int value;
  16. char* msg;
  17. uint8_t buf[16];
  18. uint8_t buflen = sizeof(buf);
  19. String str_temp;
  20. String str_humid;
  21. char *hum;
  22. char *temp;
  23. RH_ASK driver(2000, 12, 4, 0);
  24.  
  25. const char *ssid = "AndroidAP";
  26. const char *password = "52105210";
  27. //const char* ssid = "Nackademin-IOT";
  28. //const char* password = "Nack!@pr";
  29. const char* mqtt_server = "192.168.43.252";
  30.  
  31. String clientId = "ESP8266Client-feather";
  32.  
  33.  
  34. const char subscribeTopic[] = "iot/subscribe";
  35. const char publishTopic[] = "iot/publish";
  36.  
  37. WiFiClient espClient;
  38. PubSubClient client(espClient);
  39.  
  40. void initWifi() {
  41. delay(10);
  42. WiFi.begin(ssid, password);
  43.  
  44. while (WiFi.status() != WL_CONNECTED) {
  45. delay(500);
  46. Serial.print(".");
  47. }
  48.  
  49. randomSeed(micros());
  50. Serial.println(WiFi.localIP());
  51. }
  52.  
  53. void reconnect() {
  54. while (!client.connected()) {
  55. Serial.print("Attempting MQTT connection...");
  56.  
  57. if (client.connect(clientId.c_str())) {
  58. Serial.println("connected to MQTT Broker");
  59. client.subscribe(subscribeTopic);
  60. } else {
  61. delay(5000);
  62. }
  63. }
  64. }
  65.  
  66. void callback(char* topic, byte* payload, unsigned int length) {
  67. for (int i = 0; i < length; i++) {
  68. Serial.print((char)payload[i]);
  69. }
  70. Serial.println();
  71. }
  72.  
  73. void setup() {
  74. Serial.begin(115200);
  75. driver.init();
  76. initWifi();
  77. client.setServer(mqtt_server, 1883);
  78. client.setCallback(callback);
  79. u8x8.begin();
  80. u8x8.setPowerSave(0);
  81.  
  82.  
  83. }
  84.  
  85. void loop() {
  86. if (driver.recv(buf, &buflen)) {
  87. String str_out = String((char*)buf);
  88. for (int i = 0; i < str_out.length(); i++) {
  89. if (str_out.substring(i, i + 1) == ",") {
  90. str_humid = str_out.substring(i + 1);
  91. str_temp = str_out.substring(0, i);
  92. break;
  93. }
  94. }
  95. Serial.print("Temperature: ");
  96. Serial.println(str_temp);
  97. Serial.print("Humidity: ");
  98. Serial.println(str_humid);
  99. }
  100.  
  101. if (!client.connected()) {
  102. reconnect();
  103. }
  104. client.loop();
  105.  
  106. str_humid.toCharArray(hum,str_humid.length());
  107. str_temp.toCharArray(temp,str_temp.length());
  108.  
  109. long now = millis();
  110. if (now - lastMsg > 2000) {
  111. lastMsg = now;
  112. ++value;
  113. snprintf (msg, 50, "Hej hej #%ld", value);
  114. client.publish(publishTopic, msg);
  115. client.publish(publishTopic, hum);
  116. client.publish(publishTopic, temp);
  117. }
  118. u8x8.setFont(u8x8_font_chroma48medium8_r);
  119. u8x8.drawString(0,0,"Hello World!");
  120. //u8x8.refreshDisplay(); // only required for SSD1606/7
  121. delay(2000);
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement