Things used in this project

Hardware components:
Msp exp430f5529lp 5529 lp angle%5b1%5d
TI LaunchPad MSP-EXP430F5529LP MSP430 LaunchPad
×1
Everything ESP Crowtail Serial WiFi
×1
110020004 20100
SeeedStudio Grove Starter Kit for LaunchPad
×1
Software apps and online services:
Energiabadgelogo2
TI LaunchPad Energia
Hackster
ThingSpeak API

Code

crowtailCode.inoC/C++
Code used to run the Crow Tail WiFi to serial as either a webserver or to publsih data to thing speak.
#include <DHT.h>

#define DEBUG true //Prints AT responses to serial terminal, set to False if you do not want to print
#define tempHumidity 23 //Input analog pin for the temp humidity sensor
#define button1 PUSH1 
#define button2 PUSH2
#define LED2 GREEN_LED
#define LED RED_LED

String wifiSSID= "XXXXXXX"; //Set this to your WiFi SSID
String wifiKey = "XXXXXXXXX"; //Set this to your WiFi password
String apiKey = "XXXXXXX"; //API key from thing speak, used to post data
DHT dht(tempHumidity,DHT22); //Declare the temp/humidity sensor.  DHT22 is the type of sensor.  The blue sensor is the DHT11

void setup()
{
  Serial.begin(115200);
  Serial1.begin(115200);
  dht.begin();
  delay(100);
  pinMode(button1,INPUT_PULLUP);
  pinMode(button2,INPUT_PULLUP);
  pinMode(LED,OUTPUT);
  pinMode(LED2,OUTPUT);
  digitalWrite(LED,LOW);
  digitalWrite(LED2,LOW);
  Serial.println("Press S1 to enter Server mode");
  Serial.println("Press S2 to publish data to ThingSpeak");
}

void loop()
{
  if(!digitalRead(button1)){ //If entering server mode
  digitalWrite(LED2,LOW);
    for(int i = 0; i<4;i++){ //Blink LED to indicate something is happening
      digitalWrite(LED,LOW);
      delay(100);
      digitalWrite(LED,HIGH);
      delay(100);
    }
    sendData("AT+RST\r\n",2000,DEBUG); // reset module
    delay(3000);
    sendData("AT+CWMODE=2\r\n",1000,DEBUG); // configure as access point
    sendData("AT+CIFSR\r\n",1000,DEBUG); // get ip address
    sendData("AT+CIPMUX=1\r\n",1000,DEBUG); // configure for multiple connections
    sendData("AT+CIPSERVER=1,80\r\n",1000,DEBUG); // turn on server on port 80
    serverMode(); //Run the code to act as a web server
  }
  if(!digitalRead(button2)){ //if entering thing speak mode
  digitalWrite(LED,LOW);
    for(int i = 0; i<4;i++){ //Blink LED to indicate something is happening
      digitalWrite(LED2,LOW);
      delay(100);
      digitalWrite(LED2,HIGH);
      delay(100);
    }
    sendData("AT+RST\r\n",2000,DEBUG); // reset module
    delay(3000);
    sendData("AT+CWMODE=3\r\n",1000,DEBUG); //configure to access AP
    Serial.println("Attempting to connect to WiFi");
    String cnnct = ("AT+CWJAP=\"" + wifiSSID + "\",\"" + wifiKey +"\"\r\n");  //Set up string to connect to wifi
    String response = sendData(cnnct,20000,DEBUG); //Connect to wifi using credentials provided
    while (response.indexOf("FAIL") != -1){ //If connection failed, try again
      Serial.println("Attempting to connect to WiFi");
      response = sendData(cnnct,20000,DEBUG);
    }
    thingSpeak(); //enter into thing speak mode
  }
  else delay(50);  
}

void serverMode(){
  while(digitalRead(button2)){
    if(Serial1.available()) // check if the esp is sending a message 
    {
      
      while(Serial1.available())
      {
        // The esp has data so display its output to the serial window 
        char c = Serial1.read(); // read the next character.
        Serial.write(c);
      } 
      
      if(Serial1.find("+IPD,"))
      {
       delay(1000);
   
       int connectionId = Serial1.read()-48; // subtract 48 because the read() function returns 
                                             // the ASCII decimal value and 0 (the first decimal number) starts at 48
       int temperature = dht.readTemperature(true);// Set to false if want to read Celcius
       int humidity = dht.readHumidity();
       String header = "<html><head><title>HTML in 10 Simple Steps or Less</title><meta http-equiv=\"refresh\" content=\"5\" /></head><body>";
       String webpage = header + "<h1>Weather Values:</h1><p>Temperature = " + String(temperature) +"F<br>Humidity = " + String(humidity) + "%</p></body></html>";       
       int length = webpage.length();
       String cipSend = "AT+CIPSEND="; 
       cipSend += connectionId;
       cipSend += ",";
       cipSend += length;
       cipSend +="\r\n";
       sendData(cipSend,500,DEBUG); //Set up TCP connection
       sendData(webpage,500,DEBUG);  //Send the data
       String closeCommand = "AT+CIPCLOSE="; 
       closeCommand+=5; // append connection id
       closeCommand+="\r\n";    
       sendData(closeCommand,1000,DEBUG); //close TCP connection
      }
    }
  }
}

void thingSpeak(){
  while(digitalRead(button1)){
    float temperature = dht.readTemperature(true); //Set to false if want to read in celcius
    float humidity = dht.readHumidity();
    sendData("AT+CIPSTART=\"TCP\",\"184.106.153.149\",80\r\n",500,DEBUG); // turn on server on port 80
    String dataWrite = "GET /update?api_key=" + apiKey + "&field1=" + String(temperature) + "&field2=" + String(humidity) + "\r\n";
    String cmd = "AT+CIPSEND=";
    int length = dataWrite.length();
    cmd += length;
    cmd += "\r\n";
    sendData(cmd,500,DEBUG); //Set up TCP connection
    sendData(dataWrite,1000,DEBUG); //Send data to thing speak
    long int time = millis(); 
    while((time+5000)>millis() && digitalRead(button1)){ //delay for 5 seconds before sending data or until button1 is pressed
    }
  }
}

String sendData(String command, const int timeout, boolean debug)
{
    String output = "";
    
    Serial1.print(command); // send the read character to the Serial1
    
    long int time = millis();
    
    while( (time+timeout) > millis())//wait for a response until timeout is up
    {
      while(Serial1.available())
      {      
        // The esp has data so display its output to the serial window 
        char c = Serial1.read(); // read the next character.
        output+=c;
      }  
      if ((output.indexOf("FAIL") != -1)||(output.indexOf("OK")!=-1)){ //if response received break from the timeout loop
        break;
      }
    }
    
    if(debug)
    {
      Serial.print(output);
    }    
    return output;
}

Credits

81a21a12aab58cdfe8d1306c85500e45
Chris Roberts

I am an applications engineer with Texas Instruments working with the Launchpad by trade, and a maker by passion!

Contact

Replications

Did you replicate this project? Share it!

I made one

Love this project? Think it could be improved? Tell us what you think!

Give feedback

Comments

Similar projects you might like

Got Your Back
Intermediate
  • 2,014
  • 51

Full instructions

This ergonomic-sensitive chair add-on will help you chair-ish your health!

Got Your Back

#creaTIviteam

TiWo
Intermediate
  • 1,415
  • 35

Full instructions

TiWo, keepin' it cool

TiWo

Team ElectroVolts

Mounted Infrared LED Laser Tag System
Intermediate
  • 40
  • 3

A laser tag system that uses flex sensors to control an infrared LED transmitter.

FPGA Plant Model powerSUITE Buck/Boost Real Time
Intermediate
  • 215
  • 3

Protip

Allows quick evaluation of Power Supply Plant parameters for TI Buck_VMC_F2837xS project.

The Zen Tracker - The Future of Workplace Health
Intermediate
  • 2,093
  • 31

Full instructions

An IoT, lucky bamboo garden that ambiently tracks and graphs your daily activity, ensuring wealth, health, and happiness!

iot-wardrobe
Intermediate
  • 1,106
  • 15

Bring the user clothing adapted to the weather conditions.

Add projectSign up / Login