Charles London
Published © GPL3+

Tweeting Dehumidifier

Tweeting Dehumidifier project using Sparkfun ESP8266 Thing and thingspeak.com

IntermediateProtip5 hours916
Tweeting Dehumidifier

Things used in this project

Hardware components

SparkFun ESP8266 Thing - Dev Board
SparkFun ESP8266 Thing - Dev Board
Pretty much all you need for this project!
×1

Software apps and online services

ThingSpeak API
ThingSpeak API
Twitter
Twitter

Story

Read more

Code

Tweeting Dehumidifier

Arduino
This Arduino sketch is used to monitor a Dehumidifier's water level switch and send a tweet when the bucket is full.
/****************************************************************************
Tweeting Dehumidifier
A project by 2BitBot

Most of the code below was acquired from online documentation and demonstration projects
I basically just cut and paste from Sparkfun's Hookup guide documentation 
  https://learn.sparkfun.com/tutorials/esp8266-thing-hookup-guide?_ga=1.81449239.1417305721.1449861122
and ThingSpeak documentation
  http://community.thingspeak.com/tutorials/arduino/send-data-to-thingspeak-with-arduino/

Arduino Setup
  Connect contact switch leads to PIN 12 and Ground. (Done!)

*/
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>

ESP8266WiFiMulti WiFiMulti;

//***************************************************************************

// Connection Variables
const char WiFiSSID[] = "ASTROBLAST";    //Network name
const char WiFiPSK[]  = "MyPassWord";    //Network password

// Pin Definitions
const int LED_PIN = 5;        // Thing's onboard, green LED
const int ANALOG_PIN = A0;    // The only analog pin on the Thing
const int DIGITAL_PIN = 12;   // Digital pin to be read

bool SwitchState_Hist = LOW;  // Remembers the last switch state
                              //  so it only sends tweet when a change is detected  

//***************************************************************************
void setup() {
  // put your setup code here, to run once: 
 bool Led_state = LOW;

    pinMode(DIGITAL_PIN, INPUT_PULLUP);  // Set pin 12 as an input w/ pull-up
    pinMode(LED_PIN, OUTPUT);            // Setup the green LED pin for output
    digitalWrite(LED_PIN, LOW);          // Turn off the green LED

    Serial.begin(115200);
    delay(10);

    // We start by connecting to a WiFi network
    WiFiMulti.addAP(WiFiSSID, WiFiPSK);

    Serial.println();
    Serial.println();
    Serial.print("Wait for WiFi... ");

    while(WiFiMulti.run() != WL_CONNECTED) {
        Serial.print(".");
        delay(500);
        Led_state = !Led_state;
        digitalWrite(LED_PIN,Led_state);
    }

    Serial.println("");
    Serial.println("WiFi connected");
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());

    delay(500);
 
}


//**************************************************************************************
void loop() {

  bool SwitchState = digitalRead(DIGITAL_PIN);            // HIGH = Empty, LOW = Full
  
  if(SwitchState!=SwitchState_Hist){                      // If switch state change detected 
    SwitchState_Hist = SwitchState;                       //  Record current switch state for history
    if(SwitchState==LOW) {                                // If Bucket is now Full
      digitalWrite(LED_PIN, HIGH);                        //   Turn on LED
      tweetThis("Arduino: Help my bucket is full!");      //    Send Tweet
      
    } else {                                                //  Else the bucket is now empty
      tweetThis("Arduino: Bucket is empty! Back to work."); //  Send empty tweet
      digitalWrite(LED_PIN, LOW);                           //  Turn off LED
    }
  }                                                         // That's it!
  
  delay(500);
  yield(); // Do nothing
}

//***************************************************************************************
void tweetThis(String myTweet) {
  //All the details below came from ThingSpeak documentation
  byte server[]  = { 184, 106, 153, 149 }; // ThingSpeak IP Address: 184.106.153.149

    // Use WiFiClient class to create TCP connections
    WiFiClient client;

    if (!client.connect(server, 80)) {
        Serial.println("connection failed");
        Serial.println("wait 5 sec...");
        delay(5000);
        return;
    } else {

      // Create HTTP POST Data
      // Note I add Millis to the tweet to make it unique.  Twitter does not allow the 
      //  same tweet to be sent multiple times..
      myTweet = "api_key={My API KEY}&status="+myTweet+" "+String(millis(),DEC);
      
      client.print("POST /apps/thingtweet/1/statuses/update HTTP/1.1\n");
      client.print("Host: api.thingspeak.com\n");
      client.print("Connection: close\n");
      client.print("Content-Type: application/x-www-form-urlencoded\n");
      client.print("Content-Length: ");
      client.print(myTweet.length());
      client.print("\n\n");

      client.print(myTweet);
    
      //read back one line from server
      String line = client.readStringUntil('\r');
      //client.println(line);
      Serial.println(line);

      Serial.println("closing connection");
      client.stop();
    
      Serial.println("wait 5 sec...");
      delay(5000);
  }
}

Credits

Charles London

Charles London

1 project • 0 followers
Hobby Robotics, electronics and Radio

Comments