SysBee v1
Dependencies: C027_Support mbed
Fork of App_Pese_Ruche_SYSBEE by
Diff: main.cpp
- Revision:
- 1:5639bc1208d5
- Parent:
- 0:15a06b50e14e
- Child:
- 2:57d040afa937
diff -r 15a06b50e14e -r 5639bc1208d5 main.cpp --- a/main.cpp Mon Feb 22 16:28:17 2016 +0000 +++ b/main.cpp Wed Apr 11 08:44:24 2018 +0000 @@ -1,11 +1,12 @@ #include "mbed.h" +#include <string.h> //------------------------------------------------------------------------------------ -/* This example was tested on C027-U20 and C027-G35 with the on board modem. - - Additionally it was tested with a shield where the SARA-G350/U260/U270 RX/TX/PWRON - is connected to D0/D1/D4 and the GPS SCL/SDA is connected D15/D15. In this - configuration the following platforms were tested (it is likely that others +/* This example was tested on C027-U20 and C027-G35 with the on board modem. + + Additionally it was tested with a shield where the SARA-G350/U260/U270 RX/TX/PWRON + is connected to D0/D1/D4 and the GPS SCL/SDA is connected D15/D15. In this + configuration the following platforms were tested (it is likely that others will work as well) - U-BLOX: C027-G35, C027-U20, C027-C20 (for shield set define C027_FORCE_SHIELD) - NXP: LPC1549v2, LPC4088qsb @@ -20,277 +21,151 @@ // These parameters are ignored for LISA-C200 variants and can be left NULL. //------------------------------------------------------------------------------------ //! Set your secret SIM pin here (e.g. "1234"). Check your SIM manual. -#define SIMPIN NULL -/*! The APN of your network operator SIM, sometimes it is "internet" check your - contract with the network operator. You can also try to look-up your settings in +#define SIMPIN "1234" +/*! The APN of your network operator SIM, sometimes it is "internet" check your + contract with the network operator. You can also try to look-up your settings in google: https://www.google.de/search?q=APN+list */ #define APN NULL //! Set the user name for your APN, or NULL if not needed #define USERNAME NULL //! Set the password for your APN, or NULL if not needed -#define PASSWORD NULL +#define PASSWORD NULL //------------------------------------------------------------------------------------ -//#define CELLOCATE +#define RVA 0 +#define RPA 1 +#define RPHVA 2 +#define RPHPHVA 3 + +AnalogIn analog_value(PC_5); + +static float variation_poids=0; +static float poids_ruche=0; +static float poids_prece=0; + +//const char num1[]="0652330722"; //jamoud +//const char num1[]="0685350930"; //paul bonnet +//const char num1[]="0685209401"; //benji +const char num1[]="0609168455"; //garsonnat + + +static char chaine[255]; + +static int flag = 0; + +void automate() +{ + static int etat_ruche_courant = RVA; + static int etat_ruche_futur; + + etat_ruche_courant = etat_ruche_futur; + variation_poids = poids_ruche - poids_prece; + poids_prece = poids_ruche; + + switch(etat_ruche_courant) { + + case RVA: + if(poids_ruche < 40) { + if(variation_poids > 0) { + etat_ruche_futur = RVA; + } + if(variation_poids < 0) { + etat_ruche_futur = RVA; + } + if(variation_poids == 0) { + etat_ruche_futur = RVA; + } + } + if(poids_ruche >= 40) { + + sprintf(chaine, "recolte hausse n1\r\n poids = %2.f", poids_ruche); + flag = 1; + etat_ruche_futur = RPHVA; + } + + break; + + case RPHVA: + if(poids_ruche < 56) { + if(variation_poids > 0) { + etat_ruche_futur = RPHVA; + } + if(variation_poids < 0) { + etat_ruche_futur = RPHVA; + } + if(variation_poids == 0) { + etat_ruche_futur = RPHVA; + } + } + if(poids_ruche >= 56) { + sprintf(chaine, "recolte hausse n2\r\n poids = %2.f", poids_ruche); + flag = 1; + etat_ruche_futur = RPHPHVA; + } + + break; + + case RPHPHVA: + if(poids_ruche < 72) { + if(variation_poids > 0) { + etat_ruche_futur = RPHPHVA; + } + if(variation_poids < 0) { + etat_ruche_futur = RPHPHVA; + } + if(variation_poids == 0) { + etat_ruche_futur = RPHPHVA; + } + } + if(poids_ruche > 72) { + sprintf(chaine, "recolte hausse n1 & n2\r\n poids = %2.f", poids_ruche); + flag = 1; + etat_ruche_futur = RVA; + } + break; + } +} int main(void) { - int ret; -#ifdef LARGE_DATA - char buf[2048] = ""; -#else - char buf[512] = ""; -#endif - - // Create the GPS object -#if 1 // use GPSI2C class - GPSI2C gps; -#else // or GPSSerial class - GPSSerial gps; -#endif + Serial pc(USBTX, USBRX); + pc.baud(115200); // Create the modem object MDMSerial mdm; - //mdm.setDebug(4); // enable this for debugging issues - // initialize the modem - MDMParser::DevStatus devStatus = {}; - MDMParser::NetStatus netStatus = {}; - bool mdmOk = mdm.init(SIMPIN, &devStatus); - mdm.dumpDevStatus(&devStatus); - if (mdmOk) { -#if 0 - // file system API - const char* filename = "File"; - char buf[] = "Hello World"; - printf("writeFile \"%s\"\r\n", buf); - if (mdm.writeFile(filename, buf, sizeof(buf))) - { - memset(buf, 0, sizeof(buf)); - int len = mdm.readFile(filename, buf, sizeof(buf)); - if (len >= 0) - printf("readFile %d \"%.*s\"\r\n", len, len, buf); - mdm.delFile(filename); - } -#endif + + while(1) { + + poids_ruche=analog_value.read(); + poids_ruche = poids_ruche * 3300; + poids_ruche=(0.03*poids_ruche)-6.02; + wait(1); + pc.printf("%2.2f \r\n",poids_ruche); + automate(); + if(flag == 1) { + // initialize the modem + MDMParser::DevStatus devStatus = {}; + MDMParser::NetStatus netStatus = {}; - // wait until we are connected - mdmOk = mdm.registerNet(&netStatus); - mdm.dumpNetStatus(&netStatus); - } - if (mdmOk) - { - // http://www.geckobeach.com/cellular/secrets/gsmcodes.php - // http://de.wikipedia.org/wiki/USSD-Codes - const char* ussd = "*130#"; // You may get answer "UNKNOWN APPLICATION" - printf("Ussd Send Command %s\r\n", ussd); - ret = mdm.ussdCommand(ussd, buf); - if (ret > 0) - printf("Ussd Got Answer: \"%*s\"\r\n", ret, buf); + bool mdmOk = mdm.init(SIMPIN, &devStatus, D4); + while(mdmOk == false)mdmOk = mdm.init(SIMPIN, &devStatus, D4); + + bool launchOK = mdm.checkNetStatus(&netStatus); + while(launchOK == false)launchOK = mdm.checkNetStatus(&netStatus); - // join the internet connection - MDMParser::IP ip = mdm.join(APN,USERNAME,PASSWORD); - if (ip != NOIP) - { - mdm.dumpIp(ip); - printf("Make a Http Post Request\r\n"); - int socket = mdm.socketSocket(MDMParser::IPPROTO_TCP); - if (socket >= 0) - { - mdm.socketSetBlocking(socket, 10000); - if (mdm.socketConnect(socket, "mbed.org", 80)) - { - const char http[] = "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\r\n\r\n"; - mdm.socketSend(socket, http, sizeof(http)-1); - - ret = mdm.socketRecv(socket, buf, sizeof(buf)-1); - if (ret > 0) - printf("Socket Recv \"%*s\"\r\n", ret, buf); - mdm.socketClose(socket); - } - mdm.socketFree(socket); + mdm.dumpDevStatus(&devStatus); + if (mdmOk) { + mdmOk = mdm.registerNet(&netStatus); + mdm.dumpNetStatus(&netStatus); + mdm.smsSend(num1 ,chaine); } + mdm.powerOff(); + flag = 0; - int port = 7; - const char* host = "echo.u-blox.com"; - MDMParser::IP ip = mdm.gethostbyname(host); - char data[] = "\r\nxxx Socket Hello World\r\n" -#ifdef LARGE_DATA - "00 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" - "01 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" - "02 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" - "03 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" - "04 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" - - "05 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" - "06 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" - "07 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" - "08 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" - "09 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" - - "10 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" - "11 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" - "12 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" - "13 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" - "14 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" - - "15 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" - "16 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" - "17 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" - "18 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" - "19 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" -#endif - "End\r\n"; - - printf("Testing TCP sockets with ECHO server\r\n"); - socket = mdm.socketSocket(MDMParser::IPPROTO_TCP); - if (socket >= 0) - { - mdm.socketSetBlocking(socket, 10000); - if (mdm.socketConnect(socket, host, port)) { - memcpy(data, "\r\nTCP", 5); - ret = mdm.socketSend(socket, data, sizeof(data)-1); - if (ret == sizeof(data)-1) { - printf("Socket Send %d \"%s\"\r\n", ret, data); - } - ret = mdm.socketRecv(socket, buf, sizeof(buf)-1); - if (ret >= 0) { - printf("Socket Recv %d \"%.*s\"\r\n", ret, ret, buf); - } - mdm.socketClose(socket); - } - mdm.socketFree(socket); - } - - printf("Testing UDP sockets with ECHO server\r\n"); - socket = mdm.socketSocket(MDMParser::IPPROTO_UDP, port); - if (socket >= 0) - { - mdm.socketSetBlocking(socket, 10000); - memcpy(data, "\r\nUDP", 5); - ret = mdm.socketSendTo(socket, ip, port, data, sizeof(data)-1); - if (ret == sizeof(data)-1) { - printf("Socket SendTo %s:%d " IPSTR " %d \"%s\"\r\n", host, port, IPNUM(ip), ret, data); - } - ret = mdm.socketRecvFrom(socket, &ip, &port, buf, sizeof(buf)-1); - if (ret >= 0) { - printf("Socket RecvFrom " IPSTR ":%d %d \"%.*s\" \r\n", IPNUM(ip),port, ret, ret,buf); - } - mdm.socketFree(socket); - } - - // disconnect - mdm.disconnect(); } } - printf("SMS and GPS Loop\r\n"); - char link[128] = ""; - unsigned int i = 0xFFFFFFFF; - const int wait = 100; - bool abort = false; -#ifdef CELLOCATE - const int sensorMask = 3; // Hybrid: GNSS + CellLocate - const int timeoutMargin = 5; // seconds - const int submitPeriod = 60; // 1 minutes in seconds - const int targetAccuracy = 1; // meters - unsigned int j = submitPeriod * 1000/wait; - bool cellLocWait = false; - MDMParser::CellLocData loc; - - //Token can be released from u-blox site, when you got one replace "TOKEN" below - if (!mdm.cellLocSrvHttp("TOKEN")) - mdm.cellLocSrvUdp(); - mdm.cellLocConfigSensor(1); // Deep scan mode - //mdm.cellUnsolIndication(1); -#endif - //DigitalOut led(LED1); - while (!abort) { - // led = !led; -#ifndef CELLOCATE - while ((ret = gps.getMessage(buf, sizeof(buf))) > 0) - { - int len = LENGTH(ret); - //printf("NMEA: %.*s\r\n", len-2, msg); - if ((PROTOCOL(ret) == GPSParser::NMEA) && (len > 6)) - { - // talker is $GA=Galileo $GB=Beidou $GL=Glonass $GN=Combined $GP=GPS - if ((buf[0] == '$') || buf[1] == 'G') { - #define _CHECK_TALKER(s) ((buf[3] == s[0]) && (buf[4] == s[1]) && (buf[5] == s[2])) - if (_CHECK_TALKER("GLL")) { - double la = 0, lo = 0; - char ch; - if (gps.getNmeaAngle(1,buf,len,la) && - gps.getNmeaAngle(3,buf,len,lo) && - gps.getNmeaItem(6,buf,len,ch) && ch == 'A') - { - printf("GPS Location: %.5f %.5f\r\n", la, lo); - sprintf(link, "I am here!\n" - "https://maps.google.com/?q=%.5f,%.5f", la, lo); - } - } else if (_CHECK_TALKER("GGA") || _CHECK_TALKER("GNS") ) { - double a = 0; - if (gps.getNmeaItem(9,buf,len,a)) // altitude msl [m] - printf("GPS Altitude: %.1f\r\n", a); - } else if (_CHECK_TALKER("VTG")) { - double s = 0; - if (gps.getNmeaItem(7,buf,len,s)) // speed [km/h] - printf("GPS Speed: %.1f\r\n", s); - } - } - } - } -#endif -#ifdef CELLOCATE - if (mdmOk && (j++ == submitPeriod * 1000/wait)) { - j=0; - printf("CellLocate Request\r\n"); - mdm.cellLocRequest(sensorMask, submitPeriod-timeoutMargin, targetAccuracy); - cellLocWait = true; - } - if (cellLocWait && mdm.cellLocGet(&loc)){ - cellLocWait = false; - printf("CellLocate position received, sensor_used: %d, \r\n", loc.sensorUsed ); - printf(" latitude: %0.5f, longitude: %0.5f, altitute: %d\r\n", loc.latitue, loc.longitude, loc.altitutude); - if (loc.sensorUsed == 1) - printf(" uncertainty: %d, speed: %d, direction: %d, vertical_acc: %d, satellite used: %d \r\n", loc.uncertainty,loc.speed,loc.direction,loc.verticalAcc,loc.svUsed); - if (loc.sensorUsed == 1 || loc.sensorUsed == 2) - sprintf(link, "I am here!\n" - "https://maps.google.com/?q=%.5f,%.5f", loc.latitue, loc.longitude); - } - if (cellLocWait && (j%100 == 0 )) - printf("Waiting for CellLocate...\r\n"); -#endif - if (mdmOk && (i++ == 5000/wait)) { - i = 0; - // check the network status - if (mdm.checkNetStatus(&netStatus)) { - mdm.dumpNetStatus(&netStatus, fprintf, stdout); - } - - // checking unread sms - int ix[8]; - int n = mdm.smsList("REC UNREAD", ix, 8); - if (8 < n) n = 8; - while (0 < n--) - { - char num[32]; - printf("Unread SMS at index %d\r\n", ix[n]); - if (mdm.smsRead(ix[n], num, buf, sizeof(buf))) { - printf("Got SMS from \"%s\" with text \"%s\"\r\n", num, buf); - printf("Delete SMS at index %d\r\n", ix[n]); - mdm.smsDelete(ix[n]); - // provide a reply - const char* reply = "Hello my friend"; - if (strstr(buf, /*w*/"here are you")) - reply = *link ? link : "I don't know"; // reply wil location link - else if (strstr(buf, /*s*/"hutdown")) - abort = true, reply = "bye bye"; - printf("Send SMS reply \"%s\" to \"%s\"\r\n", reply, num); - mdm.smsSend(num, reply); - } - } - } - wait_ms(wait); - } - gps.powerOff(); - mdm.powerOff(); - return 0; } + + + + +