Skip to content

Instantly share code, notes, and snippets.

@jenschr
Last active Oct 11, 2020
Embed
What would you like to do?
Basic Arduino sketch to send a message to Dweet.io using GET. Written specifically for the SIM7600E-H, but may work well on other similar modules.
/*
* Basic sketch to send a message to Dweet.io using GET
* This sketch is written specifically for the SIM7600E-H
*
* Connect the R pin on the module to pin 10 on the Arduino
* Connect the T pin on the module to pin 11 on the Arduino
*
* Also enter your own deviceName below (replace "8675840")
* and you can then go and see the result at
* http://dweet.io/get/latest/dweet/for/8675840
*
* You will also have to change the PIN and APN for this
* to work with YOUR telcom provider.
*
* Written by https://github.com/jenschr
*/
String deviceName = "8675840";
#include <SoftwareSerial.h>
#define PIN_TX 11
#define PIN_RX 10
SoftwareSerial mySerial(PIN_TX, PIN_RX);
int counter = 0;
// Delcare metod with defaults here. Implementation is firhter down
void sendAndReadResponse( String command, int extraWaitInMillisecondsForResponse = 200 );
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
// Set your Serial monitor speed to this
Serial.begin(115200);
// The SIM7600 module is using 115200 baud by default
mySerial.begin(115200);
Serial.println("Wait 8 boring seconds for the green LED to turn on");
delay(8000);
// Change speed from 115200 to 19200 to prevent errors
mySerial.println("AT+IPR=19200");
delay(1000);
mySerial.begin(19200);
delay(1000);
// Setup the module - start by just saying "hi" with an AT
sendAndReadResponse("AT");
// Enter the PIN for your SIM card in the command below (unless it's 1234)
sendAndReadResponse("AT+CPIN=1234");
sendAndReadResponse("AT+CFUN=1");
sendAndReadResponse("AT+CGACT=1,1");
// Last parameter here ('internet') is your telco's APN. Google it unless you know it?
sendAndReadResponse("AT+CGDCONT=1,\"IP\",\"internet\"");
sendAndReadResponse("AT+CGPS=1");
sendAndReadResponse("AT+CGREG?");
sendAndReadResponse("AT+NETOPEN");
sendAndReadResponse("AT+IPADDR");
// Send request to Dweet.io
sendAndReadResponse("AT+CHTTPACT=\"dweet.io\",80");
mySerial.print("GET http://dweet.io/dweet/for/");
mySerial.print(deviceName);
mySerial.println("?temperature=55.5&humidity=77.7&bananas=TheBestThing HTTP/1.1\n");
mySerial.println("Host: dweet.io\n");
mySerial.println("User-Agent: mozilly\n");
mySerial.println("Content-Length: 0\n");
mySerial.print(char(26)); delay(20);
mySerial.print(char(26));
}
void sendAndReadResponse( String command, int extraWaitInMillisecondsForResponse )
{
// Send the command
mySerial.println( command );
// Setup a timeout
int requestStartTime = millis();
int millisecondsSinceRequestStarted = 0;
bool wegotResponse = false;
// Wait until we get a response (or timeout)
while ( !wegotResponse || millisecondsSinceRequestStarted < 2000 )
{
millisecondsSinceRequestStarted = millis() - requestStartTime;
if ( mySerial.available() > 0 )
{
wegotResponse = true;
}
}
// Print out the results
if ( millisecondsSinceRequestStarted < 2000 )
{
Serial.print("\nCommand: '");
Serial.print(command);
Serial.println(" timed out?\n");
}
else if (mySerial.available() > 0)
{
Serial.print("<\n");
// wait a little longer so we get the whole response
delay(extraWaitInMillisecondsForResponse);
// Print out the response to Serial monitor
while ( mySerial.available() )
{
char ch = mySerial.read();
if ( ch ) {
Serial.print(ch);
}
}
Serial.println("\n>");
}
}
// In the main loop, you can keep entering AT commands from your Serial Monitor
void loop()
{
/* send everything received from the SIM7600
to usb serial & vice versa */
if (Serial.available() > 0) {
Serial.print(">");
delay(100);
while ( Serial.available() ) {
char ch = Serial.read();
Serial.print(ch);
mySerial.print(ch);
}
}
if (mySerial.available() > 0) {
Serial.print(":");
delay(10);
while ( mySerial.available() ) {
char ch = mySerial.read();
if ( ch ) {
Serial.print(ch);
}
}
}
// blink the LED, just to show we are alive
if ( counter % 10000 == 0 ) {
digitalWrite( LED_BUILTIN, !digitalRead(LED_BUILTIN) );
}
counter++;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment