Yes of course here the code of the MKR.
#include <ArduinoHttpClient.h>
#include <WiFiNINA.h>
/////// WiFi Settings ///////
char ssid[] = "...";
char pass[] = "...";
char serverAddress[] = "..."; // server address
int port = 8025;
WiFiClient wifi;
WebSocketClient client = WebSocketClient(wifi, serverAddress, port);
int status = WL_IDLE_STATUS;
int count = 0;
void setup() {
Serial.begin(9600);
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to Network named: ");
Serial.println(ssid); // print the network name (SSID);
// Connect to WPA/WPA2 network:
status = WiFi.begin(ssid, pass);
}
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
}
void loop() {
Serial.println("starting WebSocket client");
client.begin();
while (client.connected()) {
// check if a message is available to be received
int messageSize = client.parseMessage();
if (messageSize > 0) {
Serial.println("Received a message:");
String a = client.readString();
Serial.write(a.c_str());
}
// wait 5 seconds
delay(5000);
}
Serial.println("disconnected");
}
And here the UNO's code.
#include <Wire.h>
#include <EVShield.h>
#include <EVs_EV3Touch.h>
#include <EVShieldAGS.h>
#include <EVs_EV3Infrared.h>
#define NOTE_B3 247
#define distancePin A0
#define ULTRASONIC_TRIG_PIN 11
#define ULTRASONIC_ECHO_PIN 12
EVShield evshield(0x34, 0x36);
char input;
String inputString = "";
bool stringComplete = false;
int velocity = 0;
int rotation = 0;
uint16_t value;
uint16_t range;
float distance;
//float batteryVoltage = 0;
void setup() {
Serial.begin(9600);
evshield.init(SH_HardwareI2C);
evshield.bank_a.motorReset();
evshield.bank_b.motorReset();
velocity = SH_Speed_Slow;
pinMode (distancePin, INPUT);
pinMode(ULTRASONIC_TRIG_PIN, OUTPUT);
pinMode(ULTRASONIC_ECHO_PIN, INPUT);
}
void loop() {
value = analogRead (distancePin);
range = get_gp2d12 (value);
digitalWrite(ULTRASONIC_TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(ULTRASONIC_TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(ULTRASONIC_TRIG_PIN, LOW);
long duration = pulseIn(ULTRASONIC_ECHO_PIN, HIGH);
distance=(duration/2.0)*0.0343;
if (stringComplete) {
Serial.println(inputString);
inputString = "";
stringComplete = false;
}
if (range < 100) {
Serial.print(distance);
Serial.println("cm");
Serial.print(range);
Serial.println(" mm");
evshield.bank_b.motorStop(SH_Motor_1, SH_Next_Action_BrakeHold);
evshield.bank_b.motorStop(SH_Motor_2, SH_Next_Action_BrakeHold);
}
else {
if (Serial.available() > 0) {
input = Serial.read();
if (input == 'w' || input == 'W') {
if (rotation == 1) {
evshield.bank_a.motorRunDegrees(SH_Motor_Both, SH_Direction_Reverse, velocity, 90, SH_Completion_Dont_Wait, SH_Next_Action_BrakeHold);
} else if (rotation == -1) {
evshield.bank_a.motorRunDegrees(SH_Motor_Both, SH_Direction_Forward, velocity, 90, SH_Completion_Dont_Wait, SH_Next_Action_BrakeHold);
}
rotation = 0;
Serial.println(input);
evshield.bank_b.motorRunUnlimited(SH_Motor_Both, SH_Direction_Forward, velocity);
}
else if (input == 'a' || input == 'A') {
if (rotation == 1) {
evshield.bank_a.motorRunDegrees(SH_Motor_Both, SH_Direction_Reverse, velocity, 180, SH_Completion_Dont_Wait, SH_Next_Action_BrakeHold);
} else if (rotation == 0) {
evshield.bank_a.motorRunDegrees(SH_Motor_Both, SH_Direction_Reverse, velocity, 90, SH_Completion_Dont_Wait, SH_Next_Action_BrakeHold);
}
rotation = -1;
Serial.println(input);
evshield.bank_b.motorRunUnlimited(SH_Motor_Both, SH_Direction_Forward, velocity);
}
else if (input == 's' || input == 'S') {
Serial.println(input);
evshield.bank_b.motorRunUnlimited(SH_Motor_Both, SH_Direction_Reverse, velocity);
}
else if (input == 'd' || input == 'D') {
if(rotation==-1){
evshield.bank_a.motorRunDegrees(SH_Motor_Both, SH_Direction_Forward, velocity, 180, SH_Completion_Dont_Wait, SH_Next_Action_BrakeHold);
}
else if(rotation==0){
evshield.bank_a.motorRunDegrees(SH_Motor_Both, SH_Direction_Forward, velocity, 90, SH_Completion_Dont_Wait, SH_Next_Action_BrakeHold);
}
rotation=1;
Serial.println(input);
evshield.bank_b.motorRunUnlimited(SH_Motor_Both, SH_Direction_Forward, velocity);
}
else if (input == 'c' || input == 'C') {
Serial.println(input);
tone(8, NOTE_B3, 1000 / 4);
int pauseBetweenNotes = (1000 / 4) * 1.30;
delay(pauseBetweenNotes);
noTone(8);
}
else if (input == 17) { //CTRL
Serial.println(input);
if (velocity == SH_Speed_Slow) {
velocity = SH_Speed_Medium;
}
else if (velocity == SH_Speed_Medium) {
velocity = SH_Speed_Full;
}
}
else if (input == 16) { //SHIFT
Serial.println(input);
if (velocity == SH_Speed_Full) {
velocity = SH_Speed_Medium;
}
else if (velocity == SH_Speed_Medium) {
velocity = SH_Speed_Slow;
}
}
else if (input == ' ') {
Serial.println(input);
evshield.bank_b.motorStop(SH_Motor_Both, SH_Next_Action_Brake);
}
}
}
}
void serialEvent() {
while (Serial.available()) {
char inChar = (char)Serial.read();
inputString += inChar;
if (inChar == '\n') {
stringComplete = true;
}
}
}
uint16_t get_gp2d12 (uint16_t value) {
if (value < 10) value = 10;
return ((67870.0 / (value - 3.0)) - 40.0);
}