Arduino: Converting a client.read() - c

I'm trying to check the number of unread Mails with an Arduino+Ethernet Shield, sending two IMAP-requests.
With client.read(server_answer), I store it into a char.
When I send it to serial with Serial.print(server_answer), I get the following:
* OK IMAP server ready H migmx111 92345
0 OK LOGIN completed
* STATUS INBOX (UNSEEN 1)
0 OK STATUS completed
* STATUS INBOX (MESSAGES 1917)
0 OK STATUS completed
* BYE Server logging out
0 OK LOGOUT completed
Now my question: How can I extract the two numbers (total count of mails and unread mails, in the example 1 unread and 1917 total count)?
How can I get them in two different strings?
I want to display the numbers with some some text ("You have [number] new mails!") on a LCD.
If it helps, here's interesting part of my code:
void loop()
{
updateClient();
checkAvail();
}
void updateClient()
{
if ((millis() - updateTimer) > 10000)
{
Ethernet.begin(mac, ip);
// Serial.println("connecting...");
delay(1000);
if (client.connect())
{
//Serial.println("connected");
client.println("0 login myusername mypasswd");
client.println("0 STATUS INBOX (UNSEEN)");
client.println("0 STATUS INBOX (MESSAGES)");
client.println("0 logout");
clientConnected = true;
}
else
{
Serial.println("connection failed");
}
updateTimer = millis();
}
}
void checkAvail()
{
if (clientConnected)
{
if (client.available())
{
server_answer = client.read();
Serial.print(server_answer);
}
if (!client.connected())
{
Serial.println();
// Serial.println("disconnecting.");
client.stop();
clientConnected = false;
}
}
}

Without writing your code for you, you need to break the incoming data up into chunks using strtok_r(). Looking at your code above calling strtok_r() with '(' as a delimiter then again with a space as a delimiter and then again with ')' should get you to the begining of your first number. From there atoi() will convert it to an interger. Repeating the process should get you to the second value as well.
Take a crack at this and post your code if you have any more problems.

Related

.includes firing muiltple times

I am trying to test whether a message (string) sent from the user/client is containing a word and then it will pick 1 of 2 random responses and it works, however it sends the message way too many times.
client.on("messageCreate", message => {
if(message.content.includes("dream")) {
var msgnumber= (Math.floor((Math.random() * 2) + 1));
console.log(msgnumber);
if (msgnumber===1) {
message.channel.send("did someone say dream!?");
} else if (msgnumber===2) {
message.channel.send("why we talkin' about dream... huh!?")
}
}
})
It does pick a random message if a message sent contains the keyword, one problem is it sends way too many times.
The output message
Your bot is activating itself. Whenever it posts a message containing "dream", it sees that message, and decides to post a reply, creating an infinite loop. Try adding the line if (msg.author.bot) return; right above where you check for if the message contains "dream". This will exit the function early and avoid the loop.

Trying to find a way where a discord bot can randomly send a message in a given channel

I'm using the commando framework. I want the bot I'm making to be able start randomly sending messages in a certain channel when given a command to do so. For example if I typed "!radar" I would expect the bot to continue to randomly send a predetermined message in that channel until it is told to stop.
Here's the code I attempted to use:
async run(message){
message.say("Scanning....")
while(true){
var number = 30;
var imageNumber = Math.floor (Math.random() * (number - 1 + 1)) + 1;
if(imageNumber == 1){
message.say("^ detected!");
}
}
}
When I attempt this code I don't seem to get any errors logs, but I don't get any response from the bot when I send the command.

AT command sent SMS received as Flash SMS (Class 0 SMS)

I used this link to send SMS with AT command in WPF.
But when I send SMS with CMGS command, the receiver get the SMS as Flash SMS not usual SMS. My code is as below:
//Check if modem allows you to send and receive SMS messages using AT commands, without the need to decode the binairy PDU field of the SMS first
rval = sp.SendCommand("AT+CMGF=1");
//set Text mode parameters
rval = sp.SendCommand("AT+CSMP=17,167,0,16");
string phonenumber = "xxxxxxxxxxx";
string Message = "test";
rval = sp.SendCommand("AT+CMGS=\"" + phonenumber + "\"");
if (rval.Equals("\r\n> "))
{
rval = sp.SendCommand(Message + char.ConvertFromUtf32(26) );
}
and my SendCommand is as below
public string SendCommand(String commandText)
{
if (!serialPort.IsOpen)
{
try
{
serialPort.Open();
}
catch (Exception ex)
{
LogEvents.InLogFile(ex.Message);
throw new Exception("COM port is busy");
}
}
try
{
serialPort.DiscardOutBuffer();
serialPort.DiscardInBuffer();
buff = "";
serialPort.Write(commandText + "\r");
Thread.Sleep(serialPort.ReadTimeout);
return buff;
}
catch (Exception)
{
throw new Exception("Error connection");
}
}
Can any one help me?
My other references:
developershome,
Sayeda Anila Nusrat
The fourth parameter in AT+CSMP sets the coding scheme, i don't remember in which document i've found the coding of this byte but bit 7 sets whether the message should be discarded after showing it class 0 or stored
You should set this bit to 1 to make it storable, so changing
rval = sp.SendCommand("AT+CSMP=17,167,0,16");
to
rval = sp.SendCommand("AT+CSMP=17,167,0,144");
should do the work
Bit 0 (i.e., the least significant bit) of the fourth parameter of the AT+CSMP command is a flag for whether the SMS will be flashed (when 0) or saved (when 1).
Simply put: An even number for 4th parameter will NOT save the message, while an odd number will.
Change AT+CSMP=17,167,0,16 to AT+CSMP=17,167,0,0.

