Witam, próbuię zrobić program do arduino który to wysyłał by dane przez Ethernet shild do PHP.
Lecz z jakiś przyczyn arduino nie chce się połączyć z stroną. Oto mój kod:
#include <Wire.h>
#include <SPI.h>
#include "RTClib.h"
#include <Ethernet.h>
#include <SoftwareSerial.h>
#include "I2C.h"
RTC_DS1307 RTC;
byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x01 };
EthernetClient client;
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;
int t = 0; // TEMPERATURE VAR
int h = 0; // HUMIDITY VAR
String data;
long previousMillis = 0;
unsigned long currentMillis = 0;
long interval = 250000;
SoftwareSerial HC12(53, 54); // HC-12 TX Pin, HC-12 RX Pin
void setup() {
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__));
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
}
}
}
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 = "temp1=" + fTemp + "&hum1=" + fwil;
Serial.println(data);
if (client.connect("***********.com",80)) { // REPLACE WITH YOUR SERVER ADDRESS
client.println("POST /add.php HTTP/1.1");
client.println("Host: "***********.com"); // SERVER ADDRESS HERE TOO
client.println("Content-Type: application/x-www-form-urlencoded");
client.print("Content-Length: ");
client.println(data.length());
client.println();
client.print(data);
}
if (client.connected()) {
Serial.println("coneted");
delay(500);
}
}