Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Pin Config
- * MOSI -> 13
- * MISO -> 12
- * SCK -> 14
- * CS -> 16
- */
- #include <ESP8266WiFi.h>
- #include <PubSubClient.h>
- #include <DHT.h>
- #include <SPI.h>
- #include <SD.h>
- #define DHTPIN 4
- #define DHTTYPE DHT11
- DHT dht(DHTPIN, DHTTYPE);
- // Update these with values suitable for your network.
- const char* ssid = "****";
- const char* password = "***********";
- //Update your mqtt server details
- const char* mqtt_server = "m12.cloudmqtt.com";
- const char* mqtt_user = "vzvaucdl";
- const char* mqtt_pass = "NqNXasG0Fit3";
- //
- //File to store details in SD card
- File myFile;
- WiFiClient espClient;
- PubSubClient client(espClient);
- long lastMsg = 0;
- char msg[50];
- int value = 0;
- String file_temp;
- void setup_wifi() {
- delay(10);
- // We start by connecting to a WiFi network
- Serial.println();
- Serial.print("Connecting to ");
- Serial.println(ssid);
- WiFi.begin(ssid, password);
- if (WiFi.status() != WL_CONNECTED) {
- Serial.print("..");
- delay(5000);
- }
- //Serial.println(WiFi.status());
- randomSeed(micros());
- if(WiFi.status() == WL_CONNECTED){
- Serial.println("");
- Serial.println("WiFi connected");
- Serial.println("IP address: ");
- Serial.println(WiFi.localIP());
- }
- }
- void callback(char* topic, byte* payload, unsigned int length) {
- Serial.print("Message arrived [");
- Serial.print(topic);
- Serial.print("] ");
- for (int i = 0; i < length; i++) {
- Serial.print((char)payload[i]);
- }
- Serial.println();
- // Switch on the LED if an 1 was received as first character
- if ((char)payload[0] == '1') {
- digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level
- // but actually the LED is on; this is because
- // it is acive low on the ESP-01)
- } else {
- digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH
- }
- }
- void reconnect() {
- // Loop until we're reconnected
- while (!client.connected()) {
- Serial.print("Attempting MQTT connection...");
- // Create a random client ID
- String clientId = "ESP8266Client";
- //clientId += String(random(0xffff), HEX);
- // Attempt to connect
- if (client.connect("ESP8266", mqtt_user, mqtt_pass)) {
- Serial.println("connected");
- // Once connected, publish an announcement...
- // ... and resubscribe
- client.subscribe("Temperature");
- } else {
- Serial.print("failed, rc=");
- Serial.print(client.state());
- Serial.println(" try again in 5 seconds");
- // Wait 5 seconds before retrying
- delay(5000);
- }
- }
- }
- void setup() {
- pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output
- Serial.begin(9600);
- setup_wifi();
- client.setServer(mqtt_server, 12119);
- client.setCallback(callback);
- if (!SD.begin(16)) {
- Serial.println("SD card initialization failed!");
- return;
- }
- Serial.println("SD card initialization done.");
- SD.remove("temp.txt");
- }
- void loop() {
- //Reading the sensor value for intervals of 2s
- long now = millis();
- if (now - lastMsg > 2000) {
- lastMsg = now;
- int c = dht.readTemperature();
- Serial.print("Temp: ");
- Serial.print(c);
- Serial.print("\t");
- sprintf (msg, "Temp: %d", c);
- //Checking for WiFi connection
- if(WiFi.status() == WL_CONNECTED){
- if (!client.connected()) {
- reconnect();
- }
- client.loop();
- //Checking for any previous data in the SD card and publishing them to server
- if(SD.exists("temp.txt")){
- int i=0;
- myFile = SD.open("temp.txt", FILE_WRITE);
- myFile.println("");
- myFile.close();
- delay(100);
- myFile = SD.open("temp.txt");
- if(myFile.available()){
- String line = myFile.readStringUntil('\n');
- for(i=0;i<line.length();i+=2){
- String t = line.substring(i,i+2);
- sprintf(msg, "Temp: %s", t.c_str());
- client.publish("Temperature", msg);
- }
- }
- client.publish("Temperature", msg);
- myFile.close();
- SD.remove("temp.txt");
- }
- //Publishing realtime data while WiFi is connected
- Serial.print("Publish message: ");
- Serial.println(msg);
- client.publish("Temperature", msg);
- }
- //Writing to SD card when no WiFi connection is present
- else{
- myFile = SD.open("temp.txt", FILE_WRITE);
- if (myFile) {
- Serial.print("Writing to temp.txt...");
- myFile.print(c);
- // close the file:
- myFile.close();
- Serial.println("done.");
- setup_wifi();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement