Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. #include <Adafruit_Sensor.h>
  2. #include <ESP8266WiFi.h>
  3. #include <PubSubClient.h>
  4. #include <Arduino.h>
  5. #include <U8g2lib.h>
  6. #include <RH_ASK.h>
  7.  
  8. RH_ASK driver(2000, 2,5, 0);
  9.  
  10. #ifdef U8X8_HAVE_HW_SPI
  11. #include <SPI.h>
  12. #endif
  13. #ifdef U8X8_HAVE_HW_I2C
  14. #include <Wire.h>
  15. #endif
  16.  
  17. U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
  18.  
  19. uint8_t buf[32];
  20. uint8_t buflen = 32;
  21.  
  22. const char* ssid = "Nackademin-IOT";
  23. const char* password = "Nack@!pr";
  24. const char* mqtt_server = "172.20.216.18"; //Raspberry mac: dc:a6:32:0a:e9:10
  25.  
  26. const char* subscribeTopic = "iot/publish";
  27. const char* publishTopic = "iot/publish";
  28.  
  29. WiFiClient espClient;
  30. PubSubClient client(espClient);
  31.  
  32. long lastMsg = 0;
  33. char msg[50];
  34.  
  35. void setup_wifi() {
  36.  
  37. delay(10);
  38. // We start by connecting to a WiFi network
  39. Serial.println();
  40. Serial.print("Connecting to ");
  41. Serial.println(ssid);
  42.  
  43. WiFi.begin(ssid, password);
  44.  
  45. while (WiFi.status() != WL_CONNECTED) {
  46. delay(500);
  47. Serial.print(".");
  48. }
  49.  
  50. randomSeed(micros()); //Viktigt för att alla inte ska kopplas upp samtidigt.
  51.  
  52. Serial.println("");
  53. Serial.println("WiFi connected");
  54. Serial.println("IP address: ");
  55. Serial.println(WiFi.localIP());
  56. }
  57.  
  58. void callback(char* topic, byte* payload, unsigned int length) {
  59. //Serial.print("Message arrived [");
  60. //Serial.print(topic);
  61. //Serial.print("] ");
  62. char annanCharArray[50];
  63. for (int i = 0; i < length; i++) {
  64. //Serial.print((char)payload[i]);
  65. annanCharArray[i] = payload[i];
  66. }
  67.  
  68. //Serial.println(annanCharArray);
  69. // Här kan man skriva ut på displayen. Spara payload i en annan chararray.
  70. u8g2.clearBuffer(); // clear the internal memory
  71. u8g2.setFont(u8g2_font_ncenB08_tr); // choose a suitable font
  72. u8g2.drawStr(0,10, (char*) annanCharArray); // write something to the internal memory
  73. u8g2.sendBuffer(); // transfer internal memory to the display
  74. //}
  75. //Serial.println();
  76. }
  77.  
  78. void reconnect() {
  79. while (!client.connected()) {
  80. String clientId = "ESP8266Client-";
  81. clientId += String(random(0xffff), HEX);
  82. if (client.connect(clientId.c_str())) {
  83. client.subscribe(subscribeTopic);
  84. } else {
  85. delay(5000);
  86. }
  87. }
  88. }
  89.  
  90. void setup() {
  91. driver.init();
  92. u8g2.begin();
  93. Serial.begin(115200);
  94. setup_wifi();
  95. client.setServer(mqtt_server, 1883);
  96. client.setCallback(callback);
  97. }
  98.  
  99. void loop() {
  100. if (!client.connected()) {
  101. reconnect();
  102. }
  103.  
  104. /*if(driver.recv(buf, &buflen)) {
  105. Serial.println((char*)buf);
  106. }*/
  107. client.loop();
  108.  
  109. long now = millis();
  110. if(driver.recv(buf, &buflen)) {
  111. if(now - lastMsg > 2000) {
  112. lastMsg = now;
  113. snprintf (msg, 50, "Temp: ");
  114. uint8 *uint8_pointer = buf;
  115. const char *char_pointer = (char*)uint8_pointer;
  116. strcat(msg, char_pointer);
  117. client.publish(publishTopic, msg);
  118. }
  119. }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement