Hi Arduino enthusiasts
I have a problem I'm struggling to figure out. In a nutshell; a python script requests sensor values which it then stores in a database and the second python script pulls data from the data and stores it into arrays on the arduino. I recently created a switch case to either "write" sensor values or "read" relay values. Now the problem comes when dealing with the case "ReadRelays". The python script writes 2 strings; the case statement correctly uses the first string and "forwards" in on the correct case. Straight after, the python script writes another string with the data I want enclosed in delimeters. At the moment it drops the second of the strings and outputs their ascii values in the default case statement. Instead I want this string to "feed into void RedRelays ()"
Is this at all possible and if not how can I get it to work?
Below is a copy of the my code.
/*-----( Import needed libraries )-----*/
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>
#include <RTClib.h>
/*-----( Declare Constants and Pin Numbers )-----*/
#define SOP '('
#define EOP ')'
#define photopin A0
const int temp_pin = A1;
bool started = false;
bool ended = false;
#define RELAY_ON 1
#define RELAY_OFF 0
#define Relay_1 30 // Arduino Digital I/O pin number
#define Relay_2 31
#define Relay_3 32
#define Relay_4 33
#define Relay_5 34 // Arduino Digital I/O pin number
#define Relay_6 35
#define Relay_7 36
#define Relay_8 37 // Arduino Digital I/O pin number
/*-----( Declare objects )-----*/
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(temp_pin);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
//Real Time Clock
RTC_DS1307 RTC;
/*-----( Declare Variables )-----*/
// Assign the addresses of your 1-Wire temp sensors.
DeviceAddress probe1 = { 0x28, 0x64, 0x23, 0xBD, 0x03, 0x00, 0x00, 0x5F };
DeviceAddress probe2 = { 0x28, 0xAB, 0x10, 0xBD, 0x03, 0x00, 0x00, 0x2E };
float photolevel;
float dallas1;
float dallas2;
char photo1[10];
char temp1[10];
char temp2[10];
char inData[80];
char temp;
int count = 0;
int array[11];
int incomingbyte;
byte index;
void setup() {
Serial.begin(9600);
Serial1.begin(9600);
sensors.begin(); //Get DS18B20 temperatures
sensors.setResolution(probe1, 10); //set resolution to 10bit
sensors.setResolution(probe2, 10); //set resolution to 10bit
Wire.begin(); // Start the Wire (I2C communications)
RTC.begin(); // Start the RTC Chip
}
void loop() {
if (Serial.available()>0)
{
incomingbyte=Serial.read();
switch (incomingbyte) {
case 'a':
//Serial.println(incomingbyte);
WriteSensors();
SensorAction();
break;
case 'b':
//Serial.println(incomingbyte);
ReadRelays();
RelayAction();
break;
default:
//Serial.println(incomingbyte);
break;
}
}
}
void WriteSensors()
{
DateTime now = RTC.now(); //Get time from RTC
photolevel = analogRead(photopin); //Read light level
sensors.requestTemperatures();
dallas1 = sensors.getTempC(probe1);
dallas2 = sensors.getTempC(probe2);
dtostrf(photolevel, 1, 0, photo1);
dtostrf(dallas1, 1, 2, temp1);
dtostrf(dallas2, 1, 2, temp2);
String tempAsString1 = String(photo1);
String tempAsString2 = String(temp1);
String tempAsString3 = String(temp2);
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(" ");
Serial.println(tempAsString1 + " " + tempAsString2 + " " + tempAsString3);
}
void ReadRelays()
{
// Read all serial data available, as fast as possible
while(Serial.available() > 0)
{
char inChar = Serial.read();
if(inChar == SOP)
{
index = 0;
inData[index] = '\0';
started = true;
ended = false;
}
else if(inChar == EOP)
{
ended = true;
break;
}
else
{
if(index < 79)
{
inData[index] = inChar;
index++;
inData[index] = '\0';
}
}
}
// We are here either because all pending serial
// data has been read OR because an end of
// packet marker arrived. Which is it?
if(started && ended)
{
// The end of packet marker arrived. Process the packet
if (strlen(inData) > 0)
{
char *token = strtok(inData, ",");
if(token)
{
index = 0;
array[index] = atoi(token);
while (token = strtok(NULL, ","))
{
array[index++] = atoi(token);
}
}
}
// Reset for the next packet
started = false;
ended = false;
index = 0;
inData[index] = '\0';
}
}
void RelayAction() {
Serial.print(array[0]);
Serial.print(array[1]);
Serial.print(array[2]);
Serial.print(array[3]);
Serial.print(array[4]);
Serial.print(array[5]);
Serial.print(array[6]);
Serial.print(array[7]);
Serial.print(array[8]);
Serial.print(array[9]);
Serial.print(array[10]);
Serial.println(array[11]);
}
void SensorAction() {
}
I have got ReadRelays() to work separately in void loop() but not in a case statement.
Thanks,
Damo