Problem arduino uno sketch and sim 800l modem - sim800l

The code works ok on the serial monitor and I can ses in the serial monitor that the code makes the AT command for sending a reply by sms but it doesn't send and I can't control the sketch by SMS.
I can't figure out what goes wrong. It's a long sketch so I try to set the parts of the code where the code is for the modem.
#include <SoftwareSerial.h>
const uint8_t rxPin = 2;
const uint8_t txPin = 3;
SoftwareSerial sim800(2, 3); // SIM800L rx og tx til pin 2 and 3
#define sim800 Serial
void setup() {
Serial.begin(9600);
while (!Serial) {
; // Wait for serial monitor to open
}
sim800.begin(9600);
while (!sim800) {
; // Wait for SIM800L module to start
}
sim800.println("AT");
delay(1000);
// Read response from SIM800L module
while (sim800.available()) {
String response = sim800.readString();
Serial.println(response);
if (sim800.available())
{
String message = sim800.readString();
Serial.println(message);
message.trim();
message.toUpperCase();
if(message.indexOf("ARM") >= 0) // ARM DISARM COMMAND
{
if(message.indexOf("DISARM") == 0)
{
armed = false;
send_SMS("DISARMED!");
void send_SMS(const char *message)
{
sim800.println("AT+CMGF=1"); // Set SMS mode to text
delay(100);
sim800.println("AT+CMGS=\"+123456789\""); // telefon nummer
sim800.println(message);
sim800.println((char)26); // send SMS
I can't figure out where the error is.

Related

Updated--Issue with sensors connecting to Arduino Uno via Atlas Scientific hardware serial port expander 8:1

Hardware connection and also programming issue with sensors (GPS NEO-6M module, ESP8266 etc.) connecting to Arduino Uno via Atlas Scientific serial port expander 8:1
Hardware:
Arduino Uno
Atlas Scientific serial port expander 8:1
GPS NEO-6M module
ESP8266 Wifi module
Arduino Uno to GPS module we are able to get latitude and longitude values, below is the code for the same:
#include <SoftwareSerial.h>
#include <TinyGPS.h>
long lat, lon;
SoftwareSerial gpsSerial(6, 5);
TinyGPS gps;
void setup() {
Serial.begin(9600);
gpsSerial.begin(9600);
}
void loop() {
while (gpsSerial.available()) {
if (gps.encode(gpsSerial.read())) {
gps.get_position(&lat, &lon);
Serial.print("Position:");
Serial.print("lat:"); Serial.print(lat); Serial.print(" ");
Serial.print("long:"); Serial.println(lon);
}
}
}
Our requirement is that we have four sensors that need to be connected to an Arduino Uno. For that we used an Atlas Scientific hardware serial port expander 8:1.
Below is the code which we are working to get the GPS coordinates:
#include <SoftwareSerial.h> // we have to include the SoftwareSerial library, or else we can't use it
#define rx 8 // define what pin rx is going to be
#define tx 9 // define what pin tx is going to be
SoftwareSerial mySerial(rx, tx); //define how the soft serial port is going to work
int s1 = 6; // Arduino pin 6 to control pin S1
int s2 = 5; // Arduino pin 5 to control pin S2
int s3 = 4; // Arduino pin 4 to control pin S3
const uint8_t module_count = 8; // number of modules connected to the serial port expander 1=Port1, 2= Port2 and so on
void setup() {
Serial.begin(115200); // Set the hardware serial port to 115200
mySerial.begin(9600); // Set baud rate for the software serial port to 9600
pinMode(s1, OUTPUT); // Set the digital pin as output
pinMode(s2, OUTPUT); // Set the digital pin as output
pinMode(s3, OUTPUT); // Set the digital pin as output
}
void loop() {
if (Serial.available()) { // if we get data from the computer
char c = Serial.read();
for (uint8_t i = 1; i <= module_count; i++) { // loop through the modules
Serial.print("Connecting to Port: ");
Serial.println(i);
open_port(i); // open the port
mySerial.print(c); // print character to port
delay(100); // insert a delay to wait for the reply
if (mySerial.available()) { // print reply to serial monitor
while (mySerial.available()) {
Serial.println(mySerial.read());
if (gps.encode(mySerial.read())) {
gps.get_position(&lat, &lon);
Serial.print("Position:");
Serial.print("lat:"); Serial.print(lat);
Serial.print(" ");
Serial.print("long:"); Serial.println(lon);
}
}
}
else {
Serial.print("No response received");
}
Serial.println();
}
}
}
void open_port(uint8_t _port) { //this function controls what port is opened on the serial port expander
if (_port < 1 || module_count > 8)_port = 1; //if the value of the port is within range (1-8) then open that port. If it's not in range set it to port 1
uint8_t port_bits = _port - 1;
digitalWrite(s1, bitRead(port_bits, 0)); //Here we have two commands combined into one.
digitalWrite(s2, bitRead(port_bits, 1)); //The digitalWrite command sets a pin to 1/0 (high or low)
digitalWrite(s3, bitRead(port_bits, 2)); //The bitRead command tells us what the bit value is for a specific bit location of a number
delay(2); //this is needed to make sure the channel switching event has completed
}
Kindly guide us in sorting the issue; we are having an issue with gps.encode(mySerial.read()).
Thank you in advance.
Issue was sorted out by ourselves,
Below is the working code for the same ,
#include <SoftwareSerial.h> //we have to include the SoftwareSerial library, or else we can't use it
#define rx 3 //define what pin rx is going to be
#define tx 2 //define what pin tx is going to be
SoftwareSerial mySerial(rx, tx); //define how the soft serial port is going to work
#include <TinyGPS.h>
#include <NMEAGPS.h>
int s1 = 6; //Arduino pin 6 to control pin S1
int s2 = 5; //Arduino pin 5 to control pin S2
int s3 = 4; //Arduino pin 4 to control pin S3
int i;
const uint8_t module_count = 8; //number of modules connected to the serial port expander 1=Port1, 2= Port2 and so on
TinyGPS gps;
gps_fix fix;
long lat, lon;
void setup() {
Serial.begin(9600); //Set the hardware serial port to 115200
mySerial.begin(9600); //set baud rate for the software serial port to 9600
pinMode(s1, OUTPUT); //Set the digital pin as output
pinMode(s2, OUTPUT); //Set the digital pin as output
pinMode(s3, OUTPUT); //Set the digital pin as output
}
void loop() {
if (Serial.available()) { //if we get data from the computer
char c = Serial.read();
for (uint8_t i = 1; i <= module_count; i++) { // loop through the modules
Serial.print("Connecting to Port: ");
Serial.println(i);
open_port(i); // open the port
Serial.print(c); //print character to port
delay(1000); //insert a delay to wait for the reply
//Serial.println(Serial.available());
if (Serial.available()) {
//Serial.println(gps.available(Serial));//print reply to serial monitor
while (Serial.available()) {
//Serial.write(mySerial.read());
//Serial.write(gps.encode(mySerial.read()));
//Serial.println(mySerial.available());
//Serial.println(gps.encode(mySerial.read()));
if (mySerial.available()>0) {
//Serial.println(gps.encode(mySerial.read()));
char d = byte(mySerial.read());
//Serial.println(d);
if (gps.encode(d)) {
gps.get_position(&lat, &lon);
Serial.print("Position:");
Serial.print("lat:"); Serial.print(lat); Serial.print(" ");
Serial.print("long:"); Serial.println(lon);
}
}
}
Serial.println();
}
}
}
}
void open_port(uint8_t _port) { //this function controls what port is opened on the serial port expander
if (_port < 1 || module_count > 8)_port = 1; //if the value of the port is within range (1-8) then open that port. If it's not in range set it to port 1
uint8_t port_bits = _port - 1;
digitalWrite(s1, bitRead(port_bits, 0)); //Here we have two commands combined into one.
digitalWrite(s2, bitRead(port_bits, 1)); //The digitalWrite command sets a pin to 1/0 (high or low)
digitalWrite(s3, bitRead(port_bits, 2)); //The bitRead command tells us what the bit value is for a specific bit location of a number
delay(2); //this is needed to make sure the channel switching event has completed
}

Unable to run input cature mode with SPI

I am Trying to run a web-server on AVR ATmega8 using ENC28J60 module. For this I got sample code from here. This code is working fine. Here is my code:-
#define F_CPU 8000000UL
#include <avr/io.h>
#include <string.h>
#include "ip_arp_udp_tcp.h"
#include "enc28j60.h"
#include "timeout.h"
#include "avr_compat.h"
#include "net.h"
// please modify the following two lines. mac and ip have to be unique
// in your local area network. You can not have the same numbers in
// two devices:
static uint8_t mymac[6] = {0x54,0x55,0x58,0x10,0x00,0x24};
// how did I get the mac addr? Translate the first 3 numbers into ascii is: TUX
static uint8_t myip[4] = {192,168,24,39};
// listen port for www
#define MYWWWPORT 80
//// listen port for udp
#define MYUDPPORT 1200
//
#define BUFFER_SIZE 450
static uint8_t buf[BUFFER_SIZE+1];
int main(void)
{
uint16_t plen;
uint16_t dat_p;
uint8_t i=0;
uint8_t payloadlen=0;
_delay_loop_1(50);
/*initialize enc28j60*/
enc28j60Init(mymac);
enc28j60clkout(2); // change clkout from 6.25MHz to 12.5MHz
_delay_loop_1(50); // 12ms
enc28j60PhyWrite(PHLCON,0x476);
_delay_loop_1(50); // 12ms
//init the ethernet/ip layer:
init_ip_arp_udp_tcp(mymac,myip,MYWWWPORT);
while(1)
{
// get the next new packet:
plen = enc28j60PacketReceive(BUFFER_SIZE, buf);
/*plen will ne unequal to zero if there is a valid * packet (without crc error) */
if(plen==0)
{
continue;
}
// arp is broadcast if unknown but a host may also
// verify the mac address by sending it to
// a unicast address.
if(eth_type_is_arp_and_my_ip(buf,plen))
{
make_arp_answer_from_request(buf);
continue;
}
// check if ip packets (icmp or udp) are for us:
if(eth_type_is_ip_and_my_ip(buf,plen)==0)
{
continue;
}
if(buf[IP_PROTO_P]==IP_PROTO_ICMP_V && buf[ICMP_TYPE_P]==ICMP_TYPE_ECHOREQUEST_V)
{
// a ping packet, let's send pong
make_echo_reply_from_request(buf,plen);
continue;
}
// tcp port www start, compare only the lower byte
if (buf[IP_PROTO_P]==IP_PROTO_TCP_V&&buf[TCP_DST_PORT_H_P]==0&&buf[TCP_DST_PORT_L_P]==MYWWWPORT)
{
if (buf[TCP_FLAGS_P] & TCP_FLAGS_SYN_V)
{
make_tcp_synack_from_syn(buf);
// make_tcp_synack_from_syn does already send the syn,ack
continue;
}
if (buf[TCP_FLAGS_P] & TCP_FLAGS_ACK_V)
{
init_len_info(buf); // init some data structures
// we can possibly have no data, just ack:
dat_p=get_tcp_data_pointer();
if (dat_p==0)
{
if (buf[TCP_FLAGS_P] & TCP_FLAGS_FIN_V)
{
// finack, answer with ack
make_tcp_ack_from_any(buf);
}
// just an ack with no data, wait for next packet
continue;
}
if (strncmp("GET ",(char *)&(buf[dat_p]),4)!=0)
{
plen=fill_tcp_data_p(buf,0,PSTR("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n<h1>200 OK</h1>"));
}
else
{
// Web Code
plen=fill_tcp_data_p(buf,0,PSTR("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n<p>PLANETCAST MEDIA SERVICES LTD</p>"));
plen=fill_tcp_data_p(buf,plen,PSTR("<body>"));
plen=fill_tcp_data_p(buf,plen,PSTR("<input type=\"text\" id=\"myurl\" name=\"url\" placeholder=\"Enter Url\"/><br>"));
plen=fill_tcp_data_p(buf,plen,PSTR("<input type=\"submit\" id=\"clickbutton\" name=\"Click Here\" />"));
plen=fill_tcp_data_p(buf,plen,PSTR("<script type=\"text/javascript\">"));
plen=fill_tcp_data_p(buf,plen,PSTR("document.getElementById(\"clickbutton\").onclick = function(){"));
plen=fill_tcp_data_p(buf,plen,PSTR("var url = document.getElementById(\"myurl\").value;"));
plen=fill_tcp_data_p(buf,plen,PSTR("location.href=url;"));
plen=fill_tcp_data_p(buf,plen,PSTR("};"));
plen=fill_tcp_data_p(buf,plen,PSTR("</script>"));
plen=fill_tcp_data_p(buf,plen,PSTR("</body"));
}
make_tcp_ack_from_any(buf); // send ack for http get
make_tcp_ack_with_data(buf,plen); // send data
continue;
}
}
// udp interface:
if (buf[IP_PROTO_P]==IP_PROTO_UDP_V)
{
payloadlen=buf[UDP_LEN_L_P]-UDP_HEADER_LEN;
// the received command has to start with t and be 4 char long
// e.g "test\0"
if (buf[UDP_DATA_P]=='t' && payloadlen==5)
{
make_udp_reply_from_request(buf,"hello",6,MYUDPPORT);
}
}
}
return (0);
}
Now I want to add Input Capture Mode , so I add interrupt header file then start Input capture mode in main() and then introduce sei() function. Now My code look like this:-
#define F_CPU 8000000UL
#include <avr/io.h>
#include <string.h>
#include "ip_arp_udp_tcp.h"
#include "enc28j60.h"
#include "timeout.h"
#include "avr_compat.h"
#include "net.h"
#include <avr/interrupt.h>
// please modify the following two lines. mac and ip have to be unique
// in your local area network. You can not have the same numbers in
// two devices:
static uint8_t mymac[6] = {0x54,0x55,0x58,0x10,0x00,0x24};
// how did I get the mac addr? Translate the first 3 numbers into ascii is: TUX
static uint8_t myip[4] = {192,168,24,39};
// listen port for www
#define MYWWWPORT 80
//// listen port for udp
#define MYUDPPORT 1200
//
#define BUFFER_SIZE 450
static uint8_t buf[BUFFER_SIZE+1];
int main(void)
{
TCCR1A = 0;
TCCR1B = (1<<ICNC1)|(1<<ICES1)|(1<<CS11);
TIMSK = (1<<TICIE1);
TCNT1 = 0;
sei();
uint16_t plen;
uint16_t dat_p;
uint8_t i=0;
uint8_t payloadlen=0;
_delay_loop_1(50);
/*initialize enc28j60*/
enc28j60Init(mymac);
enc28j60clkout(2); // change clkout from 6.25MHz to 12.5MHz
_delay_loop_1(50); // 12ms
enc28j60PhyWrite(PHLCON,0x476);
_delay_loop_1(50); // 12ms
//init the ethernet/ip layer:
init_ip_arp_udp_tcp(mymac,myip,MYWWWPORT);
while(1)
{
// get the next new packet:
plen = enc28j60PacketReceive(BUFFER_SIZE, buf);
/*plen will ne unequal to zero if there is a valid * packet (without crc error) */
if(plen==0)
{
continue;
}
// arp is broadcast if unknown but a host may also
// verify the mac address by sending it to
// a unicast address.
if(eth_type_is_arp_and_my_ip(buf,plen))
{
make_arp_answer_from_request(buf);
continue;
}
// check if ip packets (icmp or udp) are for us:
if(eth_type_is_ip_and_my_ip(buf,plen)==0)
{
continue;
}
if(buf[IP_PROTO_P]==IP_PROTO_ICMP_V && buf[ICMP_TYPE_P]==ICMP_TYPE_ECHOREQUEST_V)
{
// a ping packet, let's send pong
make_echo_reply_from_request(buf,plen);
continue;
}
// tcp port www start, compare only the lower byte
if (buf[IP_PROTO_P]==IP_PROTO_TCP_V&&buf[TCP_DST_PORT_H_P]==0&&buf[TCP_DST_PORT_L_P]==MYWWWPORT)
{
if (buf[TCP_FLAGS_P] & TCP_FLAGS_SYN_V)
{
make_tcp_synack_from_syn(buf);
// make_tcp_synack_from_syn does already send the syn,ack
continue;
}
if (buf[TCP_FLAGS_P] & TCP_FLAGS_ACK_V)
{
init_len_info(buf); // init some data structures
// we can possibly have no data, just ack:
dat_p=get_tcp_data_pointer();
if (dat_p==0)
{
if (buf[TCP_FLAGS_P] & TCP_FLAGS_FIN_V)
{
// finack, answer with ack
make_tcp_ack_from_any(buf);
}
// just an ack with no data, wait for next packet
continue;
}
if (strncmp("GET ",(char *)&(buf[dat_p]),4)!=0)
{
plen=fill_tcp_data_p(buf,0,PSTR("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n<h1>200 OK</h1>"));
}
else
{
// Web Code
plen=fill_tcp_data_p(buf,0,PSTR("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n<p>PLANETCAST MEDIA SERVICES LTD</p>"));
plen=fill_tcp_data_p(buf,plen,PSTR("<body>"));
plen=fill_tcp_data_p(buf,plen,PSTR("<input type=\"text\" id=\"myurl\" name=\"url\" placeholder=\"Enter Url\"/><br>"));
plen=fill_tcp_data_p(buf,plen,PSTR("<input type=\"submit\" id=\"clickbutton\" name=\"Click Here\" />"));
plen=fill_tcp_data_p(buf,plen,PSTR("<script type=\"text/javascript\">"));
plen=fill_tcp_data_p(buf,plen,PSTR("document.getElementById(\"clickbutton\").onclick = function(){"));
plen=fill_tcp_data_p(buf,plen,PSTR("var url = document.getElementById(\"myurl\").value;"));
plen=fill_tcp_data_p(buf,plen,PSTR("location.href=url;"));
plen=fill_tcp_data_p(buf,plen,PSTR("};"));
plen=fill_tcp_data_p(buf,plen,PSTR("</script>"));
plen=fill_tcp_data_p(buf,plen,PSTR("</body"));
}
make_tcp_ack_from_any(buf); // send ack for http get
make_tcp_ack_with_data(buf,plen); // send data
continue;
}
}
// udp interface:
if (buf[IP_PROTO_P]==IP_PROTO_UDP_V)
{
payloadlen=buf[UDP_LEN_L_P]-UDP_HEADER_LEN;
// the received command has to start with t and be 4 char long
// e.g "test\0"
if (buf[UDP_DATA_P]=='t' && payloadlen==5)
{
make_udp_reply_from_request(buf,"hello",6,MYUDPPORT);
}
}
}
return (0);
}
ISR(TIMER1_CAPT_vect)
{
//do something
}
Now when I run my code , I got no webpage on my browser. So, I comment out sei() function. After this my code works fine. So, I check SPI settings in enc28j60.c. As per my knowledge SPI is not running on interrupt , so it shouldn't be affected by sei(). Here is my SPi settings in enc28j60.c:-
#define ENC28J60_CONTROL_PORT PORTB
#define ENC28J60_CONTROL_DDR DDRB
#define ENC28J60_CONTROL_CS 2
#define ENC28J60_CONTROL_SO 4
#define ENC28J60_CONTROL_SI 3
#define ENC28J60_CONTROL_SCK 5
void enc28j60Init(uint8_t* macaddr)
{
// initialize I/O
// ss as output:
ENC28J60_CONTROL_DDR |= 1<<ENC28J60_CONTROL_CS;
CSPASSIVE; // ss=0
//
ENC28J60_CONTROL_DDR |= 1<<ENC28J60_CONTROL_SI | 1<<ENC28J60_CONTROL_SCK; // mosi, sck output
cbi(ENC28J60_CONTROL_DDR,ENC28J60_CONTROL_SO); // MISO is input
//
cbi(ENC28J60_CONTROL_PORT,ENC28J60_CONTROL_SI); // MOSI low
cbi(ENC28J60_CONTROL_PORT,ENC28J60_CONTROL_SCK); // SCK low
So, can anyone tell me why my SPI mode is geting effected by sei() function and how get rid of this.
I have not read all your code but i think you are all time in interrupt. In normal mode the timer are not reset when it reach the top. Try with TCNT1 = 0; in the ISR.

how send sms by Arduino Nano and Sim900a

I try to use Arduino GSM library for sending sms.I have an Arduino Nano Board And an sim900 module and that connect together by serial port.but can't send sms.
also for testing I use this example code in Arduino website:
#include <GSM.h>
#define PINNUMBER ""
// initialize the library instance
GSM gsmAccess;
GSM_SMS sms;
void setup()
{
// initialize serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.println("SMS Messages Sender");
// connection state
boolean notConnected = true;
// Start GSM shield
// If your SIM has PIN, pass it as a parameter of begin() in quotes
while (notConnected)
{
if (gsmAccess.begin(PINNUMBER) == GSM_READY)
notConnected = false;
else
{
Serial.println("Not connected");
delay(1000);
}
}
Serial.println("GSM initialized");
}
void loop()
{
Serial.print("Enter a mobile number: ");
char remoteNum[20]; // telephone number to send sms
readSerial(remoteNum);
Serial.println(remoteNum);
// sms text
Serial.print("Now, enter SMS content: ");
char txtMsg[200];
readSerial(txtMsg);
Serial.println("SENDING");
Serial.println();
Serial.println("Message:");
Serial.println(txtMsg);
// send the message
sms.beginSMS(remoteNum);
sms.print(txtMsg);
sms.endSMS();
Serial.println("\nCOMPLETE!\n");
}
/*
Read input serial
*/
int readSerial(char result[])
{
int i = 0;
while (1)
{
while (Serial.available() > 0)
{
char inChar = Serial.read();
if (inChar == '\n')
{
result[i] = '\0';
Serial.flush();
return 0;
}
if (inChar != '\r')
{
result[i] = inChar;
i++;
}
}
}
It seems you are having issues using the GSM libraries that come with Arduino as they use soft serial
You can then use GSM arduino library provided by GROUND LAB
This library gives you option to select the serial port. check the documentation on wiki
/*First you have to make the gsmSMS object, the arguments are in order
*GsmSMS (#1 Name of the serial port connected to GSM (your choice),#2 the address of millis function(just copy whats there) )*/
gsmSMS myGsmSMS(Serial3,&millis,&Serial); //gsmSMS TELIT SMS
This library mentions that it is for Telit but it works for sim900 as well

Reading serial data from an input pin in Arduino

I have a GPS serial signal coming from Dout of XBee. I checked it with oscilloscope and signal exists.
How can I read serial signal with Arduino (I assume I should feed it to an input pin)?
And then print it to Serial Monitor.
I found the solution to read the GPS signal sent to the RX pin.
It is in the Arduino IDE under menu File → Examples → Communications → SerialEvent
String inputString = ""; // A string to hold incoming data
boolean stringComplete = false; // Whether the string is complete
void setup() {
// Initialize serial:
Serial.begin(9600);
// Reserve 200 bytes for the inputString:
inputString.reserve(200);
}
void loop() {
// Print the string when a newline arrives:
if (stringComplete) {
Serial.println(inputString);
// Clear the string:
inputString = "";
stringComplete = false;
}
}
/*
SerialEvent occurs whenever a new data comes in the
hardware serial RX. This routine is run between each
time loop() runs, so using delay inside loop can delay
response. Multiple bytes of data may be available.
*/
void serialEvent() {
while (Serial.available()) {
// Get the new byte:
char inChar = (char)Serial.read();
// Add it to the inputString:
inputString += inChar;
// If the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == '\n') {
stringComplete = true;
}
}
}
Now connect the wire that has serial signal to the RX (pin 0) and run Serial Monitor and you will see something like this:
$GPGLL,5652.36437,N,08901.52702,W,225356.00,A,D*73
$GPGSV,4,1,15,07,42,306,33,08,16,110,33,09,53,253,37,10,05,281,23*72
$GPRMC,225357.00,A,5652.36445,N,08901.52698,W,0.145,,180315,,,D*62

Trouble with Sending HTTP 1.1 GET request

I am trying to use PubNub to publish a message to a channel but I always get "Connection Failed!".
I know the structure of a publish URL is the following:
http://pubsub.pubnub.com/publish/<PUB-KEY>/<SUB-KEY>/0/<CHANNEL>/0/%22<MESSAGE>%22
I have tested the URL on Google Chrome and it works just fine.
The following is a C flavored code is running on an Arduino UNO + Adafruit HUZZAH CC3000 WIFI Breakout. This is not an Arduino hardware question per se, because I believe there is nothing wrong with my circuit. My issue is with the structure of the HTTP GET request created using Adafruit CC3000 Library.
I am having to go with sending a GET rather than using PubNub Arduino Library because it doesn't seem to support CC3000 WIFI module. They have a JSON WIFI and Ethernet examples but both do not communicate with the CC3000 WIFI module.
char PUBKEY[] = "XXXXXXX";
char SUBKEY[] = "XXXXXXX";
Adafruit_CC3000_Client client = cc3000.connectTCP(ip, 80);
if (client.connected())
{
client.print("GET /publish/");
client.print(PUBKEY);
client.print("/");
client.print(SUBKEY);
client.print("/0/");
client.print("MyPubChannel"); // Channel Name
client.print("/0/%22");
client.print("Hello World from Arduino!"); // Msg to publish
client.print("%22");
client.println(" HTTP/1.1");
client.print("Host: ");
client.println("pubsub.pubnub.com");
client.println();
}
else
{
Serial.println(F("Connection failed"));
}
I have read this page and understood that a correct GET request would be of the form:
GET /pub/WWW/TheProject.html HTTP/1.1
Host: www.w3.org
I am pretty sure there is something wrong with my GET request. Can anyone look at this and provide ANY hints as to what could be going wrong?
I am finally able to connect to PubNub using the code below.
#include <Adafruit_CC3000.h>
#include <ccspi.h>
#include <SPI.h>
#include <string.h>
#include "utility/debug.h"
#define ADAFRUIT_CC3000_IRQ 3 // MUST be an interrupt pin!
#define ADAFRUIT_CC3000_VBAT 5
#define ADAFRUIT_CC3000_CS 10
#define WLAN_SSID "XXXWifi"
#define WLAN_PASS "XXX"
#define WLAN_SECURITY WLAN_SEC_WPA2
#define IDLE_TIMEOUT_MS 3
uint32_t ip;
// PubNub Setup
#define WEBSITE "pubsub.pubnub.com"
#define WEBPAGE "/publish/"
char PUBKEY[] = "pub-XXXX";
char SUBKEY[] = "sub-XXXX";
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT, SPI_CLOCK_DIVIDER); // you can change this clock speed
Adafruit_CC3000_Client www;
void setup(void)
{
Serial.begin(115200);
/* Initialize the module */
Serial.print(F("\n Initializing WIFI Adapter..."));
if (!cc3000.begin())
{
Serial.println(F("Couldn't begin()! Check your wiring?"));
while(1);
}
Serial.print(F("Ready!\n"));
Serial.print(F("Connecting to [")); Serial.print(WLAN_SSID);
Serial.print(F("]..."));
if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
Serial.println(F("Failed!"));
while(1);
}
Serial.print(F("Connected!\n"));
//Wait for DHCP to complete
Serial.print(F("Waiting for DHCP..."));
while (!cc3000.checkDHCP())
{
delay(100); // ToDo: Insert a DHCP timeout!
}
Serial.print(F("Done!\n"));
Serial.print(F("Resolving PubNub..."));
ip = 0;
// Try looking up the website's IP address
while (ip == 0) {
if (! cc3000.getHostByName(WEBSITE, &ip)) {
Serial.println(F("Couldn't resolve!"));
}
delay(500);
}
Serial.print(F("Resolved!\n"));
www = cc3000.connectTCP(ip, 80);
for(int i=0;i<=50;i++)
{
char buffer[4];
dtostrf(i, 4, 0, buffer);
Publish("ch2", buffer);
}
/* You need to make sure to clean up after yourself or the CC3000 can freak out */
/* the next time your try to connect ... */
Serial.println(F("\n\nDisconnecting"));
www.close();
cc3000.disconnect();
}
void loop(void)
{
delay(1000);
}
void Publish(char* CH, char* MSG)
{
if (www.connected()) {
www.fastrprint(F("GET "));
www.fastrprint(WEBPAGE);
www.fastrprint(PUBKEY);
www.fastrprint(F("/"));
www.fastrprint(SUBKEY);
www.fastrprint(F("/0/"));
www.fastrprint(CH);
www.fastrprint(F("/0/%22"));
www.fastrprint(MSG);
www.fastrprint(F("%22"));
www.fastrprint(F(" HTTP/1.1\r\n"));
www.fastrprint(F("Host: "));
www.fastrprint(WEBSITE);
www.fastrprint(F("\r\n"));
www.fastrprint(F("\r\n"));
www.println();
} else {
Serial.println(F("Connection failed"));
return;
}
//Read data until either the connection is closed, or the idle timeout is reached.
unsigned long lastRead = millis();
while (www.connected() && (millis() - lastRead < IDLE_TIMEOUT_MS)) {
while (www.available()) {
char c = www.read();
//Serial.print(c);
lastRead = millis();
}
}
}
char* clean(char* MSG)
{
return MSG;
}