Hackster is hosting Hackster Holidays, Finale: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Tuesday!Stream Hackster Holidays, Finale on Tuesday!
vicente zavala
Published © LGPL

IoT Speech Recognition

Control a servo, LED lamp or any device connected to WiFi, using Android app.

BeginnerProtip9,264
IoT Speech Recognition

Things used in this project

Hardware components

Arduino Micro
Arduino Micro
×1
SparkFun WiFi Breakout - CC3000
×1
SparkFun Servo - Generic Metal Gear (Micro Size)
×1
LED (generic)
LED (generic)
×1
Android device
Android device
×1

Software apps and online services

IoT Speech Recognition
IoT WiFi | Bluetooth Continuous Speech Recognition

Story

Read more

Schematics

basic schematic

Code

ATmega32u4_IOT_LED_Servo

Arduino
/*
*	Author		: Zavala Ortiz Vicente Arturo.
*	language	: .cpp
*	Created		: 3/11/2015 11:56:57 AM
*	Name		: ATmega32u4_IOT_LED_Servo.cpp
*	Update		: 1/9/2017 02:29:15 PM
*/

#include <util/delay.h>
#include <stdlib.h>

#include "Servo.h"
#include "Network.h"

/*****		wifi Global Variables		*****/
// Connection info data lengths
#define MAX_MSG_LEN							100
#define SERVO_PIN								10
#define LED_PIN								  9

// Constants
char ap_ssid[]				    = "SSID of network";
char ap_password[]			  = "Password of network";

unsigned int ap_security	= WLAN_SEC_WPA2;	// Security of network
unsigned long timeout	  	= 3000;				// Milliseconds

// Global Variables
CC3000		wifi		= CC3000();
NETWORK		client  = NETWORK(wifi);

char in_message[MAX_MSG_LEN]	= {0};

/*****		Other Global Variables		*****/
uint8_t	  	i;
static char data[20];
double		  value = 0;

Servo servo;	// create servo object to control a servo

/*****		Motors Global Variables		*****/
void setup()
{
	/* add setup code here */
	ConnectionInfo connection_info;
	
	// Initialize CC3000 (configure SPI communications)
	#ifdef CC3000_DEBUG
		Serial.begin(115200);
	#endif
	
	// Initialize CC3000 (configure SPI communications)
	if(!wifi.init(SS)) {
		#ifdef CC3000_DEBUG
			DEBUGPRINTLN(PSTR("Initialize CC3000 FAIL!"));
		#endif
		return;
	}

	else {
    #ifdef CC3000_DEBUG
		  DEBUGPRINTLN(PSTR("Initialize CC3000 OK"));
		#endif
	}
	
	uint8_t connection_cnt = 0;
	while(connection_cnt <= WL_MAX_ATTEMPT_CONNECTION)
	{
		#ifdef CC3000_DEBUG
			DEBUGPRINT(PSTR("Connection try: "));
			printDec(connection_cnt);
			DEBUGPRINT(PSTR("\n"));
		#endif

		if(!wifi.connect(ap_ssid, ap_security, ap_password, timeout)) {
			#ifdef CC3000_DEBUG
				DEBUGPRINTLN(PSTR("Error: Could not connect to AP!"));
			#endif
		}

		else
		break;
		
		connection_cnt++;
		_delay_ms(2000);
	}
	
	// Gather connection details and print IP address
	if(!wifi.getConnectionInfo(connection_info) )
	{
		#ifdef CC3000_DEBUG
			DEBUGPRINTLN(PSTR("Error: Could not obtain connection details"));
		#endif
		
		return;
	}
	
	else
	{
		#ifdef CC3000_DEBUG
			DEBUGPRINT(PSTR("IP Address: "));
			printIPAddr(connection_info.ip_address);
		#endif
	}
	
	pin_mode(LED_PIN, OUTPUT);
	servo.attach(SERVO_PIN);  // attaches the servo on pin 9 to the servo object
	servo.write(0);
	_delay_ms(20);
}

void loop()
{
	/* add main program code here */	
	if(!client.isOpen())	
	{
		if(client.open(UDP_SOCKET) == 0) {
			DEBUGPRINT(PSTR("Create SOCKET ERROR!"));
			client.close();
		}
		
		if(!client.Bind(4443)) {
			#ifdef CC3000_DEBUG
				DEBUGPRINTLN(PSTR("Binding SOCKET Error!"));
			#endif
		}
	}
	
	#ifdef CC3000_DEBUG
		DEBUGPRINTLN(PSTR("WAITING FOR MESSAGE!"));
	#endif
	
	if(client.readFrom(4443, in_message, MAX_MSG_LEN, SOCKOPT_RECV_TIMEOUT) == -1) 
	{
		#ifdef CC3000_DEBUG
			DEBUGPRINTLN(PSTR("NO MESSAGE RECEIVED"));
		#endif
	}
	
	else 
	{
		if(client.http_gets(data, "word", in_message))
		{
			if((strcmp(data, "left") == 0 )) {
				servo.write(180);                  // sets the servo position according to the scaled value
				_delay_ms(20);
			}
		
			else if((strcmp(data, "center") == 0)) {
				servo.write(90);                  // sets the servo position according to the scaled value
				_delay_ms(20);
			}
		
			else if((strcmp(data, "right") == 0)) {
				servo.write(0);                  // sets the servo position according to the scaled value
				_delay_ms(20);
			}
			
			else if((strcmp(data, "on") == 0)) {
				 value = atoi(data);				 
				 digitalWrite(LED_PIN, HIGH);
				
				_delay_ms(20);
			}
			
			else if((strcmp(data, "off") == 0)) {
				value = atoi(data);
				digitalWrite(LED_PIN, LOW);
				
				_delay_ms(20);
			}	
		}
	}
	
	client.fulsh_buffer(in_message);
	client.fulsh_buffer(data);
	_delay_ms(20);
}

Credits

vicente zavala
11 projects • 24 followers
Freelance Programmer, Internet Of Things (IOT), CNC Plasma Cutting Creator, Software Developer.

Comments