Advertisement
Guest User

Untitled

a guest
May 25th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.48 KB | None | 0 0
  1. /*
  2. * Pin Config
  3. * MOSI -> 13
  4. * MISO -> 12
  5. * SCK -> 14
  6. * CS -> 16
  7. */
  8.  
  9. #include <ESP8266WiFi.h>
  10. #include <PubSubClient.h>
  11. #include <DHT.h>
  12. #include <SPI.h>
  13. #include <SD.h>
  14.  
  15. #define DHTPIN 4
  16. #define DHTTYPE DHT11
  17. DHT dht(DHTPIN, DHTTYPE);
  18.  
  19. // Update these with values suitable for your network.
  20. const char* ssid = "****";
  21. const char* password = "***********";
  22.  
  23. //Update your mqtt server details
  24. const char* mqtt_server = "m12.cloudmqtt.com";
  25. const char* mqtt_user = "vzvaucdl";
  26. const char* mqtt_pass = "NqNXasG0Fit3";
  27. //
  28.  
  29. //File to store details in SD card
  30. File myFile;
  31.  
  32. WiFiClient espClient;
  33. PubSubClient client(espClient);
  34.  
  35. long lastMsg = 0;
  36. char msg[50];
  37. int value = 0;
  38.  
  39. String file_temp;
  40.  
  41. void setup_wifi() {
  42.  
  43. delay(10);
  44. // We start by connecting to a WiFi network
  45. Serial.println();
  46. Serial.print("Connecting to ");
  47. Serial.println(ssid);
  48.  
  49. WiFi.begin(ssid, password);
  50.  
  51. if (WiFi.status() != WL_CONNECTED) {
  52. Serial.print("..");
  53. delay(5000);
  54. }
  55. //Serial.println(WiFi.status());
  56. randomSeed(micros());
  57. if(WiFi.status() == WL_CONNECTED){
  58. Serial.println("");
  59. Serial.println("WiFi connected");
  60. Serial.println("IP address: ");
  61. Serial.println(WiFi.localIP());
  62. }
  63. }
  64.  
  65. void callback(char* topic, byte* payload, unsigned int length) {
  66. Serial.print("Message arrived [");
  67. Serial.print(topic);
  68. Serial.print("] ");
  69. for (int i = 0; i < length; i++) {
  70. Serial.print((char)payload[i]);
  71. }
  72. Serial.println();
  73.  
  74. // Switch on the LED if an 1 was received as first character
  75. if ((char)payload[0] == '1') {
  76. digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level
  77. // but actually the LED is on; this is because
  78. // it is acive low on the ESP-01)
  79. } else {
  80. digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH
  81. }
  82.  
  83. }
  84.  
  85. void reconnect() {
  86. // Loop until we're reconnected
  87. while (!client.connected()) {
  88. Serial.print("Attempting MQTT connection...");
  89. // Create a random client ID
  90. String clientId = "ESP8266Client";
  91. //clientId += String(random(0xffff), HEX);
  92. // Attempt to connect
  93. if (client.connect("ESP8266", mqtt_user, mqtt_pass)) {
  94. Serial.println("connected");
  95. // Once connected, publish an announcement...
  96. // ... and resubscribe
  97. client.subscribe("Temperature");
  98. } else {
  99. Serial.print("failed, rc=");
  100. Serial.print(client.state());
  101. Serial.println(" try again in 5 seconds");
  102. // Wait 5 seconds before retrying
  103. delay(5000);
  104. }
  105. }
  106. }
  107.  
  108. void setup() {
  109. pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output
  110. Serial.begin(9600);
  111. setup_wifi();
  112. client.setServer(mqtt_server, 12119);
  113. client.setCallback(callback);
  114. if (!SD.begin(16)) {
  115. Serial.println("SD card initialization failed!");
  116. return;
  117. }
  118. Serial.println("SD card initialization done.");
  119. SD.remove("temp.txt");
  120. }
  121.  
  122. void loop() {
  123.  
  124. //Reading the sensor value for intervals of 2s
  125. long now = millis();
  126. if (now - lastMsg > 2000) {
  127. lastMsg = now;
  128. int c = dht.readTemperature();
  129.  
  130. Serial.print("Temp: ");
  131. Serial.print(c);
  132. Serial.print("\t");
  133. sprintf (msg, "Temp: %d", c);
  134.  
  135. //Checking for WiFi connection
  136. if(WiFi.status() == WL_CONNECTED){
  137. if (!client.connected()) {
  138. reconnect();
  139. }
  140. client.loop();
  141. //Checking for any previous data in the SD card and publishing them to server
  142. if(SD.exists("temp.txt")){
  143. int i=0;
  144. myFile = SD.open("temp.txt", FILE_WRITE);
  145. myFile.println("");
  146. myFile.close();
  147. delay(100);
  148. myFile = SD.open("temp.txt");
  149.  
  150. if(myFile.available()){
  151. String line = myFile.readStringUntil('\n');
  152. for(i=0;i<line.length();i+=2){
  153. String t = line.substring(i,i+2);
  154. sprintf(msg, "Temp: %s", t.c_str());
  155. client.publish("Temperature", msg);
  156. }
  157. }
  158. client.publish("Temperature", msg);
  159. myFile.close();
  160. SD.remove("temp.txt");
  161. }
  162. //Publishing realtime data while WiFi is connected
  163. Serial.print("Publish message: ");
  164. Serial.println(msg);
  165. client.publish("Temperature", msg);
  166.  
  167. }
  168. //Writing to SD card when no WiFi connection is present
  169. else{
  170. myFile = SD.open("temp.txt", FILE_WRITE);
  171. if (myFile) {
  172. Serial.print("Writing to temp.txt...");
  173. myFile.print(c);
  174. // close the file:
  175. myFile.close();
  176. Serial.println("done.");
  177. setup_wifi();
  178. }
  179. }
  180. }
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement