Cant seem to figure out how to make the waitBTNPressed Function work, keeps returning invalid operands of types void and bool to binary operator== - boolean

void waitBtnPressed() {
while(1) {
if (digitalRead(LP_S1_PIN) == 0) break;
if (digitalRead(LP_S2_PIN) == 0) break;
digitalWrite(LP_RGB_LED_GREEN_PIN, HIGH);
delay(500);
digitalWrite(LP_RGB_LED_GREEN_PIN, LOW);
delay(500); }
}
void driveStraight() {
setMotorDirection(BOTH_MOTORS,MOTOR_DIR_FORWARD);
**Cause the robot to drive forward **
enableMotor(BOTH_MOTORS);
**"Turn on" the motor **
setMotorSpeed(BOTH_MOTORS,WHEELSPEED);
** Set motor speed **
delay(DELAY_MILLI_SECONDS_100CM);
disableMotor(BOTH_MOTORS);
Halt motors
delay(10000);
}
void setup() {
Serial.begin(115200);
delay(500);
setupRSLK();
** Left and right button on LaunchPad **
pinMode(LP_S1_PIN, INPUT_PULLUP);
pinMode(LP_S2_PIN, INPUT_PULLUP);
}
void loop() {
if (waitBtnPressed()==true) {
delay(2000);
driveStraight();
This code is a push button function that is supposed to trigger a drivestraight function in one loop

Related

STM32 - RS485 request-response

I am trying to send request to measuring device and receive it's response using UART with interrupts. However communication is unstable, I am receiving incomplete or corrupted responses. I am not sure but I think it's because switching driver enable signal. Could you look at my code and give me some advice what am I doing wrong?
Here is my code:
int main(void){
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART3_UART_Init();
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_9,GPIO_PIN_SET); //RS 485 transmit mode
while (1)
{
if(HAL_UART_Transmit_IT(&huart3, (uint8_t*)aTxBuffer, 2) != HAL_OK)
{
while(1);
}
while (UartReady != SET);
UartReady = RESET;
if(HAL_UART_Receive_IT(&huart3, (uint8_t*)aRxBuffer, 4) != HAL_OK)
{
while(1);
}
while (UartReady != RESET);
//do somethink with received data
}
}
Here are my callback functions:
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *UartHandle)
{
/* Set transmission flag: transfer complete */
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_9,GPIO_PIN_RESET); //RS 485 receive mode
//DataRecieved = 0;
UartReady = SET;
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle)
{
/* Set transmission flag: transfer complete */
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_9,GPIO_PIN_SET); //RS 485 transmit mode
//DataRecieved = 1;
UartReady = SET;
}
Thank you very much
The data you transmit, does it also appear at your RX-buffer, while you expect it not to?
I am receiving incomplete or corrupted responses. I am not sure but I
think it's because switching driver enable signal. Could you look at
my code and give me some advice what am I doing wrong?
Not sure, but maybe it would help to toggle the DE pin not in the interrupt callback functions, and instead switch to "transmit mode" right before invoking HAL_UART_Transmit_IT() and then to switch back to receive mode after the transmission, or even after calling HAL_UART_Receive_IT(), to make sure it will catch the incoming bytes. Tiny delays do matter sometimes:
void set_transmit_mode(void)
{
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_9, GPIO_PIN_SET); // RS 485 transmit mode
}
void set_receive_mode(void)
{
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_9, GPIO_PIN_RESET); // RS 485 receive mode
}
{
//...
set_transmit_mode();
if (HAL_UART_Transmit_IT(&huart3, (uint8_t*)aTxBuffer, 2) != HAL_OK) {
// ...
}
while (UartReady != SET);
//set_receive_mode();
UartReady = RESET;
if (HAL_UART_Receive_IT(&huart3, (uint8_t*)aRxBuffer, 4) != HAL_OK) {
// ...
}
set_receive_mode();
while (UartReady != RESET);
//...
}

"No Socket Available" - Wifi Shield on Arduino

