Introduction: Use a Linkit ONE to Cloud Control a Color LED

About: I am a former English teacher turned Interactive Media Instructor turned STEM teacher turned computer science teacher. I like to make, fix, and take apart. Few things are more fun than taking something apart t…

This is the first of what I hope to be many projects. This has been a learning process for me and I hope to integrate what I have learned here into future projects. This project assumes you have already set up arduino to work with the Linkit One.

Step 1: Tools/Materials

1 - Computer

1 - RGB LED

1 - USB cable

1 - Linkit One

1 - Internet Acess

Step 2: Code

There are a few things you will have add some specific information to the code for it to work properly. The name of your wifi and password and the device id and key of your cloud.

//#include
#include #include #include #include #include #define WIFI_AP "" #define WIFI_PASSWORD "" #define WIFI_AUTH LWIFI_WPA // choose from LWIFI_OPEN, LWIFI_WPA, or LWIFI_WEP. #define per 50 #define per1 3 #define DEVICEID "" // Input your deviceId #define DEVICEKEY "" // Input your deviceKey #define SITE_URL "api.mediatek.com"

LWiFiClient c; unsigned int rtc; unsigned int lrtc; unsigned int rtc1; unsigned int lrtc1; char port[4]={0}; char connection_info[21]={0}; char ip[21]={0}; int portnum; int val = 0; String tcpdata = String(DEVICEID) + "," + String(DEVICEKEY) + ",0"; String upload_led; String tcpcmd_led_on = "LED_Controller,1"; String tcpcmd_led_off = "LED_Controller,0"; String tcpcmd_blue_on = "LED_BlueControl,1"; String tcpcmd_blue_off = "LED_BlueControl,0"; String tcpcmd_green_on = "LED_GreenControl,1"; String tcpcmd_green_off = "LED_GreenControl,0";

LWiFiClient c2; HttpClient http(c2);

void setup() { LTask.begin(); LWiFi.begin(); Serial.begin(115200); while(!Serial) delay(1000); /* comment out this line when Serial is not present, ie. run this demo without connect to PC */

Serial.println("Connecting to AP"); while (0 == LWiFi.connect(WIFI_AP, LWiFiLoginInfo(WIFI_AUTH, WIFI_PASSWORD))) { delay(1000); } Serial.println("calling connection");

while (!c2.connect(SITE_URL, 80)) { Serial.println("Re-Connecting to WebSite"); delay(1000); } delay(100);

pinMode(13, OUTPUT); pinMode(12, OUTPUT); pinMode(11, OUTPUT); getconnectInfo(); connectTCP(); }

void getconnectInfo(){ //calling RESTful API to get TCP socket connection c2.print("GET /mcs/v2/devices/"); c2.print(DEVICEID); c2.println("/connections.csv HTTP/1.1"); c2.print("Host: "); c2.println(SITE_URL); c2.print("deviceKey: "); c2.println(DEVICEKEY); c2.println("Connection: close"); c2.println(); delay(500);

int errorcount = 0; while (!c2.available()) { Serial.println("waiting HTTP response: "); Serial.println(errorcount); errorcount += 1; if (errorcount > 10) { c2.stop(); return; } delay(100); } int err = http.skipResponseHeaders();

int bodyLen = http.contentLength(); Serial.print("Content length is: "); Serial.println(bodyLen); Serial.println(); char c; int ipcount = 0; int count = 0; int separater = 0; while (c2) { int v = c2.read(); if (v != -1) { c = v; Serial.print(c); connection_info[ipcount]=c; if(c==',') separater=ipcount; ipcount++; } else { Serial.println("no more content, disconnect"); c2.stop();

} } Serial.print("The connection info: "); Serial.println(connection_info); int i; for(i=0;i

} //getconnectInfo

void uploadstatus(){ //calling RESTful API to upload datapoint to MCS to report LED status Serial.println("calling connection"); LWiFiClient c2;

while (!c2.connect(SITE_URL, 80)) { Serial.println("Re-Connecting to WebSite"); delay(1000); } delay(100); if(digitalRead(13)==1) upload_led = "LED_Display,,1"; else upload_led = "LED_Display,,0"; int thislength = upload_led.length(); HttpClient http(c2); c2.print("POST /mcs/v2/devices/"); c2.print(DEVICEID); c2.println("/datapoints.csv HTTP/1.1"); c2.print("Host: "); c2.println(SITE_URL); c2.print("deviceKey: "); c2.println(DEVICEKEY); c2.print("Content-Length: "); c2.println(thislength); c2.println("Content-Type: text/csv"); c2.println("Connection: close"); c2.println(); c2.println(upload_led); delay(500);

int errorcount = 0; while (!c2.available()) { Serial.print("waiting HTTP response: "); Serial.println(errorcount); errorcount += 1; if (errorcount > 10) { c2.stop(); return; } delay(100); } int err = http.skipResponseHeaders();

int bodyLen = http.contentLength(); Serial.print("Content length is: "); Serial.println(bodyLen); Serial.println(); while (c2) { int v = c2.read(); if (v != -1) { Serial.print(char(v)); } else { Serial.println("no more content, disconnect"); c2.stop();

} } }

void connectTCP(){ //establish TCP connection with TCP Server with designate IP and Port c.stop(); Serial.println("Connecting to TCP"); Serial.println(ip); Serial.println(portnum); while (0 == c.connect(ip, portnum)) { Serial.println("Re-Connecting to TCP"); delay(1000); } Serial.println("send TCP connect"); c.println(tcpdata); c.println(); Serial.println("waiting TCP response:"); } //connectTCP

void heartBeat(){ Serial.println("send TCP heartBeat"); c.println(tcpdata); c.println(); } //heartBeat

void loop() //Red led { //Check for TCP socket command from MCS Server String tcpcmd=""; while (c.available()) { int v = c.read(); if (v != -1) { Serial.print((char)v); tcpcmd += (char)v; if (tcpcmd.substring(40).equals(tcpcmd_led_on)){ digitalWrite(13, HIGH); Serial.print("Switch LED ON "); tcpcmd=""; } else if(tcpcmd.substring(40).equals(tcpcmd_led_off)){ digitalWrite(13, LOW); Serial.print("Switch LED OFF"); tcpcmd=""; } else if(tcpcmd.substring(40).equals(tcpcmd_blue_on)){ digitalWrite(12, HIGH); Serial.print("Switch LED ON "); tcpcmd=""; } else if(tcpcmd.substring(40).equals(tcpcmd_blue_off)){ digitalWrite(12, LOW); Serial.print("Switch LED OFF"); tcpcmd=""; } else if(tcpcmd.substring(40).equals(tcpcmd_green_on)){ digitalWrite(11, HIGH); Serial.print("Switch LED ON "); tcpcmd=""; } else if(tcpcmd.substring(40).equals(tcpcmd_green_off)){ digitalWrite(11, LOW); Serial.print("Switch LED OFF"); tcpcmd=""; } } }

  LDateTime.getRtc(&rtc);
  if ((rtc - lrtc) >= per) {
    heartBeat();
    lrtc = rtc;
  }
  //Check for report datapoint status interval
  LDateTime.getRtc(&rtc1);
  if ((rtc1 - lrtc1) >= per1) {
    uploadstatus();
    lrtc1 = rtc1;
  }
  
}

Step 3: Setting Up Your Cloud

  1. Go to the mediatek website.
  2. Navigate to Cloud Sandbox and Open Sandbox.
  3. Click on the wrench link.
  4. Click on create prototype.
  5. Fill out the information for your prototype.
  6. Go through the tutorial.
  7. Add four data channels.
    1. Create an ON/OFF display channel for reporting on the status of the LED. Data channel id: LED_Display
    2. Create an ON/OFF controller channel for controlling a color. Data channel id: LED_Controller
    3. Create an ON/OFF controller channel for controlling a color. Data channel Id: LED_BlueControl
    4. Create an ON/OFF controller channel for controlling a color. Data channel Id: LED_GreenControl
  8. Be sure the ids from the channels match the code.
  9. Create a test device.
  10. Get the device id and device key to use in your code.

I have arranged the pictures above in the order you should see them when setting up your cloud.

Step 4: Setting Up Your Linkit One

You will need to set up your Linkit One. Attach the wifi antenna to the Linkit One. Place the RGB led with the ground at the ground pin and the other colors in 13, 12, and 11. Attach the Linkit One to your computer via USB. Upload your sketch to your Linkit One. Turn on your serial monitor. Once the device is connected you will be able to control it with the cloud.

Step 5: Testing

Now you will need to test your setup. I am going to share some of the problems I encountered when testing my device. My LED was not fully seated in the pins once. I have to push it in to make it fit. Another time it would not connect to the serial monitor. I got an error that said the device was unavailable. I just hit the reset button on the side of the board. If it doesn't work try checking your cloud ids and make sure everything matches. Check all you connections. Hopefully it works. If it does not work just go back through the steps or comment below, and I will try to help.


Step 6: Next Step

At this point you will want to think about how you can use a color changing LED that can be controlled by the cloud. So far all my kids like playing with it. For me it is a fun toy and project I plan on building on.

Epilog Contest VII

Participated in the
Epilog Contest VII

Make It Glow! Contest

Participated in the
Make It Glow! Contest