Expected primary-expression before 'const' - arduino

I am trying to send one UDP packet but get stuck on concatenate between different data type and don't know how to solve it.
The code:
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
WiFiUDP Udp;
const char* UdpIPaddress = "192.168.0.240";
unsigned int UdpPort = 4210;
const char* Udpmessage = "Message";
int a=0;
float Temp=29.25;
void setup() {
Serial.begin (115200);
}
void loop() {
if(a==0){
a = a + 1;
const char* sendmessage = const char*(a) + "=" + const char*(Temp) + "=" + Udpmessage;
UdpSend(sendmessage, UdpIPaddress, UdpPort);
}
}
void UdpSend(const char* message, const char * ipaddress, int port){
Serial.println("Send message: ");
Udp.beginPacket(ipaddress, port);
Udp.write(message);
Udp.endPacket();
}
Arduino IDE compiler error: expected primary-expression before 'const'
Please help...

Found the answer of my own question:
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
WiFiUDP Udp;
const char* UdpIPaddress = "192.168.0.240";
unsigned int UdpPort = 4210;
char buf[40];
const char* Udpmessage = "Message";
int a=0;
float Temp=29.25;
void setup() {
Serial.begin (115200);
}
void loop() {
if(a==0){
a = a + 1;
sprintf(buf, "%d=%f=%s", a,Temp, Udpmessage);
UdpSend(buf, UdpIPaddress, UdpPort);
}
}
void UdpSend(const char* message, const char * ipaddress, int port){
Serial.println("Send message: ");
Udp.beginPacket(ipaddress, port);
Udp.write(message);
Udp.endPacket();
}
Works perfectly :) :) :)

Related

Esp server socket disconnected

Hy guys I´m trying to send a message from my phone in a server ESP32 but any app tcp connection that I try returns me disconnected. Im doing somehing wrong? my code is missing something... I don´t know
#include <Arduino.h>
#include <WiFi.h>
#include "NetManager.hpp"
constexpr char SSID[] = "ESP32Server";
constexpr char PASSWORD[] = "12345678";
constexpr int SERVER_PORT = 80;
NetManager netManager(SERVER_PORT, SSID, PASSWORD);
int temperature = 24;
void handleCommandReceive(int command)
{
temperature = command;
}
int handleCommandSend()
{
return temperature;
}
void setup()
{
Serial.begin(115200);
netManager.setup(handleCommandReceive, handleCommandSend);
}
void loop()
{
netManager.loop();
delay(10);
}
Complete code
https://github.com/ProjetosESP32/air-esp
I tried in Flutter too with a socket package but i got same error

Hello World With NRF24L01

I have Arduino and a Duinotech NRF24L01, I am trying to send the string "Hello world" with maniacs bug RF24 library however, I think it cannot detect the incoming RF signal.
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
RF24 radio(7, 8); // CE, CSN
const uint64_t pipe = 0xF0F0F0F0E1L;
void setup() {
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(0, pipe);
radio.startListening();
}
void loop() {
if (radio.available()) {
char text[32] = "";
radio.read(&text, sizeof(text));
Serial.println(text);
}
else {
Serial.println("Data was not found");
}
In the read code, it would always execute data was not found. This makes me think that maybe it does not find the RF signal at all.
Here is the code that writes the data.
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
RF24 radio(7, 8); // CE, CSN
int text = 1;
const uint64_t pipe = 0xF0F0F0F0E1LL;
void setup() {
Serial.begin(9600);
radio.begin();
radio.openWritingPipe(pipe);
radio.stopListening();
}
void loop() {
radio.write(&text, sizeof(text));
Serial.println("Sending Data");
delay(1000);
}
Try this
For the transmitter Instead of const uint64_t pipe = 0xF0F0F0F0E1LL; use const byte address[6] = "00001"; as the address and then have you void setup like the code below
void setup() {
radio.begin();
radio.openWritingPipe(address);
radio.setPALevel(RF24_PA_MIN);
radio.stopListening();
}
Then ensure that you have a value set for the test variable to be transmitted as below
void loop() {
const char text[] = "Hello World";
radio.write(&text, sizeof(text));
Serial.println("Sending Data");
delay(1000);
}
At the receiver end have this code running
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7, 8); // CE, CSN
const byte address[6] = "00001";
void setup() {
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(0, address);
radio.setPALevel(RF24_PA_MIN);
radio.startListening();
}
void loop() {
if (radio.available()) {
char text[32] = "";
radio.read(&text, sizeof(text));
Serial.println(text); //This will print out the received value
}
}
PS: Ensure that all the connections are done to the right pins
AND you can test if the NRF24L01 chip is connected correctly by adding the code below
bool result = radio.isChipConnected ();
Serial.println (result);
it should print out a 1 to the serial monitor if the NRF24L01 chip is connected correctly

Arduino interfacing with GPS gy Neo 6Mv2

