gaztea tech robotica 2016

38
ROBOTICA GAZTEATECH 2016 SVET IVANTCHEV

Upload: svet-ivantchev

Post on 22-Jan-2018

364 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Gaztea Tech Robotica 2016

ROBOTICA GAZTEATECH 2016

SVET IVANTCHEV

Page 2: Gaztea Tech Robotica 2016

SETUP

• Arduino: https://www.arduino.cc/en/Main/Software

• Driver CH340 (si es necesario: OSX, Win 7,8. OK en Linux, Win10)

• Soporte para ESP8266:

• Ir a Arduino (o File) -> Preferences

• En “Additional Boards Managers URLs” poner:

http://arduino.esp8266.com/package_esp8266com_index.json

Page 3: Gaztea Tech Robotica 2016
Page 4: Gaztea Tech Robotica 2016

• Ir a Tools -> Board -> Boards Manager …

Page 5: Gaztea Tech Robotica 2016

• Buscar esp8266 e instalar:

Page 6: Gaztea Tech Robotica 2016

• Seleccionar la placa en: Tools -> Board -> NodeMCU 1.0 (ESP-12E Module)

Page 7: Gaztea Tech Robotica 2016

BLINK

Page 8: Gaztea Tech Robotica 2016
Page 9: Gaztea Tech Robotica 2016
Page 10: Gaztea Tech Robotica 2016
Page 11: Gaztea Tech Robotica 2016

int ledPin = D0; // pin LED

void setup() { // nothing happens in setup}

void loop() { // fade in from min to max in increments of 5 points: for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) { analogWrite(ledPin, fadeValue); delay(30); }

// fade out from max to min in increments of 5 points: for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) { analogWrite(ledPin, fadeValue); delay(30); }}

FADE

Page 12: Gaztea Tech Robotica 2016

LDR

Page 13: Gaztea Tech Robotica 2016

void setup() { Serial.begin(9600);}

void loop() { // read the input on analog pin 0: int sensorValue = analogRead(A0); // Convert the analog reading (which goes from 0 - 1023) // to a voltage (0 - 3.3V): float voltage = sensorValue * (3.3 / 1023.0); Serial.println(voltage);}

LDR

Page 14: Gaztea Tech Robotica 2016

SERVO

Page 15: Gaztea Tech Robotica 2016

#include <Servo.h> Servo myservo; // create servo object to control a servo

void setup() { myservo.attach(D8); // connect the servo to D8 } void loop() { int pos;

for(pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } for(pos = 180; pos>=0; pos-=1) { // goes from 180 degrees to 0 degrees myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } }

SERVO

Page 16: Gaztea Tech Robotica 2016

#define TRIGGER 12 // D6#define ECHO 13 // D7 (with voltage divider!)

void setup() { Serial.begin (9600); pinMode(TRIGGER, OUTPUT); pinMode(ECHO, INPUT); pinMode(BUILTIN_LED, OUTPUT); pinMode(D0, OUTPUT);}

void loop() { long duration, distance; digitalWrite(TRIGGER, LOW); delayMicroseconds(2); digitalWrite(TRIGGER, HIGH); delayMicroseconds(10); digitalWrite(TRIGGER, LOW); duration = pulseIn(ECHO, HIGH); distance = (duration/2) / 29.1; Serial.println(distance); if (distance < 10) digitalWrite(D0, LOW); else digitalWrite(D0, HIGH); delay(300);}

HC-SR04

Page 17: Gaztea Tech Robotica 2016

NEOPIXEL

Page 18: Gaztea Tech Robotica 2016

GND

+3.3VD8

Dout DoutDin Din

pixel 0 pixel 1 pixel 2

Page 19: Gaztea Tech Robotica 2016

#include <Adafruit_NeoPixel.h>

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(3, D8, NEO_GRB + NEO_KHZ800);

void setup() { pixels.begin(); pixels.show(); }

void loop() { for(int i=0; i<3; i++) { pixels.setPixelColor(i, pixels.Color(0, 150, 0)); pixels.show(); delay(300); }}

NEOPIXELS I

Nota: Instalar la librería NeoPixel si en necesario desde Sketch -> Include Library -> Manage Libraries …

Page 20: Gaztea Tech Robotica 2016

#include <Adafruit_NeoPixel.h>

Adafruit_NeoPixel strip = Adafruit_NeoPixel(3, D8, NEO_GRB + NEO_KHZ800);

void setup() { strip.begin(); strip.show(); // Initialize all pixels to 'off'}

void loop() { colorWipe(strip.Color(255, 0, 0), 20); // Red colorWipe(strip.Color(0, 255, 0), 20); // Green colorWipe(strip.Color(0, 0, 255), 20); // Blue}

// Fill the dots one after the other with a colorvoid colorWipe(uint32_t c, uint8_t wait) { for(uint16_t i=0; i<strip.numPixels(); i++) { strip.setPixelColor(i, c); strip.show(); delay(wait); }}

NEOPIXEL II

Page 21: Gaztea Tech Robotica 2016

MQTT

MQTT BROKER

CLIENTE CLIENTE

CLIENTE

CLIENTE CLIENTE

CLIENTE

publish

publish

publish

subscribe

subscribe

“topic” -> “message”

Page 22: Gaztea Tech Robotica 2016

MQTT UI

Page 23: Gaztea Tech Robotica 2016

RED LOCAL ENTRE LOS ROBOTS

WIFI

Nombre de la red WiFi: Pi3-AP

Contraseña: raspberry

IP del broker MQTT: 172.24.1.1

Alternativamente puede considerar algún servicio público de MQTT como por ejemploel servidor de pruebas de HiveMQ: broker.hivemq.com

Page 24: Gaztea Tech Robotica 2016

CLIENTE MQTT PARA CHROME

Page 25: Gaztea Tech Robotica 2016

CLIENTE MQTT PARA ANDROID

Page 26: Gaztea Tech Robotica 2016

CLIENTE MQTT PARA IOS

Page 27: Gaztea Tech Robotica 2016

TOPICS Y REGLAS

r1/ldr 2.1r2/ldr 1.4r42/ldr 1.0r13/ip Hello from …

r1/cmd/led1 onr42/cmd/led2 offr42/cmd/led1 on

r1/cmd/motL 800r1/cmd/motR 800

r13/cmd/rgb 016:200:000r42/cmd/rgbhex #0FAA00

De cada robot al MQTT:

Del MQTT a los robots:

Posibles suscripciones:

r1/+

+/ldr

r42/#

r13/cmd/+

+/ip

Page 28: Gaztea Tech Robotica 2016

remote_001

#include <ESP8266WiFi.h>#include <PubSubClient.h>

const char* ssid = "Pi3-AP";const char* password = "raspberry";const char* mqtt_server = "172.24.1.1";

char msg[50];long ldr;

WiFiClient espClient;PubSubClient client(espClient);long lastMsg = 0;

void setup() { Serial.begin(9600); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println("WiFi connected. IP address: "); Serial.println(WiFi.localIP()); client.setServer(mqtt_server, 1883);}

void reconnect() { while (!client.connected()) { Serial.print("Attempting MQTT connection..."); if (client.connect("ESP8266Client")) { Serial.println("connected"); client.publish("r1/ip", "Hello world from R1"); } else { Serial.println(" try again in 5 seconds"); delay(5000); } }}

void loop() { if (!client.connected()) { reconnect(); } client.loop();

long now = millis(); if (now - lastMsg > 500) { lastMsg = now; ldr = analogRead(A0); snprintf (msg, 75, "%ld", ldr); client.publish("r1/ldr", msg); } }

COMO PUBLICAR EN MQTT

Page 29: Gaztea Tech Robotica 2016

#include <ESP8266WiFi.h>#include <PubSubClient.h>

const char* ssid = "Pi3-AP";const char* password = "raspberry";const char* mqtt_server = "172.24.1.1";long lastMsg = 0;char msg[50];long ldr;

WiFiClient espClient;PubSubClient client(espClient);

void setup() { pinMode(D0, OUTPUT); pinMode(D1, OUTPUT); setup_wifi(); client.setServer(mqtt_server, 1883); client.setCallback(callback); Serial.begin(9600);}

void setup_wifi() { delay(20); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); }

Serial.println(""); Serial.println("WiFi connected. IP address: "); Serial.println(WiFi.localIP());}

void callback(char* topic, byte* payload, unsigned int length) { String topic_str(topic); String cmd; String param; for (int i = 0; i < length; i++) { param += (char)payload[i]; }

Serial.print("Message arrived: "); Serial.print(topic); Serial.print(" "); Serial.println(param);

// "r1/cmd/abc" -> abc cmd = topic_str.substring( topic_str.lastIndexOf('/')+1 ); if (cmd == "led1") { if (param=="on") digitalWrite(D0, LOW); else digitalWrite(D0, HIGH); } else if (cmd == "led2") { if (param=="on") digitalWrite(D1, HIGH); else digitalWrite(D1, LOW); } }

void reconnect() { while (!client.connected()) { Serial.print("Attempting MQTT connection..."); if (client.connect("ESP8266Client")) { Serial.println("connected"); client.publish("r1/ip", "Hello world from R1"); client.subscribe("r1/cmd/+"); } else { Serial.print("failed, rc="); Serial.print(client.state()); Serial.println(" try again in 5 seconds"); delay(5000); } }}

la función void loop() es igual que en el ejemplo anterior

PUBLISH Y SUBSCRIBE CON MQTT

Page 30: Gaztea Tech Robotica 2016

HTTPS://PROCESSING.ORG

PROCESSING

Page 31: Gaztea Tech Robotica 2016

void setup() { size(400,400);}

void draw() { ellipse(mouseX, mouseY, 60, 60);}

Page 32: Gaztea Tech Robotica 2016

MQTT CON PROCESSINGimport mqtt.*;

MQTTClient client;int ldrvalue;

void setup() { client = new MQTTClient(this); client.connect("mqtt://172.24.1.1", "proc1"); client.subscribe("r1/ldr"); size(500,500);}

void draw() { clear(); ellipse(250, 250, ldrvalue/2, ldrvalue/2);}

void messageReceived(String topic, byte[] payload) { String spayload = new String(payload); ldrvalue = int(spayload); println("new message: " + topic + " - " + spayload);}

mqtt_rec

Page 33: Gaztea Tech Robotica 2016

PUBLICAR EN MQTT

import mqtt.*;

MQTTClient client;

void setup() { client = new MQTTClient(this); client.connect("mqtt://172.24.1.1", "pc42");}

void draw() {}

void keyPressed() { switch(key) { case 'a': client.publish("/r42/led1", "on"); break; case 'z': client.publish("/r42/led1", "off"); break; }}

mqtt_001

Page 34: Gaztea Tech Robotica 2016

!!! VM-NC cuando esta conectado

al ordenador y a la batería

VM+

-

motor A

motor B

CONEXIONES DE MOTORES DC

Page 35: Gaztea Tech Robotica 2016

CONTROL DE MOTORES DC

pinMode(D1, OUTPUT);pinMode(D2, OUTPUT);pinMode(D3, OUTPUT);pinMode(D4, OUTPUT);

digitalWrite(D3, HIGH); // Dirección Motor AdigitalWrite(D4, HIGH); // Dirección Motor B

analogWrite(D1, 800); // Velocidad Motor AanalogWrite(D2, 800); // Velocidad Motor A

En el setup

Control

Page 36: Gaztea Tech Robotica 2016

CONTROL DE ENCHUFES 433MHZ

MQTT Y RC

Page 37: Gaztea Tech Robotica 2016

CÓDIGOS

• Instalar la librería rc-switch de https://github.com/sui77/rc-switch/

• Cargar File -> Examples -> rc-switch -> ReceiveDemo_Advanced

Page 38: Gaztea Tech Robotica 2016

#include <ESP8266WiFi.h>#include <PubSubClient.h>#include <RCSwitch.h>

const char* ssid = "Pi3-AP";const char* password = "raspberry";const char* mqtt_server = "172.24.1.1";long lastMsg = 0;char msg[50];long ldr;

WiFiClient espClient;PubSubClient client(espClient);RCSwitch mySwitch = RCSwitch();

void setup() { pinMode(D0, OUTPUT); setup_wifi(); client.setServer(mqtt_server, 1883); client.setCallback(callback); mySwitch.enableTransmit(16); Serial.begin(9600);}

void setup_wifi() { delay(20); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); }

Serial.println(""); Serial.println("WiFi connected. IP address: "); Serial.println(WiFi.localIP());}

void callback(char* topic, byte* payload, unsigned int length) { String topic_str(topic); String cmd; String param; for (int i = 0; i < length; i++) { param += (char)payload[i]; }

Serial.print("Message arrived: "); Serial.print(topic); Serial.print(" "); Serial.println(param);

// "r1/cmd/abc" -> abc cmd = topic_str.substring( topic_str.lastIndexOf('/')+1 ); if (cmd == "plug1") { if (param=="on") mySwitch.send("010001000000000000010100"); else mySwitch.send("010001000000000000000000"); } else if (cmd == "plug2") { if (param=="on") mySwitch.send("010001000100000000010100"); else mySwitch.send("010001000100000000000000"); } }

void reconnect() { while (!client.connected()) { Serial.print("Attempting MQTT connection..."); if (client.connect("ESP8266Client")) { Serial.println("connected"); client.subscribe("svet/office/+"); } else { Serial.print("failed, rc="); Serial.print(client.state()); Serial.println(" try again in 5 seconds"); delay(5000); } }}

la función void loop() es igual que en los ejemplos anteriores

CONTROL CON MQTT