evenjc

Arduino - Slave Node

Jun 7th, 2019
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. byte expected_sensor_type_byte = 0x33;
  2. byte expected_hello_byte = 0x32;
  3. byte expected_datarequest_byte = 0x31;
  4.  
  5. float temperature, humidity, pressure;
  6.  
  7. void setup() {
  8.   //Initiating serial communication and flushing serial port, just for good measure
  9.   Serial.begin(9600);
  10.   Serial.flush();
  11.   randomSeed(analogRead(A0));
  12.  
  13. }
  14.  
  15. void loop() {
  16.   // Check if there is something on the serial port
  17.   if (Serial.available()) {
  18.     //read available data, save to recieved_data
  19.     int recieved_data = Serial.read();
  20.  
  21.     //Did we recieve the hello byte?
  22.     //This is used by the Raspberry PI to ensure a stable connection
  23.     if (recieved_data == expected_hello_byte) {
  24.       Serial.print("HELLO");
  25.       delay(50);
  26.     }
  27.     //Did we recieve the sensor type byte?
  28.     if (recieved_data == expected_sensor_type_byte) {
  29.       //These lines defines the values to be logged in the csv-file
  30.       //First line is the name of your Arduino Node
  31.       //The following lines are optional, and you can add or remove as many as you want
  32.       //For each Serial.print("name of value") + Serial.print("\t") a column will be generated.
  33.       Serial.print("TempNode");
  34.       Serial.print("\t");
  35.       Serial.print("Temp [C]");
  36.       Serial.print("\t");
  37.       Serial.print("Humidity [%]");
  38.       Serial.print("\t");
  39.       Serial.print("Pressure [kPa]");
  40.       delay(50);
  41.     }
  42.     //Did we recieve the data request byte?
  43.     if (recieved_data == expected_datarequest_byte) {
  44.       //Reading sensor values
  45.       temperature = random(-30, 3000)/100.00;
  46.       humidity = random(3000,9000)/100.00;
  47.       pressure = random(95000, 105000)/100.00;
  48.  
  49.       //Printing them to the serial port.
  50.       //This has to be the same order as the names from last if.
  51.       Serial.print(temperature);
  52.       Serial.print(";");
  53.       Serial.print(humidity);
  54.       Serial.print(";");
  55.       Serial.print(pressure);
  56.       Serial.print(";");
  57.     }
  58.   }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment