4 changed files with 247 additions and 0 deletions
@ -0,0 +1,95 @@ |
|||||
|
#include <SoftwareSerial.h> |
||||
|
#include "SIM800L.h" |
||||
|
|
||||
|
#define SIM800_RX_PIN 10 |
||||
|
#define SIM800_TX_PIN 11 |
||||
|
#define SIM800_RST_PIN 7 |
||||
|
|
||||
|
SIM800L* sim800l; |
||||
|
|
||||
|
const char APN[] = "TM"; |
||||
|
String BASE_URL = "http://2.238.194.8/index.php?"; |
||||
|
//String BASE_URL = "https://postman-echo.com/get?";
|
||||
|
|
||||
|
String s = ""; |
||||
|
char URL[100]; |
||||
|
|
||||
|
|
||||
|
int anemometerPin = 6; |
||||
|
int anemometerVal = 0; |
||||
|
int anemometerState = 0; |
||||
|
unsigned long anemometerDuration = 0; |
||||
|
unsigned long startime = 0; |
||||
|
unsigned long timer = 0; |
||||
|
int windSpeed = 0; |
||||
|
|
||||
|
int northPin = 4; |
||||
|
int eastPin = 5; |
||||
|
int southPin = 2; |
||||
|
int westPin = 3; |
||||
|
|
||||
|
int timerLimit = 3000;//300000; // 5 minutes
|
||||
|
|
||||
|
bool printSensorsData = true; |
||||
|
|
||||
|
String windDirection = ""; |
||||
|
|
||||
|
|
||||
|
|
||||
|
void setup() { |
||||
|
// Initialize Serial Monitor for debugging
|
||||
|
Serial.begin(115200); |
||||
|
while(!Serial); |
||||
|
|
||||
|
// Initialize a SoftwareSerial
|
||||
|
SoftwareSerial* serial = new SoftwareSerial(SIM800_RX_PIN, SIM800_TX_PIN); |
||||
|
serial->begin(9600); |
||||
|
delay(1000); |
||||
|
|
||||
|
// Initialize SIM800L driver with an internal buffer of 200 bytes and a reception buffer of 512 bytes, debug disabled
|
||||
|
sim800l = new SIM800L((Stream *)serial, SIM800_RST_PIN, 200, 512); |
||||
|
|
||||
|
// Setup module for GPRS communication
|
||||
|
setupModule(); |
||||
|
} |
||||
|
|
||||
|
void loop() { |
||||
|
|
||||
|
windDirection = ""; |
||||
|
anemometerVal = digitalRead(anemometerPin); |
||||
|
|
||||
|
if ((anemometerState == 1) && (anemometerVal == 0)) { |
||||
|
anemometerDuration = millis() - startime; |
||||
|
startime = millis(); |
||||
|
windSpeed = 2500.0/anemometerDuration; |
||||
|
} |
||||
|
|
||||
|
anemometerState = anemometerVal; |
||||
|
|
||||
|
if(millis() >= timer) { |
||||
|
|
||||
|
Serial.println("--------------------------"); |
||||
|
Serial.println("Sending..."); |
||||
|
|
||||
|
timer = millis() + timerLimit; |
||||
|
|
||||
|
if(digitalRead(northPin)) {windDirection = windDirection + "N";} |
||||
|
if(digitalRead(southPin)) {windDirection = windDirection + "S";} |
||||
|
if(digitalRead(eastPin)) {windDirection = windDirection + "E";} |
||||
|
if(digitalRead(westPin)) {windDirection = windDirection + "W";} |
||||
|
|
||||
|
if(printSensorsData) { |
||||
|
Serial.print("Wind speed:\t"); |
||||
|
Serial.print(windSpeed); |
||||
|
Serial.println("Km/h"); |
||||
|
|
||||
|
Serial.print("Wind direction:\t"); |
||||
|
Serial.println(windDirection); |
||||
|
|
||||
|
} |
||||
|
|
||||
|
sendRequest(); |
||||
|
|
||||
|
} |
||||
|
|
||||
|
} |
@ -0,0 +1,43 @@ |
|||||
|
|
||||
|
void getWind() { |
||||
|
|
||||
|
windDirection = ""; |
||||
|
anemometerVal = digitalRead(anemometerPin); |
||||
|
|
||||
|
//Serial.println(anemometerVal);
|
||||
|
|
||||
|
if ((anemometerState == 1) && (anemometerVal == 0)) { |
||||
|
anemometerDuration = millis() - startime; |
||||
|
startime = millis(); |
||||
|
windSpeed = 2500.0/anemometerDuration; |
||||
|
} |
||||
|
|
||||
|
anemometerState = anemometerVal; |
||||
|
|
||||
|
if(millis() >= timer) { |
||||
|
|
||||
|
Serial.println("--------------------------"); |
||||
|
|
||||
|
timer = millis() + timerLimit; |
||||
|
|
||||
|
if(digitalRead(northPin)) {windDirection = windDirection + "N";} |
||||
|
if(digitalRead(southPin)) {windDirection = windDirection + "S";} |
||||
|
if(digitalRead(eastPin)) {windDirection = windDirection + "E";} |
||||
|
if(digitalRead(westPin)) {windDirection = windDirection + "W";} |
||||
|
|
||||
|
if(printSensorsData) { |
||||
|
Serial.print("Wind speed:\t"); |
||||
|
Serial.print(windSpeed); |
||||
|
Serial.println("Km/h"); |
||||
|
|
||||
|
Serial.print("Wind direction:\t"); |
||||
|
Serial.println(windDirection); |
||||
|
|
||||
|
} |
||||
|
|
||||
|
sendRequest(); |
||||
|
//delay(5000);
|
||||
|
|
||||
|
} |
||||
|
|
||||
|
} |
@ -0,0 +1,72 @@ |
|||||
|
|
||||
|
void sendRequest() { |
||||
|
|
||||
|
s = BASE_URL; |
||||
|
s.concat("windSpeed="); |
||||
|
s.concat(windSpeed); |
||||
|
s.concat("&windDirection="); |
||||
|
s.concat(windDirection); |
||||
|
s.toCharArray(URL, 100); |
||||
|
|
||||
|
Serial.println(URL); |
||||
|
|
||||
|
// Establish GPRS connectivity (5 trials)
|
||||
|
bool connected = false; |
||||
|
for(uint8_t i = 0; i < 5 && !connected; i++) { |
||||
|
delay(1000); |
||||
|
connected = sim800l->connectGPRS(); |
||||
|
} |
||||
|
|
||||
|
// Check if connected, if not reset the module and setup the config again
|
||||
|
if(connected) { |
||||
|
Serial.println(F("GPRS connected !")); |
||||
|
} else { |
||||
|
Serial.println(F("GPRS not connected !")); |
||||
|
Serial.println(F("Reset the module.")); |
||||
|
sim800l->reset(); |
||||
|
setupModule(); |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
Serial.println(F("Start HTTP GET...")); |
||||
|
Serial.println(URL); |
||||
|
|
||||
|
// Do HTTP GET communication with 10s for the timeout (read)
|
||||
|
uint16_t rc = sim800l->doGet(URL, 10000); |
||||
|
if(rc == 200) { |
||||
|
// Success, output the data received on the serial
|
||||
|
Serial.print(F("HTTP GET successful (")); |
||||
|
Serial.print(sim800l->getDataSizeReceived()); |
||||
|
Serial.println(F(" bytes)")); |
||||
|
Serial.print(F("Received : ")); |
||||
|
Serial.println(sim800l->getDataReceived()); |
||||
|
} else { |
||||
|
// Failed...
|
||||
|
Serial.print(F("HTTP GET error ")); |
||||
|
Serial.println(rc); |
||||
|
} |
||||
|
|
||||
|
// Close GPRS connectivity (5 trials)
|
||||
|
bool disconnected = sim800l->disconnectGPRS(); |
||||
|
for(uint8_t i = 0; i < 5 && !connected; i++) { |
||||
|
delay(1000); |
||||
|
disconnected = sim800l->disconnectGPRS(); |
||||
|
} |
||||
|
|
||||
|
if(disconnected) { |
||||
|
Serial.println(F("GPRS disconnected !")); |
||||
|
} else { |
||||
|
Serial.println(F("GPRS still connected !")); |
||||
|
} |
||||
|
|
||||
|
// Go into low power mode
|
||||
|
bool lowPowerMode = sim800l->setPowerMode(MINIMUM); |
||||
|
if(lowPowerMode) { |
||||
|
Serial.println(F("Module in low power mode")); |
||||
|
} else { |
||||
|
Serial.println(F("Failed to switch module to low power mode")); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
windSpeed = 0; |
||||
|
} |
@ -0,0 +1,37 @@ |
|||||
|
|
||||
|
void setupModule() { |
||||
|
// Wait until the module is ready to accept AT commands
|
||||
|
while(!sim800l->isReady()) { |
||||
|
Serial.println(F("Problem to initialize AT command, retry in 1 sec")); |
||||
|
delay(1000); |
||||
|
} |
||||
|
Serial.println(F("Setup Complete!")); |
||||
|
|
||||
|
// Wait for the GSM signal
|
||||
|
uint8_t signal = sim800l->getSignal(); |
||||
|
while(signal <= 0) { |
||||
|
delay(1000); |
||||
|
signal = sim800l->getSignal(); |
||||
|
} |
||||
|
Serial.print(F("Signal OK (strenght: ")); |
||||
|
Serial.print(signal); |
||||
|
Serial.println(F(")")); |
||||
|
delay(1000); |
||||
|
|
||||
|
// Wait for operator network registration (national or roaming network)
|
||||
|
NetworkRegistration network = sim800l->getRegistrationStatus(); |
||||
|
while(network != REGISTERED_HOME && network != REGISTERED_ROAMING) { |
||||
|
delay(1000); |
||||
|
network = sim800l->getRegistrationStatus(); |
||||
|
} |
||||
|
Serial.println(F("Network registration OK")); |
||||
|
delay(1000); |
||||
|
|
||||
|
// Setup APN for GPRS configuration
|
||||
|
bool success = sim800l->setupGPRS(APN); |
||||
|
while(!success) { |
||||
|
success = sim800l->setupGPRS(APN); |
||||
|
delay(5000); |
||||
|
} |
||||
|
Serial.println(F("GPRS config OK")); |
||||
|
} |
Loading…
Reference in new issue