Steven Zuniga
Published

Summer Bridge SZ

My eye opening experience to IOT and C.

BeginnerWork in progress20 hours9
Summer Bridge SZ

Things used in this project

Hardware components

Argon
Particle Argon
×1
Base Shield V2
Seeed Studio Base Shield V2
×1
Grove - Light Sensor
Seeed Studio Grove - Light Sensor
×1
Grove - 6-Axis Accelerometer&Gyroscope
Seeed Studio Grove - 6-Axis Accelerometer&Gyroscope
×1
Grove - Temperature Sensor
Seeed Studio Grove - Temperature Sensor
×1
NeoPixel Ring: WS2812 5050 RGB LED
Adafruit NeoPixel Ring: WS2812 5050 RGB LED
×1

Software apps and online services

VS Code
Microsoft VS Code

Story

Read more

Schematics

Circuit Breadboard

Circuit Schematic

Code

Neopixel

C/C++
/*
 * Project neo
 * Description:4
 * Author:
 * Date:
 */

#include "neopixel.h"
#include "colors.h"
#include "math.h"

#define PIXEL_PIN A2
#define PIXEL_COUNT 16
#define PIXEL_TYPE WS2812B

Adafruit_NeoPixel pixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);

int i, pixelNum, color;
bool buttonState;
float bright, t, freq;

SYSTEM_MODE(SEMI_AUTOMATIC);
SYSTEM_THREAD(ENABLED);

// setup() runs once, when the device is first turned on.
void setup() {
  // Put initialization like pinMode and begin functions here.

  pinMode(D10, INPUT);
  freq = 1 / 3.0;
  pixel.begin();
  pixel.setPixelColor(5, red);
  pixel.show();
  i = 0;

}

// loop() runs over and over again, as quickly as it can execute.
void loop() {
  // The core of your code will likely live here.
  //pixel.setBrightness(256);
  buttonState = digitalRead(D10);
  while(buttonState == true) {
    t = millis() / 1000.0;
    bright = 127.5 * sin(2 * M_PI * freq * t) + 127.5;
    pixel.setBrightness(bright);
    i++;
    pixelNum = i % 14;
    color = i % 6;
    pixel.setPixelColor(pixelNum, rainbow[color]);
    pixel.show();
    delay(10);
    buttonState = digitalRead(D10);
  }
  
}

Motion

C/C++
/*
 * Project Motion
 * Description: Learn to use accelerometer
 * Author: 
 * Date:
 */

#include <ADXL345.h>

int x, y, z;
float ax, ay, az, pitch, roll;

ADXL345 adxl; 

SYSTEM_MODE(SEMI_AUTOMATIC);
SYSTEM_THREAD(ENABLED);

void setup() {
    Serial.begin(9600);
    adxl.powerOn();
}

void loop() {
    //read the accelerometer values and store them in variables  x,y,z    
    adxl.readXYZ(&x, &y, &z); 
    //Serial.printf("values of X = %i, Y = %i, Z = %i\n",x,y,z);

    // Get acceleration in g's
    double xyz[3];
    adxl.getAcceleration(xyz);
    ax = xyz[0];
    ay = xyz[1];
    az = xyz[2];
    pitch = asin(ax) * (360/(2 * M_PI));
    roll = atan2(ay,az) * (360/(2 * M_PI));
    Serial.printf("values of X = %0.2f, Y = %0.2f, Z = %0.2f pitch = %0.2f, roll = %0.2f\n",ax,ay,az, pitch, roll);

    delay(500);

}

The Cloud

C/C++
/*
 * Project TheCloud
 * Description: Starter Code for Adafruit.io
 * Author: Brian Rashap
 * Date: 6-Aug-2020
 */

// Need to install library Adafruit_MQTT
#include <Adafruit_MQTT.h>
#include "Adafruit_MQTT/Adafruit_MQTT.h" 
#include "Adafruit_MQTT/Adafruit_MQTT_SPARK.h" 
#include "Grove_Temperature_And_Humidity_Sensor.h"

// We will create credentials.h using the below
#include "credentials.h"