Multiple Timers Arduino

Hi I had a question about timers on that Arduino.
I have 5 physical buttons (piezos) that I am getting the analog input from. I am then having them write out a keyboard key. My issue is when one is hit I want it to be unable to hit for "x" amount of time. I tried using delay, but this ended up delaying the whole program, thus 2 buttons could not be hit at the same time. Could someone explain to me how to do this with timers? I want 5 separate timers 1 for each button that controls a Boolean, I would need 5 separate timers for 5 separate if statements. (See code).
//SNARE LOOP2
if(sensorValueA0 == 0)
{
if(SnareHit == false)
{
Keyboard.write(115);
SnareHit = true;
//Use timer here to delay this part of the system
SnareHit = false;
}
}
//BASS DRUM LOOP
if(sensorValueA1 == 0)
{
if(BassHit == false)
{
Keyboard.write(98);
BassHit = true;
//Use timer here to delay this part of the system
BassHit = false;
}
}
Thanks.
You can use the millis() function, something similar to the following code:
if(ButtonPress==true){
time=millis() //time was previously declared as unsigned long
if(time>=5000){ //5000 = 5 sec
ButtonPress==false
}
}
It will not stop the arduino loop as dealy() does.
More info: http://playground.arduino.cc/Code/AvoidDelay
Perhaps you are trying to de-bounce the button. I usually do this in the main loop, and expect 5 consecutive "pressed" reads before I say the button is really pressed, something like this:
int button1PressedCount = 0;
int debounceCounter = 5; // Number of successive reads before we say the switch is pressed
boolean buttonPressed = false;
int inputPin1 = 7;
void setup() {
// Grounding the input pin causes it to actuate
pinMode(inputPin1, INPUT ); // set the input pin 1
digitalWrite(inputPin1, HIGH); // set pin 1 as a pull up resistor.
}
void loop()
{
// Some code
// Check button, we evaluate below
checkButton();
// Some more code
}
void checkButton() {
if (digitalRead(inputPin) == 0) {
// We need consecutive pressed counts to treat this is pressed
if (button1PressedCount < debounceCounter) {
button1PressedCount += 1;
// If we reach the debounce point, mark the start time
if (button1PressedCount == debounceCounter) {
// Button is detected as pressed!
buttonPressed = true;
}
}
} else {
if (button1PressedCount == debounceCounter) {
// We were pressed, but are not any more
buttonPressed = false;
}
button1PressedCount = 0;
}
}
Also it seems using an analogue input with a check if the analogue value is exactly equal to 0 might be a bit sensitive in noisy environments. This is why I use a digital input and the internal pull up resistor.

Running and stopping a processing code automatically

What i wanna do is this; I want to run the processing code below, and after 2 minutes it should stop automatically and make itself run again and after 2 min stop again and again. I mean I don't want to run the code by clicking run button manually.
I'm so beginner on processing, I would be so glad if you can answer I desperately need this.
import processing.serial.*;
// The serial port:
Serial myPort;
String dataReading = "";
String [] dataOutput = {};
void setup() {
size(500,500);
// Open the port you are using at the rate you want:
myPort = new Serial(this, Serial.list()[7], 9600);
//in my case,the Serial port the Arduino is connected to is 9th on the serial list, hence the [8]
//to get access to the serial list you can use >> println(Serial.list());
myPort.bufferUntil('\n');
}
void draw() {
//...
}
void serialEvent(Serial myPort) {
dataReading = myPort.readString();
if(dataReading!=null){
dataOutput = append(dataOutput, dataReading);
saveData();
}
}
void saveData() {
println("saving to txt file...");
saveStrings("data/data.txt", dataOutput);
}

Resources