Send time from arduino A to arduino B

Hiiii All

Can you help me? I tried using python to get the time on the pc. I use this reference Demo of PC-Arduino comms using Python - #5 by Robin2 - Interfacing w/ Software on the Computer - Arduino Forum I managed to get pc time. But I am confused to enter the time I get from python to send to other arduino using rf24network. I am newbie in terms of coding. How to take the time result from python I put in this object payload_t payload = {packets_sent++} Then I send it to arduino B

RF24NetworkHeader header (/ * to node * / 0);
Bool ok = network.write (header, & payload, sizeof (payload))

Below is my complete code

Arduino code

#include <RF24Network.h>
#include <RF24.h>
#include <DHT.h>
#include <SPI.h>
#include <printf.h>

#define DHTPIN 2
#define DHTTYPE DHT11

DHT  dht(DHTPIN, DHTTYPE);


RF24 radio(9,10);                // nRF24L01(+) radio attached using Getting Started board 
RF24Network network(radio);      // Network uses that radio
const uint16_t this_node = 01;    // Address of our node in Octal format ( 04,031, etc)

//const unsigned long interval = 5000;
//unsigned long last_sent;

const byte buffSize = 40;
char inputBuffer[buffSize];
const char startMarker = '<';
const char endMarker = '>';
byte bytesRecvd = 0;
boolean readInProgress = false;
boolean newDataFromPC = false;

struct payload_t {                 // Structure of our payload
  float temperature;
  float humidity;
  //What should i add here
};

//=============

void setup() {
  Serial.begin(9600);
  printf_begin();
  // tell the PC we are ready
  Serial.println("<Arduino is ready>");
 
  SPI.begin();
  radio.begin();
  network.begin(/*channel*/ 90, /*node address*/ this_node);
  dht.begin();
  
}

//=============

void loop() {
    network.update();                  // Check the network regularly
    
    getDataFromPC();
    replyToPC();
}

//=============

void getDataFromPC() {
    // receive data from PC and save it into inputBuffer    
  if(Serial.available() > 0) {
    char x = Serial.read();
      // the order of these IF clauses is significant
    if (x == endMarker) {
      readInProgress = false;
      newDataFromPC = true;
      inputBuffer[bytesRecvd] = 0;
    }
    
    if(readInProgress) {
      inputBuffer[bytesRecvd] = x;
      bytesRecvd ++;
      if (bytesRecvd == buffSize) {
        bytesRecvd = buffSize - 1;
      }
    }

    if (x == startMarker) { 
      bytesRecvd = 0; 
      readInProgress = true;
    }
  }
}

void sendDataToPeer(){
  float t = dht.readTemperature();
  float h = dht.readHumidity();
  
  payload_t payload = {t,h}; //And I am confused to insert inputbuffer from python to struct pyload
  RF24NetworkHeader header(/*to node*/ 0);
  bool ok = network.write(header,&payload,sizeof(payload));
  if (ok)
    Serial.println("Data Sensor Send OK");
  else
    Serial.println("Data Sensor Send Failed");
}

void replyToPC() {
  if (newDataFromPC) {
    newDataFromPC = false;
    Serial.print("<Msg ");
    Serial.print(inputBuffer);
    Serial.println(">");
    sendDataToPeer();
  }
}

Python code

def sendToArduino(sendStr):
    ser.write(sendStr)


# ======================================

def recvFromArduino():
    global startMarker, endMarker

    ck = ""
    x = "z"  # any value that is not an end- or startMarker
    byteCount = -1  # to allow for the fact that the last increment will be one too many

    # wait for the start character
    while ord(x) != startMarker:
        x = ser.read()

    # save data until the end marker is found
    while ord(x) != endMarker:
        if ord(x) != startMarker:
            ck = ck + x
            byteCount += 1
        x = ser.read()

    return (ck)


# ============================

def waitForArduino():
    # wait until the Arduino sends 'Arduino Ready' - allows time for Arduino reset
    # it also ensures that any bytes left over from a previous message are discarded

    global startMarker, endMarker

    msg = ""
    while msg.find("Arduino is ready") == -1:

        while ser.inWaiting() == 0:
            pass

        msg = recvFromArduino()

        print msg
        print


# ======================================

def runTest(td):
    numLoops = td
    waitingForReply = False

    n = 0
    while n < numLoops:

        waktu = str(time.asctime(time.localtime(time.time())))

        if waitingForReply == False:
            sendToArduino('<'+waktu+'>')
            print "Pesan Ke-"+str(n)+" : "+ waktu
            waitingForReply = True

        if waitingForReply == True:

            while ser.inWaiting() == 0:
                pass

            dataRecvd = recvFromArduino()
            print "Pesan Balasan : " + dataRecvd
            n += 1
            waitingForReply = False

            print "==========="

        time.sleep(1)


# ======================================

# THE DEMO PROGRAM STARTS HERE

# ======================================

import serial
import time


# NOTE the user must ensure that the serial port and baudrate are correct
serPort = "COM4"
baudRate = 9600
ser = serial.Serial(serPort, baudRate)
print "Serial port " + serPort + " opened  Baudrate " + str(baudRate)

startMarker = 60
endMarker = 62

waitForArduino()
runTest(10)

ser.close

Your help is very valuable to me

payload_t payload = {packets_sent++};

That can't be right. payload_t is defined as a struct containing a long and a char array. And here you try to set it equal to just an unsigned long. So it will send over a 2 then a 3 then a 4, but nowhere are you putting any time into that payload.

Haii Delta_G

That can't be right. payload_t is defined as a struct containing a long and a char array. And here you try to set it equal to just an unsigned long. So it will send over a 2 then a 3 then a 4, but nowhere are you putting any time into that payload.
[/quote]

Can you help me to fix it?
I really need it

Your help is very valuable to me

Well I've given all I can give from what I can see. Can you add anything to the discussion? Where should the payload be coming from? What should be in it?

Delta_G:
Well I've given all I can give from what I can see. Can you add anything to the discussion? Where should the payload be coming from? What should be in it?

Sorry i incorrectly entered the arduino code. Arduino code I have fixed. I want to enter the time i get from python to structure payload and i will send it to arduino B using rf24network

Well now your question at least makes some sense.

struct payload_t {                 // Structure of our payload
  float temperature;
  float humidity;
  //What should i add here
};

What is the data type of the thing you got from the PC? You stored it in a char array did you not? So put a char array there. Make sure it is big enough.

Then when you are filling the payload you'll have to split things up. You can't do it all in one line. You'll have to set the members of the struct one at a time and then use strcpy to copy the inputBuffer into the char array in the payload struct.

Delta_G:
Then when you are filling the payload you'll have to split things up. You can't do it all in one line. You'll have to set the members of the struct one at a time and then use strcpy to copy the inputBuffer into the char array in the payload struct.

Can you give an example. Thanks for your help

ahmat:
Can you give an example. Thanks for your help

An example of how to put things into a struct? I'm pretty sure there are thousands of C++ refs that go over that in great detail. Please at least give it a try before you just ask me to do it for you.

Delta_G:
An example of how to put things into a struct? I'm pretty sure there are thousands of C++ refs that go over that in great detail. Please at least give it a try before you just ask me to do it for you.

Thank you so much Delta_G. I will try your suggestion

This reminds me of the Goon Show script in which the stupid one was asked the time and he said "hold on a minute, I have it written on a piece of paper. A nice man gave it to me this morning"

...R
Simple nRF24L01+ Tutorial