#define DHTPIN D2
DHT dht(DHTPIN);


/************ Global State (you don't need to change this!) ***   ***************/ 
TCPClient TheClient; 

// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details. 
Adafruit_MQTT_SPARK mqtt(&TheClient,AIO_SERVER,AIO_SERVERPORT,AIO_USERNAME,AIO_KEY); 

/****************************** Feeds ***************************************/ 
// Setup Feeds to publish or subscribe 
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname> 
Adafruit_MQTT_Publish mqttTemp = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/TempF");
Adafruit_MQTT_Publish mqttCelc = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/TempC");
Adafruit_MQTT_Publish mqttHum = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/Humidity");
Adafruit_MQTT_Publish mqttLight = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/LightLevel");

Adafruit_MQTT_Subscribe mqttLED = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/LED");

/************Declare Variables*************/
unsigned long last, lastTime;
float h, t, f;
int ledOn, light;

SYSTEM_MODE(SEMI_AUTOMATIC);

void setup() {
  Serial.begin(9600);
  waitFor(Serial.isConnected, 15000); //wait for Serial Monitor to startup

  
  Serial.println("DHT11 Begin!!");
  dht.begin();
  pinMode(A0, INPUT);
  pinMode(D4, OUTPUT);

  //Connect to WiFi without going to Particle Cloud
  WiFi.connect();
  while(WiFi.connecting()) {
    Serial.printf(".");
  }

  // Setup MQTT subscription for onoff feed.
  mqtt.subscribe(&mqttLED);
}

void loop() {
  // Validate connected to MQTT Broker
  MQTT_connect();

  //Read Humidity
  //h = dht.getHumidity();
  //Read Temp Celcius
  //t = dht.getTempCelcius();
  //Read Temp Farenheit
  //f = dht.getTempFarenheit();

  //Check if any reads failed
    // if(isnan(h) || isnan(t) || isnan(f))
    // {
    //   Serial.printf("Failed to read from DHT11 sensor!\n");
    //   return;
    // }
    Serial.printf("Temp(C) : %0.2f, Temp(F) : %0.2f, Humid : %0.2f, Light Level : %0.2f\n",t,f,h,light);

  // Ping MQTT Broker every 2 minutes to keep connection alive
  if ((millis()-last)>120000) {
      Serial.printf("Pinging MQTT \n");
      if(! mqtt.ping()) {
        Serial.printf("Disconnecting \n");
        mqtt.disconnect();
      }
      last = millis();
  }

  // publish to cloud every 30 seconds
  f = dht.getTempFarenheit();
  h = dht.getHumidity();
  t = dht.getTempCelcius();
  light = analogRead(A0);
  if((millis()-lastTime > 10000)) {
    if(mqtt.Update()) {
      mqttTemp.publish(f);
      mqttHum.publish(h);
      mqttCelc.publish(t);
      mqttLight.publish(light);
      Serial.printf("Publishing %0.2f \n",f); 
      } 
    lastTime = millis();
  }


  // this is our 'wait for incoming subscription packets' busy subloop
  Adafruit_MQTT_Subscribe *subscription;
  while ((subscription = mqtt.readSubscription(1000))) {
    if (subscription == &mqttLED) {
      ledOn = atoi((char *)mqttLED.lastread);
      analogWrite(D4, ledOn);
          Serial.printf("Received %i from Adafruit.io feed FeedNameB \n",ledOn);
    }
  }
}

// Function to connect and reconnect as necessary to the MQTT server.
void MQTT_connect() {
  int8_t ret;
 
  // Stop if already connected.
  if (mqtt.connected()) {
    return;
  }
 
  Serial.print("Connecting to MQTT... ");
 
  while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
       Serial.printf("%s\n",(char *)mqtt.connectErrorString(ret));
       Serial.printf("Retrying MQTT connection in 5 seconds..\n");
       mqtt.disconnect();
       delay(5000);  // wait 5 seconds
  }
  Serial.printf("MQTT Connected!\n");
}

Credits

Steven Zuniga

Steven Zuniga

1 project • 2 followers

Comments

Add projectSign up / Login