Skip to content

Instantly share code, notes, and snippets.

@schappim
Forked from electricimp/ArduinoUART.device.nut
Last active August 29, 2015 14:26

Revisions

  1. Electric Imp revised this gist May 15, 2014. 2 changed files with 52 additions and 29 deletions.
    34 changes: 24 additions & 10 deletions ArduinoUART.device.nut
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,15 @@
    // electric imp device code
    server.log("Device Started");

    arduino <- hardware.uart57;

    function arduinoData() {
    function arduinoData()
    {
    // Read the UART for data sent by Arduino to indicate
    // the state of its LED.

    local b = arduino.read();
    while(b != -1) {
    while(b != -1)
    {
    // As long as UART read value is not -1, we're getting data

    local state = "Unknown";
    if (b == 0x10) state = "Off";
    if (b == 0x11) state = "On"
    @@ -14,11 +18,21 @@ function arduinoData() {
    }
    }

    function blink(state)
    {
    // Write state (1 or 0) to the Arduino

    server.log("Setting LED to: " + state);
    arduino.write(state);
    imp.wakeup(1.0, function(){blink(1 - state);});
    }

    // Alias UART to which Arduino is connected

    arduino <- hardware.uart57;
    arduino.configure(9600, 8, PARITY_NONE, 1, NO_CTSRTS, arduinoData);

    // Start blinking

    blink(1);

    function blink(state) {
    server.log("Setting LED to: " + state);
    arduino.write(state);
    imp.wakeup(1.0, function() { blink(1-state); });
    } blink(1);
    47 changes: 28 additions & 19 deletions ElectricImpSerial.cc
    Original file line number Diff line number Diff line change
    @@ -1,24 +1,33 @@
    // arduino device code
    int led = 13; // led pin number
    // Arduino device code

    void setup() {
    Serial.begin(9600); // configure serial
    pinMode(led, OUTPUT); // configure LED pin
    digitalWrite(led, 0); // turn LED off
    int led = 13; // LED pin number

    void setup()
    {
    Serial.begin(9600); // Configure serial
    pinMode(led, OUTPUT); // Configure LED pin
    digitalWrite(led, 0); // Turn LED off
    }

    void loop() {
    int b = 0;
    // if there's data available
    if (Serial.available () > 0) {
    // read a byte
    b = Serial.read();
    if (b == 0x00) {
    digitalWrite(led, LOW);
    Serial.write(0x10);
    } else if (b == 0x01) {
    digitalWrite(led, HIGH);
    Serial.write(0x11);
    void loop()
    {
    int b = 0;

    // If there's data available
    if (Serial.available () > 0)
    {
    // Read a byte
    b = Serial.read();

    if (b == 0x00)
    {
    digitalWrite(led, LOW);
    Serial.write(0x10);
    }
    else if (b == 0x01)
    {
    digitalWrite(led, HIGH);
    Serial.write(0x11);
    }
    }
    }
    }
  2. Electric Imp renamed this gist Dec 17, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. Electric Imp renamed this gist Dec 17, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. Electric Imp revised this gist Sep 25, 2013. 2 changed files with 0 additions and 0 deletions.
    File renamed without changes.
    File renamed without changes.
  5. Electric Imp created this gist Sep 25, 2013.
    24 changes: 24 additions & 0 deletions UARTArduino.cc
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    // arduino device code
    int led = 13; // led pin number

    void setup() {
    Serial.begin(9600); // configure serial
    pinMode(led, OUTPUT); // configure LED pin
    digitalWrite(led, 0); // turn LED off
    }

    void loop() {
    int b = 0;
    // if there's data available
    if (Serial.available () > 0) {
    // read a byte
    b = Serial.read();
    if (b == 0x00) {
    digitalWrite(led, LOW);
    Serial.write(0x10);
    } else if (b == 0x01) {
    digitalWrite(led, HIGH);
    Serial.write(0x11);
    }
    }
    }
    24 changes: 24 additions & 0 deletions UARTArduino.device.nut
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    // electric imp device code
    server.log("Device Started");

    arduino <- hardware.uart57;

    function arduinoData() {
    local b = arduino.read();
    while(b != -1) {
    local state = "Unknown";
    if (b == 0x10) state = "Off";
    if (b == 0x11) state = "On"
    server.log("LED: " + state);
    b = arduino.read();
    }
    }

    arduino.configure(9600, 8, PARITY_NONE, 1, NO_CTSRTS, arduinoData);


    function blink(state) {
    server.log("Setting LED to: " + state);
    arduino.write(state);
    imp.wakeup(1.0, function() { blink(1-state); });
    } blink(1);