Serial communication on Arduino - arduino

I have an assignment for school where I need to turn on a led with the serial message #ON%, and turn the led off with #OFF%. The # and % are the identifiers for the correct string. So I made this code:
(bericht means message in Dutch)
String readString = "";
int recievedCharacter;
String bericht = "";
int ledPin = 6;
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
pinMode(ledPin, OUTPUT);
}
void loop()
{
while (Serial.available() > 0)
{
delay(4);
char readChar = (char) Serial.read(); // 'Convert' to needed type
bericht += + readChar; // concatenate char to message
}
if(bericht.startsWith("#"))
{
if(bericht == "#ON%")
{
Serial.println(bericht);
Serial.println("goed");
digitalWrite(ledPin, HIGH);
//message = "";
}
if(bericht == "#OFF%")
{
Serial.println("goed");
digitalWrite(ledPin, LOW);
//message = "";
}
}
}
The problem is the program will never get into the if(bericht == "#ON%") section...
Sorry if this is a silly question but with a lot of googling I just can't figure it out...

The problem is here:
bericht += + readChar; // concatenate char to message // XXX '+ char' => int
this actually appends an integer to the message. Remove the +:
bericht += readChar; // concatenate char to message // Goed!

Related

Using a HashMap to store the sensor addresses with tempetures on Arduino

I have started a small project to hopefully replace RPis running a Java library with Arduinos.
(I am normally working with Java, so not as familiar with C)
There are multiple temp sensors connected to the board. I read the values and want to store them with a reference to the sensor address. When a value changes, an update of all the sensors with their address and the temperatures is send to the server (hence I need the store to compare the values every 10 seconds).
I am trying to use the HashMap from Arduino Playground, as on a first look it seemed to do what I need and seems lightweight.
However, when reading the address of the temp sensor from a variable from the HashMap it doesn't return the right one (when there is some data pre-set in the hashmap):
int strLen = sAddress.length();
char *cAddress = (char *)malloc(strLen+1);
sAddress.toCharArray(cAddress, strLen+1);
byte position = sensorHashMap.getIndexOf(cAddress);
However, if I replace the *cAddress with:
char *cAddress = "28aae25501412c";
it does find it. So what am I doing wrong?
My approach to store the temp values with the address as a reference might not be the best, and it seems that the code crashes later on when trying to update the value but I haven't gone down that far yet. If there is a better solution than I am very open to some suggestions off course.
The full code below:
#include <HashMap.h>
#include <OneWire.h>
#include <ESP8266WiFi.h>
#include <DS18B20.h>
char wifiSSID[] = "xxxx";
char wifiPassword[] = "xxxx";
unsigned long lastTempCheck = 0;
const byte HASH_SIZE = 10; // Max 10 (temp) sensors etc
HashType<char*, float> hashRawArray[HASH_SIZE];
HashMap<char*, float> sensorHashMap = HashMap<char*, float>( hashRawArray , HASH_SIZE );
int sensorsRegistered = 1;
WiFiClient client;
DS18B20 ds(5); // Is D1
void setup()
{
Serial.begin(115200);
Serial.println();
WiFi.begin(wifiSSID, wifiPassword);
Serial.print("Connecting");
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("---");
Serial.println("Connected to wifi");
Serial.print("Connected, IP address: ");
Serial.println(WiFi.localIP());
Serial.println("Setup completed, ready to run...");
// Test data
sensorHashMap[0]("name", 18);
sensorHashMap[1]("test", 200);
sensorHashMap[2]("qwer", 1234);
sensorHashMap[3]("28fffa6f51164ae", 123);
sensorHashMap[4]("28aae25501412c", 456);
}
void loop() {
// Duty cycle of the application
delay(100);
if ((millis() < lastTempCheck) || (millis() - lastTempCheck > 1000 * 10)) {
// Verifying the HashMap works with the pre-set values.
Serial.print("Checking pre-set values: ");
Serial.println( sensorHashMap.getIndexOf("28fffa6f51164ae"), DEC );
Serial.print("Checking sensor value: ");
Serial.println( sensorHashMap.getValueOf("28fffa6f51164ae") );
while (ds.selectNext()) {
float temp = ds.getTempC();
uint8_t address[8];
ds.getAddress(address);
String sAddress = "";
for (uint8_t i = 0; i < 8; i++) {
sAddress += String(address[i], HEX);
}
int strLen = sAddress.length();
char *cAddress = (char *)malloc(strLen+1);
sAddress.toCharArray(cAddress, strLen+1);
//char *cAddress = "28aae25501412c";
byte position = sensorHashMap.getIndexOf(cAddress);
Serial.print("Position: ");
Serial.println( position);
Serial.println( sensorHashMap.getIndexOf(cAddress), DEC );
if (position < HASH_SIZE) {
Serial.print("Updating sensor value, currently: ");
Serial.println( sensorHashMap.getValueOf(cAddress));
sensorHashMap[position](cAddress, temp); //ds.getTempC()
} else {
Serial.print("Creating sensor value, id is going to be ");
Serial.println(sensorsRegistered);
sensorHashMap[sensorsRegistered](cAddress, temp);
sensorsRegistered++;
}
free(address);
}
lastTempCheck = millis();
}
}

Sending two arrays to Arduino from Processing

I'm trying to pass multiple variables (n number of strings and n number of ints) from processing to my Arduino. I found this tutorial online and managed to send a single value. Now I have two arrays that both need to be accessed by the Arduino filesTypes[] and filesSizes[]. filesTypes[] consists of a 3 char long strings while fileSizes[] is an array of different integers.
Here is my Processing code:
import processing.serial.*;
Serial myPort; // Create object from Serial class
String[] fileTypes;
int[] fileSizes;
String[][] lines;
void setup()
{
size(200,200); //make our canvas 200 x 200 pixels big
String portName = Serial.list()[1]; //change the 0 to a 1 or 2 etc. to
match your port
myPort = new Serial(this, portName, 9600);
launch( sketchPath("") + "/test.bat");
}
void draw() {
if (mousePressed == true)
{ //if we clicked in the window
txtToStrg();
myPort.write('1'); //send a 1
txtToStrg();
} else
{ //otherwise
myPort.write('0'); //send a 0
}
}
void txtToStrg(){
String[] lines = loadStrings("list.txt");
fileTypes = new String[lines.length];
fileSizes = new int[lines.length];
for (int i = 0 ; i < lines.length; i++) {
if(lines[i] != null) {
String[] splitLine = split(lines[i], ' ');
fileTypes[i] = splitLine[0];
fileSizes[i] = int(splitLine[1]);
println(fileTypes[i] + " = " + fileSizes[i]);
}
}
}
And here my Arduino code:
char val; // Data received from the serial port
int ledPin = 4 ; // Set the pin to digital I/O 13
void setup() {
pinMode(ledPin, OUTPUT); // Set pin as OUTPUT
Serial.begin(9600); // Start serial communication at 9600 bps
}
void loop() {
if (Serial.available())
{ // If data is available to read,
val = Serial.read(); // read it and store it in val
}
if (val == '1')
{ // If 1 was received
digitalWrite(ledPin, HIGH); // turn the LED on
} else {
digitalWrite(ledPin, LOW); // otherwise turn it off
Serial.print(val);
}
delay(10); // Wait 10 milliseconds for next reading
}
If a pass anything but a char it stops working.

Arduino control with serial input more then 1 character

I have the script below and it works.
But I want to control it with a word like on or off instead of one character.
I tried and searched a lot but without success.
/*
Simple LED sketch
*/
int led = 13; // Pin 13
void setup()
{
pinMode(led, OUTPUT); // Set pin 13 as digital out
// Start up serial connection
Serial.begin(115200); // baud rate
}
void loop()
{
if (Serial.available()) {
int ser = Serial.read(); //read serial as ascii integer
if (ser == 'a') { //is this serial byte the ASCII equivalent of 0 through 9?
digitalWrite(led, HIGH); // on
Serial.println("aan");
}
else if (ser == 'u') {
digitalWrite(led, LOW); // off
Serial.println("uit");
}
}
}
Use Serial.readStringUntil(terminator) to read a string from the serial.
The sent string needs to be terminated with a newline character.
Chose Newline in the Arduino IDE's Serial Monitor.
String cmd = "";
void loop()
{
if (Serial.available()) {
cmd = Serial.readStringUntil('\n');
if (cmd == "on") {
digitalWrite(led, HIGH); // on
Serial.println("aan");
}
else if (cmd == "off") {
digitalWrite(led, LOW); // off
Serial.println("uit");
}
}
}
Your problem is that you are using a char you need to declare a string so you can compare that string to the input, the guy above gave a ok/very slacky/too complex for you answer (sorry no offense I am just trying to show contrast between a complex and a easy answer not trying to offend you) and partially is because he did not explain what he is doing and because he is doing unnecessary/useless work. There is a function in c++ called Serial.readString(), much easier then the stuff up there. Assuming your level of programming (no offense) from your question here is a quick review on data types:
String = ""
int = integer number {1,2,3,4,5,6,...}
char = '' <- Notice the difference from String = ""
float = floating point number {1.2,4.5,...}
(These are not all of them, there is more like byte and so on but just make sure you know how to use the above first)
/*
Simple LED sketch
*/
int led = 7; // Pin 13
String ser; //Declare the string that is going to store what your are going to
//write in the serial
void setup(){
// Start up serial connection
//It's good convention to start the serial before pinMode or any other thing
//in setup
Serial.begin(9600); //I don't know why you need such an high baud rate in
//your thing, 9600 for what you have to do is more than fine, this is just at
//what speed the serial is read (in a very general explanation)
pinMode(led, OUTPUT); // Set pin 13 as digital out
//make sure you put in here wheter you want to start the led "on" or "off"
//Eg you want it to start on, then digitalWrite(led, HIGH);
}
void loop(){
ser = Serial.readString();
if(Serial.available() == 0) { //You can also use a while loop if you want to
//This is telling the arduino: if there is something in the serial, then do...
if(ser == "on"){
Serial.println("on");
digitalWrite(led, HIGH);
}else if(ser == "off"){
Serial.println("off");
digitalWrite(led, LOW);
}
}
}
Hope it helped!
Also notice how the above code with
if(Serial.available())
This is a quite WEIRD and SHADY statement and might not work. That's because you are not really telling the int value into the function Serial.available
As the arduino guidelines specify:
int incomingByte = 0; // for incoming serial data
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}
void loop() {
// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
// say what you got:
Serial.print("I received: ");
Serial.println(incomingByte, DEC);
}
}
viewable here

Sending and receiving sms with arduino, using sim900

I have a strange problem with my project. I am able to send and receive SMS messages but for some reason not from the same project.
This little one sends a message, and is working properly:
#include <SoftwareSerial.h>
SoftwareSerial SIM900(7, 8);
String no = "+38762701893";
String message = "this is some message";
void setup() {
SIM900.begin(19200);
delay(20000);
}
void sendSMS(String number, String mess) {
SIM900.print("AT+CMGF=1\r");
delay(100);
SIM900.println("AT + CMGS = \"" + number + "\"");
delay(100);
SIM900.println(mess);
delay(100);
SIM900.println((char)26);
delay(100);
SIM900.println();
delay(5000);
}
void loop() {
sendSMS(no, message);
do {} while (1);
}
However, if I want this same function to work under a little bigger program which receives SMS messages and responds to them, it doesn't work.
This is the complete code of the not working example:
#include <SoftwareSerial.h>
SoftwareSerial SIM900(7, 8);
String no = "+122333444"; //phone number
String message = "";
char inchar;
int b = 13;
bool state = 0;
void setup()
{
Serial.begin(19200);
SIM900.begin(19200);
delay(5000);
SIM900.print("AT+CMGF=1\r");
delay(100);
SIM900.print("AT+CNMI=2,2,0,0,0\r");
delay(100);
Serial.println("Ready...");
SIM900.begin(19200);
delay(500);
pinMode(b, OUTPUT);
}
void sendSMS(String number, String mess) {
SIM900.print("AT+CMGF=1\r");
delay(200);
SIM900.println("AT+CMGS=\"" + number + "\"");
delay(100);
SIM900.println(mess);
delay(100);
SIM900.println((char)26);
delay(100);
SIM900.println();
delay(8000);
}
void loop()
{
if (state) {
sendSMS(no, message);
state = 0;
}
if (SIM900.available() > 0) {
inchar = SIM900.read();
Serial.print(inchar);
if (inchar == '#') {
delay(10);
inchar = SIM900.read();
Serial.print(inchar);
if (inchar == 'a') {
delay(10);
inchar = SIM900.read();
Serial.print(inchar);
if (inchar == '0') {
digitalWrite(b, LOW);
message = "something is off";
} else if (inchar == '1') {
digitalWrite(b, HIGH);
message = "something is on";
}
}
state = 1;
}
}
}
This program receives the SMS, turns on the LED, or turns it off, depending of the SMS content, but it won't reply for some reason. Any help is greatly appreciated.
Your code for sending SMS is incorrect.I would suggest using a library like gsmlib for arduino it will handle all the tasks appropriately.
Firstly
SIM900.print("AT+CMGF=1\r");
This should be done once in setup().
After sending SIM900.println("AT+CMGS=\"" + number + "\""); you need to wait for the modem to respond with > after which you can send the text. In your case you are assuming that the modem would have sent it within 200ms.
There are several gsm libs available where you can just use a function like sendSMS(number,text) and it will handle all the other stuff.
I did that in C#, but you can convert to Arduino easily:
private void sendSMS_GSM()
{
if (serialPort.IsOpen)
{
strResponseSim = "";
serialPort.WriteLine("AT+CMGF=1\r\n");
while (strResponseSim != "AT+CMGF=1\r\r\nOK\r\n") ;
strResponseSim = "";
serialPort.WriteLine("AT+CSCS=\"GSM\"\r\n");
while (strResponseSim != "AT+CSCS=\"GSM\"\r\r\nOK\r\n") ;
strResponseSim = "";
serialPort.WriteLine("AT+CMGS=\"" + txtPhone.Text + "\"\r\n");
serialPort.WriteLine(txtMessage.Text);
serialPort.Write(new byte[] { 26 }, 0, 1);
while (strResponseSim == "OK") ;
strResponseSim = "";
}
}

Arduino read string from serial line and compare

I'm trying to read a string from the serial line and compare it to a list of commands. If the string is a valid command the Arduino should go ahead and do something, and return some info on the serial line.
However, my comaprison always fails (give me the "not a valid command response"). I have tried sending the word "temp" both from the Arduino serial monitor and from a Python script.
My arduino code:
int sensorPin = 0; // Sensor connected to A0
int ledPin = 13; // Led connected to 13
int reading = 0; // Value read from A0
float voltage = 0; // Voltage we read
float temperatureC = 0; // Temperature we measure
String inputString= ""; // Set string empty
String Temperature = "temp"; // The command we are looking for
boolean stringComplete = false; // See if we are done reading from serial line
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
inputString.reserve(200); // Reserve space for inputString in memory
}
void serialEvent() {
// Read data from serial line until we get a \n.
// Store data in inputString
while (Serial.available()){
char inChar = (char)Serial.read();
inputString += inChar;
if (inChar == '\n'){
stringComplete = true;
}
}
}
void loop() {
serialEvent(); // See if there are data on serial line and get it
if (stringComplete){ // If we are done reading on serial line
if (inputString == Temperature){ //WHY YOU FAIL ME?
digitalWrite(ledPin, HIGH);
voltage = (analogRead(sensorPin) * 5.0)/1024.0;
temperatureC = (voltage - 0.5) * 100;
Serial.print(voltage); Serial.println(" volts");
Serial.print(temperatureC); Serial.println(" degrees C");
delay(5000);
digitalWrite(ledPin, LOW);
}
else{
Serial.print("Not a valid command:");
Serial.print(' '+inputString);
}
// Reset so we can wait for a new command
inputString = "";
stringComplete = false;
}
}
First of all I'd avoid using the String object. It's better, in my opinion, to just use char arrays. They are lighter and avoid memory allocation and deallocation.
By the way, why are you reserving the space if, later, you are assigning it a new empty string?
Anyway, I think that the problem is that you are appending also the new line to the string. Moreover, in a windows environment a new line is a '\r' followed by a '\n', so you get different behaviour in Win and Linux.
I'd just replace
inputString += inChar;
if (inChar == '\n'){
stringComplete = true;
}
with
if ((inChar == '\r') || (inChar == '\n')){
stringComplete = (inputString.length() > 0);
} else {
inputString += inChar;
}
EDIT: I'd also add a break in the stringComplete case, since otherwise multple commands can't be detected. So:
if ((inChar == '\r') || (inChar == '\n')){
if(inputString.length() > 0) {
stringComplete = true;
break;
}
} else {
inputString += inChar;
}
Try
inputString += inChar.trim();
Instead of.
inputString += inChar;

Resources