Getting data from Arduino to Linux

Hello!
I've seen many great projects online for gardening with Arduino and while I'm waiting for the parts for one of those, I decided to try and get a simple data logging system up.

I'm running a Yun with measuring of temperature and moisture, all in a small waterproof container with 3 LEDs (red, yellow and green).
I did the tests with a Leonardo and moved over to the Yun now, it works when it comes to sending serial and changing the LEDs.

Now to the tricky part... I have a Raspberry Pi 2 running SQLite, Apache w/PHP and I have everything I need except getting the temperature and moisture from the Arduino and into variables in python so that I can send it to the SQLite.

(TLDR;)

Question:
Could someone help me with a sketch example that sends to integers over the bridge and a python script that receives it?

In case it's needed, here is the code I'm working with, at the moment it won't let me connect over wifi though, might be a bug in there :wink:

#include <OneWire.h>

OneWire  ds(11); //Temp on 11
const int VAL_PROBE = 0; // Analog pin 0

void setup() {
  Serial.begin(9600);
  pinMode(10, OUTPUT); // Red LED
  pinMode(9, OUTPUT);  // Yellow LED
  pinMode(8, OUTPUT); // Green LED
}

void loop() {
  delay(1000); // wait for a second
  int moisture = analogRead(VAL_PROBE); // Read moisture level
  
  if (moisture >= 700) // If moisture is 700 or above, All is okay - green light
  {
    digitalWrite(8, HIGH); // Green is ON
    digitalWrite(9, LOW); // Yellow is off
    digitalWrite(10, LOW); // Red is off
    
  }
  else // if under 700
  {
    if (moisture > 400) // but above 400 - warning light
    {
      digitalWrite(9, HIGH); //Yellow is ON
      digitalWrite(10, LOW); // Red is off
      digitalWrite(8, LOW);// Green is off
    }
    else // if neither, time for water.
    {
      digitalWrite(10, HIGH); // Red is ON
      digitalWrite(9, LOW); //Yellow is off
      digitalWrite(8, LOW);// Green is 
    }
  }
  byte i;
  byte present = 0;
  byte type_s;
  byte data[12];
  byte addr[8];
  float celsius, fahrenheit;
  
  if ( !ds.search(addr)) {
    ds.reset_search();
    delay(250);
    return;
  }

  if (OneWire::crc8(addr, 7) != addr[7]) {
      return;
  }
  
  // the first ROM byte indicates which chip
  switch (addr[0]) {
    case 0x10:
      type_s = 1;
      break;
    case 0x28:
      type_s = 0;
      break;
    case 0x22:
      type_s = 0;
      break;
    default:
      return;
  } 

  ds.reset();
  ds.select(addr);
  ds.write(0x44, 1);        // start conversion, with parasite power on at the end
  
  delay(1000); 
  // we might do a ds.depower() here, but the reset will take care of it.
  
  present = ds.reset();
  ds.select(addr);    
  ds.write(0xBE);         // Read Scratchpad


  for ( i = 0; i < 9; i++) {           // we need 9 bytes
    data[i] = ds.read();
  }
  int16_t raw = (data[1] << 8) | data[0];
  if (type_s) {
    raw = raw << 3; // 9 bit resolution default
    if (data[7] == 0x10) {
      // "count remain" gives full 12 bit resolution
      raw = (raw & 0xFFF0) + 12 - data[6];
    }
  } else {
    byte cfg = (data[4] & 0x60);
    // at lower res, the low bits are undefined, so let's zero them
    if (cfg == 0x00) raw = raw & ~7;  // 9 bit resolution, 93.75 ms
    else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
    else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
    //// default is 12 bit resolution, 750 ms conversion time
  }

  celsius = (float)raw / 16.0;
  Serial.println(moisture);
  Serial.println(celsius);  
  fahrenheit = celsius * 1.8 + 32.0;
  delay(1000);
}

Gladly answering my own questions with a link.
It would seem that I just had to alter my Google search term to get the proper forum posts.

http://forum.arduino.cc/index.php?topic=188998.0

Glad you found it. 8)

Another way to get the data to your RasPi is to have the sketch put() the appropriate data (moisture, temperature, red/yellow/green status) to the Bridge. Then skip the Python code on the Linux side, and have the RasPi fetch the data directly by accessing http://arduino.local/data/get using curl() or some similar tool to make a web (AJAX) style request. The result of the request will be a JSON structure containing all of the key/value pairs. In this way, the RasPi can poll for the data directly, as needed.

ShapeShifter:
Glad you found it. 8)

Another way to get the data to your RasPi is to have the sketch put() the appropriate data (moisture, temperature, red/yellow/green status) to the Bridge. Then skip the Python code on the Linux side, and have the RasPi fetch the data directly by accessing http://arduino.local/data/get using curl() or some similar tool to make a web (AJAX) style request. The result of the request will be a JSON structure containing all of the key/value pairs. In this way, the RasPi can poll for the data directly, as needed.

Oh, That would be even better!
I uploaded the D12 example just now and called the web service with /data/get/D12 and got {"value":"1","key":"D12","response":"get"}, some simple parsing should let me grab that info on the Pi..
I also took a gamble with http://arduino.local/data/put/D12/0 and it turned off the light...

...I have to say... I love this xD
Thanks for the tip ShapeShifter, Karma for you.

edit: What is the quickest way to HTTP GET in Python? - Stack Overflow allows me to put data to the Yun and get the feedback

Now, if you want a neat trick, put a few more data items on the bridge, not just D12. Then try http://arduino.local/data/get and see what you see... (Note, there is no key name after get) There is no need to make a call for each individual data item, you can get them all at once.

Now, to make the parsing even easier, check out Python's JSON.loads function

What's the difference among bridge.put() and client.println()?

jumpjack:
What's the difference among bridge.put() and client.println()?

Bridge.put() stores a named value that can later be read by Bridge.get() (either by the sketch or by Linux code) or can be read using a web request of the form http://arduino.local/data/get/key_name. It is an asynchronous method of storing named data for later retrieval by many methods, both local and remote. You can think of it as network accessible shared variables.

Client.println() writes the output to the network socket that is being managed by the client object. That output will be received by whatever is connected to the other end of the socket. It a synchronous communications method, much like a serial port, but running over a network.

thanks