Arduino uno met internetmodule

Arduino specifieke Software
Berichten: 1
Geregistreerd: 25 Nov 2018, 16:37

Arduino uno met internetmodule

Berichtdoor joostgrunwald » 25 Nov 2018, 16:42

Ik ben samen met een vriend bezig met ons profielwerkstuk waarin we een robotje aan het bouwen zijn die moet fungeren als autonome pakketbezorger.
Alles werkt eigenlijk vrij soepel, maar we krijgen de wifi module maar niet goed geconfigureerd.

De code bestaat uit twee onderdelen: een aanstuurder geschreven in visual studio c sharp en een client alsin de arduino.
Via een kabeltje werkt dit prima maar via wifi lukt het ons niet om een goed resultaat te vidnen.

Error
- niet specifiek een error maar communicatie via de seriele monitor op 115200 baud levert totaal geen resultaten op

Code: Alles selecteren
//arduino delivery robot source code
//written in Arduino, .NET environment

///////////////////////////////////////////////
//fase 1 initializing constants//
//////////////////////////////////////////////

//wifi
#include <SoftwareSerial.h>

SoftwareSerial wifiSerial(11, 12);
bool DEBUG = true;   //show more logs
int responseTime = 25; //communication timeout

//pins
const int motorpinLV = 3;
const int motorpinRV = 2;
const int motorpinLA = 13;
const int motorpinRA = 10;
const int echoPin = 5;
const int trigPin = 4;
const int echoPinR = 8;
const int trigPinR = 9;
const int echoPinL = 7;
const int trigPinL = 6;
const int buzzerPin = 3;
//constants
long durationfront = 0;
int distancefront = 0;
long durationleft = 0;
int distanceleft = 0;
long durationright = 0;
int distanceright = 0;
int counter = 0;
int speedRV = 0;
int speedLV = 0;
int speedRA = 0;
int speedLA = 0;
int leftcounter = 0;
int rightcounter = 0;

int rpmV = 0; //rotations a minute for the front motors (less powerful)
int rpmA = 16500; //rotations a minute for the back motors (more powerfull, 4 weel based driving)
int millperdegreeV = 1/rpmV * 60000 * 360;   //1000 for minute - ms, 360 degrees
int millperdegreeA = 1/rpmA * 60000 * 360; //1000 for minute - ms, 360 degrees
float timecodeexcecutes = 0; //use arduino executed time counter
float timefordegreeV = millperdegreeV + timecodeexcecutes;
float timefordegreeA = millperdegreeA + timecodeexcecutes;