I have an LDR/Photocell which sends a value 1 or 0 (depending on the value) to a text file on my web address. The code works for a few seconds then prints out No Socket available.
Any help will be appreciated; code below.
#include <SPI.h>
#include <WiFi.h>
int LDR = A0;
int LED = 11;
char ssid[] = "SSID";
char password[] = "password";
int status = WL_IDLE_STATUS;
char server[] = "www.example.com";
int value;
void setup() {
Serial.begin(9600);
pinMode(LDR, INPUT);
pinMode(LED, OUTPUT);
connectWifi();
printWifiStatus();
// postData();
}
void loop() {
value = analogRead(LDR);
postData();
delay(10000);
}
void connectWifi() {
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to ");
Serial.println(ssid);
status = WiFi.begin(ssid, password);
delay(500);
}
}
void printWifiStatus() {
// Print the SSID of the network you're attached to
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// Print your WiFi shield's IP address
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// Print the received signal strength
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
void postData() {
WiFiClient client;
if (client.connect(server, 80)) {
Serial.println("Connecting...");
if (value > 350) {
Serial.println("0");
digitalWrite(LED, LOW);
String data = "value=0";
client.print("GET /example/client.php?");
client.print(data);
client.println(" HTTP/1.1");
client.println("Host: www.example.com");
client.println("Connection: close");
client.println(); client.println();
//client.stop();
} else {
Serial.println("1");
digitalWrite(LED, HIGH);
String data = "value=1";
client.print("GET /example/client.php?");
client.print(data);
client.println(" HTTP/1.1");
client.println("Host: www.example.com");
client.println("Connection: close");
client.println(); client.println();
//client.stop();
}
} else {
Serial.println("Connection failed");
client.stop();
}
}
Output:
Attempting to connect to SSID
SSID: SSID
IP Address: 255.255.255.255
signal strength (RSSI):-47 dBm
Connecting...
1
Connecting...
0
Connecting...
0
Connecting...
0
No Socket available
Connection failed
No Socket available
Connection failed
No Socket available
Connection failed
No Socket available
Actual web address omitted.
first of all, try not use "delay()" and your are calling this function "postData()" every 0,5 seconds. Try use the millis() function to do the timer thing, like this:
unsigned long timerBefore = 0;
const int timer = 1000; //1 second
now inside your loop()
unsigned long timerNow=millis();
if((unsigned long)(timerNow-timerBefore)>=timer){
postData();
timerBefore=millis();
}
that code i'll not "pause" your microcontroller and will call that function every one second.

connect two ESP8266 to one MQTT broker cause hang

I have 2 ESP8266 PubSubClients who are connecting to MQTT broker installed on Raspberry PI3. I'm able to connect them and make a ON/OFF operation and its okay! but when I'm going to use both of them to operating its will be hang and stuck in reconnecting loop, when i turn one of them off, its work fine.
this is my code on ESP8266:
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
// Update these with values suitable for your network.
const char* ssid = "SSID";
const char* password = "PASSWORD";
const char* mqtt_server = "192.168.1.10";
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
void setup() {
//pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output
//Serial.begin(115200);
Serial.begin(9600);
pinMode(0, OUTPUT);
digitalWrite(0, HIGH);
pinMode(2, OUTPUT);
digitalWrite(2, HIGH);
pinMode(4, OUTPUT);
digitalWrite(4, HIGH);
pinMode(5, OUTPUT);
digitalWrite(5, HIGH);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
//1 ( pin 0 )
if (strcmp(topic,"/home/1/ard1/p1/com")==0) {
if (payload[0] == '0'){
digitalWrite(0, HIGH);
Serial.print("Turning Light ON");
delay(100);
client.publish("/home/1/ard1/p1/state","0");
}
else if (payload[0] == '1')
{
digitalWrite(0, LOW);
Serial.print("Turning Light OFF");
delay(100);
client.publish("/home/1/ard1/p1/state","1");
}
}
//2 ( pin 2 )
if (strcmp(topic,"/home/1/ard1/p2/com")==0) {
if (payload[0] == '0'){
digitalWrite(2, HIGH);
Serial.print("Turning Light ON");
delay(100);
client.publish("/home/1/ard1/p2/state","0");
}
else if (payload[0] == '1')
{
digitalWrite(2, LOW);
Serial.print("Turning Light OFF");
delay(100);
client.publish("/home/1/ard1/p2/state","1");
}
}
//3 ( pin 4 )
if (strcmp(topic,"/home/1/ard1/p3/com")==0) {
if (payload[0] == '0'){
digitalWrite(4, HIGH);
Serial.print("Turning Light ON");
delay(100);
client.publish("/home/1/ard1/p3/state","0");
}
else if (payload[0] == '1')
{
digitalWrite(4, LOW);
Serial.print("Turning Light OFF");
delay(100);
client.publish("/home/1/ard1/p3/state","1");
}
}
//4 ( pin 5 )
if (strcmp(topic,"/home/1/ard1/p4/com")==0) {
if (payload[0] == '0'){
digitalWrite(5, HIGH);
Serial.print("Turning Light ON");
delay(100);
client.publish("/home/1/ard1/p4/state","0");
}
else if (payload[0] == '1')
{
digitalWrite(5, LOW);
Serial.print("Turning Light OFF");
delay(100);
client.publish("/home/1/ard1/p4/state","1");
}
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("ESP8266Client")) {
Serial.println("connected");
// Once connected, publish an announcement...
client.publish("outTopic", "hello world");
client.subscribe("/home/1/ard1/#");
// ... and resubscribe
client.subscribe("inTopic");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
long now = millis();
if (now - lastMsg > 2000) {
lastMsg = now;
++value;
snprintf (msg, 75, "hello world #%ld", value);
Serial.print("Publish message: ");
Serial.println(msg);
client.publish("outTopic", msg);
}
}
I changed these below lines for one of them but the result is the same.
WiFiClient espClient;
PubSubClient client(espClient);
to:
WiFiClient espClient1;
PubSubClient client(espClient1);
Many Thanks.
Your problem is this line:
if (client.connect("ESP8266Client")) {
Every client needs a unique client id, hard coding it to ESP8266Client means that when the second client connects the broker will kick the first one off, which will then try and reconnect which will kick the second off. This just ends up stuck in a loop.

Arduino & CC3000 & Motorshield & Robot

I have a 4 Motor Drive car that has ARD MEGA / CC3000 / Motorshield on it.
I have written a sketch that runs REST and a webpage with buttons, which works fine in Serial.
I have written a sketch that controls the Motors Forwards / Backwards / Left / Right and this works fine.
When I combine the 2 Sketches and press FORWARD on the webpage it works but then 'Hangs'.
void loop()
{
// Handle any multicast DNS requests
mdns.update();
Adafruit_CC3000_ClientRef client = restServer.available();
rest.handle(client);
wdt_reset();
if(!cc3000.checkConnected()){while(1){}}
wdt_reset();
}
int forward(String command) {
goforwards();
return 1;
}
int backward(String command) {
gobackwards();
return 1;
}
int left(String command) {
Serial.println(F("GO LEFT..."));
//goleft();
return 1;
}
int right(String command) {
Serial.println(F("GO RIGHT..."));
//goright();
return 1;
}
int estop(String command) {
Serial.println(F("Asked to STOP..."));
allstop();
return 1;
}
void goforwards() {
Serial.println(F("GO FORWARD..."));
motor(1, FORWARD, 150); motor(4, FORWARD, 150); motor(2, FORWARD, 150); motor(3, FORWARD, 150); delay(500);
allstop();
}
void gobackwards() {
Serial.println(F("GO BACKWARD..."));
motor(1, BACKWARD, 150); motor(4, BACKWARD, 150); motor(2, BACKWARD, 150); motor(3, BACKWARD, 150); delay(500);
allstop();
}
void goleft() {motor(1, FORWARD, 175); motor(4, FORWARD, 175); motor(2, FORWARD, 50); motor(3, FORWARD, 50);delay(2000); }
void goright() {motor(1, FORWARD, 50); motor(4, FORWARD, 50); motor(2, FORWARD, 175); motor(3, FORWARD, 175); delay(2000); }
void allstop() {motor(1, RELEASE, 0); motor(2, RELEASE, 0);motor(3, RELEASE, 0);motor(4, RELEASE, 0);delay(1); }
void motor(int nMotor, int command, int speed)
{
int motorA, motorB;
if (nMotor >= 1 && nMotor <= 4)
{
switch (nMotor)
{
case 1:
motorA = MOTOR1_A;
motorB = MOTOR1_B;
break;
case 2:
motorA = MOTOR2_A;
motorB = MOTOR2_B;
break;
case 3:
motorA = MOTOR3_A;
motorB = MOTOR3_B;
break;
case 4:
motorA = MOTOR4_A;
motorB = MOTOR4_B;
break;
default:
break;
}
switch (command)
{
case FORWARD:
motor_output (motorA, HIGH, speed);
motor_output (motorB, LOW, -1); // -1: no PWM set
break;
case BACKWARD:
motor_output (motorA, LOW, speed);
motor_output (motorB, HIGH, -1); // -1: no PWM set
break;
case BRAKE:
motor_output (motorA, LOW, 255); // 255: fully on.
motor_output (motorB, LOW, -1); // -1: no PWM set
break;
case RELEASE:
motor_output (motorA, LOW, 0); // 0: output floating.
motor_output (motorB, LOW, -1); // -1: no PWM set
break;
default:
break;
}
}
}
void motor_output (int output, int high_low, int speed)
{
int motorPWM;
switch (output)
{
case MOTOR1_A:
case MOTOR1_B:
motorPWM = MOTOR1_PWM;
break;
case MOTOR2_A:
case MOTOR2_B:
motorPWM = MOTOR2_PWM;
break;
case MOTOR3_A:
case MOTOR3_B:
motorPWM = MOTOR3_PWM;
break;
case MOTOR4_A:
case MOTOR4_B:
motorPWM = MOTOR4_PWM;
break;
default:
// Use speed as error flag, -3333 = invalid output.
speed = -3333;
break;
}
if (speed != -3333)
{
shiftWrite(output, high_low);
if (speed >= 0 && speed <= 255)
{
analogWrite(motorPWM, speed);
}
}
}
void shiftWrite(int output, int high_low)
{
static int latch_copy;
static int shift_register_initialized = false;
if (!shift_register_initialized)
{
pinMode(MOTORLATCH, OUTPUT);
pinMode(MOTORENABLE, OUTPUT);
pinMode(MOTORDATA, OUTPUT);
pinMode(MOTORCLK, OUTPUT);
// Set pins for shift register to default value (low);
digitalWrite(MOTORDATA, LOW);
digitalWrite(MOTORLATCH, LOW);
digitalWrite(MOTORCLK, LOW);
// Enable the shift register, set Enable pin Low.
digitalWrite(MOTORENABLE, LOW);
// start with all outputs (of the shift register) low
latch_copy = 0;
shift_register_initialized = true;
}
// The defines HIGH and LOW are 1 and 0.
// So this is valid.
bitWrite(latch_copy, output, high_low);
// Use the default Arduino 'shiftOut()' function to
// shift the bits with the MOTORCLK as clock pulse.
// The 74HC595 shiftregister wants the MSB first.
// After that, generate a latch pulse with MOTORLATCH.
shiftOut(MOTORDATA, MOTORCLK, MSBFIRST, latch_copy);
delayMicroseconds(5); // For safety, not really needed.
digitalWrite(MOTORLATCH, HIGH);
delayMicroseconds(5); // For safety, not really needed.
digitalWrite(MOTORLATCH, LOW);
}
The code above is the Meat where something is going wrong. The code above this is all fine.
WHen the code gets the command 'forward' it runs the VOID goforwards(); but then 'hangs'. Same for 'backwards'.
Seems to get stuck in these VOIDS and so does not respond to the next command from webpage.
Any help is appreciated!

Get Xbee response from Serial and send to a browser

I am trying to do some experiments with Arduino, Ethernet Shield and Xbee Shield.
I demonstrate my set up board like this:
Group 1: Arduino Uno + Xbee shield : broadcast the signal
Group 2: Arduino Uno + Xbee shield + Ethernet shield: receive the signal from
group 1, get the signal strength from AT command and print it into the browser.
The problem here is I can't get the result after sending to the Serial my ATDB command, actually, I am not sure it did worked as I expected.
Here is the code that I used to retrieve the signal strength.
int data;
void setup()
{
Serial.begin(9600);
}
void receiver_checker(){
delay(1200);
Serial.print("+++");
delay(1200);
bool bOK = false;
while (Serial.available() > 0) {
Serial.write(Serial.read());
bOK = true;
}
if(bOK)
{
Serial.println();
Serial.println("ATDB");
delay(100);
while (Serial.available() > 0) {
Serial.write(Serial.read());
}
Serial.println();
}
Serial.println();
}
void loop()
{
while(Serial.available() > 0){
data = Serial.read();
if(data == '1'){
// Broadcaster 1
//Serial.println("1------------------");
receiver_checker();
}
}
}
This part worked as I expected, it printed out in hex number the signal strength of the last package that it received.
Here is the code I combined the previous one and the server part from Web Server tutorial:
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0xCA, 0xFE, 0x00, 0x00, 0x00, 0x02
};
IPAddress ip(1, 1, 1, 2);
int data;
int count = 0;
char result;
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
// Serial.print("server is at ");
// Serial.println(Ethernet.localIP());
}
void loop() {
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
// Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
// Serial.write(c);
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close"); // the connection will be closed after completion of the response
client.println("Refresh: 5"); // refresh the page automatically every 5 sec
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
if (Serial.available() > 0) {
// read the oldest byte in the serial buffer:
data = Serial.read();
// if it's a capital H (ASCII 72), turn on the LED:
if (data == '1') {
count += 1;
client.print("The number of times:");
client.print(count);
result = receiver_checker();
client.print("========================");
client.print(result);
client.print("========================");
}
}
client.println("</html>");
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
Serial.println("client disconnected");
}
}
char receiver_checker(){
char signal;
delay(1200);
Serial.print("+++");
delay(1200);
bool bOK = false;
while (Serial.available() > 0) {
Serial.write(Serial.read());
bOK = true;
}
if(bOK)
{
Serial.println();
Serial.println("ATDB");
delay(100);
while (Serial.available() > 0) {
signal = Serial.read();
}
Serial.println();
}
Serial.println();
return signal;
}
If there is another way to interact with the Xbee shield not go through Serial like I ask and get response directly, please let me know!
Your receiver_checker() function is reading characters back from the XBee module, and just returning the last character received, which is likely a carriage return or line feed.
Update the function to return an int, and replace your while (Serial.available() > 0) with the following:
signal = (int) strtoul(Serial.readString().c_str(), 0, 16);
That's for when the XBee returns a hexadecimal value. If it's returning a decimal value, change the 16 parameter to a 10.