I have written the following code. I want the string returned from the method
displayInfo( )
to be updated and printed just once, but the method sends strings repeatedly. If I copy the same code in void setup( ) function it is not printing any value.
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
static const int RXPin = 12, TXPin = 13;
static const uint32_t GPSBaud = 9600;
// The TinyGPS++ object
TinyGPSPlus gps;
// The serial connection to the GPS
SoftwareSerial ss(RXPin, TXPin);
String msg="";
String message="";
void setup()
{
Serial.begin(9600);
ss.begin(GPSBaud);
}
void loop()
{
while (ss.available() > 0)
if (gps.encode(ss.read()))
message = displayInfo();
Serial.print(message);
}
String displayInfo()
{
if (gps.location.isValid())
{
String lati=String(gps.location.lat(), 3);
String logi=String(gps.location.lng(),3);
msg=lati+","+logi+"\n";
return(msg);
}
}
I have updated the code to recover some of the errors like function returning value with a global variable but it still does not provide me a single String value even after I had put the void loop( ) code in void setup( )
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
static const int RXPin = 12, TXPin = 13;
static const uint32_t GPSBaud = 9600;
// The TinyGPS++ object
TinyGPSPlus gps;
// The serial connection to the GPS device
SoftwareSerial ss(RXPin, TXPin);
String msg="";
void setup()
{
Serial.begin(9600);
ss.begin(GPSBaud);
while (ss.available()>0)
if (gps.encode(ss.read()))
displayInfo();
Serial.print(msg);
}
void loop()
{
// This sketch displays information every time a new sentence is correctly encoded.
}
void displayInfo()
{
//Serial.print(F("Location: "));
if (gps.location.isValid())
{
String lati=String(gps.location.lat(), 3);
String longi=String(gps.location.lng(), 3);
msg="location: "+lati+","+longi+"\n";
}
else
{
msg=msg+"invalid";
}
}
EDIT:
The ss (SoftwareSerial) has a buffer wich contains the data that is ready to be send. ss.begin() will return 0 in your setup since the buffer is still empty, therefore the while loop will not be iterated even once.
The loop() function of the arduino works like a while so by placing the content of that while loop, and replacing the while with an if, you will be able to keep testing until you have a message in the buffer.
By adding a boolean to check if you have already sent a message, you can make sure only 1 message will be sent.
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
static const int RXPin = 12, TXPin = 13;
static const uint32_t GPSBaud = 9600;
TinyGPSPlus gps;
SoftwareSerial ss(RXPin, TXPin);
boolean sent = false;
void setup()
{
Serial.begin(9600);
ss.begin(GPSBaud);
}
void loop()
{
if (ss.available()>0 && sent == false){
if (gps.encode(ss.read())){
String msg = displayInfo();
if (msg != NULL){
Serial.print(msg);
sent = true;
}
}
}
}
String displayInfo()
{
if (gps.location.isValid())
{
String msg="";
String lati=String(gps.location.lat(), 3);
String logi=String(gps.location.lng(),3);
msg=lati+","+logi+"\n";
return(msg);
}
else{
return NULL;
}
}
By returning a NULL in the displayinfo()'s else statement, and testing for it within the loop(), you can ensure that you will only print a message when everything was working.

communicating arduino to the other Arduino using RF24 getting the wrong result

I use these codes to transfer hello worlds but i just receive
"&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&"
in the receiver.
I don't understand what is the problem.
*******************Transmitter code:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7, 8);
const byte rxAddr[6] = "00001";
void setup()
{
radio.begin();
radio.setRetries(15, 15);
radio.openWritingPipe(rxAddr);
radio.stopListening();
}
void loop()
{
const char text[] = "Hello World";
radio.write(&text, sizeof(text));
delay(1000);
}
*****************receiver code:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7, 8);
const byte rxAddr[6] = "00001";
void setup()
{
while (!Serial);
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(0, rxAddr);
radio.startListening();
}
void loop()
{
if (radio.available())
{
char text[32] = {0};
radio.read(&text, sizeof(text));
Serial.println(text);
}
}
On the sending site you use:
const char text[] = "Hello World";
radio.write(&text, sizeof(text));
On the receiver site:
char text[32] = {0};
radio.read(&text, sizeof(text));
Are the both sizeof(text) equals?
In the sending part text is 11 bytes
In the receiving part text is 32 bytes

Uint8_to Char array Arduino sketch

I am using this sh1 function. Instead of printing the hash value I want to store it in a char array or string for further processing.
#include "sha1.h"
#include <stdlib.h>
void setup(){
Serial.begin(9600);
}
void loop() {
uint8_t *hash;
Sha1.init();
Sha1.print("This is a message to hash");
hash = Sha1.result();
for (int j=0;j<20;j++)
Serial.print("0123456789abcdef"[hash>>4]);
Serial.print("0123456789abcdef"[hash&0xf]);
}
Serial.println();
delay(1000);
}
char digitArray[]= {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
char buffer[40];
for (int i = 0; i<40; i+=2){
buffer[i] = digitArray[(hash[i/2] & 0xF0) >> 4];
buffer[i+1] = digitArray[(hash[i/2] & 0x0F)];
}

Resources