//computercommunication
String inputString = "";         // a string to hold incoming data
boolean stringComplete = false;  // whether the string is complete
String commandString = "";
boolean isConnected = false;
//////////////////////////////
//fase 2 setting up//
//////////////////////////////
void setup() {
 //configuring pins
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT); //we throw out the triggerpin and take in the echopin.
 pinMode(trigPinR, OUTPUT);
 pinMode(echoPinR, INPUT);
 pinMode(trigPinL, OUTPUT);
 pinMode(echoPinL, INPUT);
 pinMode(motorpinLV, OUTPUT);
 pinMode(motorpinRV, OUTPUT);
 pinMode(motorpinLA, OUTPUT);
 pinMode(motorpinRA, OUTPUT);
 pinMode(buzzerPin, OUTPUT);


 Serial.begin(115200); // Starts the serial communication
 wifiSerial.begin(115200);
 Serial.println("<Arduino is ready>"); //tell the pc we are ready.

  sendToWifi("AT+CWMODE=2",responseTime,DEBUG); // configure as access point
  sendToWifi("AT+CIFSR",responseTime,DEBUG); // get ip address
  sendToWifi("AT+CIPMUX=1",responseTime,DEBUG); // configure for multiple connections
  sendToWifi("AT+CIPSERVER=1,80",responseTime,DEBUG); // turn on server on port 80
 
  sendToUno("Wifi connection is running!",responseTime,DEBUG);
}
////////////////////////////////////
//fase 3 constant loop//
////////////////////////////////////
void loop() {

 //wificonfiguration
  if(Serial.available()>0){
     String message = readSerialMessage();
    if(find(message,"debugEsp8266:")){
      String result = sendToWifi(message.substring(13,message.length()),responseTime,DEBUG);
      if(find(result,"OK"))
        sendData("\nOK");
      else
        sendData("\nEr");
    }
  }
  if(wifiSerial.available()>0){
   
    String message = readWifiSerialMessage();
   
    if(find(message,"esp8266:")){
       String result = sendToWifi(message.substring(8,message.length()),responseTime,DEBUG);
      if(find(result,"OK"))
        sendData("\n"+result);
      else
        sendData("\nErrRead");               //At command ERROR CODE for Failed Executing statement
    }else
    if(find(message,"HELLO")){  //receives HELLO from wifi
        sendData("\\nHI!");    //arduino says HI
    }else if(find(message,"LEDON")){
      //sending ph level:
      digitalWrite(13,HIGH);
    }else if(find(message,"LEDOFF")){
      //sending ph level:
      digitalWrite(13,LOW);
    }
    else{
      sendData("\nErrRead");                 //Command ERROR CODE for UNABLE TO READ
    }
  }
  delay(responseTime);

 
 measuredistanceF(); //runs the void measuredistance for the front, then for the left and right,
 measuredistanceR(); //these voids measure the distance to object,
 measuredistanceL(); //therefore make sure it won’t crash.
 unsigned long now1 = millis(); //now1 = time application is running
 
 if(stringComplete)
{
 stringComplete = false;
 getCommand();
 if(commandString.equals("STAR")) //star is the 4-digit version of starting up
 {
    speedRV = 0;
    speedLV = 0;
    speedRA = 0;
    speedLA = 0;
 }
 else if (commandString.equals("STOP")) //speaks for itself
 {
    speedRV = 0;
    speedLV = 0;
    speedRA = 0;
    speedLA = 0;
    noTone(3); //3 is pin 3, equals buzzer
 }
 
 //////////////////////////////////////
 //forward gearbox start//
 //////////////////////////////////////
 else if(commandString.equals("FORW"))
 {
    speedRV = 0;
    speedLV = 0;
    speedRA = 0;
    speedLA = 0;
    tone(3, 0);
 }
 else if(commandString.equals("FOR1")) //1 stands for gear 1
 {
    if (distancefront < 75 && distancefront > 15) //case 1 attempt to turn
     {
       avoidobstacle();
     }
     else if (distancefront < 15) //to close to an object to turn - point should not be reached
     {
       speedRV = 0;
       speedLV = 0;
       speedRA = 0;
       speedLA = 0;
     }
     else //nothing blocks it
     {
       if (rightcounter > 0) //go back from avoiding a obstacle going to the right
       {
           speedRV = 255;
           speedRA = 255;
           speedLV = 0;
           speedLA = 0;
           rightcounter = rightcounter - 1;
       }
       else if (leftcounter > 0)
       {
           speedRV = 0;
           speedRA = 0;
           speedLV = 255;
           speedLA = 255;
           leftcounter = leftcounter - 1;
       }
       else
       {
           speedRV = 64;
           speedLV = 64;
           speedRA = 64;
           speedLA = 64;
           tone(3, 0);
       }
     }
 }
 else if(commandString.equals("FOR2")) //second gear
 {
    if (distancefront < 75 && distancefront > 15) //case 1 attempt to turn
     {
       avoidobstacle();
     }
     else if (distancefront < 15) //to close to an object to turn - point should not be reached
     {
       speedRV = 0;
       speedLV = 0;
       speedRA = 0;
       speedLA = 0;
     }
     else //nothing blocks it
     {
       if (rightcounter > 0) //go back from avoiding a obstacle going to the right
       {
           speedRV = 255;
           speedRA = 255;
           speedLV = 0;
           speedLA = 0;
           rightcounter = rightcounter - 1;
       }
       else if (leftcounter > 0)
       {
           speedRV = 0;
           speedRA = 0;
           speedLV = 255;
           speedLA = 255;
           leftcounter = leftcounter - 1;
       }
       else
       {
           speedRV = 127;
           speedLV = 127;
           speedRA = 127;
           speedLA = 127;
           tone(3, 0);
       }
     }
 }
 else if(commandString.equals("FOR3")) //third gear
 {
    if (distancefront < 75 && distancefront > 15) //case 1 attempt to turn
     {
       avoidobstacle();
     }
     else if (distancefront < 15) //to close to an object to turn - point should not be reached
     {
       speedRV = 0;
       speedLV = 0;
       speedRA = 0;
       speedLA = 0;
     }
     else //nothing blocks it
     {
       if (rightcounter > 0) //go back from avoiding a obstacle going to the right
       {
           speedRV = 255;
           speedRA = 255;
           speedLV = 0;
           speedLA = 0;
           rightcounter = rightcounter - 1;
       }
       else if (leftcounter > 0)
       {
           speedRV = 0;
           speedRA = 0;
           speedLV = 255;
           speedLA = 255;
           leftcounter = leftcounter - 1;
       }
       else
       {
           speedRV = 153;
           speedLV = 153;
           speedRA = 153;
           speedLA = 153;
           tone(3, 0);
       }
     }
 }
 else if(commandString.equals("FOR4")) //fourth gear
 {
    if (distancefront < 75 && distancefront > 15) //case 1 attempt to turn
     {
       avoidobstacle();
     }
     else if (distancefront < 15) //to close to an object to turn - point should not be reached
     {
       speedRV = 0;
       speedLV = 0;
       speedRA = 0;
       speedLA = 0;
     }
     else //nothing blocks it
     {
       if (rightcounter > 0) //go back from avoiding a obstacle going to the right
       {
           speedRV = 255;
           speedRA = 255;
           speedLV = 0;
           speedLA = 0;
           rightcounter = rightcounter - 1;
       }
       else if (leftcounter > 0)
       {
           speedRV = 0;
           speedRA = 0;
           speedLV = 255;
           speedLA = 255;
           leftcounter = leftcounter - 1;
       }
       else
       {
           speedRV = 191;
           speedLV = 191;
           speedRA = 191;
           speedLA = 191;
           tone(3, 0);
       }
     }
 }
 else if(commandString.equals("FORS")) //fifth gear, super mode, extra speed
 {
    if (distancefront < 75 && distancefront > 15) //case 1 attempt to turn
     {
       avoidobstacle();
     }
     else if (distancefront < 15) //to close to an object to turn - point should not be reached
     {
       speedRV = 0;
       speedLV = 0;
       speedRA = 0;
       speedLA = 0;
     }
     else //nothing blocks it
     {
       if (rightcounter > 0) //go back from avoiding a obstacle going to the right
       {
           speedRV = 255;
           speedRA = 255;
           speedLV = 0;
           speedLA = 0;
           rightcounter = rightcounter - 1;
       }
       else if (leftcounter > 0)
       {
           speedRV = 0;
           speedRA = 0;
           speedLV = 255;
           speedLA = 255;
           leftcounter = leftcounter - 1;
       }
       else
       {
           speedRV = 255;
           speedLV = 255;
           speedRA = 255;
           speedLA = 255;
           tone(3, 10);
       }
     }
 }
 ///////////////////////////////
 //left gearbox start//
 ///////////////////////////////
 else if (commandString.equals("LEFF")) //left free gear
 {
   speedRV = 0;
   speedRA = 0;
   speedLV = 0;
   speedLA = 0;
   tone(3, 1000); //replaced star wars melody with tone to save cpu resources
 }
 else if (commandString.equals("LEF1")) //left first gear
 {
   speedRV = 64;
   speedRA = 64;
   speedLV = 0;
   speedLA = 0;
   tone(3, 1000); //replaced star wars melody with tone to save cpu resources
 }
 else if (commandString.equals("LEF2")) //left second gear
 {
   speedRV = 127;
   speedRA = 127;
   speedLV = 0;
   speedLA = 0;
   tone(3, 1000); //replaced star wars melody with tone to save cpu resources
 }
 else if (commandString.equals("LEF3"))
 {
   speedRV = 153;
   speedRA = 153;
   speedLV = 0;
   speedLA = 0;
   tone(3, 1000); //replaced star wars melody with tone to save cpu resources
 }
 else if (commandString.equals("LEF4"))
 {
   speedRV = 191;
   speedRA = 191;
   speedLV = 0;
   speedLA = 0;
   tone(3, 1000); //replaced star wars melody with tone to save cpu resources
 }
 else if (commandString.equals("LEFS")) //super gear mode
 {
   speedRV = 255;
   speedRA = 255;
   speedLV = 0;
   speedLA = 0;
   tone(3, 10); //replaced star wars melody with tone to save cpu resources
 }
 /////////////////////////////////
 //right gearbox start//
 /////////////////////////////////
 else if (commandString.equals("RIGF")) //right, gear = free
 {
   speedRV = 0;
   speedRA = 0;
   speedLV = 0;
   speedLA = 0;
    tone(3, 100); //replaced star wars melody with tone to save cpu resources
 }
 else if (commandString.equals("RIG1"))
 {
   speedRV = 0;
   speedRA = 0;
   speedLV = 64;
   speedLA = 64;
   tone(3, 100); //replaced star wars melody with tone to save cpu resources
 }
   else if (commandString.equals("RIG2"))
 {
   speedRV = 0;
   speedRA = 0;
   speedLV = 127;
   speedLA = 127;
   tone(3, 100); //replaced star wars melody with tone to save cpu resources
 }
   else if (commandString.equals("RIG3"))
 {
   speedRV = 0;
   speedRA = 0;
   speedLV = 153;
   speedLA = 153;
   tone(3, 100); //replaced star wars melody with tone to save cpu resources
 }
   else if (commandString.equals("RIG4"))
 {
   speedRV = 0;
   speedRA = 0;
   speedLV = 191;
   speedLA = 191;
   tone(3, 100); //replaced star wars melody with tone to save cpu resources
 }
   else if (commandString.equals("RIGS")) //sport/super mode
 {
   speedRV = 0;
   speedRA = 0;
   speedLV = 255;
   speedLA = 255;
    tone(3, 10); //replaced star wars melody with tone to save cpu resources
 }
 else if (commandString.equals("SOUN") > 0)
 {
    tone(3, 555, 1000); //when horn pressed make sound for one second.
 }
  inputString = "";
 //code executes anyway but depends on the foregoing for what input both wheels get.
 //can now make full use of four wheel drive
 analogWrite(motorpinLV, speedLV);
 analogWrite(motorpinLA, speedLA);
 analogWrite(motorpinRV, speedRV);
 analogWrite(motorpinRA, speedRA);
 }
}
void avoidobstacle()
{
 if (distanceleft > 50 && distanceright > 50)
 {
 int distancedifference = distanceleft - distanceright;
 if (distancedifference > 0 || distancedifference == 0) //go left
 {
     speedRV = 255;
     speedRA = 255;
     speedLV = 0;
     speedLA = 0;
     leftcounter += 1;
 }
 else if (distancedifference < 0) //go right
 {
     speedRV = 0;
     speedRA = 0;
     speedLV = 255;
     speedLA = 255;
     rightcounter += 1;
 }
 }
 else if(distanceleft > 50 && distanceright < 50) //go left
 {
     speedRV = 255;
     speedRA = 255;
     speedLV = 0;
     speedLA = 0;
     leftcounter += 1;
 }
 else if (distanceleft < 50 && distanceright > 50) //go right
 {
     speedRV = 0;
     speedRA = 0;
     speedLV = 255;
     speedLA = 255;
     rightcounter += 1;
 }
 else
 {
   speedRV = 0;
   speedLV = 0;
   speedRA = 0;
   speedLA = 0;
 }
}
void getCommand()
{
 if(inputString.length()>0)
 {
    commandString = inputString.substring(1,5);
 }
}
String getTextToPrint()
{
 String value = inputString.substring(5,inputString.length()-2);
 return value;
}
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 measuredistanceF ()
{
 const long interval = 500;
 static unsigned long lasttime = 0;
 unsigned long now2 = millis();

 if (now2 - lasttime > interval)
 {
 lasttime = now2;
 //set time limit to 0,5 seconds
 digitalWrite(trigPin, LOW);
 delayMicroseconds(2);
 //gives a small sound pulse for approx 10 milliseconds
 digitalWrite(trigPin, HIGH);
 delayMicroseconds(10);
 digitalWrite(trigPin, LOW);
 //hears the echo of the sound, returns the time duration the sound travelled
 durationfront = pulseIn(echoPin, HIGH);
 // Calculating the distance by taking the speed of sound and dividing it by two (to it and back)
 distancefront = durationfront*0.034/2;
 //Serial.print("the distance to the front is: ");
 //Serial.print(distancefront);
 //Serial.print('\n');
 }
}
void measuredistanceR ()
{
 const long intervalR = 500;
 static unsigned long lasttimeR = 0;
 unsigned long nowR = millis();

 if (nowR - lasttimeR > intervalR)
 {
 lasttimeR = nowR;
 //set time limit to 0,5 seconds
 digitalWrite(trigPinR, LOW);
 delayMicroseconds(5);
 //gives a small sound pulse for approx 10 milliseconds
 digitalWrite(trigPinR, HIGH);
 delayMicroseconds(10);
 digitalWrite(trigPinR, LOW);
 //hears the echo of the sound, returns the time duration the sound travelled
 durationright = pulseIn(echoPinR, HIGH);
 // Calculating the distance by taking the speed of sound and dividing it by two (to it and back)
 distanceright = durationright*0.034/2;
 //Serial.print("the distance to the right is: ");
 //Serial.print(distanceright);
 //Serial.print('\n');
 }
}
void measuredistanceL ()
{
 const long intervalL = 500;
 static unsigned long lasttimeL = 0;
 unsigned long nowL = millis();

 if (nowL - lasttimeL > intervalL)
 {
 lasttimeL = nowL;
 //set time limit to 0,5 seconds
 digitalWrite(trigPinL, LOW);
 delayMicroseconds(2);
 //gives a small sound pulse for approx 10 milliseconds
 digitalWrite(trigPinL, HIGH);
 delayMicroseconds(10);
 digitalWrite(trigPinL, LOW);
 //hears the echo of the sound, returns the time duration the sound travelled
 durationleft = pulseIn(echoPinL, HIGH);
 // Calculating the distance by taking the speed of sound and dividing it by two (to it and back)
 distanceleft = durationleft*0.034/2;
 //Serial.print("the distance to the left is: ");
 //Serial.print(distanceleft);
 //Serial.print('\n');
 }
}

/*
* Name: sendData
* Description: Function used to send string to tcp client using cipsend
* Params:
* Returns: void
*/
void sendData(String str){
  String len="";
  len+=str.length();
  sendToWifi("AT+CIPSEND=0,"+len,responseTime,DEBUG);
  delay(100);
  sendToWifi(str,responseTime,DEBUG);
  delay(100);
  sendToWifi("AT+CIPCLOSE=5",responseTime,DEBUG);
}


/*
* Name: find
* Description: Function used to match two string
* Params:
* Returns: true if match else false
*/
boolean find(String string, String value){
  if(string.indexOf(value)>=0)
    return true;
  return false;
}


/*
* Name: readSerialMessage
* Description: Function used to read data from Arduino Serial.
* Params:
* Returns: The response from the Arduino (if there is a reponse)
*/
String  readSerialMessage(){
  char value[100];
  int index_count =0;
  while(Serial.available()>0){
    value[index_count]=Serial.read();
    index_count++;
    value[index_count] = '\0'; // Null terminate the string
  }
  String str(value);
  str.trim();
  return str;
}



/*
* Name: readWifiSerialMessage
* Description: Function used to read data from ESP8266 Serial.
* Params:
* Returns: The response from the esp8266 (if there is a reponse)
*/
String  readWifiSerialMessage(){
  char value[100];
  int index_count =0;
  while(wifiSerial.available()>0){
    value[index_count]=wifiSerial.read();
    index_count++;
    value[index_count] = '\0'; // Null terminate the string
  }
  String str(value);
  str.trim();
  return str;
}



