This time, we need to figure out how to receive the data from AWS and control our devices.

For my Rpi, I’m following the guide that comes from github

https://github.com/amzn/alexa-skills-kit-js

This is a library that allows my Rpi to access the AWS IOT service.

When I want to connect my Rpi to control my Arduino, a problem happened. I was using a log converter to connect these two devices, but the data is not stable at all. I  was really frustrated for 2 weeks, I was thinking that it is my code problem, but when I replace the logic converter with a simple USB cable, everything goes perfectly.

Here is my Arduino code

int datapin = 4;
int clockpin = 6;
int latchpin = 5;
byte data = 0;
String inputString = “”; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
//
//int rum=0;
//int coke=0;
//int vodka=0;
//int tonic=0;
//int cointreau=0;
//int pinapple=0;
//int coconut=0;
//int lemon=0;
//int cranberry=0;

unsigned int ArrayKey[9];
const int arraySize=9;

char receivedChar;
boolean newData = false;

void setup() {
inputString = “”;
Serial.begin(9600);
pinMode(datapin, OUTPUT);
pinMode(clockpin, OUTPUT);
pinMode(latchpin, OUTPUT);
for(int i=0;i<9;i++){
ArrayKey[i]=0;
}
pumpsignal(ArrayKey,9);
}

void loop() {

if (stringComplete) {
char *tmp;
int i=0;
tmp=strtok(&inputString[0],”,”);
while(tmp){
ArrayKey[i++]=atoi(tmp);
tmp=strtok(NULL,”,”);
}
Serial.print(“received”);
Serial.println(inputString);
pumpsignal(ArrayKey,9);
inputString = “”;
stringComplete = false;
}
}

void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == ‘\n’) {
stringComplete = true;

}
}
}

void shiftWrite(int desiredPin, boolean desiredState, int delaytime){
bitWrite(data,desiredPin,desiredState);
shiftOut(datapin, clockpin, MSBFIRST, data);
digitalWrite(latchpin, HIGH);
delay(delaytime);
digitalWrite(latchpin, LOW);
}

void pumpsignal(int Array[],int sizeOfArray){
while(Array[0]>0){
shiftWrite(1, HIGH, Array[0]*1000); // turn LED on
Serial.println(“rum”),
shiftWrite(1, LOW, 0); // turn LED off
Array[0]=0;
}
while(Array[1]>0){
shiftWrite(2, HIGH, Array[1]*1000); // turn LED on
Serial.println(“coke”),
shiftWrite(2, LOW, 0); // turn LED off
Array[1]=0;
}
while(Array[2]>0){
shiftWrite(3, HIGH, Array[2]*1000); // turn LED on
Serial.println(“vodka”),
shiftWrite(3, LOW, 0); // turn LED off
Array[2]=0;
}
while(Array[3]>0){
shiftWrite(4, HIGH,Array[3]*1000); // turn LED on
Serial.println(“tonic”),
shiftWrite(4, LOW, 0); // turn LED off
Array[3]=0;
}
while(Array[4]>0){
shiftWrite(5, HIGH,Array[4]*1000); // turn LED on
Serial.println(“cointreau”),
shiftWrite(5, LOW, 0); // turn LED off
Array[4]=0;
}
while(Array[5]>0){
shiftWrite(6, HIGH, Array[5]*1000); // turn LED on
Serial.println(“pinapple”),
shiftWrite(6, LOW, 0); // turn LED off
Array[5]=0;
}
while(Array[6]>0){
shiftWrite(7, HIGH, Array[6]*1000); // turn LED on
Serial.println(“coconut”),
shiftWrite(7, LOW, 0); // turn LED off
Array[6]=0;
}
while(Array[7]>0){
shiftWrite(8, HIGH, Array[7]*1000); // turn LED on
Serial.println(“lemon”),
shiftWrite(8, LOW, 0); // turn LED off
Array[7]=0;
}
while(Array[8]>0){
shiftWrite(9, HIGH, Array[8]*1000); // turn LED on
Serial.println(“cranberry”),
shiftWrite(9, LOW, 0); // turn LED off
Array[8]=0;
}

 

}

So, right now, I finally can use my Alexa to control my pumps.

Here is the video.