Question

In: Computer Science

Write an Arduino code that calls a web service to show the location of the iss...

Write an Arduino code that calls a web service to show the location of the iss after a couple of seconds and displays in on an LCD

Solutions

Expert Solution

#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
#include "printf.h"
#include <string.h>
#include <Servo.h>
#include <Stepper.h>

// Definition des Pins CE, CSN pour le module NRF
#define RF_CE    9
#define RF_CSN   10

// Definition des constantes du stepper
#define STEPS 200  // Max steps for one revolution
#define RPM 60     // Max RPM

#define DELAY 20

int SVRO = 2;    // PIN de controle du servo

int commandeServo = 0; // commande du servo
String respMsg;
String msg;
int steps = 0; // commande du moteur

Stepper stepper(STEPS, 4, 5, 6, 7); // PIN du moteur

Servo myservo;

RF24 radio(RF_CE,RF_CSN);

byte myID = 0x01; // Definition de l'id de l'Arduino 

byte pipes[][7] = {"master","slave","idle"};

struct payload_request_t
{
  uint8_t number;
  uint8_t destination;
  char message[14];
};

struct payload_general_t
{
  uint8_t number;
  char message[15];
};

payload_request_t incoming;
payload_general_t outgoing;

void sendReply() // fonction d'envoi de la reponse au raspberry
{
  // affichage du message dans le moniteur
  Serial.print("Message de retour de la commande \""); 
  Serial.print(outgoing.message);
  Serial.println("\"");
  
  radio.stopListening(); // envoi du message
  radio.openWritingPipe(pipes[1]);
  delay(10);
  radio.write(&outgoing,sizeof(payload_general_t)+1);
  delay(10);
  radio.startListening();
}

void traitement(char *commande, int *servo, int *pas) 
// fonction qui separe la commande moteur de la commande du servo
{
  // modele commande set=com_servo;com_mot
  String comServ = "";
  String comMot = "";
  int i=4;
  int n = strlen(commande);
 
  while(commande[i] != ';'){ // recuperation de la commande du servo
    comServ += commande[i];
    i++;    
  }

  i++;
  while(i<n){ // recuperation de la commande du moteur
    comMot += commande[i];
    i++;    
  }

  *servo = comServ.toInt();
  *pas = comMot.toInt();
  
}

void setup() {
  
  Serial.begin(9600); // Initialisation de la communication Serie
  
  stepper.setSpeed(RPM); // Parametre la vitesse du moteur
  
  myservo.attach(SVRO);
  myservo.write(0); // servo a l'angle 0

  printf_begin();
  radio.begin();   // initialisation de la communication NRF
  radio.setAutoAck(1); 
  radio.setRetries(1,3);
  radio.enableDynamicPayloads();
  radio.openWritingPipe(pipes[1]);
  radio.openReadingPipe(1, pipes[0]);
  radio.startListening();
  radio.printDetails();
}

void loop() {
 
  if(radio.available()) {
    
    radio.read(&incoming, sizeof(payload_request_t));
    // Affichage de la commande demandee dans la console
    Serial.print("Commande recu \"");
    Serial.println(incoming.message); 
    
  // Aduino verifie que c'est bien a son ID que la demande s'adresse
  if (incoming.destination==myID) {
      
   
    if (strncmp(incoming.message, "get", 3) == 0) {
   // Si le message est "get", on retourne la position du servo et la derniere commande du moteur
   
      msg = "S" + String(commandeServo) + "M" + String(steps);
  
    
    } else if (strncmp(incoming.message, "set", 3) == 0) {
      // Sinon si la commande est de type "set"
      traitement(incoming.message,&commandeServo,&steps); 
      // On extrait les deux commandes
      
       if ((commandeServo < 0) || (commandeServo > 90)) {
          msg = "OORS "; // out of range servo
          } else {      
            myservo.write(commandeServo); // on commande le servo
             // on prepare le message de retour
            msg = "S = " + String(commandeServo) + " ";
            }
          Serial.println(respMsg);
       if ((steps == 0) || (steps < 0 - STEPS) || ( steps > STEPS )) {
          msg += "OORM"; // out of range moteur
          } else {  
            // on prepare le message de retour
              msg += "M = " + String(steps); 
              delay(DELAY); 
              if ( steps > 0) { // Sens trigo
                  for (int i=0;i<steps;i++) { 
                     stepper.step(1);
                     delay(DELAY);   
                  }
              } else { // Sens inverse
                  for (int i=0;i>steps;i--) {
                    stepper.step(-1);
                    delay(DELAY); 
                  }  
               }
          }
          // copie du message dans le message de retour
          msg.toCharArray(outgoing.message, 16); 
          Serial.println(outgoing.message); 
     
    } else { // Si la commande est de type inconnu
      strcpy(outgoing.message, "?");
    }

    outgoing.number = incoming.number;
    sendReply(); // On envoie la reponse
    }
  }
  delay(100);
}

Related Solutions

arduino c code only write a code that counts down a timer on serial monitor and...
arduino c code only write a code that counts down a timer on serial monitor and if A1 is typed into serial monitor prints the timer counting down and writes at 1 second hello and at 5 secs goodbye and repeats every 5 secs A2 is typed as above at 2 seconds writes hello and 3 seconds writes goodbye A3 same as above but at 3 seconds says hello and 2 seconds goodbye This continues until c is pressed to...
THIS IS FOR ARDUINO PROGRAMMING Write code that checks the sensor data and meets the following...
THIS IS FOR ARDUINO PROGRAMMING Write code that checks the sensor data and meets the following conditions: For night conditions, turn on white LED 1 For day conditions, turn off white LED 1 Code should check intervals every 5 seconds. NEED THE BELOW CODE TO FIT THE ABOVE REQUIREMENTS. const int lightSensor = 5; //light sensor variable float sensValue = 0; //variable to hold sensor readings const int button = 3; //pin for button reads const int LED1 = 5;...
Write code using the Arduino IDE that compiles with no errors. 2-bit adder: The code must...
Write code using the Arduino IDE that compiles with no errors. 2-bit adder: The code must read two digital input signals and turn on two LEDS as needed to show the sum of the inputs. e.g. 0 + 1 = 01.
FOR ARDUINO PROGRAMMING; WRITE CODE TO FIT THE BELOW REQUIREMENTS; 1. LED 1 TURNS ON AND...
FOR ARDUINO PROGRAMMING; WRITE CODE TO FIT THE BELOW REQUIREMENTS; 1. LED 1 TURNS ON AND STAYS ON THE ENTIRE TIME THE BOARD IS RUNNING EXCEPT AT 30 SECOND INTERVALS THEN LED 1 TURNS OFF AND BACK ON 2. LED 2 TURNS ON IN LIGHT CONDITIONS AND OFF IN DARK CONDITION THANK YOU!
FOR ARDUINO PROGRAMMING; WRITE CODE TO FIT THE BELOW REQUIREMENTS 1. LED 3 TURNS ON IN...
FOR ARDUINO PROGRAMMING; WRITE CODE TO FIT THE BELOW REQUIREMENTS 1. LED 3 TURNS ON IN DARK CONDITIONS AND OFF IN LIGHT CONDITIONS 2. LED 4 TURNS ON WITH FIRST BUTTON PRESS AND STAYS ON UNTIL A SECOND BUTTON PRESS
Using an Arduino MEGA write the code for the following problem. You have a pinball machine...
Using an Arduino MEGA write the code for the following problem. You have a pinball machine with four switches on the playboard. When a ball rolls over one of the switches that action must trigger the Arduino to add 20 points to the score being displayed on a 16x4 LCD, and actuate a relay that will trigger a bumping solenoid elsewhere on the board. The solenoid should return to home after the switch is no longer depressed by the ball....
Write C++ code that prompts the user for a username and password (strings), and it calls...
Write C++ code that prompts the user for a username and password (strings), and it calls the login() function with the username and password as arguments. You may assume that username and password have been declared elsewhere. Your code should print error messages if login() generates an exception: LoginException: "Username not found or password incorrect" ServerException: "The login server is busy and you cannot log in" AccessException: "Your account is forbidden from logging into this server" Any other exception: "Unknown...
Write C++ code that prompts the user for a username and password (strings), and it calls...
Write C++ code that prompts the user for a username and password (strings), and it calls the login() function with the username and password as arguments. You may assume that username and password have been declared elsewhere. Use virtual functions, pure virtual functions, templates, and exceptions wherever possible. Please explain the code. Your code should print error messages if login() generates an exception: LoginException: "Username not found or password incorrect" ServerException: "The login server is busy and you cannot log...
Can you write an arduino code to learn Hx711 and two Strain gauge value with Half...
Can you write an arduino code to learn Hx711 and two Strain gauge value with Half wheatstone bridge?
Write an Arduino code that does the following. Generate 50 random numbers between the numbers 100...
Write an Arduino code that does the following. Generate 50 random numbers between the numbers 100 and 300. Pick a number at random out of these 50 random variables. a. Determine the probability of the chosen number being greater than 200. This may be achieved by counting the numbers that are greater than 200 and dividing the count by 50. Make sure you, i.Formulate the appropriate if-conditions to check for a number being greater than 200 ii. Use a for-loop...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT