Hi all
I have a cobra running SDK 4.3.6 and tried the latest PPP code from cellular radio to connect to a webpage and send some variables. The code is working ok.
I’m using the same cobra board to test RTC, everything works ok, my board goes to deepsleep and wakes up after 2 minutes.
When i joined the two pieces of code, the RTC doesn’t work anymore, it always wake up after 1 minute. I’m using the board without VS.
If i comment the part where i power on CR, RTC works ok, so it seems that CR conflicts with RTC…
Here is my code, i had some comments on it :
using Gadgeteer.Modules.GHIElectronics;
using GHI.Networking;
using GHI.Processor;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using Microsoft.SPOT.Net.NetworkInformation;
using System;
using System.Net;
using System.Text;
using System.Threading;
using G120 = GHI.Pins.G120;
using GT = Gadgeteer;
namespace Cerb_CR
{
public partial class Program
{
private CellularRadio cr;
private void RegisterExtenderSockets()
{
var socket = GT.Socket.SocketInterfaces.CreateNumberedSocket(8);
socket.SupportedTypes = new char[] { 'I', 'K', 'U', 'X' };
socket.CpuPins[3] = G120.P0_10;
socket.CpuPins[4] = G120.P2_0;
socket.CpuPins[5] = G120.P0_16;
socket.CpuPins[6] = G120.P0_6;
socket.CpuPins[7] = G120.P0_17;
socket.CpuPins[8] = G120.P0_27;
socket.CpuPins[9] = G120.P0_28;
socket.SerialPortName = "COM2";
GT.Socket.SocketInterfaces.RegisterSocket(socket);
}
void ProgramStarted()
{
Debug.Print("Program Started");
//**************************************************
// PART 1 - set datetime of the RTC
//**************************************************
DateTime DT;
try
{
DT = RealTimeClock.GetDateTime();
Debug.Print("Current Real-time Clock " + DT.ToString());
}
catch
{
Debug.Print("The date was bad and caused a bad time");
DT = new DateTime(2014, 1, 1, 1, 1, 1);
RealTimeClock.SetDateTime(DT);
}
if (DT.Year < 2011)
{
Debug.Print("Time is not resonable");
}
Debug.Print("Current Real-time Clock " + RealTimeClock.GetDateTime().ToString());
DT = new DateTime(2014, 9, 15, 7, 30, 0);
RealTimeClock.SetDateTime(DT);
Debug.Print("New Real-time Clock " + RealTimeClock.GetDateTime().ToString());
//**************************************************
// PART 2 - hibernate for 2 seconds, just to test...
//**************************************************
Debug.Print("Hibernating");
Thread.Sleep(500);
RealTimeClock.SetAlarm(RealTimeClock.GetDateTime().AddSeconds(120));
Debug.Print("now is : " + RealTimeClock.GetDateTime().Date + "-" + RealTimeClock.GetDateTime().Month + "-" + RealTimeClock.GetDateTime().Date + " - " + RealTimeClock.GetDateTime().Hour + ":" + RealTimeClock.GetDateTime().Minute + ":" + RealTimeClock.GetDateTime().Second);
Debug.Print("Start Hibernate at: " + RealTimeClock.GetAlarm().Date + "-" + RealTimeClock.GetAlarm().Month + "-" + RealTimeClock.GetAlarm().Date + " - " + RealTimeClock.GetAlarm().Hour + ":" + RealTimeClock.GetAlarm().Minute + ":" + RealTimeClock.GetAlarm().Second);
PowerState.WakeupEvents |= HardwareEvent.OEMReserved2;
PowerState.Sleep(SleepLevel.Off, HardwareEvent.OEMReserved2);
Debug.Print("Awaiking...");
Thread.Sleep(500);
//**************************************************
// PART 3 - hibernate ok, now register cellular radio
//**************************************************
this.RegisterExtenderSockets();
cr = new CellularRadio(8);
NetworkChange.NetworkAvailabilityChanged += (a, b) => Debug.Print("Network availability changed: " + b.IsAvailable.ToString());
NetworkChange.NetworkAddressChanged += (a, b) => Debug.Print("Network address changed");
Thread.Sleep(2000);
this.cr.ModuleInitialized += this.cellularRadio_ModuleInitialized;
Thread.Sleep(2000);
this.cr.PowerOn(40);
Mainboard.SetDebugLED(true);
}
void cellularRadio_ModuleInitialized(CellularRadio sender)
{
Mainboard.SetDebugLED(false);
Thread.Sleep(15000);
this.cr.UseThisNetworkInterface("my_ISP_Address", "", "", PPPSerialModem.AuthenticationType.Pap);
while (this.cr.NetworkInterface.IPAddress == "0.0.0.0")
{
Debug.Print("Waiting on DHCP");
Thread.Sleep(250);
}
Thread.Sleep(2000);
if (this.cr.NetworkInterface.IPAddress != "0.0.0.0")
{
//**************************************************
// PART 4 - infinite loop: send TCP request, hibernate, wake up, ...
//**************************************************
while (1 == 1)
{
Mainboard.SetDebugLED(true);
using (var req = HttpWebRequest.Create("http://mysite/read.php?txt=CAP&id=123456&value=99") as HttpWebRequest)
{
req.KeepAlive = false;
req.ContentLength = 0;
using (var res = req.GetResponse() as HttpWebResponse)
{
using (var stream = res.GetResponseStream())
{
int read = 0, total = 0;
var buffer = new byte[1024];
do
{
read = stream.Read(buffer, 0, buffer.Length);
total += read;
Thread.Sleep(20);
} while (read != 0);
Debug.Print("TCP answer: " + new string(Encoding.UTF8.GetChars(buffer)));
if (new string(Encoding.UTF8.GetChars(buffer)) == "ok")
{
Mainboard.SetDebugLED(false);
}
}
}
}
Debug.Print("Hibernating");
Thread.Sleep(500);
RealTimeClock.SetAlarm(RealTimeClock.GetDateTime().AddSeconds(120));
Debug.Print("now is : " + RealTimeClock.GetDateTime().Date + "-" + RealTimeClock.GetDateTime().Month + "-" + RealTimeClock.GetDateTime().Date + " - " + RealTimeClock.GetDateTime().Hour + ":" + RealTimeClock.GetDateTime().Minute + ":" + RealTimeClock.GetDateTime().Second);
Debug.Print("Start Hibernate at: " + RealTimeClock.GetAlarm().Date + "-" + RealTimeClock.GetAlarm().Month + "-" + RealTimeClock.GetAlarm().Date + " - " + RealTimeClock.GetAlarm().Hour + ":" + RealTimeClock.GetAlarm().Minute + ":" + RealTimeClock.GetAlarm().Second);
PowerState.WakeupEvents |= HardwareEvent.OEMReserved2;
PowerState.Sleep(SleepLevel.Off, HardwareEvent.OEMReserved2);
Debug.Print("Awaiking...");
Thread.Sleep(500);
}
}
}
}
}