Witam stworzyłem kod który to ma odczytywać dane z odbiornika i przesyłać je do mysqla, lecz coś nie działa. Oto kod:
Kod:
#include <Wire.h>
#include <Streaming.h> // http://arduiniana.org/libraries/streaming/
#include <SPI.h>
#include "RTClib.h"
#include <Ethernet.h>
#include <SoftwareSerial.h>
#include "I2C.h" //I2C Library can be downloaded below
RTC_DS1307 RTC;
const byte numChars = 40;
char receivedChars[numChars];
char tempChars[numChars];
String fTemp, fPress, fwil, fpomiar, iilosc;
boolean newData = false;
static unsigned long NowMillis = 0;
static unsigned long LastMillis = 0;
static unsigned long LoopMillis = 0;
int mies;
String data;
long previousMillis = 0;
unsigned long currentMillis = 0;
long interval = 250000; // READING INTERVAL
SoftwareSerial HC12(53, 54); // HC-12 TX Pin, HC-12 RX Pin
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
char server[] = "192,168,0,107";
IPAddress ip(192,168,0,177);
EthernetClient client;
void setup() {
if (client.connect(server, 80)) {
Serial.println("connected");
Serial.begin(9600); // Serial port to computer
HC12.begin(9600);
Wire.begin();
RTC.begin();
// Check to see if the RTC is keeping time. If it is, load the time from your computer.
if (! RTC.isrunning()) {
Serial.println("RTC is NOT running!");
// This will reflect the time that your sketch was compiled
RTC.adjust(DateTime(__DATE__, __TIME__));
}
}
}
void loop() {
recvWithStartEndMarkers();
if (newData == true) {
strcpy(tempChars, receivedChars); // this temporary copy is necessary to protect the original data
// because strtok() used in parseData() replaces the commas with \0
parseData(); // Strip required data out of received string
showParsedData();
newData = false;
}
}
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (HC12.available() > 0 && newData == false) {
rc = HC12.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0';
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
void parseData() { // split the data into its parts
char * strtokIndx; // this is used by strtok() as an index
strtokIndx = strtok(tempChars,","); // Get the first part of the data
fTemp = atof(strtokIndx);
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
fPress = atof(strtokIndx);
strtokIndx = strtok(NULL, ",");
fwil = atof(strtokIndx);
strtokIndx = strtok(NULL, ",");
fpomiar = atof(strtokIndx);
strtokIndx = strtok(NULL, ",");
iilosc = atol(strtokIndx);
}
void showParsedData() {
DateTime now = RTC.now();
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print('/');
Serial.print(now.year(), DEC);
Serial.print(' ');
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute()-23, DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.print(" : T ");
Serial.print(fTemp);
Serial.print(" : Pr ");
Serial.print(fPress);
Serial.print(" : wil ");
Serial.print(fwil);
Serial.print(" : pomiar ");
Serial.print(fpomiar);
Serial.print(" : ilosc ");
Serial.print(iilosc);
Serial.print('/t');
LastMillis = NowMillis;
NowMillis = millis();
LoopMillis = NowMillis - LastMillis;
Serial.print(" : LoopTime ");
Serial.println(LoopMillis);
delay(500);
//-------------------------------------------------
data = "temp=" + fTemp + "|" + fwil + "|" + fPress;
if (client.connect(server, 8080)) {
client.print("GET /add.php?"); // This
client.print("value="); // This
client.print(data); // And this is what we did in the testing section above. We are making a GET request just like we would from our browser but now with live data from the sensor
client.println(" HTTP/1.1");
client.println("Host: 192.168.0.107");
client.println("Connection: close");
client.println(); // Empty line
client.println(); // Empty line
client.stop(); // Closing connection to server
}else {
// If Arduino can't connect to the server (your computer or web page)
Serial.println("--> connection failed\n");
}
//--------------------------------------------------
}