Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
Luo Zeqing
Published © LGPL

Weather Station With A7670 -- Remote Monitoring

A weather station with Maduino Zero 4G LTE(CAT1 A7670), which support HTTP, and can transfer data to the web page.

IntermediateProtip3 hours420
Weather Station With A7670 -- Remote Monitoring

Story

Read more

Schematics

Hardware

Code

weather_station_with_A7670.ino

Arduino
#include <stdio.h>
#include <string.h>

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DHT.h>




#define DEBUG true
#define LTE_RESET_PIN 6

#define LTE_PWRKEY_PIN 5
#define pinInterrupt A0
#define DHTPIN 3
#define LIGHT 8


#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1    // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3c

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);


String Apikey = "MQ4YEKPFFF3TOTEY";



float speed_value = 0;

int Count = 0;
int count_light=0;

int Mqtttime = 0;
String light_index ="";
String light_intensity ="";
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 1000; // the debounce time; increase if the output flickers
unsigned long debounceDelay2 = 2000;

#define DHTTYPE DHT11 
DHT dht(DHTPIN, DHTTYPE);

void setup() {
  // put your setup code here, to run once:

    SerialUSB.begin(115200);
    Serial1.begin(115200);

    pinMode(LTE_RESET_PIN, OUTPUT);
    digitalWrite(LTE_RESET_PIN, LOW);
  
    pinMode(LTE_PWRKEY_PIN, OUTPUT);
    digitalWrite(LTE_RESET_PIN, LOW);
    delay(100);
    digitalWrite(LTE_PWRKEY_PIN, HIGH);
    delay(3000);
    digitalWrite(LTE_PWRKEY_PIN, LOW);




    
    SerialUSB.println("Maduino Zero 4G LTE CAT1 Test Start!");
  
    SerialUSB.println("Wait a few minutes for 4G star");

    delay(3000);
    


  
    sendData("AT", 1000, DEBUG);
    delay(5000);
    sendData("AT+CICCID", 1000, DEBUG);
    delay(5000);
    sendData("AT+SIMCOMATI", 1000, DEBUG);
    delay(5000);
    sendData("AT+COPS?", 1000, DEBUG); 
    delay(5000);
    sendData("AT+CPIN?", 3000, DEBUG);
    delay(5000);
    sendData("AT+CSQ", 1000, DEBUG);
    delay(5000);
    sendData("AT+CREG?", 1000, DEBUG);
    delay(5000);
    sendData("AT+CGREG?", 1000, DEBUG);
    delay(5000);
    sendData("AT+CPSI?", 1000, DEBUG);
    delay(5000);
    sendData("AT+CGDCONT=1,\"IP\",\"CMNET\"", 1000, DEBUG);
    delay(5000);
    sendData("AT+CGATT=1", 1000, DEBUG);
    delay(5000);
    sendData("AT+HTTPINIT", 1000, DEBUG);
    delay(5000);
    sendData("AT+HTTPPARA=\"URL\",\"http://api.thingspeak.com/update?api_key="+Apikey+"\"\r\n", 2000, DEBUG);
    delay(5000);
    sendData("AT+HTTPACTION=0\r\n", 3000, DEBUG);
    delay(5000);
    sendData("AT+HTTPTERM\r\n", 3000, DEBUG);
    delay(5000);


    Wire.begin();
    if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
        SerialUSB.println(F("SSD1306 allocation failed"));
        for (;;); // Don't proceed, loop forever
    }
    else{
      SerialUSB.println(F("SSD1306 allocation successful"));
    }
    display.clearDisplay();
    delay(1000);

    
    pinMode(pinInterrupt, INPUT_PULLUP);
    attachInterrupt(digitalPinToInterrupt(pinInterrupt), onChange, FALLING);
    dht.begin();
    pinMode(LIGHT, INPUT); 


}

void loop() {
  // put your main code here, to run repeatedly:

   delay(2000);
   float humidity_value = dht.readHumidity();
   float temperature_value = dht.readTemperature();
   SerialUSB.print("Humidity:");
   SerialUSB.print(humidity_value);
   SerialUSB.println(" %"); 
   SerialUSB.print("Temperature:");
   SerialUSB.print(temperature_value);
   SerialUSB.println(" C");

   wind_speed();

   
   light_str();
    String light_l = light_intensity;
    String http_str = "AT+HTTPPARA=\"URL\",\"http://api.thingspeak.com/update?api_key=" + Apikey + "&field1=" + String(speed_value)+"&field2=" +String(temperature_value) + "&field3="+ String(humidity_value)+"&field4="+light_index +"\"\r\n";
    sendData("AT+HTTPINIT\r\n", 2000, DEBUG);
    delay(5000);
    sendData(http_str, 2000, DEBUG);
    delay(5000);
    sendData("AT+HTTPACTION=0\r\n", 3000, DEBUG);
    delay(5000);
    sendData("AT+HTTPTERM\r\n", 3000, DEBUG);
    delay(5000);
    
    light_intensity = "";
    light_index = "";

    display.clearDisplay();
    display.setCursor(1, 5);
    display.setTextColor(WHITE);
    display.setTextSize(1);
    display.println("Humidity:" + String(humidity_value) + " %" );
    display.setCursor(1, 15);
    display.setTextColor(WHITE);
    display.setTextSize(1);
    display.println("Temperature:" + String(temperature_value) + " C" );
    display.setCursor(1, 25);
    display.setTextColor(WHITE);
    display.setTextSize(1);
    display.println("Wind speed:" + String(speed_value) + "m/s");
    display.setCursor(1, 35);
    display.setTextColor(WHITE);
    display.setTextSize(1);
    display.println(light_l);
    display.display();

}




String sendData(String command, const int timeout, boolean debug)
{
    String response = "";
    Serial1.println(command);
    long int time = millis();
    while ((time + timeout) > millis())
    {
        while (Serial1.available())
        {
            char c = Serial1.read();
            response += c;
        }
    }
    if (debug)
    {
        SerialUSB.print(response);
    }
    return response;
}





void wind_speed()
{
  lastDebounceTime = millis();
  Count = 0;
  while(!((millis() - lastDebounceTime) > debounceDelay));
  if ((millis() - lastDebounceTime) > debounceDelay)
  {

    speed_value = Count * 8.75 * 0.01;
     SerialUSB.print("Wind speed:");
     SerialUSB.print(speed_value);    
     SerialUSB.println("m/s");
    Count = 0;
  }
}


void onChange()
{
  if (digitalRead(pinInterrupt) == LOW)
    Count++;
}


void light_str()
{
   lastDebounceTime = millis();
  count_light = 0;
  while((millis() - lastDebounceTime) < debounceDelay)
  {
      delay(5);
      if (digitalRead(LIGHT) == 0){
        count_light++;
      }
  }
      if(count_light>100)
      {
        SerialUSB.println("Strong ultraviolet ray");
        light_intensity =light_intensity +"Strong ultraviolet ray";
        light_index = light_index + "1";
      }
      else
      {
        SerialUSB.println("Weak ultraviolet ray");
        light_intensity =light_intensity + "Weak ultraviolet ray";
        light_index = light_index + "0";
      }
      count_light = 0;
  } 

Credits

Luo Zeqing
46 projects • 14 followers
Makerfabs ESP32/ Lora/ Lorawan, IoT Hardware
Contact

Comments

Please log in or sign up to comment.