EvanOverman
Posts: 1
Joined: Wed Mar 04, 2020 12:08 am

How do I send digital/binary info over wifi?

Wed Mar 04, 2020 12:14 am

I'm programming in c++ and need a way to send data to another raspberry pi running this code

Code: Select all

/*
*
* Made By Evan Overman
* For Raspberry Pi 3 B+
* Designed to Interact with Arduino Nano Every
* March 2, 2020
*
*/

#include <iostream>
#include <string>
#include <wiringPi.h>

using namespace std;

int main (void) 
{
  //Set Up Wiring Pi
  wiringPiSetup();

  //Set Pin Modes for Interacting With Arduino Nano Every
  pinMode(29, OUTPUT);
  pinMode(28, OUTPUT);

  //Give Pins Names for Easy Use
  const int toLight = 29;
  const int toMoniters = 28;

  //Declare Variables for User Input
  string commandInput;
  string commandOutput;

  //Tell the User that the Program has Started and is Ready for Input
  cout << "Started" << endl;
  cout << "command::";

  //Get the User's Desired Command
  cin >> commandInput;

  //Deside What to do Based on the User's Command
  bool looping = true; 
  while(looping == true)
  {
    if(commandInput == "toggle29")
    {
      //Toggles the Output of Pin 29
      digitalWrite(29, !digitalRead(29));
      cout << "Toggled Pin 29 to " << digitalRead(29) << endl;
    }
    else if(commandInput == "toggle28")
    {
      //Toggles the Output of Pin 28
      digitalWrite(28, !digitalRead(28));
      cout << "Toggled Pin 28 to " << digitalRead(28) << endl;
    }
    else if(commandInput == "read29")
    {
      //Tells the User the Current State of Pin 29
      cout << "Pin 29 is Currently on " << digitalRead(29) << endl;
    }
    else if(commandInput == "read28")
    {
      //Tells the User the Current State of Pin 28
      cout << "Pin 28 is Currently on " << digitalRead(28) << endl;
    }
    else
    {
      //Tells the User that Their Command Entry was Invalid
      cout << "That is not Currently a Command" << endl;
    }

    //Let the User Know that the Program is Ready for Input Again
    cout << "command::";
    cin >> commandInput;
  }
}
This code send data to an arduino that interacts with a relay. I need a way to send digital/binary info to be put into either a bool variable or be read digital by wiring pi. This would be over wifi or bluetooth. If you have any solutions please respond with them. Thanks! :D

Heater
Posts: 15817
Joined: Tue Jul 17, 2012 3:02 pm

Re: How do I send digital/binary info over wifi?

Wed Mar 04, 2020 7:39 am

I use NATS to send messages between my machines and programs https://nats.io/
Memory in C++ is a leaky abstraction .

User avatar
B.Goode
Posts: 10138
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: How do I send digital/binary info over wifi?

Wed Mar 04, 2020 8:25 am

MQTT is another tool that allows data transfer over a network connection between an RPi and an Arduino.

LdB
Posts: 1549
Joined: Wed Dec 07, 2016 2:29 pm

Re: How do I send digital/binary info over wifi?

Wed Mar 04, 2020 11:00 am

Secured SSH, unsecured Telnet are the obvious choices.

Heater
Posts: 15817
Joined: Tue Jul 17, 2012 3:02 pm

Re: How do I send digital/binary info over wifi?

Wed Mar 04, 2020 12:20 pm

Not if you are programming in C/C++ they are not.
Memory in C++ is a leaky abstraction .

LdB
Posts: 1549
Joined: Wed Dec 07, 2016 2:29 pm

Re: How do I send digital/binary info over wifi?

Wed Mar 04, 2020 3:24 pm

Sorry I cant take that serious it's 40 lines of code each direction on linux sockets.
If you want to encrypt its a few more lines from the standard encrypt libraries
Throw in a CRC check for the transfer
Whole thing is probably 200 lines of code with two threads listener and sender.

Anyone beyond novice should basically be able to do it because there are clear examples to follow.

From the standard linux socket samples

SERVER CODE which just pushes time to anything that connects its a listener

Code: Select all

#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <time.h> 

int main(void)
{
    int listenfd = 0, connfd = 0;
    struct sockaddr_in serv_addr = {0}; 

    char sendBuff[1025] = {0};
    time_t ticks; 

    listenfd = socket(AF_INET, SOCK_STREAM, 0);

    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    serv_addr.sin_port = htons(5000); 

    bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)); 

    listen(listenfd, 3); 

    while(1)
    {
        connfd = accept(listenfd, (struct sockaddr*)NULL, NULL); 
        printf("Connection made\r\n");
        ticks = time(NULL);
        snprintf(sendBuff, sizeof(sendBuff), "%.24s\r\n", ctime(&ticks));
        write(connfd, sendBuff, strlen(sendBuff)); 

        close(connfd);
        sleep(1);
     }
}

CLIENT CODE that displays the time .. you need to change IP to the server

Code: Select all

#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h> 

int main(void)
{
    int sockfd = 0, n = 0;
    char recvBuff[1024] = { 0 };
    struct sockaddr_in serv_addr = { 0 };

    if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    {
        printf("\n Error : Could not create socket \n");
        return 1;
    } 

    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(5000); 

    // Change this address as required
    if(inet_pton(AF_INET, "192.168.1.4", &serv_addr.sin_addr)<=0) 
    {
        printf("\n inet_pton error occured\n");
        return 1;
    } 

    if( connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
    {
       printf("\n Error : Connect Failed \n");
       return 1;
    } 

    while ( (n = read(sockfd, recvBuff, sizeof(recvBuff)-1)) > 0)
    {
        recvBuff[n] = 0;
        if(fputs(recvBuff, stdout) == EOF)
        {
            printf("\n Error : Fputs error\n");
        }
    } 

    if(n < 0)
    {
        printf("\n Read error \n");
    } 

    return 0;
}

PhatFil
Posts: 1672
Joined: Thu Apr 13, 2017 3:55 pm
Location: Oxford UK

Re: How do I send digital/binary info over wifi?

Wed Mar 04, 2020 4:04 pm

Depends on where abouts you want to focus your energies, I would opt for Mqtt as suggested above . https://github.com/eclipse/paho.mqtt.c
and https://github.com/knolleary/pubsubclient,

LdB
Posts: 1549
Joined: Wed Dec 07, 2016 2:29 pm

Re: How do I send digital/binary info over wifi?

Wed Mar 04, 2020 5:54 pm

No issue but you also need to make the OP aware of the security problems with these large projects they always come under attack.

Baracuda has a test setup to show you how easy it is to exploit MQTT and what you need to do about it but a search of MQTT and hack will give you the idea.
https://embedded-app-server.info/MQTT.lsp

Dont get me wrong SSH can be attacked by bruteforce but usually you just jail the IP for a period if it fails 3 times.

By the look of the code he gave it isn't commercial so I don't think it's an issue but when making recommendation give them all the data so they don't waste time.

Return to “C/C++”