PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0
Showing posts with label arduino. Show all posts
Showing posts with label arduino. Show all posts

Monday, October 17, 2022

[FIXED] How to get the last octet of an IP address into 3 separate ints?

 October 17, 2022     arduino, integer, ip     No comments   

Issue

I need to get the last octet of an IP address into 3 separate ints to control a MAX7219.

I know the octet is an uint8_t

Using IPAddress ip = Wifi.localIP();, say my ip[3] was 148, I need:
int b1 = 1
int b2 = 4
int b3 = 8
but say ip[3] was only 8, then b1 and b2 have to be 0.


Solution

First thing that came to mind; there are probably more elegant ways to do this, but it works:

b3 = ip[3] % 10;
b2 = ((ip[3] % 100) - b3) / 10;
b1 = (ip[3] - (10 * b2) - b3) / 100;

Untested, but you get the idea.

Not sure why you would need separate integers, though.



Answered By - ocrdu
Answer Checked By - Timothy Miller (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Monday, July 11, 2022

[FIXED] How to manage several Serial messages in processing

 July 11, 2022     arduino, io, messages, processing     No comments   

Issue

I am reading the UID of my RFID card and storing it in a variable called myUID.

After that I am authorizing to the card with the factory key and read block number 4 (which has been written to earlier) and store it in a string readBlock.

On the Arduino, I print out the variables onto the serial interface like so.

Serial.println(myUID);
Serial.println(readBlock);

On the client side, I use a Java program that reads in serial data. My program uses the Processing Library.

Serial mySerial;
PrintWriter output;

void setup() {
 output = createWriter( "data.txt" );
 mySerial = new Serial( this, Serial.list()[0], 9600 );
 mySerial.bufferUntil('\n');
}

void draw(){
  while (mySerial.available() > 0) {
  String inBuffer = mySerial.readString();   
  if (inBuffer != null)
    output.println(inBuffer); 
  }
}
void keyPressed() { // Press a key to save the data
  output.flush(); // Write the remaining data
  output.close(); // Finish the file
  exit(); // Stop the program
}

Now my data.txt is expected to look like

xxx xxx xxx xxx (uid of card)
00 00 00 00 00 00 00 00 ... (read block from card)

but looks like

237 63 58 1
07
37 37 95
 37 
97 98 50 54 37 5
4 55 102 55 52 
45 98

I have tried several things like readStringUntil('\n'); in the Processing Library but without success.


Solution

For everyone interested, I have fixed the problem myself with many hours of searching Google, so maybe this will help someone in the future:

I could fix it with this code:

import processing.serial.*;
int count = 0;
String input = "";
String fileName = dataPath("SET FILEPATH HERE");

Serial mySerial;

import java.io.*;

void setup() {
  mySerial = new Serial(this, Serial.list()[0], 9600);
  mySerial.bufferUntil('\n');

  File f = new File(fileName);
  if (f.exists()) {
    f.delete();
  }
}

void draw(){}

// listen to serial events happening
void serialEvent(Serial mySerial){
  input = mySerial.readStringUntil('\n'); 
  write(input, count);
  count++;
}

// function for writing the data to the file
void write(String inputString, int counter) {
  // should new data be appended or replace any old text in the file?  
  boolean append = false;

  // just for my purpose, because I have got two lines of serial which need to get written to the file 
  //(Line 1: UID of card, Line 2: Read block of card)  
  if(counter < 2){
    append = true;  
  }
  else{
    count = 0;
  }


  try {
    File file = new File("D:/xampp/htdocs/pizza/src/rfid/data.txt");

    if (!file.exists()) {
      file.createNewFile();
    }

    FileWriter fw = new FileWriter(file, append);
    BufferedWriter bw = new BufferedWriter(fw);
    PrintWriter pw = new PrintWriter(bw);

    pw.write(inputString + '\n');
    pw.close();
  }
  catch(IOException ioe) {
    System.out.println("Exception ");
    ioe.printStackTrace();
  }
}


Answered By - TheEarthkin
Answer Checked By - Willingham (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Wednesday, July 6, 2022

[FIXED] What happens if I pass a temporary reference and store it as a class member?

 July 06, 2022     arduino, arduino-c++, c++, constructor, pass-by-reference     No comments   

Issue

I have a class that stores a reference to some kind of application state, which it then mutates during operation:

class Mutator {
    private:
        State& _state;

    public:
        Mutator(State& state);
        ...
};

Mutator::Mutator(State& state) : _state(state) {

}
...

Normally I would create and pass the state like this:

State state;
Mutator mutator(state);

What would happen to the state reference in my Mutator class, if I initialize the Mutator like this:

Mutator mutator(State());

I assume, since the state reference is temporary, the Mutator._state member will point to a memory location which may or may not contain the state value which leads to unpredictable behaviour. Is this correct?


Solution

If you use a reference, then the variable needs to be initialized (as not being NULL) in the constructor. This is safer, since you are assured the reference is really pointing to an existing object.

About

Mutator mutator(State());

This does not make much sense, unless the State object is used only as input for the Mutator constructor; however if it changes the newly created State variable, when the constructor returns and mutator is created, the State() instance is deleted.

Also (see note of NathanOliver below), it is not legal C++ code and not portable.

Update

Test application:

class State {
    public: State() {}
};

class Mutator {
    private:  State& _state;
    public:   Mutator(State& state) : _state(state)  { }
};

void setup() {
    Mutator mutator(State());
}

int main() {
    return 0;
}

The code above compiler and links correctly on an Arduino compiler (1.8.9):

Sketch uses 260 bytes (0%) of program storage space. Maximum is 253952 bytes.
Global variables use 0 bytes (0%) of dynamic memory, leaving 8192 bytes for local variables. Maximum is 8192 bytes.


Answered By - Michel Keijzers
Answer Checked By - Willingham (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Monday, June 27, 2022

[FIXED] How can I extract information from Arduino graph.Or from the Processing software connected with Arduino

 June 27, 2022     arduino, arduino-uno, graph, processing     No comments   

Issue

I am using AD8232 ECG sensor and I need PR interval, OT interval, R peak etc. I can generate the graph. But from the graph, I need these parameters to extract programmatically Can anyone help me.


Solution

The Arduino software was actually based in part off of Processing - that’s the beauty of open-source projects. Once we have an open sketch, our first step is to import the Serial library. Go to Sketch->Import Library->Serial

You should now see a line like import processing.serial.*; at the top of your sketch. Magic! Underneath our import statement, we need to declare some global variables. All this means is that these variables can be used anywhere in our sketch. Add these two lines beneath the import statement:

Serial myPort;  // Create object from Serial class
String val;     // Data received from the serial port

In order to listen to any serial communication, we have to get a Serial object (we call it myPort but you can it whatever you like), which lets us listen in on a serial port on our computer for any incoming data. We also need a variable to receive the actual data coming in. In this case, since we’re sending a String (the sequence of characters ‘Hello, World!’) from Arduino, we want to receive a String in Processing. Just like Arduino has setup() and loop(), Processing has setup() and draw() (instead of loop).

For our setup() method in Processing, we’re going to find the serial port our Arduino is connected to and set up our Serial object to listen to that port.

void setup()
{
  String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port
  myPort = new Serial(this, portName, 9600);
}

Remember how we set Serial.begin(9600) in Arduino? Well, if we don’t want that gobbledy-gook I was talking about, we had better put 9600 as that last argument in our Serial object in Processing as well. This way Arduino and Processing are communicating at the same rate. Happy times!

In our draw() loop, we’re going to listen in on our Serial port and we get something, stick that something in our val variable and prints it to the console (that black area at the bottom of your Processing sketch).

void draw()
{
  if ( myPort.available() > 0) 
  {  // If data is available,
  val = myPort.readStringUntil('\n');         // read it and store it in val
  } 
println(val); //print it out in the console
}

Ta-Da! If you hit the ‘run’ button (and your Arduino is plugged in with the code on the previous page loaded up), you should see a little window pop-up, and after a sec, you should see `Hello, World!‘ appear in the Processing console. Over and over.Excellent! We’ve now conquered how to send data from Arduino to Processing. Our next step is to figure out how to go the opposite way - sending data from Processing to Arduino.



Answered By - Arshdeep Kharbanda
Answer Checked By - David Goodson (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Wednesday, April 20, 2022

[FIXED] How to run Ai Thinker A9G gps module using arduino uno & Arduino IDE?

 April 20, 2022     arduino, connection, gsm     No comments   

Issue

I couldn't find any blog or solution for connecting and run A9G Ai thinker GSM/GPRS GPS module with Arduino Uno using Arduino IDE. Found only one video but that show the connection of A9G using USB to TTL converter. But i need the correct connection details for Arduino uno and A9G. How to connect them? (let me inform that i connect A9G rx with arduino tx, tx with arduino rx and GRnd and Vcc (5v+) and use the SMAD port but didn't work) which board and port have to use for IDE to use A9G? How to put AT commands in IDE using arduino code. Thank you in advance.


Solution

after a research I successfully implemented A9G with Arduino Uno here is the code:

#include <SoftwareSerial.h> // Library for using serial communication
SoftwareSerial SIM900(7, 8); // Pins 7, 8 are used as used as software serial pins

String incomingData;   // for storing incoming serial data
//String message = "";   // A String for storing the message
//int relay_pin = 2;    // Initialized a pin for relay module
char msg;
char call;
void setup()
{
  Serial.begin(115200); // baudrate for serial monitor
  SIM900.begin(115200); // baudrate for GSM shield
  

  Serial.println("GSM SIM900A BEGIN");
  Serial.println("Enter character for control option:");
  Serial.println("h : to disconnect a call");
  Serial.println("i : to receive a call");
  Serial.println("s : to send message");
  Serial.println("c : to make a call");  
  Serial.println("e : to redial");
  Serial.println("l : to read location");
  Serial.println();
  delay(100);

  
  /*SIM900.println("AT+GPS=1");
  delay(100);
  SIM900.println("AT+GPSRD=5");
  delay(5000);*/
  
  // set SMS mode to text mode
  SIM900.print("AT+CMGF=1\r");  
  delay(100);
  
  // set gsm module to tp show the output on serial out
  SIM900.print("AT+CNMI=2,2,0,0,0\r"); 
  delay(100);
}

void loop()
{
  
  receive_message();


if (Serial.available()>0)
   switch(Serial.read())
  {
    case 's':
      SendMessage();
      break;
    case 'c':
      MakeCall();
      break;
    case 'h':
      HangupCall();
      break;
    case 'e':
      RedialCall();
      break;
    case 'i':
      ReceiveCall();
      break;
    case 'l':
      ReadLocation();
      break;
  }
       
}

void receive_message()
{
  if (SIM900.available() > 0)
  {
    incomingData = SIM900.readString(); // Get the data from the serial port.
    Serial.print(incomingData); 
    delay(10); 
  }
}

void SendMessage()
{
  SIM900.println("AT+CMGF=1");    //Sets the GSM Module in Text Mode
  delay(1000);  // Delay of 1000 milli seconds or 1 second
  SIM900.println("AT+CMGS=\"x\"\r"); // Replace x with mobile number
  delay(1000);
  SIM900.println("sim900a sms");// The SMS text you want to send
  delay(100);
   SIM900.println((char)26);// ASCII code of CTRL+Z
  delay(1000);
}

void ReceiveMessage()
{
  SIM900.println("AT+CNMI=2,2,0,0,0"); // AT Command to recieve a live SMS
  delay(1000);
  if (SIM900.available()>0)
  {
    msg=SIM900.read();
    Serial.print(msg);
  }
}

void MakeCall()
{
  SIM900.println("ATD+201020516469;"); // ATDxxxxxxxxxx; -- watch out here for semicolon at the end, replace your number here!!
  Serial.println("Calling  "); // print response over serial port
  delay(1000);
}


void HangupCall()
{
  SIM900.println("ATH");
  Serial.println("Hangup Call");
  delay(1000);
}

void ReceiveCall()
{
  SIM900.println("ATA");
  delay(1000);
  {
    call=SIM900.read();
    Serial.print(call);
  }
}

void RedialCall()
{
  SIM900.println("ATDL");
  Serial.println("Redialing");
  delay(1000);
}
void ReadLocation(){

  SIM900.println("AT+GPS=1");
  delay(1000);
  SIM900.println("AT+GPSRD=5");
  delay(1000);
  
  }

set the serial monitor baud rate to 115200 and replace your number in MakeCall function

Edit: I suffered from noise in serial monitor when using baud rate 11500 by replacing it to 9600 the noise have gone.

Edit: Important note the gps will give proper reading if the antenna is in open area and the module is connecting with external power source otherwise it will give you old reading.



Answered By - Mohammed Ashraf
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Older Posts Home

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
All Comments
Atom
All Comments

Copyright © PHPFixing