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