// includes start
#include <Arduino.h>
#include <Microduino_ColorLED.h>
#include <ESP8266.h>
#if defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega128RFA1__)
	#define EspSerial Serial1
	#define UARTSPEED 115200
	#define RESTORE 1
#endif
#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328__) || defined(__AVR_ATmega328P__)
	#include <SoftwareSerial.h>
	SoftwareSerial mySerial(2, 3);
	#define EspSerial mySerial
	#define UARTSPEED 9600
	#define RESTORE 0
#endif
ESP8266 wifi(&EspSerial);
#include <Microduino_Matrix.h>
#include <Servo.h>
#include <Microduino_Gesture.h>
// includes end
// definition start
bool wifimCottonbegin(const char* ssid, const char* pass, const char* id, const char* token);
void servo_move(Servo &servo, uint8_t anglebegin, uint8_t angleend, int _duration);
String uploadData(char* _st, int _data);
String uploadData(char* _st, char* _data);
bool gestureSta(uint8_t _gestureState);
void gggg();
// definition end
ColorLED strip_A0 = ColorLED(16, A0);
uint8_t Addr64[MatrixPix_X][MatrixPix_Y] = {{64}};
Matrix display64 = Matrix(Addr64, TYPE_COLOR);
uint8_t rgbArray_64_522240_6[8][8][3] = {{{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0}},{{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,255,0}},{{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,255,0},{0,0,0}},{{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,255,0},{0,0,0},{0,0,0}},{{0,255,0},{0,0,0},{0,0,0},{0,0,0},{0,255,0},{0,0,0},{0,0,0},{0,0,0}},{{0,0,0},{0,255,0},{0,0,0},{0,255,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0}},{{0,0,0},{0,0,0},{0,255,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0}},{{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0}}};
Servo servo_6;
uint8_t rgbArray_64_267386880_9[8][8][3] = {{{255,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{255,0,0}},{{0,0,0},{255,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{255,0,0},{0,0,0}},{{0,0,0},{0,0,0},{255,0,0},{0,0,0},{0,0,0},{255,0,0},{0,0,0},{0,0,0}},{{0,0,0},{0,0,0},{0,0,0},{255,0,0},{255,0,0},{0,0,0},{0,0,0},{0,0,0}},{{0,0,0},{0,0,0},{0,0,0},{255,0,0},{255,0,0},{0,0,0},{0,0,0},{0,0,0}},{{0,0,0},{0,0,0},{255,0,0},{0,0,0},{0,0,0},{255,0,0},{0,0,0},{0,0,0}},{{0,0,0},{255,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{255,0,0},{0,0,0}},{{255,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{255,0,0}}};
Gesture gestureSensor;
uint8_t gestureState;
int gi;
int time;
bool wifimCottonbegin(const char* ssid, const char* pass, const char* id, const char* token) {
	WifiInit(EspSerial, UARTSPEED);
	if (RESTORE) {
		wifi.restore();
		delay(2000);
	}
	Serial.print(F("Start Connection mCotton...Waiting for 5S..."));
	while (!wifi.setOprToStation());
	while (!wifi.joinAP(ssid, pass));
	wifi.setWiFiconnected(true);
	while (!wifi.mqttSetServer("mCotton.microduino.cn", (1883)));
	while (!wifi.mqttConnect(id, id, token));
	wifi.setMqttConnected(true);
	while (!wifi.mqttSetDiveceIDToken(id, token));
	char SUBSCRIBTOPICA[30] = "";
	strcat(SUBSCRIBTOPICA, "ca/");
	strcat(SUBSCRIBTOPICA, id);
	while (!wifi.mqttSetSubscrib(SUBSCRIBTOPICA));
	char SUBSCRIBTOPICP[30] = "";
	strcat(SUBSCRIBTOPICP, "cp/");
	strcat(SUBSCRIBTOPICP, id);
	while (!wifi.mqttSetSubscrib(SUBSCRIBTOPICP));
	return true;
}
void servo_move(Servo &servo, uint8_t anglebegin, uint8_t angleend, int _duration) {
	if (angleend > anglebegin) {
		for (uint8_t a = 0; a < angleend - anglebegin; a++) {
			servo.write(anglebegin + a);
			delay(_duration / (angleend - anglebegin));
		}
	} else if (angleend < anglebegin) {
		for (uint8_t a = 0; a < anglebegin - angleend; a++) {
			servo.write(anglebegin - a);
			delay(_duration / (anglebegin - angleend));
		}
	}
}
String uploadData(char* _st, int _data) {
	String send_data = "";
	send_data = "{\"";
	send_data += _st;
	send_data += "\":\"";
	send_data += _data;
	send_data += "\"}";
	return send_data;
}
String uploadData(char* _st, char* _data) {
	String send_data = "";
	send_data = "{\"";
	send_data += _st;
	send_data += "\":\"";
	send_data += _data;
	send_data += "\"}";
	return send_data;
}
bool gestureSta(uint8_t _gestureState) {
	if (gestureSensor.isGestureAvailable() ) {
		gestureState = gestureSensor.readGesture();
	}
	if (gestureState == _gestureState) {
		gestureState = 0;
		return true;
	} else {
		return false;
	}
}
void gggg(){
	while(!((!(((gi) == 0))) && (!(((gi) == ""))))) {
		if(gestureSta(DIR_UP)) {
			gi = 1;
			Serial.println("up");
		} else {
			if(gestureSta(DIR_DOWN)) {
				gi = 2;
				Serial.println("down");
			} else {
				if(gestureSta(DIR_LEFT)) {
					gi = 3;
					Serial.println("left");
				} else {
					if(gestureSta(DIR_RIGHT)) {
						gi = 4;
						Serial.println("right");
					} else {
						gi = 0;
					}
				}
			}
		}
	}
}
void setup() {
	// setup define start
	strip_A0.begin();
	Wire.begin();
	servo_6.attach(6);
	gestureSensor.begin();
	gestureSensor.setGestureGain(GGAIN_1X);
	gestureSensor.enableGestureSensor(true);
	// setup define end
	Serial.begin(57600);
	strip_A0.setPixelColor(0, 0x7a0606);
	strip_A0.show();
	if(wifimCottonbegin("microduino", "microduino", "5d5bac2b364ace0016335184", "7VR0JFzJCm6R")) {
		strip_A0.setPixelColor(0, 0x2c99f1);
		strip_A0.show();
		Serial.println("hello mCookie!");
	}
	time = 3;
}
void loop() {
	gi = 0;
	gggg();
	if(((gi) == 1)) {
		Serial.println("1");
		gi = 0;
		gggg();
		if(((gi) == 2)) {
			Serial.println("2");
			gi = 0;
			gggg();
			if(((gi) == 3)) {
				Serial.println("3");
				gi = 0;
				gggg();
				if(((gi) == 4)) {
					Serial.println("bingo");
					for (int y = 0; y < 8; y++) {
						for (int x = 0; x < 8; x++) {
							display64.setLedColor(x, y, rgbArray_64_522240_6[y][x][0], rgbArray_64_522240_6[y][x][1], rgbArray_64_522240_6[y][x][2]);
						}
					}
					servo_move(servo_6, 90, 180, 0.5 * 1000);
				}
			}
		}
	} else {
		for (int y = 0; y < 8; y++) {
			for (int x = 0; x < 8; x++) {
				display64.setLedColor(x, y, rgbArray_64_267386880_9[y][x][0], rgbArray_64_267386880_9[y][x][1], rgbArray_64_267386880_9[y][x][2]);
			}
		}
		time = (time)-1;
		Serial.print("TIME:");
		Serial.println(time);
		if(((time) == 0)) {
			wifi.mqttPublishM(uploadData("control_C1", "warning"));
			delay(1 * 1000);
			time = 3;
		}
	}
}
1. 手势密码防盗门
编程语言: C/C++(Arduino)
软件工具:Arduino IDE 1.8.4
创建时间:2019/08/26
作者: | 成瑶琪,吴紫玉,吴书玲 |
立即注册