ALL POSTS BY

Blog

Intel Edison and the relayr Cloud

The Intel Edison is a tiny board-computer with a dual core, integrating WiFi, Bluetooth 4.0, 1 GB DDR and 4 GB flash memory. The 40 multiplexed GPIO pins and open source hardware allow rapid relayr prototyping, getting smart IoT ideas out there in no time. The purpose of this post is to help you get started prototyping with the Intel Edison.

 

Setting up the Intel Edison

Go to relayr's Developer Dashboard   to check out IoT projects and documentation!

 

  1. Assemble your board. Connect Power supply, and both USB and serial ports to your PC/Mac.
  2. Go to the Edison getting started page and Download your firmware/integrated installer. Be sure to download the newest version, otherwise installation and error identification could take awhile. We've used the m_iot_dev_kit_2015.0.023.tar for mac. Run the installer and follow the steps. It will take a while to set up everything.
  3. If you choose to, you could setup a serial terminal. To do so, open up a terminal and run ls /dev/cu.usbserial-*
    The command should return a list of devices connected to a serial port.
  4. Open a screen with: screen /dev/cu.usbserial-yourdevice 115200 -L
  5. Log in with root. If you encounter problems go to this page.
  6. The Edison comes with an integrated WiFi module, so setting up WiFi is an integral step. Configure a serial connection as described above. Type the following: configure_edison --wifi and scan. Choose your network by typing the numbers and enter a password. Make a note of your IP and wlan0address.
  7. Choose an IDE for development. An easy way to start with is the Arduino IDE. For more experienced programmers Eclipse:C/C++ or Javascript may be good options.
  8. As a test, open up your Arduino IDE, in Tools/Boards select the Intel Edison. (If not all libraries are installed, search for Intel in 'Manage Libraries' and install all dependencies). As a start choose the Blink sketch and define your port (not the serial one). Compile and Upload. The Terminal should say Transfer complete and the DS2LED should blink. Your board is now working properly.

Prototyping Using NodeJS

The Intel Edison is capable of running nodeJS which allows for incorporating many different libraries. For example cyclonJs or our own nodeJS SDK. The Intel Edison comes with a free IDE which allows for easy integration with the relayr cloud. Here is a short example using a template and the mqtt library. All you need is your board, the installed XDK and npm.

 

Additional Setup - NodeJS

 

Intel provides an easy entry point into programming with the Intel XDK IoT Edition. So install it and connect with your Edison in following way to configure wifi and passwords.

 

  1. Setup an ssh connection via terminal ssh root@edison.local
  2. Configure your WiFi edison_configure --wifi
  3. And most importantly configure a password, otherwise the XDK won't connect to your device edison_configure --password
  4. Start the Intel XDK IoT Edition and create a new Project, choose a template and click on continue. We've decided on a simple analogRead, define your path and you should end up with the following JavaScript template:
    var mraa = require('mraa'); //require mraa
    console.log('MRAA Version: ' + mraa.getVersion()); //write the mraa version to the console
    
    var analogPin0 = new mraa.Aio(0); //setup access analog input Analog pin #0 (A0)
    console.log(analogValue); //write the value of the analog pin to the console
  5. To test, connect your IoT Device with the XDK. You do this by selecting under IoT Device your device. Make sure that you have the right IP address. You are now connected and able to develop.
  6. If you encounter problems start the XDK daemon manually with the following command:
    <code>
    systemctl enable xdk-daemon
    systemctl restart xdk-daemon
  7. Upload the analogRead template and hit the run button. In the Terminal you should see the following (depending on your version and sensor reading):
    <code>
    MRAA Version: v0.8.0
    633

Integrating with the relayr Cloud with NodeJS

 

  1. Back in your terminal, navigate to your project folder and install mqtt via npm.
    npm install mqtt --save

  2. In XDK check whether your package.json has the following dependency:
    "dependencies": {
    "mqtt": "^1.5.0" }
     
  3. Add the following to your code:
    //Install mqtt library using: npm install mqtt
    var mqtt = require('mqtt');
     
  4. Access the relayr dashboard to get your credentials and create a new device. Select the NodeJS code sample.
  5. Copy and paste the following into your project. If you want publish more often change the SetInterval() interval from 1000 to, for example, 500.
    var client = mqtt.connect({
    	  servers:[{'host':'mqtt.relayr.io'}],
    	  username: "e997122b-b552-43ff-8754-07aeef100f01",
    	  password: "SdoUd1zf.B-Z",
    	  clientId: "T6ZcSK7VSQ/+HVAeu7xAPAQ",
    	  protocol : 'mqtt',
    	  rejectUnauthorized : false,
    	});
    
    client.on('connect', function() {
    
      //subscribe to commands sent from the dashboard or other clients
      client.subscribe("/v1/e997122b-b552-43ff-8754-07aeef100f01/"+"/cmd");
    
      client.on('message', function (topic, message) {
        console.log(message.toString());
      });
    
      //simple timer to send a message every 1 second
      var publisher = setInterval(function(){
    
        var analogValue = analogPin0.read(); //read the value of the analog pin
    
        // publish a message to a topic
        var data = JSON.stringify({meaning:"someMeaning", value: analogValue});
    
        client.publish("/v1/e997122b-b552-43ff-8754-07aeef100f01/"+"/data", data, function() {
          console.log("Message is published");
        });
    
      }, 1000);
    
    });

  6. You can change the value: "30" to the actual analogValue, by inserting the following statement in the setInterval method:
    var analogValue = analogPin0.read(); //read the value of the analog pin

  7. Click the Upload button, wait until the package is installed and run your program. The terminal should output that you are publishing and you should see your data in the devices section of the developer dashboard.

Prototyping using Arduino

Here is another option to work and prototype using the Intel Edison.

 

Publishing Data via the Arduino.ino Sketch

 

Access the relayr dashboard to get your credentials and create a new device. Select the Arduino code sample.

 

The Arduino Code

 

The Arduino code is a mixture of the two libraries: Wifi.h and PubSubClient.h. Both are compatible with the Intel Edison board.

  1. Import the following libraries
    //libraries
    #include
    #include
    #include

  2. Setup variables for WiFi credentials
    //Wifi credentials
    char ssid[] = "xxx"; // your network SSID (name)
    char password[] = "xxxx"; // your network password (use for WPA, or use as key for WEP)

  3. Paste and Copy your received credentials
    //define your mqtt credentials
    #define DEVICE_ID "deba7e6a-78ed-4f8f-a3bc-8771a26e659d"
    #define MQTT_USER "deba7e6a-78ed-4f8f-a3bc-8771a26e659d"
    #define MQTT_PASSWORD "w_qiC1l6U80t"
    #define MQTT_CLIENTID "T3rp+anjtT4+jvIdxom5lnQ" //can be anything else
    #define MQTT_TOPIC "/v1/deba7e6a-78ed-4f8f-a3bc-8771a26e659d/"
    #define MQTT_SERVER "mqtt.relayr.io"

  4. Declare additional variables, mainly for controlling the LED and the publishing time.
        //to check whether wifi is active
    	int status = WL_IDLE_STATUS;
    
    	//LED
    	const int led = 13;
    	int ledState = LOW;
    
    	//time variables
    	unsigned long lastPublishTime = 0;
    	unsigned long lastBlinkTime = 0;;
    
    int publishingPeriod = 400; //do not set under 200ms or the cloud will kick you
    
    	//for the messages
    	char msg[50];
    	char message_buff[100];
     
  5. Initialize the clients
    //Initialize Wificlient
    WiFiClient wifiClient;
    //Initialize Pubsubclient
    PubSubClient client(wifiClient);

  6. In the setup define your LED, the serial port(), your WiFi and the MQTT client
    //setup
    void setup() {
    
      //define LED pin
      pinMode(led, OUTPUT);
    
      //define Serial
      Serial.begin(9600);
    
      //setup the WIFI
      setup_wifi();
    
      //setup MQTT server
      client.setServer(MQTT_SERVER, 1883);
      // and callback method for receiving messages
      client.setCallback(callback);
      //setup publishing period
      publishingPeriod = publishingPeriod > 200 ? publishingPeriod : 200;
      //connect ans subscribe
      mqtt_connect();
    }

  7. The mqtt_connect method simply uses the client to connect and subscribe to a topic
    void mqtt_connect() {
    
      Serial.println("Connecting to mqtt server");
      if (client.connect(MQTT_CLIENTID, MQTT_USER, MQTT_PASSWORD)) {
        Serial.println("Connection success, subscribing to topic");
        //subscribe to a topic
        client.subscribe("/v1/"DEVICE_ID"/cmd");
      }
      else {
        Serial.println("Connection failed, check your credentials or wifi");
      }
    
    }

  8. The following is a standard method for connecting your WiFi client to your local WiFi network
    //just sets up the Wifi with the creds
    void setup_wifi() {
    
      delay(10);
      // We start by connecting to a WiFi network
      Serial.println();
      Serial.print("Connecting to ");
      Serial.println(ssid);
    
      WiFi.begin(ssid, password);
    
      while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
      }
    
      Serial.println("");
      Serial.println("WiFi connected");
      Serial.println("IP address: ");
      Serial.println(WiFi.localIP());
    
    }

  9. The callback method listens on incoming data
    //callback method for incoming data
    void callback(char* topic, byte* payload, unsigned int length) {
    
      Serial.print("Message arrived [");
      Serial.print(topic);
      Serial.print("] ");
      for (int i = 0; i < length; i++) {
        Serial.print((char)payload[i]);
      }
      Serial.println();
    
      // Switch on the LED if an 1 was received as first character
      if ((char)payload[0] == '1') {
        digitalWrite(led, LOW);   // Turn the LED on (Note that LOW is the voltage level
        // but actually the LED is on; this is because
        // it is acive low on the ESP-01)
      } else {
        digitalWrite(led, HIGH);  // Turn the LED off by making the voltage HIGH
      }
    
    }

  10. A self defined publish method takes the readings and formats them into Json objects
    void publish() {
    
      //create our json payload
      String pubString = "{\"meaning\":\"temperature\", \"value\":";
      //read and add sensor data to payload
      pubString += analogRead(A0);
      pubString += "}";
      pubString.toCharArray(message_buff, pubString.length() + 1);
      //publish our json payload
      client.publish("/v1/"DEVICE_ID"/data", message_buff);
      Serial.println("Publishing " + String(message_buff));
    
    }

  11. The method Blink() was created for feedback. The LED blinks whenever it is called
    void blink(int interval) {
    
      if (millis() - lastBlinkTime > interval) {
        // save the last time you blinked the LED
        lastBlinkTime = millis();
        if (ledState == LOW)
          ledState = HIGH;
        else
          ledState = LOW;
        // set the LED with the ledState of the variable:
        digitalWrite(led, ledState);
      }
    }

  12. You can use the following loop to make sure you stay connected and publish data continuously
    void loop() {
    
      if (client.connected()) {
    
        client.loop();
        if (millis() - lastPublishTime > publishingPeriod) {
    
          publish();
    
        }
        //just some blinking feedback
        blink(publishingPeriod / 2);
      }
    }
     

Check out this Github repository for additional information and prototyping options.

 

Go to relayr's Developer Dashboard   to check out IoT projects and documentation!