Skip to content

Commit

Permalink
change to arduino mega 2560
Browse files Browse the repository at this point in the history
  • Loading branch information
andyduino committed Sep 24, 2015
1 parent 91ea2b0 commit f9e5f61
Show file tree
Hide file tree
Showing 44 changed files with 4,277 additions and 37 deletions.
74 changes: 37 additions & 37 deletions MQTT-for-SIM900/MQTT-for-SIM900.ino
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
/*
Very, very primitive. MQTT Test program to send data to a MQTT Server
V00 / 09th March 2013, omer sever, [email protected]
initial version w/o valid IOC parameters
V01 / 10th March 2013, ates yurdakul, [email protected]
modified for IOC parameters
disabled GPS for simplicity
Expand All @@ -20,10 +18,11 @@

#include <mqtt.h>
#include <Math.h>
//#include <SoftwareSerial.h>
#include <SoftwareSerial.h>
#include <String.h>
#include "DHT.h"

SoftwareSerial GPRS(10, 11); // RX, TX
/* #include <SoftwareSerial.h>
#include "TinyGPS.h"
TinyGPS gps;
Expand Down Expand Up @@ -87,27 +86,27 @@ int sentCount = 0;

void setup() {
pinMode(13, OUTPUT);
pinMode(14, OUTPUT); // GSM
pinMode(22, OUTPUT); // GSM
digitalWrite(0, HIGH);


Serial.begin(9600); //debug
Serial1.begin(19200); // GPRS module
GPRS.begin(9600); // GPRS module
// gpss.begin(9600);
dht.begin();
}
void loop(){
delay(20000);// wait for GPRS getting stable

//for(int k=0;k++;k<2){
get_AM2301_Data();

// digitalWrite(22,HIGH);
digitalWrite(13,HIGH);
delay(20000);// wait for GPRS getting stable

//get_AM2301_Data();
get_water_temp();

Serial.println("Checking if GPRS is ready");
Serial1.println("AT");
GPRS.println("AT");
delay(1000);
gprsReady = isGPRSReady();

if (gprsReady == true){
Serial.println("GPRS Ready");
String json = buildJson();
Expand All @@ -119,16 +118,16 @@ void loop(){
/* The arguments here are:
clientID, IP, Port, Topic, Message
*/
sendMQTTMessage("agrinode", "iot.eclipse.org", "1883", "agrinode01",jsonStr);
sendMQTTMessage("agrinode", "test.mosquitto.org", "1883", "agrinode01",jsonStr);

}//and of if
//digitalWrite(22,LOW);
digitalWrite(13,LOW);
delay(60000);

delay(10000);
//} //end of for

while(1);
}


/*
uint32_t parsedecimal(char *str) {
uint32_t d = 0;
while (str[0] != 0) {
Expand All @@ -139,7 +138,7 @@ uint32_t parsedecimal(char *str) {
str++;
}
return d;
}
}*/

void readline() {
/*char c;
Expand All @@ -159,10 +158,12 @@ void readline() {
}*/
}
boolean isGPRSReady(){
Serial1.println("AT+CGATT?");
GPRS.println("AT");
GPRS.println("AT");
GPRS.println("AT+CGATT?");
index = 0;
while (Serial1.available()){
data1 = (char)Serial1.read();
while (GPRS.available()){
data1 = (char)GPRS.read();
Serial.write(data1);
gprsStr[index++] = data1;
}
Expand All @@ -180,56 +181,56 @@ boolean isGPRSReady(){
}

void sendMQTTMessage(char* clientId, char* brokerUrl, char* brokerPort, char* topic, char* message){
Serial1.println("AT"); // Sends AT command to wake up cell phone
GPRS.println("AT"); // Sends AT command to wake up cell phone
Serial.println("send AT to wake up GPRS");
delay(1000); // Wait a second
digitalWrite(13, HIGH);
Serial1.println("AT+CSTT=\"m-wap\",\"mms\",\"mms\""); // Puts phone into GPRS mode
// digitalWrite(13, HIGH);
GPRS.println("AT+CSTT=\"m-wap\",\"mms\",\"mms\""); // Puts phone into GPRS mode
Serial.println("AT+CSTT=\"m-wap\",\"mms\",\"mms\"");
delay(2000); // Wait a second
Serial1.println("AT+CIICR");
GPRS.println("AT+CIICR");
Serial.println("AT+CIICR");
delay(2000);
Serial1.println("AT+CIFSR");
GPRS.println("AT+CIFSR");
Serial.println("AT+CIFSR");
delay(2000);
strcpy(atCommand, "AT+CIPSTART=\"TCP\",\"");
strcat(atCommand, brokerUrl);
strcat(atCommand, "\",\"");
strcat(atCommand, brokerPort);
strcat(atCommand, "\"");
Serial1.println(atCommand);
GPRS.println(atCommand);
Serial.println(atCommand);
// Serial.println("AT+CIPSTART=\"TCP\",\"mqttdashboard.com\",\"1883\"");
delay(2000);
Serial1.println("AT+CIPSEND");
GPRS.println("AT+CIPSEND");
Serial.println("AT+CIPSEND");
delay(2000);
mqttMessageLength = 16 + strlen(clientId);
Serial.println(mqttMessageLength);
mqtt_connect_message(mqttMessage, clientId);
for (int j = 0; j < mqttMessageLength; j++) {
Serial1.write(mqttMessage[j]); // Message contents
GPRS.write(mqttMessage[j]); // Message contents
Serial.write(mqttMessage[j]); // Message contents
Serial.println("");
}
Serial1.write(byte(26)); // (signals end of message)
GPRS.write(byte(26)); // (signals end of message)
Serial.println("Sent");
delay(10000);
Serial1.println("AT+CIPSEND");
GPRS.println("AT+CIPSEND");
Serial.println("AT+CIPSEND");
delay(2000);
mqttMessageLength = 4 + strlen(topic) + strlen(message);
Serial.println(mqttMessageLength);
mqtt_publish_message(mqttMessage, topic, message);
for (int k = 0; k < mqttMessageLength; k++) {
Serial1.write(mqttMessage[k]);
GPRS.write(mqttMessage[k]);
Serial.write((byte)mqttMessage[k]);
}
Serial1.write(byte(26)); // (signals end of message)
GPRS.write(byte(26)); // (signals end of message)
Serial.println("-------------Sent-------------"); // Message contents
delay(5000);
Serial1.println("AT+CIPCLOSE");
GPRS.println("AT+CIPCLOSE");
Serial.println("AT+CIPCLOSE");
delay(2000);
}
Expand All @@ -244,7 +245,7 @@ void get_AM2301_Data() {
Serial.println("Failed to read from DHT sensor");
Air_Humidity=0;
Air_Temp=0;
return;
// return;
}
}

Expand Down Expand Up @@ -315,4 +316,3 @@ String buildJson() {
data+="}";
return data;
}

142 changes: 142 additions & 0 deletions libraries/DS1307RTC/DS1307RTC.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/*
* DS1307RTC.h - library for DS1307 RTC
Copyright (c) Michael Margolis 2009
This library is intended to be uses with Arduino Time.h library functions
The library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 Dec 2009 - Initial release
5 Sep 2011 updated for Arduino 1.0
*/

#include <Wire.h>
#include "DS1307RTC.h"

#define DS1307_CTRL_ID 0x68

DS1307RTC::DS1307RTC()
{
Wire.begin();
}

// PUBLIC FUNCTIONS
time_t DS1307RTC::get() // Aquire data from buffer and convert to time_t
{
tmElements_t tm;
if (read(tm) == false) return 0;
return(makeTime(tm));
}

bool DS1307RTC::set(time_t t)
{
tmElements_t tm;
breakTime(t, tm);
tm.Second |= 0x80; // stop the clock
write(tm);
tm.Second &= 0x7f; // start the clock
write(tm);
}

// Aquire data from the RTC chip in BCD format
bool DS1307RTC::read(tmElements_t &tm)
{
uint8_t sec;
Wire.beginTransmission(DS1307_CTRL_ID);
#if ARDUINO >= 100
Wire.write((uint8_t)0x00);
#else
Wire.send(0x00);
#endif
if (Wire.endTransmission() != 0) {
exists = false;
return false;
}
exists = true;

// request the 7 data fields (secs, min, hr, dow, date, mth, yr)
Wire.requestFrom(DS1307_CTRL_ID, tmNbrFields);
if (Wire.available() < tmNbrFields) return false;
#if ARDUINO >= 100
sec = Wire.read();
tm.Second = bcd2dec(sec & 0x7f);
tm.Minute = bcd2dec(Wire.read() );
tm.Hour = bcd2dec(Wire.read() & 0x3f); // mask assumes 24hr clock
tm.Wday = bcd2dec(Wire.read() );
tm.Day = bcd2dec(Wire.read() );
tm.Month = bcd2dec(Wire.read() );
tm.Year = y2kYearToTm((bcd2dec(Wire.read())));
#else
sec = Wire.receive();
tm.Second = bcd2dec(sec & 0x7f);
tm.Minute = bcd2dec(Wire.receive() );
tm.Hour = bcd2dec(Wire.receive() & 0x3f); // mask assumes 24hr clock
tm.Wday = bcd2dec(Wire.receive() );
tm.Day = bcd2dec(Wire.receive() );
tm.Month = bcd2dec(Wire.receive() );
tm.Year = y2kYearToTm((bcd2dec(Wire.receive())));
#endif
if (sec & 0x80) return false; // clock is halted
return true;
}

bool DS1307RTC::write(tmElements_t &tm)
{
Wire.beginTransmission(DS1307_CTRL_ID);
#if ARDUINO >= 100
Wire.write((uint8_t)0x00); // reset register pointer
Wire.write(dec2bcd(tm.Second)) ;
Wire.write(dec2bcd(tm.Minute));
Wire.write(dec2bcd(tm.Hour)); // sets 24 hour format
Wire.write(dec2bcd(tm.Wday));
Wire.write(dec2bcd(tm.Day));
Wire.write(dec2bcd(tm.Month));
Wire.write(dec2bcd(tmYearToY2k(tm.Year)));
#else
Wire.send(0x00); // reset register pointer
Wire.send(dec2bcd(tm.Second)) ;
Wire.send(dec2bcd(tm.Minute));
Wire.send(dec2bcd(tm.Hour)); // sets 24 hour format
Wire.send(dec2bcd(tm.Wday));
Wire.send(dec2bcd(tm.Day));
Wire.send(dec2bcd(tm.Month));
Wire.send(dec2bcd(tmYearToY2k(tm.Year)));
#endif
if (Wire.endTransmission() != 0) {
exists = false;
return false;
}
exists = true;
return true;
}

// PRIVATE FUNCTIONS

// Convert Decimal to Binary Coded Decimal (BCD)
uint8_t DS1307RTC::dec2bcd(uint8_t num)
{
return ((num/10 * 16) + (num % 10));
}

// Convert Binary Coded Decimal (BCD) to Decimal
uint8_t DS1307RTC::bcd2dec(uint8_t num)
{
return ((num/16 * 10) + (num % 16));
}

bool DS1307RTC::exists = false;

DS1307RTC RTC = DS1307RTC(); // create an instance for the user

37 changes: 37 additions & 0 deletions libraries/DS1307RTC/DS1307RTC.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* DS1307RTC.h - library for DS1307 RTC
* This library is intended to be uses with Arduino Time.h library functions
*/

#ifndef DS1307RTC_h
#define DS1307RTC_h

#include <Time.h>

// library interface description
class DS1307RTC
{
// user-accessible "public" interface
public:
DS1307RTC();
static time_t get();
static bool set(time_t t);
static bool read(tmElements_t &tm);
static bool write(tmElements_t &tm);
static bool chipPresent() { return exists; }

private:
static bool exists;
static uint8_t dec2bcd(uint8_t num);
static uint8_t bcd2dec(uint8_t num);
};

#ifdef RTC
#undef RTC // workaround for Arduino Due, which defines "RTC"...
#endif

extern DS1307RTC RTC;

#endif


Loading

0 comments on commit f9e5f61

Please sign in to comment.