/*
* Name: sendToWifi
* Description: Function used to send data to ESP8266.
* Params: command - the data/command to send; timeout - the time to wait for a response; debug - print to Serial window?(true = yes, false = no)
* Returns: The response from the esp8266 (if there is a reponse)
*/
String sendToWifi(String command, const int timeout, boolean debug){
  String response = "";
  wifiSerial.println(command); // send the read character to the esp8266
  long int time = millis();
  while( (time+timeout) > millis())
  {
    while(wifiSerial.available())
    {
    // The esp has data so display its output to the serial window
    char c = wifiSerial.read(); // read the next character.
    response+=c;
    } 
  }
  if(debug)
  {
    Serial.println(response);
  }
  return response;
}

/*
* Name: sendToWifi
* Description: Function used to send data to ESP8266.
* Params: command - the data/command to send; timeout - the time to wait for a response; debug - print to Serial window?(true = yes, false = no)
* Returns: The response from the esp8266 (if there is a reponse)
*/
String sendToUno(String command, const int timeout, boolean debug){
  String response = "";
  Serial.println(command); // send the read character to the esp8266
  long int time = millis();
  while( (time+timeout) > millis())
  {
    while(Serial.available())
    {
      // The esp has data so display its output to the serial window
      char c = Serial.read(); // read the next character.
      response+=c;
    } 
  }
  if(debug)
  {
    Serial.println(response);
  }
  return response;
}


Advertisement

Terug naar Arduino software

Wie is er online?

Gebruikers in dit forum: Geen geregistreerde gebruikers en 3 gasten