With limited memory on the arduino (and other devices), scriptr.io will be responsible of connecting to third party APIs, using complex authentication schemes like oauth, passing authentication headers, doing math… It also allows updating the logic without having to update the firmware on the devices.
Features
- Shows the current temperature based on a remote API
- Shows the current sky state based on a remote API
- Switches between temperature or sky states based on position of device (straight or upside down)
- Has all logic calculated in scriptr.io (contacting weather apis, mapping temperature and sky states to servo rotation degrees, ..
Required Items
- 1x arduino uno board ( http://arduino.cc/en/Main/ArduinoBoardUno )
- 1x cc3000 wifi shield ( http://www.adafruit.com/product/1491 )
- 1x mercury tilt switch ( ex: http://www.amazon.com/Mercury-Sensor-Module-Official-Arduino/dp/B00PQAKGL2 )
- 1x servo motor (ex: http://www.amazon.com/Hitec-31311S-HS-311-Standard-Universal/dp/B0006O3WVE )
- Connectors and wires
- Soldering iron and solder
- A box that fits all of the above and which can stand straight or updside down
Step-by-step guide
- Connect the Arduino to the internet. For this, we’re using the Adafruit cc3000 wifi shield http://www.adafruit.com/product/1491
- Wire / Solder the arduino to the shield. The full set of steps and documentation is taken from https://learn.adafruit.com/adafruit-cc3000-wifi/cc3000-shield
- The CC3000 Shield ships with a strip of header pins.
Break off 6, 8, or 10-pin sections and insert them into the header sockets of your Arduino
- Place the shield over the pins, and carefully solder each one in place
- In this example, you will not have to short the three jumpers on the bottom of the cc3000 shield.
- The CC3000 Shield ships with a strip of header pins.
- Write the code that connects to an Access Point
/*************************************************** Wireless library: adafruit.com servo library: arduino.cc ****************************************************/ #include #include #include /********change these to match your AP **************/ #define WLAN_SSID "ssid" #define WLAN_PASS "password" /****************************************************/ //-------begin adafruit wifi setup -----------------// // These are the interrupt and control pins #define ADAFRUIT_CC3000_IRQ 3 // MUST be an interrupt pin! // These can be any two pins #define ADAFRUIT_CC3000_VBAT 5 #define ADAFRUIT_CC3000_CS 10 // Use hardware SPI for the remaining pins // On an UNO, SCK = 13, MISO = 12, and MOSI = 11 Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT, SPI_CLOCK_DIVIDER); // you can change this clock speed #define WLAN_SECURITY WLAN_SEC_WPA2 // Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2 #define IDLE_TIMEOUT_MS 3000 // Amount of time to wait (in milliseconds) with no data // received before closing the connection. If you know the server // you're accessing is quick to respond, you can reduce this value. //-------end adafruit wifi setup -------------------// if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) { Serial.println(F("Failed!")); while(1); } Serial.println(F("Connected")); /* Wait for DHCP to complete */ Serial.println(F("Request DHCP")); while (!cc3000.checkDHCP()) { delay(100); // ToDo: Insert a DHCP timeout! }
- Wire / Solder the arduino to the shield. The full set of steps and documentation is taken from https://learn.adafruit.com/adafruit-cc3000-wifi/cc3000-shield
- Connect the servo to the now-internet-enabled arduino board
- Servos are controlled by a PWM (Pulse Width Modulation) signal. The width of the pulse is what controls the angle of rotation of the servo. Luckily, Arduino comes with a lot of libraries, of which Servo.h, so we won’t have to build that ourselves
- Servos typically have three wires: GND, PWR, CTRL. GND and PWR for DC power supply (connect them to GND and 5V on the arduino, or an external power supply) and CTRL, which will be connected to a pwm pin on the arduino (we will use pin #9 as it isn’t used by the wifi shield. You can pick any pin that isn’t only a digital one)
- Control the servo’s position
#include //init servo code servo.attach(9); int pos = 0; servo.write(servPos);
- Connect the mercury tilt switch
- The switch we’re using in this tutorial already comes on a board with its resistor. We will only have to connect its three pins (GND, PWR, OUT).
- GND and PWR need to be connected to GND and 3.3V on the arduino board
- OUT will be connected to any unused digital pin on the arduino board (we will use pin #2)
- The switch we’re using in this tutorial already comes on a board with its resistor. We will only have to connect its three pins (GND, PWR, OUT).
- Write the arduino code
The code on the arduino side will be minimal- Setup
- Setup the wifi shield
- Setup the servo code
- Setup the tilt switch
- Loop
- Detect the position of the tilt switch
- If it changed, issue a call to scriptr.io
- If it is the same, check the time for the previous call. If under a certain threshold (15 minutes), delay for 100ms, else issue a call to scriptr.io
- Setup
- Write the scriptr.io script
The code on scriptr.io’s side will contain all the logic- Decide what API to connect to, based on the tilt switch position
- Decide what info to extract, based on the tilt switch position
- Map temperatures to servo degrees
- Map all the possible sky states returned by the api to a limited set that fits on a small dial (6 total: sunny, partly cloudy, cloudy, rainy, thunderstorm, snowy)
- Map the limited set of sky states to servo degrees
You Can find the full code for the Weather Machine Below: