Al Short
Published © MIT

Swellclock

Swellclock gives you a visual indication of surfing conditions at your local beach or any other beach.

AdvancedWork in progress1,094
Swellclock

Things used in this project

Hardware components

Arduino Yun
Arduino Yun
×1
Servos (Tower Pro MG996R)
×1

Story

Read more

Schematics

Swellclock V1.0

Code

Swellclock V1.0

Arduino
Version 1.0 I am trying to simplify this by using a Photon board
#include <Servo.h>
#include <Bridge.h>
#include <HttpClient.h>
#include <Console.h>

float height, pressure = 0;
String webData;
int charNumber = 0;
int combinedindex, heightindex, periodindex = 0;
float hservo, pservo ;

Servo myservo1;
Servo myservo2;

void setup() 
{
  // Bridge takes about two seconds to start up
  // it can be helpful to use the on-board LED
  // as an indicator for when it has initialized
  myservo1.attach(9);
  myservo2.attach(8);
  myservo1.write(0);
  myservo2.write(0);
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);
  Bridge.begin();
  digitalWrite(13, HIGH);

  //Serial.begin(9600);
  Console.begin();
  while(!Console);
  //while (!Serial); // wait for a serial connection
}

void loop()
{
  // Initialize the client library
  HttpClient client;

  // Make a HTTP request:
  client.get("http://magicseaweed.com/api/6324520cb90816fa8eef8ff17dc6a44f/forecast/?spot_id=1253");
  //http://magicseaweed.com/api/6324520cb90816fa8eef8ff17dc6a44f/forecast/?spot_id=1
  // if there are incoming bytes available
  // from the server, read them and print them:
  while (client.available())
  {
    char c = client.read();
    charNumber++;   //count the number of characters

    if (charNumber > 200)  //lets save some space
    //300 put 300 for primary data  
    {
      webData = webData + c;
    }
    if (charNumber > 400)//lets save some space
    //500 put 300 for primary data
    {

      Console.println(webData);
      combinedindex = webData.indexOf("combined");  //search for word "combined". this part is not need because we are reading latest data from the web site
      if (combinedindex > 0)
      {
        //Console.println(combinedindex);
      }

      heightindex = webData.indexOf("height"); //search for word "height"
      if (heightindex > 0)
      {
        Console.print("Height : ");
        //  Console.println(webData[heightindex+8]);
        hservo = getfloat('h', heightindex);  //get the float value of height
        turnhservo(hservo);   //turn the servo according to height value
        Console.println(hservo);
      }
      periodindex = webData.indexOf("period");
      {
        Console.print("Period : ");
        //Console.println(periodindex);
        pservo = getfloat('p', periodindex);
        turnpservo(pservo);
        Console.println(pservo);
        webData = "";    //set the webData string to null
        charNumber = 0;  //reset the number of characters counted
        break;      // stop the while loop
      }
    }
  }
  Serial.flush();

  delay(50000);
}

float getfloat(char d, int startindex)   // first argument consider if the input is for height or for period
{                                        //second argument is for starting index of height oor period in webData string
  static int start = 0;
  String val;
  if ( d == 'h')
  {
    start =  startindex + 8;           // digits start 8 chars after the start of "height" word
    for (int i = 0; i <= 5; i++)        //check chracters for 4 times, until it meets ","
    {
      if (webData[start + i] == ',')  //if it meet "," before 4 th character, break the for loop and stop the loop
      {
        return val.toFloat();     //return the extracted value in float
        break;
      }
      else
      {
        val = val + webData[start + i];   //combine characters and build a new string
      }
    }
  }

  else if ( d == 'p')
  {
    start =  startindex + 8;
    for (int i = 0; i < 4; i++)
    {
      if (webData[start + i] == ',')
      {
        return val.toFloat();
        break;
      }
      else
      {
        val = val + webData[start + i];
      }
    }
  }
}


void turnhservo(float hservo)   //this is for turn the servo
{
  if (hservo >= 0.1 && hservo <= 2.0 )
  {
    myservo1.write(140);
    Console.println("Servo1 angle : 140");
  }
  else if (hservo >= 2.1 && hservo <= 6.0 )
  {
    myservo1.write(125);
    Console.println("Servo1 angle : 125");
  }
  
  else if (hservo >= 6.1 && hservo <= 10.0 )
  {
    myservo1.write(100);
    Console.println("Servo1 angle : 100");
  }
  else if (hservo >= 10.1 && hservo <= 12.0 )
  {
    myservo1.write(60);
    Console.println("Servo1 angle : 60");
  }
   else if (hservo >= 12.1 && hservo <= 14.0 )
  {
    myservo1.write(40);
    Console.println("Servo1 angle : 40");
  }

  else if (hservo >= 14.1 && hservo <= 16.0 )
  {
    myservo1.write(35);
    Console.println("Servo1 angle : 35");
  }
   else if (hservo >= 16.1 && hservo <= 99.0 )
  {
    myservo1.write(10);
    Console.println("Servo1 angle : 10");
  }
}


void turnpservo(float pservo)
{
  if (pservo >= 0.1 && pservo <= 2.0)
  {
    myservo2.write(160);
    Console.println("Servo2 angle : 160");
  }
  else if (pservo >= 2.1 && pservo <= 4.0)
  {
    myservo2.write(125);
    Console.println("Servo2 angle : 125");
  }
  else if (pservo >= 4.1 && pservo <= 6.0)
  {
    myservo2.write(120);
    Console.println("Servo2 angle : 120");
  }
  else if (pservo >= 6.1 && pservo <= 8.0)
    {
      myservo2.write(100);
      Console.println("Servo2 angle : 100");
    }
  else if (pservo >= 8.1 && pservo <= 10.0)
    {
      myservo2.write(90);
      Console.println("Servo2 angle : 90");
    }
  else if (pservo >= 10.1 && pservo <= 12.0)
    {
      myservo2.write(70);
      Console.println("Servo2 angle : 70");
    }
  else if (pservo >= 12.1 && pservo <= 14.0)
    {
      myservo2.write(10);
      Console.println("Servo2 angle : 10");
    } 

  else if (pservo >= 14.1 && pservo <= 18.0)
    {
      myservo2.write(5);
      Console.println("Servo2 angle : 5");
    }
}

Credits

Al Short
2 projects • 2 followers

Comments