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
#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);
}