I have a NodeMCU 1.0 and a ESP8266 12-E I created some code to check a GPS signal and report back, on the NodeMCU the code works 100% no problems but when I load the code on a ESP8266 12-e (with NodeMCU flashed on it) I can not get the GPS signal to show up wired to the same GPOI ports and everything.
Most of the code:
#include <TimGPS.h>
#include <SoftwareSerial.h >
#include "FS.h"
extern "C" {
#include "gpio.h"
#include "user_interface.h"
}
static const int RXPin = 12, TXPin = 255;
static const uint32_t GPSBaud = 9600;
TimGPS gps;
SoftwareSerial ss(RXPin, TXPin);
void printGPSData()
{
Serial.print("Latitude : ");
Serial.println(gps.location.lat(), 6);
Serial.print("Longitude : ");
Serial.println(gps.location.lng(), 6);
Serial.print("Speed : ");
Serial.println(gps.speed.mph(), 6);
Serial.print("Satellites: ");
Serial.println(gps.satellites.value());
Serial.print("Time UTC : ");
Serial.print(gps.time.hour());
Serial.print(":");
Serial.print(gps.time.minute());
Serial.print(":");
Serial.println(gps.time.second());
}
static void smartDelay(unsigned long ms)
{
unsigned long start = millis();
do
{
while (ss.available())
gps.encode(ss.read());
} while (millis() - start < ms);
}
void setup() {
Serial.begin(115200);
Serial.println("Startup");
ss.begin(GPSBaud);
}
void loop() {
if (millis() > 5000 && gps.charsProcessed() < 10) {
Serial.println(F("No GPS data received: check wiring"));
}
else
{
printGPSData(url);
}
}
Related
I have Arduino and a Duinotech NRF24L01, I am trying to send the string "Hello world" with maniacs bug RF24 library however, I think it cannot detect the incoming RF signal.
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
RF24 radio(7, 8); // CE, CSN
const uint64_t pipe = 0xF0F0F0F0E1L;
void setup() {
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(0, pipe);
radio.startListening();
}
void loop() {
if (radio.available()) {
char text[32] = "";
radio.read(&text, sizeof(text));
Serial.println(text);
}
else {
Serial.println("Data was not found");
}
In the read code, it would always execute data was not found. This makes me think that maybe it does not find the RF signal at all.
Here is the code that writes the data.
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
RF24 radio(7, 8); // CE, CSN
int text = 1;
const uint64_t pipe = 0xF0F0F0F0E1LL;
void setup() {
Serial.begin(9600);
radio.begin();
radio.openWritingPipe(pipe);
radio.stopListening();
}
void loop() {
radio.write(&text, sizeof(text));
Serial.println("Sending Data");
delay(1000);
}
Try this
For the transmitter Instead of const uint64_t pipe = 0xF0F0F0F0E1LL; use const byte address[6] = "00001"; as the address and then have you void setup like the code below
void setup() {
radio.begin();
radio.openWritingPipe(address);
radio.setPALevel(RF24_PA_MIN);
radio.stopListening();
}
Then ensure that you have a value set for the test variable to be transmitted as below
void loop() {
const char text[] = "Hello World";
radio.write(&text, sizeof(text));
Serial.println("Sending Data");
delay(1000);
}
At the receiver end have this code running
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7, 8); // CE, CSN
const byte address[6] = "00001";
void setup() {
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(0, address);
radio.setPALevel(RF24_PA_MIN);
radio.startListening();
}
void loop() {
if (radio.available()) {
char text[32] = "";
radio.read(&text, sizeof(text));
Serial.println(text); //This will print out the received value
}
}
PS: Ensure that all the connections are done to the right pins
AND you can test if the NRF24L01 chip is connected correctly by adding the code below
bool result = radio.isChipConnected ();
Serial.println (result);
it should print out a 1 to the serial monitor if the NRF24L01 chip is connected correctly
I have been working on a project. I have two sketches. One is to get GPS location and one is send location via SMS (GSM Module). I want to combine both sketches.
GPS sketch:
#include <SoftwareSerial.h>
#include <TinyGPS.h>
//long lat,lon; // create variable for latitude and longitude object
float flat, flon;
SoftwareSerial gpsSerial(4, 3); // create gps sensor connection
TinyGPS gps; // create gps object
void setup(){
Serial.begin(9600); // connect serial
gpsSerial.begin(9600); // connect gps sensor
}
static void print_float(float val, float invalid, int len, int prec)
{
if (val == invalid)
{
while (len-- > 1)
Serial.print('*');
Serial.print(' ');
}
else
{
Serial.print(val, prec);
int vi = abs((int)val);
int flen = prec + (val < 0.0 ? 2 : 1); // . and -
flen += vi >= 1000 ? 4 : vi >= 100 ? 3 : vi >= 10 ? 2 : 1;
for (int i=flen; i<len; ++i)
Serial.print(' ');
}
// smartdelay(0);
}
void loop(){
while(gpsSerial.available()){ // check for gps data
if(gps.encode(gpsSerial.read())){ // encode gps data
// gps.get_position(&lat,&lon); // get latitude and longitude
gps.f_get_position(&flat, &flon);
String lat = String(flat,6);
String lon = String(flon,6);
Serial.print(lat);
Serial.print(' ');
Serial.println(lon);
}
}
}
GSM sketch:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(9, 10);
void setup()
{
mySerial.begin(9600); // Setting the baud rate of GSM Module
Serial.begin(9600); // Setting the baud rate of Serial Monitor (Arduino)
delay(100);
}
int index=0;
long dlat=0,dlong=0;
char st[256],st1[256],st2[256];
void RecieveMessage()
{
mySerial.println("AT+CNMI=2,2,0,0,0"); // AT Command to receive a live SMS
// mySerial.println("cheking");
// mySerial.println(string);
}
void loop()
{
if (Serial.available()>0)
RecieveMessage();
if (mySerial.available()>0)
{
//int st;
st[index++] = mySerial.read();
//Serial.write(st[index-1]);
if(index>=51&&index<=63){
st1[index-51]=st[index-1];
Serial.write(st1[index-51]);
}
if(index>=65&&index<=77){
st2[index-65]=st[index-1];
Serial.write(st2[index-65]);
}
/*if(index==77)
{
int i=0;
mySerial.print(st1);
for(i=0;i<13;i++)
{
if(st1[i]!='.')
dlat=dlat*10+(st1[i]-'0');
if(st2[i]!='.')
dlong=dlong*10+(st2[i]-'0');
//Serial.print(dlat);
}
//Serial.print(dlat);
//Serial.print(" ");
//Serial.print(dlong);
*/
}
}
I want to merge these two sketches so that I can send location via GSM sketch and receive location through GPS sketch.
Any help will be appreciated.
Thanks.
The following way you can merge your two sketches :
#include <AltSoftSerial.h>
#include <SoftwareSerial.h>
#include <TinyGPS.h>
//long lat,lon; // create variable for latitude and longitude object
float flat, flon;
SoftwareSerial gpsSerial(4, 3); // create gps sensor connection
TinyGPS gps; // create gps object
static const int RXPin = 8, TXPin = 9;
AltSoftSerial mySerial(RXPin, TXPin);
int index=0;
long dlat=0,dlong=0;
char st[256],st1[256],st2[256];
void RecieveMessage()
{
mySerial.println("AT+CNMI=2,2,0,0,0"); // AT Command to receive a live SMS
// mySerial.println("cheking");
// mySerial.println(string);
}
void setup(){
Serial.begin(9600); // connect serial
gpsSerial.begin(9600); // connect gps sensor
mySerial.begin(9600); // Setting the baud rate of GSM Module
delay(100);
}
static void print_float(float val, float invalid, int len, int prec)
{
if (val == invalid)
{
while (len-- > 1)
Serial.print('*');
Serial.print(' ');
}
else
{
Serial.print(val, prec);
int vi = abs((int)val);
int flen = prec + (val < 0.0 ? 2 : 1); // . and -
flen += vi >= 1000 ? 4 : vi >= 100 ? 3 : vi >= 10 ? 2 : 1;
for (int i=flen; i<len; ++i)
Serial.print(' ');
}
// smartdelay(0);
}
void loop(){
while(gpsSerial.available()){ // check for gps data
if(gps.encode(gpsSerial.read())){ // encode gps data
// gps.get_position(&lat,&lon); // get latitude and longitude
gps.f_get_position(&flat, &flon);
String lat = String(flat,6);
String lon = String(flon,6);
Serial.print(lat);
Serial.print(' ');
Serial.println(lon);
if (Serial.available()>0)
RecieveMessage();
if (mySerial.available()>0)
{
//int st;
st[index++] = mySerial.read();
//Serial.write(st[index-1]);
if(index>=51&&index<=63){
st1[index-51]=st[index-1];
Serial.write(st1[index-51]);
}
if(index>=65&&index<=77){
st2[index-65]=st[index-1];
Serial.write(st2[index-65]);
}
/*if(index==77)
{
int i=0;
mySerial.print(st1);
for(i=0;i<13;i++)
{
if(st1[i]!='.')
dlat=dlat*10+(st1[i]-'0');
if(st2[i]!='.')
dlong=dlong*10+(st2[i]-'0');
//Serial.print(dlat);
}
//Serial.print(dlat);
//Serial.print(" ");
//Serial.print(dlong);
*/
}
}
}
}
P.S : I have changed the pins for GPS and rest part is same irrespective of adding AltSoftSerial it is a library same as SoftwareSerial (Download it from manage libraries and it requires pins 8 and 9)
I'm trying to send the x and y values of a joystick between two arduino mega 2560 with an nrf24l01. So far I have a transmitter and reciever code but I get wrong values on the reciever.
Transmitter:
#include <SPI.h>
#include <RF24.h>
RF24 radio(7,8);
byte addresses[][6] = {"0x115331","0x222331"};
typedef struct{
int X;
int Y;
int K;
}
Values;
Values v;
void setup() {
pinMode(10, INPUT);
Serial.begin(115200);
radio.begin();
radio.setPALevel(RF24_PA_LOW);
radio.setChannel(108);
radio.setPayloadSize(32);
radio.openWritingPipe(addresses[1]);
radio.openReadingPipe(1,addresses[0]);
}
void loop() {
radio.stopListening();
v.X = analogRead(A0);
v.Y = analogRead(A1);
v.K = digitalRead(10);
Serial.print("X = ");
Serial.println(v.X);
Serial.print("Y = ");
Serial.println(v.Y);
Serial.print("Button status: ");
if (v.K == HIGH)
{
Serial.println("pressed");
}
else
{
Serial.println("not pressed");
}
radio.write(&v,sizeof(v));
delay(500);
}
Reciever:
#include <SPI.h>
#include <RF24.h>
RF24 radio(7,8);
byte addresses[][6] = {"0x115331","0x222331"};
typedef struct{
int X;
int Y;
int K;
}
Values;
Values v;
void setup() {
Serial.begin(115200);
radio.begin();
radio.setPALevel(RF24_PA_LOW);
radio.openWritingPipe(addresses[0]);
radio.openReadingPipe(1,addresses[1]);
radio.setChannel(108);
radio.setPayloadSize(32);
radio.startListening();
}
void loop() {
radio.startListening();
if ( radio.available() )
{
while(radio.available())
{
radio.read(&v, sizeof(v));
}
}
radio.stopListening();
Serial.println(v.X);
Serial.println(v.Y);
Serial.println(v.K);
delay(1000);
}
I'm using the pin configuration from this website: https://arduino-info.wikispaces.com/Nrf24L01-2.4GHz-HowTo
i need help in integrating the two libraries so that i can send the GPS data via GSM . Information regarding the use of two special Serial is needed and also a help with the code is needed .
The below segmnet containts the code for the GPS shield this has to be used to generate the location and this data has to be sent via gsm to a mobile number.
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
/*
This sample sketch demonstrates the normal use of a TinyGPS++ (TinyGPSPlus) object.
It requires the use of SoftwareSerial, and assumes that you have a
4800-baud serial GPS device hooked up on pins 4(rx) and 3(tx).
*/
static const int RXPin = 4, TXPin = 3;//was 4 and 3;
static const uint32_t GPSBaud = 9600;
// The TinyGPS++ object
TinyGPSPlus gps;
// The serial connection to the GPS device
SoftwareSerial ss(RXPin, TXPin);
void setup()
{
Serial.begin(115200);
ss.begin(GPSBaud);
Serial.println(F("GPS GSM tracking system"));
Serial.println(F("Sabdadon Presents"));
Serial.print(F("Search and Rescue")); Serial.println(TinyGPSPlus::libraryVersion());
Serial.println(F("Sabarish"));
Serial.println();
}
void loop()
{
// This sketch displays information every time a new sentence is correctly encoded.
while (ss.available() > 0)
if (gps.encode(ss.read()))
displayInfo();
if (millis() > 500000 && gps.charsProcessed() < 10)
{
Serial.println(F("No GPS detected: check wiring."));
while(true);
}
}
void displayInfo()
{
delay(10000);
Serial.print(F("Location: "));
if (gps.location.isValid())
{
Serial.print(gps.location.lat(), 5);
Serial.print(F(","));
Serial.print(gps.location.lng(), 5);
// latitude=gps.location.lat();
//longitude=gps.location.lng();
//if(latitude && longitude)
}
else
{
Serial.print(F("INVALID"));
}
Serial.print(F(" Date/Time: "));
if (gps.date.isValid())
{
Serial.print(gps.date.month());
Serial.print(F("/"));
Serial.print(gps.date.day());
Serial.print(F("/"));
Serial.print(gps.date.year());
}
else
{
Serial.print(F("INVALID"));
}
Serial.print(F(" "));
if (gps.time.isValid())
{
if (gps.time.hour() < 10) Serial.print(F("0"));
Serial.print(gps.time.hour());
Serial.print(F(":"));
if (gps.time.minute() < 10) Serial.print(F("0"));
Serial.print(gps.time.minute());
Serial.print(F(":"));
if (gps.time.second() < 10) Serial.print(F("0"));
Serial.print(gps.time.second());
Serial.print(F("."));
if (gps.time.centisecond() < 10) Serial.print(F("0"));
Serial.print(gps.time.centisecond());
}
else
{
ss.read();
Serial.print(F("INVALID"));
}
Serial.println();
}
FOR GSM
#include "Adafruit_FONA.h"
#define FONA_RX 2//2
#define FONA_TX 3//3
#define FONA_RST 4//4
char replybuffer[255];
#include <SoftwareSerial.h>
#include <AltSoftSerial.h>
SoftwareSerial fonaSS = SoftwareSerial(FONA_TX, FONA_RX);
SoftwareSerial *fonaSerial = &fonaSS;
Adafruit_FONA fona = Adafruit_FONA(FONA_RST);
uint8_t readline(char *buff, uint8_t maxbuff, uint16_t timeout = 0);
uint8_t type;
void setup()
{
while (!Serial);
Serial.begin(115200);
Serial.println(F("FONA basic test"));
Serial.println(F("Initializing....(May take 3 seconds)"));
fonaSerial->begin(4800);
if (! fona.begin(*fonaSerial)) {
Serial.println(F("Couldn't find FONA"));
while (1);
}
type = fona.type();
Serial.println(F("FONA is OK"));
Serial.print(F("Found "));
switch (type) {
case FONA800L:
Serial.println(F("FONA 800L")); break;
case FONA800H:
Serial.println(F("FONA 800H")); break;
case FONA808_V1:
Serial.println(F("FONA 808 (v1)")); break;
case FONA808_V2:
Serial.println(F("FONA 808 (v2)")); break;
case FONA3G_A:
Serial.println(F("FONA 3G (American)")); break;
case FONA3G_E:
Serial.println(F("FONA 3G (European)")); break;
default:
Serial.println(F("???")); break;
}
// Print module IMEI number.
char imei[15] = {0}; // MUST use a 16 character buffer for IMEI!
uint8_t imeiLen = fona.getIMEI(imei);
if (imeiLen > 0) {
Serial.print("Module IMEI: "); Serial.println(imei);
}
}
void loop()
{ Serial.print(F("FONA> "));
while (! Serial.available() ) {
if (fona.available()) {
Serial.write(fona.read());
}
}
// send an SMS!
char sendto[21], message[141];
flushSerial();
Serial.print(F("Send to #"));
readline(sendto, 20);
Serial.println(sendto);
Serial.print(F("Type out one-line message (140 char): "));
readline(message, 140);
Serial.println(message);
if (!fona.sendSMS(sendto, message)) {
Serial.println(F("Failed"));
} else {
Serial.println(F("Sent!"));
}
}
void flushSerial() {
while (Serial.available())
Serial.read();
}
char readBlocking() {
while (!Serial.available());
return Serial.read();
}
uint16_t readnumber() {
uint16_t x = 0;
char c;
while (! isdigit(c = readBlocking())) {
//Serial.print(c);
}
Serial.print(c);
x = c - '0';
while (isdigit(c = readBlocking())) {
Serial.print(c);
x *= 10;
x += c - '0';
}
return x;
}
uint8_t readline(char *buff, uint8_t maxbuff, uint16_t timeout) {
uint16_t buffidx = 0;
boolean timeoutvalid = true;
if (timeout == 0) timeoutvalid = false;
while (true) {
if (buffidx > maxbuff) {
//Serial.println(F("SPACE"));
break;
}
while (Serial.available()) {
char c = Serial.read();
//Serial.print(c, HEX); Serial.print("#"); Serial.println(c);
if (c == '\r') continue;
if (c == 0xA) {
if (buffidx == 0) // the first 0x0A is ignored
continue;
timeout = 0; // the second 0x0A is the end of the line
timeoutvalid = true;
break;
}
buff[buffidx] = c;
buffidx++;
}
if (timeoutvalid && timeout == 0) {
//Serial.println(F("TIMEOUT"));
break;
}
delay(1);
}
buff[buffidx] = 0; // null term
return buffidx;
}
Here is a step-by-step to mix your GPS input device and your GSM output device.
Remainder for Arduino principles:
The void setup() function is performed one time after startup.
The void loop() function is performed periodically after the
setup().
Step1 - declaration of GPS device and Serial link
// GPS and Serial link
static const int RXPin = 4, TXPin = 3;//was 4 and 3;
static const uint32_t GPSBaud = 9600;
// The TinyGPS++ object
TinyGPSPlus DeviceGPS;
// The serial connection to the GPS device
SoftwareSerial SerialGPS(RXPin, TXPin);
Step2 - declaration of GSM/FONA device and Serial link
Including the SendTo SMS number !!!
#define FONA_RX 2//2
#define FONA_TX 3//3
#define FONA_RST 4//4
// The serial connection to the GSM device
SoftwareSerial SerialFONA = SoftwareSerial(FONA_TX, FONA_RX);
// The FONA/GSM Cellular Module device
Adafruit_FONA DeviceFONA = Adafruit_FONA(FONA_RST);
// The destination SMS number
static const char *sSendTo = "<NUMBER>";
Step3 - setup() function for (Console, GPS and GSM)
It is possible to add some extra Init.
// only execute once
void setup()
{
// Wait and Init Console
while (!Serial); // Serial over USB
Serial.begin(115200);
// Init GPS link
SerialGPS.begin(GPSBaud);
Serial.print(F("TinyGPSPlus ver: "));
Serial.println(TinyGPSPlus::libraryVersion());
// Init GSM link
SerialFONA.begin(4800);
if (! DeviceFONA.begin(SerialFONA)) {
Serial.println(F("Couldn't find FONA"));
while (1); // Stop working
}
// Add some extra Init
}
Step4 - loop() function to wait GPS location and send SMS
It is possible to use String() to create the SMS based on the
acquired DeviceGPS.location.lng() and DeviceGPS.location.lat().
// executed periodicaly
void loop()
{
// check until GPS message
while (SerialGPS.available() > 0) {
// get for a complete GPS message
DeviceGPS.encode(SerialGPS.read());
}
// flush GSM serial link
while (SerialFONA.available() > 0) {
if (DeviceFONA.available()) {
DeviceFONA.flush();
}
}
// send an SMS!
char sendto[21], message[141];
// Wait for location (lng, lat, alt) is OK
if (DeviceGPS.location.isValid()) {
// ==> create SMS with longitude & latitude
}
}
I'm trying to request temperatures from my DS18B20 sensor to post on plot.ly, but it seems my loop function is only running once; after connecting to plot.ly and creating the graph, the temperature is printed once in the serial monitor and does not seem to continue! Any help is greatly appreciated. Here is my code:
#include <Adafruit_CC3000.h>
#include <ccspi.h>
#include <SPI.h>
#include <plotly_streaming_cc3000.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define WLAN_SSID "wifi"
#define WLAN_PASS "********"
#define WLAN_SECURITY WLAN_SEC_WPA2
OneWire oneWire(10);
DallasTemperature sensors(&oneWire);
#define nTraces 1
char *tokens[nTraces] = {"token"};
plotly graph("username", "token", tokens, "filename", nTraces);
void wifi_connect(){
/* Initialise the module */
Serial.println(F("\n... Initializing..."));
if (!graph.cc3000.begin())
{
Serial.println(F("... Couldn't begin()! Check your wiring?"));
while(1);
}
// Optional SSID scan
// listSSIDResults();
if (!graph.cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
Serial.println(F("Failed!"));
while(1);
}
Serial.println(F("... Connected!"));
/* Wait for DHCP to complete */
Serial.println(F("... Request DHCP"));
while (!graph.cc3000.checkDHCP())
{
delay(100); // ToDo: Insert a DHCP timeout!
unsigned long aucDHCP = 14400;
unsigned long aucARP = 3600;
unsigned long aucKeepalive = 10;
unsigned long aucInactivity = 20;
if (netapp_timeout_values(&aucDHCP, &aucARP, &aucKeepalive, &aucInactivity) != 0) {
Serial.println("Error setting inactivity timeout!");
}
}
}
void setup() {
graph.maxpoints = 100;
Serial.begin(9600);
sensors.begin();
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
wifi_connect();
bool success;
success = graph.init();
if(!success){while(true){}}
graph.openStream();
}
void loop(void) {
Serial.print("Requesting temperatures...");
sensors.requestTemperatures();
Serial.println("DONE");
Serial.print("Temperature for Device 1 is: ");
Serial.print(sensors.getTempFByIndex(0));
graph.plot(millis(), sensors.getTempFByIndex(0), tokens[0]);
delay(500);
}