Lab #6: Wireless Lab - IR and WiFi

IR Receiver

Our results printed to the Serial monitor after pressing 1, 2, and 3 on the remote.

1 - C9767F76

2 -C8155AB1

3 - B6996DAE

4 - 969AE844

5 -4AAFAC67

6 - 9C2A936C

7 - 833ED333

8 - 55F2B93

9 -DE78B0D0

0 -F32F72D7


Code for IR Receiver


#include <IRremote.h>


int RECV_PIN = 11; //IR Receiver connected to pin 11

int yellow = 3;
int green = 4;

IRrecv irrecv(RECV_PIN);

decode_results results;

void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
pinMode(yellow, OUTPUT);
pinMode(green, OUTPUT);
}

void loop() {
  results.value = 0x00;
  if (irrecv.decode(&results)) { //if a code received
    Serial.println(results.value, HEX); //displays in hexidecimal format
   //pressing 1 on remote
   if(results.value == 0xC9767F76){
    digitalWrite(yellow, HIGH);
     //pressing 2 on remote
   }else if(results.value == 0xC8155AB1){
    digitalWrite(green, HIGH);
     //pressing 4 on remote
   }else if(results.value == 0x969AE844){
    digitalWrite(yellow, LOW);
    //pressing 5 on remote
   }else if(results.value == 0x4AAFAC67){
    digitalWrite(green, LOW);
   }
   irrecv.resume(); //Receive the next value 
  }
}







Code for IR Transmitter


#include <IRremote.h>


IRsend irsend;

int numBits = 32;
int IRcode;
String command = "";

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:

if(Serial.available()>0){
  char input = Serial.read();
  command+=input;
  if(input == '\n'){
    command="";
  }
  else{
    if(command=="1"){
      //IRcode=on;
      Serial.println("1");
      irsend.sendNEC(0xC8155AB1, numBits);
      command="";
    }
    else if(command=="2"){
      //IRcode=off;
      Serial.println("2");
      irsend.sendNEC(0x4AAFAC67,numBits);
      command="";
    }
  }
}

}




Wi-Fi

// This #include statement was automatically added by the Particle IDE.
#include <ThingSpeak.h>

/*
  WriteVoltage

  Reads an analog voltage from pin 0, and writes it to a channel on ThingSpeak every 20 seconds.

  ThingSpeak ( https://www.thingspeak.com ) is an analytic IoT platform service that allows you to aggregate, visualize and analyze live data streams in the cloud.

  Copyright 2017, The MathWorks, Inc.

  Documentation for the ThingSpeak Communication Library for Particle is in the README.md file where the library was installed.
  See the accompanying license file for licensing information.
*/

#include "ThingSpeak.h"

TCPClient client;

/*
  *****************************************************************************************
  **** Visit https://www.thingspeak.com to sign up for a free account and create
  **** a channel.  The video tutorial http://community.thingspeak.com/tutorials/thingspeak-channels/
  **** has more information. You need to change this to your channel, and your write API key
  **** IF YOU SHARE YOUR CODE WITH OTHERS, MAKE SURE YOU REMOVE YOUR WRITE API KEY!!
  *****************************************************************************************/
unsigned long myChannelNumber = 728488;
const char * myWriteAPIKey = "AU01XFECNKMASPR7";

void setup() {
  ThingSpeak.begin(client);
}

void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  // Convert the analog reading
  // On Particle: 0 - 4095 maps to 0 - 3.3 volts
  float voltage = sensorValue * (3.3 / 4095.0);

  // Write to ThingSpeak. There are up to 8 fields in a channel, allowing you to store up to 8 different
  // pieces of information in a channel.  Here, we write to field 1.
  ThingSpeak.writeField(myChannelNumber, 1, voltage, myWriteAPIKey);
  delay(20000); // ThingSpeak will only accept updates every 15 seconds.

}

readweatherstation.ino

// This #include statement was automatically added by the Particle IDE.
#include <ThingSpeak.h>

/*
  ReadWeatherStation
 
  Reads the latest weather data every 60 seconds from the public MathWorks
  weather station in Natick, MA https://thingspeak.com/channels/12397 on ThingSpeak.
 
  ThingSpeak ( https://www.thingspeak.com ) is an analytic IoT platform service that allows you to aggregate, visualize and analyze live data streams in the cloud.
 
  Copyright 2017, The MathWorks, Inc.

  Documentation for the ThingSpeak Communication Library for Particle is in the README.md file where the library was installed.
  See the accompanying license file for licensing information.
*/

#include "ThingSpeak.h"

// On Particle Core, Photon, and Electron the results are published to the Particle dashboard using events.
// Go to http://dashboard.particle.io, click on the logs tab, and you'll see the events coming in.
TCPClient client;

/*
  This is the ThingSpeak channel number for the MathwWorks weather station
  https://thingspeak.com/channels/12397.  It senses a number of things and puts them in the eight
  field of the channel:
  Field 1 - Wind Direction (degrees where 0 is North)
  Field 2 - Wind Speed (MPH)
  Field 3 - Humidity (%RH)
  Field 4 - Temperature (Degrees F)
  Field 5 - Rainfall (inches since last measurement)
  Field 6 - Atmospheric Pressure (inHg)
*/
unsigned long weatherStationChannelNumber = 12397;

void setup() {
  ThingSpeak.begin(client);
}

void loop() {
  float windDirection = ThingSpeak.readFloatField(weatherStationChannelNumber,1);
  float windSpeed = ThingSpeak.readFloatField(weatherStationChannelNumber,2);
  float humidity = ThingSpeak.readFloatField(weatherStationChannelNumber,3);
  float temperature = ThingSpeak.readFloatField(weatherStationChannelNumber,4);
  float rainfall = ThingSpeak.readFloatField(weatherStationChannelNumber,5);
  float pressure = ThingSpeak.readFloatField(weatherStationChannelNumber,6);

  Particle.publish("thingspeak-weather", "Current weather conditions in Natick: ",60,PRIVATE);
  Particle.publish("thingspeak-weather", String(temperature) + " degrees F, " + String(humidity) + "% humidity",60,PRIVATE);
  Particle.publish("thingspeak-weather", "Wind at " + String(windSpeed) + " MPH at " + String (windDirection) + " degrees",60,PRIVATE);
    if(rainfall > 0)
{
  Particle.publish("thingspeak-weather", "Pressure is " + String(pressure) + " inHg, and it's raining",60,PRIVATE);
}
else
{
  Particle.publish("thingspeak-weather", "Pressure is " + String(pressure) + " inHg",60,PRIVATE);
}

  delay(60000); // Note that the weather station only updates once a minute


}


Comments

Popular posts from this blog

Lab #3: ColorPAL

Lab #4: Non-volatile memory, Servo Motors and MATLAB

Lab #5: Bio-Signals