Skip to content

Instantly share code, notes, and snippets.

@jenschr
Last active November 4, 2020 11:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jenschr/2d248362a4c3c5651145b5f009586513 to your computer and use it in GitHub Desktop.
Save jenschr/2d248362a4c3c5651145b5f009586513 to your computer and use it in GitHub Desktop.
Basic Particle Photon 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
* This specific version is for Particle Photon.
*
* Connect the R pin on the module to pin 19 on the Photon
* Connect the T pin on the module to pin 18 on the Photon
*
* 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";
int counter = 0;
// Declare metod with defaults here. Implementation is further down
void sendAndReadResponse( String command, int extraWaitInMillisecondsForResponse = 200 );
void setup()
{
pinMode(D7, OUTPUT);
// Set your Serial monitor speed to this
Serial.begin(115200);
// The SIM7600 module is using 115200 baud by default
Serial1.begin(115200);
// Wait for SIM to come online
Serial.println("Wait 8 boring seconds for the green LED to turn on");
delay(1000);
Serial.println("Wait 7 boring seconds for the green LED to turn on");
delay(1000);
Serial.println("Wait 6 boring seconds for the green LED to turn on");
delay(1000);
Serial.println("Wait 5 boring seconds for the green LED to turn on");
delay(1000);
Serial.println("Wait 4 boring seconds for the green LED to turn on");
delay(1000);
Serial.println("Wait 3 boring seconds for the green LED to turn on");
delay(1000);
Serial.println("Wait 2 boring seconds for the green LED to turn on");
delay(1000);
Serial.println("Wait 1 boring second for the green LED to turn on");
delay(1000);
// Change speed from 115200 to 19200 to prevent errors
//Serial1.println("AT+IPR=19200");
//delay(1000);
//Serial1.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");
Serial1.print("GET http://dweet.io/dweet/for/");
Serial1.print(deviceName);
Serial1.println("?temperature=55.5&humidity=77.7&bananas=TheBestThing HTTP/1.1\n");
Serial1.println("Host: dweet.io\n");
Serial1.println("User-Agent: mozilly\n");
Serial1.println("Content-Length: 0\n");
Serial1.print(char(26)); delay(20);
Serial1.print(char(26));
}
void sendAndReadResponse( String command, int extraWaitInMillisecondsForResponse )
{
// Send the command
Serial.print( "Sending: " );
Serial.println( command );
Serial1.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 ( Serial1.available() > 0 )
{
wegotResponse = true;
}
Particle.process();
}
// Print out the results
if ( millisecondsSinceRequestStarted < 2000 )
{
Serial.print("\nCommand: '");
Serial.print(command);
Serial.println(" timed out?\n");
}
else if (Serial1.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 ( Serial1.available() )
{
char ch = Serial1.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);
Serial1.print(ch);
}
}
if (Serial1.available() > 0) {
Serial.print(":");
delay(10);
while ( Serial1.available() ) {
char ch = Serial1.read();
if ( ch ) {
Serial.print(ch);
}
}
}
// blink the LED, just to show we are alive
if ( counter % 1000 == 0 ) {
digitalWrite( D7, !digitalRead(D7) );
}
counter++;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment